blob: 2956460684cf9306f7bea818a45f62ce0668f239 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000036 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
37 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
38 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregorab452ba2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
81 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Eli Friedman2993cda2009-03-27 20:56:17 +000084 NNS != NNSEnd; ) {
85 // This loop iterates, then destroys so that it doesn't cause invalid
86 // reads.
87 // FIXME: Find a less fragile way to do this!
88 NestedNameSpecifier* N = &*NNS;
89 ++NNS;
90 N->Destroy(*this);
91 }
Douglas Gregorab452ba2009-03-26 23:50:42 +000092
93 if (GlobalNestedNameSpecifier)
94 GlobalNestedNameSpecifier->Destroy(*this);
95
Eli Friedmanb26153c2008-05-27 03:08:09 +000096 TUDecl->Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000097
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
100void ASTContext::PrintStats() const {
101 fprintf(stderr, "*** AST Context Stats:\n");
102 fprintf(stderr, " %d types total.\n", (int)Types.size());
103 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000104 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000105 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
106 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
110 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000111 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112
113 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
114 Type *T = Types[i];
115 if (isa<BuiltinType>(T))
116 ++NumBuiltin;
117 else if (isa<PointerType>(T))
118 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000119 else if (isa<BlockPointerType>(T))
120 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000121 else if (isa<LValueReferenceType>(T))
122 ++NumLValueReference;
123 else if (isa<RValueReferenceType>(T))
124 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000125 else if (isa<MemberPointerType>(T))
126 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000127 else if (isa<ComplexType>(T))
128 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 else if (isa<ArrayType>(T))
130 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000131 else if (isa<VectorType>(T))
132 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ++NumFunctionP;
137 else if (isa<TypedefType>(T))
138 ++NumTypeName;
139 else if (TagType *TT = dyn_cast<TagType>(T)) {
140 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000141 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000143 case TagDecl::TK_struct: ++NumTagStruct; break;
144 case TagDecl::TK_union: ++NumTagUnion; break;
145 case TagDecl::TK_class: ++NumTagClass; break;
146 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000148 } else if (isa<ObjCInterfaceType>(T))
149 ++NumObjCInterfaces;
150 else if (isa<ObjCQualifiedInterfaceType>(T))
151 ++NumObjCQualifiedInterfaces;
152 else if (isa<ObjCQualifiedIdType>(T))
153 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000154 else if (isa<TypeOfType>(T))
155 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000156 else if (isa<TypeOfExprType>(T))
157 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
190 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000191 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000192 NumLValueReference*sizeof(LValueReferenceType)+
193 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000194 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000195 NumFunctionP*sizeof(FunctionProtoType)+
196 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000197 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000198 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
201
202void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000203 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000204}
205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206void ASTContext::InitBuiltinTypes() {
207 assert(VoidTy.isNull() && "Context reinitialized?");
208
209 // C99 6.2.5p19.
210 InitBuiltinType(VoidTy, BuiltinType::Void);
211
212 // C99 6.2.5p2.
213 InitBuiltinType(BoolTy, BuiltinType::Bool);
214 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000215 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 InitBuiltinType(CharTy, BuiltinType::Char_S);
217 else
218 InitBuiltinType(CharTy, BuiltinType::Char_U);
219 // C99 6.2.5p4.
220 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
221 InitBuiltinType(ShortTy, BuiltinType::Short);
222 InitBuiltinType(IntTy, BuiltinType::Int);
223 InitBuiltinType(LongTy, BuiltinType::Long);
224 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
225
226 // C99 6.2.5p6.
227 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
228 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
229 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
230 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
231 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
232
233 // C99 6.2.5p10.
234 InitBuiltinType(FloatTy, BuiltinType::Float);
235 InitBuiltinType(DoubleTy, BuiltinType::Double);
236 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000237
Chris Lattner3a250322009-02-26 23:43:47 +0000238 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
239 InitBuiltinType(WCharTy, BuiltinType::WChar);
240 else // C99
241 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000242
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000243 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000244 InitBuiltinType(OverloadTy, BuiltinType::Overload);
245
246 // Placeholder type for type-dependent expressions whose type is
247 // completely unknown. No code should ever check a type against
248 // DependentTy and users should never see it; however, it is here to
249 // help diagnose failures to properly check for type-dependent
250 // expressions.
251 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // C99 6.2.5p11.
254 FloatComplexTy = getComplexType(FloatTy);
255 DoubleComplexTy = getComplexType(DoubleTy);
256 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000257
Steve Naroff7e219e42007-10-15 14:41:52 +0000258 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000260 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000262 ClassStructType = 0;
263
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000264 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000265
266 // void * type
267 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268}
269
Chris Lattner464175b2007-07-18 17:52:12 +0000270//===----------------------------------------------------------------------===//
271// Type Sizing and Analysis
272//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000273
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000274/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
275/// scalar floating point type.
276const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
277 const BuiltinType *BT = T->getAsBuiltinType();
278 assert(BT && "Not a floating point type!");
279 switch (BT->getKind()) {
280 default: assert(0 && "Not a floating point type!");
281 case BuiltinType::Float: return Target.getFloatFormat();
282 case BuiltinType::Double: return Target.getDoubleFormat();
283 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
284 }
285}
286
Chris Lattneraf707ab2009-01-24 21:53:27 +0000287/// getDeclAlign - Return a conservative estimate of the alignment of the
288/// specified decl. Note that bitfields do not have a valid alignment, so
289/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000290unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000291 unsigned Align = Target.getCharWidth();
292
293 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
294 Align = std::max(Align, AA->getAlignment());
295
Chris Lattneraf707ab2009-01-24 21:53:27 +0000296 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
297 QualType T = VD->getType();
298 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000299 if (!T->isIncompleteType() && !T->isFunctionType()) {
300 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
301 T = cast<ArrayType>(T)->getElementType();
302
303 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
304 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000305 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000306
307 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000308}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000309
Chris Lattnera7674d82007-07-13 22:13:22 +0000310/// getTypeSize - Return the size of the specified type, in bits. This method
311/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000312std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000313ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000314 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000315 uint64_t Width=0;
316 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000317 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000318#define TYPE(Class, Base)
319#define ABSTRACT_TYPE(Class, Base)
320#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
321#define DEPENDENT_TYPE(Class, Base) case Type::Class:
322#include "clang/AST/TypeNodes.def"
323 assert(false && "Should not see non-canonical or dependent types");
324 break;
325
Chris Lattner692233e2007-07-13 22:27:08 +0000326 case Type::FunctionNoProto:
327 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000328 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000329 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000330 case Type::VariableArray:
331 assert(0 && "VLAs not implemented yet!");
332 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000333 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000334
Chris Lattner98be4942008-03-05 18:54:05 +0000335 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000336 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000337 Align = EltInfo.second;
338 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000339 }
Nate Begeman213541a2008-04-18 23:10:10 +0000340 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000341 case Type::Vector: {
342 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000343 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000344 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000345 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000346 // If the alignment is not a power of 2, round up to the next power of 2.
347 // This happens for non-power-of-2 length vectors.
348 // FIXME: this should probably be a target property.
349 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000350 break;
351 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000352
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000354 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000355 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000356 case BuiltinType::Void:
357 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000358 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = Target.getBoolWidth();
360 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000361 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000362 case BuiltinType::Char_S:
363 case BuiltinType::Char_U:
364 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000366 Width = Target.getCharWidth();
367 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000368 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000369 case BuiltinType::WChar:
370 Width = Target.getWCharWidth();
371 Align = Target.getWCharAlign();
372 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000373 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000374 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000375 Width = Target.getShortWidth();
376 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000377 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000378 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000379 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000380 Width = Target.getIntWidth();
381 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000383 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000384 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000385 Width = Target.getLongWidth();
386 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000387 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000388 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000389 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000390 Width = Target.getLongLongWidth();
391 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 break;
393 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000394 Width = Target.getFloatWidth();
395 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000396 break;
397 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000398 Width = Target.getDoubleWidth();
399 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000400 break;
401 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000402 Width = Target.getLongDoubleWidth();
403 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000405 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000406 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000407 case Type::FixedWidthInt:
408 // FIXME: This isn't precisely correct; the width/alignment should depend
409 // on the available types for the target
410 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000411 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000412 Align = Width;
413 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000414 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000415 // FIXME: Pointers into different addr spaces could have different sizes and
416 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000417 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000419 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000420 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000421 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000422 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000423 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000424 case Type::BlockPointer: {
425 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
426 Width = Target.getPointerWidth(AS);
427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000430 case Type::Pointer: {
431 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000432 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000433 Align = Target.getPointerAlign(AS);
434 break;
435 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000436 case Type::LValueReference:
437 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000438 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000439 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000440 // FIXME: This is wrong for struct layout: a reference in a struct has
441 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000442 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000443 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000444 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000445 // the GCC ABI, where pointers to data are one pointer large, pointers to
446 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000447 // other compilers too, we need to delegate this completely to TargetInfo
448 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000449 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
450 unsigned AS = Pointee.getAddressSpace();
451 Width = Target.getPointerWidth(AS);
452 if (Pointee->isFunctionType())
453 Width *= 2;
454 Align = Target.getPointerAlign(AS);
455 // GCC aligns at single pointer width.
456 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000457 case Type::Complex: {
458 // Complex types have the same alignment as their elements, but twice the
459 // size.
460 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000461 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000462 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000463 Align = EltInfo.second;
464 break;
465 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000466 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000467 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000468 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
469 Width = Layout.getSize();
470 Align = Layout.getAlignment();
471 break;
472 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000473 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000474 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000475 const TagType *TT = cast<TagType>(T);
476
477 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000478 Width = 1;
479 Align = 1;
480 break;
481 }
482
Daniel Dunbar1d751182008-11-08 05:48:37 +0000483 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000484 return getTypeInfo(ET->getDecl()->getIntegerType());
485
Daniel Dunbar1d751182008-11-08 05:48:37 +0000486 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000487 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
488 Width = Layout.getSize();
489 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000490 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000491 }
Chris Lattner71763312008-04-06 22:05:18 +0000492 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000493
Chris Lattner464175b2007-07-18 17:52:12 +0000494 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000495 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000496}
497
Chris Lattner34ebde42009-01-27 18:08:34 +0000498/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
499/// type for the current target in bits. This can be different than the ABI
500/// alignment in cases where it is beneficial for performance to overalign
501/// a data type.
502unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
503 unsigned ABIAlign = getTypeAlign(T);
504
505 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000506 if (T->isSpecificBuiltinType(BuiltinType::Double))
507 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000508
509 return ABIAlign;
510}
511
512
Devang Patel8b277042008-06-04 21:22:16 +0000513/// LayoutField - Field layout.
514void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000515 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000516 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000517 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000518 uint64_t FieldOffset = IsUnion ? 0 : Size;
519 uint64_t FieldSize;
520 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000521
522 // FIXME: Should this override struct packing? Probably we want to
523 // take the minimum?
524 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
525 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000526
527 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
528 // TODO: Need to check this algorithm on other targets!
529 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000530 FieldSize =
531 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000532
533 std::pair<uint64_t, unsigned> FieldInfo =
534 Context.getTypeInfo(FD->getType());
535 uint64_t TypeSize = FieldInfo.first;
536
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000537 // Determine the alignment of this bitfield. The packing
538 // attributes define a maximum and the alignment attribute defines
539 // a minimum.
540 // FIXME: What is the right behavior when the specified alignment
541 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000542 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000543 if (FieldPacking)
544 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000545 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
546 FieldAlign = std::max(FieldAlign, AA->getAlignment());
547
548 // Check if we need to add padding to give the field the correct
549 // alignment.
550 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
551 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
552
553 // Padding members don't affect overall alignment
554 if (!FD->getIdentifier())
555 FieldAlign = 1;
556 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000557 if (FD->getType()->isIncompleteArrayType()) {
558 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000559 // query getTypeInfo about these, so we figure it out here.
560 // Flexible array members don't have any size, but they
561 // have to be aligned appropriately for their element type.
562 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000563 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000564 FieldAlign = Context.getTypeAlign(ATy->getElementType());
565 } else {
566 std::pair<uint64_t, unsigned> FieldInfo =
567 Context.getTypeInfo(FD->getType());
568 FieldSize = FieldInfo.first;
569 FieldAlign = FieldInfo.second;
570 }
571
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000572 // Determine the alignment of this bitfield. The packing
573 // attributes define a maximum and the alignment attribute defines
574 // a minimum. Additionally, the packing alignment must be at least
575 // a byte for non-bitfields.
576 //
577 // FIXME: What is the right behavior when the specified alignment
578 // is smaller than the specified packing?
579 if (FieldPacking)
580 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000581 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
582 FieldAlign = std::max(FieldAlign, AA->getAlignment());
583
584 // Round up the current record size to the field's alignment boundary.
585 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
586 }
587
588 // Place this field at the current location.
589 FieldOffsets[FieldNo] = FieldOffset;
590
591 // Reserve space for this field.
592 if (IsUnion) {
593 Size = std::max(Size, FieldSize);
594 } else {
595 Size = FieldOffset + FieldSize;
596 }
597
598 // Remember max struct/class alignment.
599 Alignment = std::max(Alignment, FieldAlign);
600}
601
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000602void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
603 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000604 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
605 if (SuperClass)
606 CollectObjCIvars(SuperClass, Fields);
607 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
608 E = OI->ivar_end(); I != E; ++I) {
609 ObjCIvarDecl *IVDecl = (*I);
610 if (!IVDecl->isInvalidDecl())
611 Fields.push_back(cast<FieldDecl>(IVDecl));
612 }
613}
614
615/// addRecordToClass - produces record info. for the class for its
616/// ivars and all those inherited.
617///
618const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
619{
620 const RecordDecl *&RD = ASTRecordForInterface[D];
621 if (RD)
622 return RD;
623 std::vector<FieldDecl*> RecFields;
624 CollectObjCIvars(D, RecFields);
625 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
626 D->getLocation(),
627 D->getIdentifier());
628 /// FIXME! Can do collection of ivars and adding to the record while
629 /// doing it.
630 for (unsigned int i = 0; i != RecFields.size(); i++) {
631 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
632 RecFields[i]->getLocation(),
633 RecFields[i]->getIdentifier(),
634 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000635 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000636 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000637 }
638 NewRD->completeDefinition(*this);
639 RD = NewRD;
640 return RD;
641}
Devang Patel44a3dde2008-06-04 21:54:36 +0000642
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000643/// setFieldDecl - maps a field for the given Ivar reference node.
644//
645void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
646 const ObjCIvarDecl *Ivar,
647 const ObjCIvarRefExpr *MRef) {
648 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
649 lookupFieldDeclForIvar(*this, Ivar);
650 ASTFieldForIvarRef[MRef] = FD;
651}
652
Chris Lattner61710852008-10-05 17:34:18 +0000653/// getASTObjcInterfaceLayout - Get or compute information about the layout of
654/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000655/// position information.
656const ASTRecordLayout &
657ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
658 // Look up this layout, if already laid out, return what we have.
659 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
660 if (Entry) return *Entry;
661
662 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
663 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000664 ASTRecordLayout *NewEntry = NULL;
665 unsigned FieldCount = D->ivar_size();
666 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
667 FieldCount++;
668 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
669 unsigned Alignment = SL.getAlignment();
670 uint64_t Size = SL.getSize();
671 NewEntry = new ASTRecordLayout(Size, Alignment);
672 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000673 // Super class is at the beginning of the layout.
674 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000675 } else {
676 NewEntry = new ASTRecordLayout();
677 NewEntry->InitializeLayout(FieldCount);
678 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000679 Entry = NewEntry;
680
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000681 unsigned StructPacking = 0;
682 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
683 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000684
685 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
686 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
687 AA->getAlignment()));
688
689 // Layout each ivar sequentially.
690 unsigned i = 0;
691 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
692 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
693 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000694 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000695 }
696
697 // Finally, round the size of the total struct up to the alignment of the
698 // struct itself.
699 NewEntry->FinalizeLayout();
700 return *NewEntry;
701}
702
Devang Patel88a981b2007-11-01 19:11:01 +0000703/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000704/// specified record (struct/union/class), which indicates its size and field
705/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000706const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000707 D = D->getDefinition(*this);
708 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000709
Chris Lattner464175b2007-07-18 17:52:12 +0000710 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000711 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000712 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000713
Devang Patel88a981b2007-11-01 19:11:01 +0000714 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
715 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
716 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000717 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000718
Douglas Gregore267ff32008-12-11 20:41:00 +0000719 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000720 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000721 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000722
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000723 unsigned StructPacking = 0;
724 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
725 StructPacking = PA->getAlignment();
726
Eli Friedman4bd998b2008-05-30 09:31:38 +0000727 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000728 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
729 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000730
Eli Friedman4bd998b2008-05-30 09:31:38 +0000731 // Layout each field, for now, just sequentially, respecting alignment. In
732 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000733 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000734 for (RecordDecl::field_iterator Field = D->field_begin(),
735 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000736 Field != FieldEnd; (void)++Field, ++FieldIdx)
737 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000738
739 // Finally, round the size of the total struct up to the alignment of the
740 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000741 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000742 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000743}
744
Chris Lattnera7674d82007-07-13 22:13:22 +0000745//===----------------------------------------------------------------------===//
746// Type creation/memoization methods
747//===----------------------------------------------------------------------===//
748
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000749QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000750 QualType CanT = getCanonicalType(T);
751 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000752 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000753
754 // If we are composing extended qualifiers together, merge together into one
755 // ExtQualType node.
756 unsigned CVRQuals = T.getCVRQualifiers();
757 QualType::GCAttrTypes GCAttr = QualType::GCNone;
758 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000759
Chris Lattnerb7d25532009-02-18 22:53:11 +0000760 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
761 // If this type already has an address space specified, it cannot get
762 // another one.
763 assert(EQT->getAddressSpace() == 0 &&
764 "Type cannot be in multiple addr spaces!");
765 GCAttr = EQT->getObjCGCAttr();
766 TypeNode = EQT->getBaseType();
767 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000768
Chris Lattnerb7d25532009-02-18 22:53:11 +0000769 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000770 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000771 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000772 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000773 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000774 return QualType(EXTQy, CVRQuals);
775
Christopher Lambebb97e92008-02-04 02:31:56 +0000776 // If the base type isn't canonical, this won't be a canonical type either,
777 // so fill in the canonical type field.
778 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000779 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000780 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000781
Chris Lattnerb7d25532009-02-18 22:53:11 +0000782 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000783 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000784 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000785 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000786 ExtQualType *New =
787 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000788 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000789 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000790 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000791}
792
Chris Lattnerb7d25532009-02-18 22:53:11 +0000793QualType ASTContext::getObjCGCQualType(QualType T,
794 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000795 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000796 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000797 return T;
798
Chris Lattnerb7d25532009-02-18 22:53:11 +0000799 // If we are composing extended qualifiers together, merge together into one
800 // ExtQualType node.
801 unsigned CVRQuals = T.getCVRQualifiers();
802 Type *TypeNode = T.getTypePtr();
803 unsigned AddressSpace = 0;
804
805 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
806 // If this type already has an address space specified, it cannot get
807 // another one.
808 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
809 "Type cannot be in multiple addr spaces!");
810 AddressSpace = EQT->getAddressSpace();
811 TypeNode = EQT->getBaseType();
812 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000813
814 // Check if we've already instantiated an gc qual'd type of this type.
815 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000816 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000817 void *InsertPos = 0;
818 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000819 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000820
821 // If the base type isn't canonical, this won't be a canonical type either,
822 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000823 // FIXME: Isn't this also not canonical if the base type is a array
824 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000825 QualType Canonical;
826 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000828
Chris Lattnerb7d25532009-02-18 22:53:11 +0000829 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000830 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
831 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
832 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000833 ExtQualType *New =
834 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000835 ExtQualTypes.InsertNode(New, InsertPos);
836 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000837 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000838}
Chris Lattnera7674d82007-07-13 22:13:22 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840/// getComplexType - Return the uniqued reference to the type for a complex
841/// number with the specified element type.
842QualType ASTContext::getComplexType(QualType T) {
843 // Unique pointers, to guarantee there is only one pointer of a particular
844 // structure.
845 llvm::FoldingSetNodeID ID;
846 ComplexType::Profile(ID, T);
847
848 void *InsertPos = 0;
849 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
850 return QualType(CT, 0);
851
852 // If the pointee type isn't canonical, this won't be a canonical type either,
853 // so fill in the canonical type field.
854 QualType Canonical;
855 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000856 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000857
858 // Get the new insert position for the node we care about.
859 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000860 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 }
Steve Narofff83820b2009-01-27 22:08:43 +0000862 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 Types.push_back(New);
864 ComplexTypes.InsertNode(New, InsertPos);
865 return QualType(New, 0);
866}
867
Eli Friedmanf98aba32009-02-13 02:31:07 +0000868QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
869 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
870 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
871 FixedWidthIntType *&Entry = Map[Width];
872 if (!Entry)
873 Entry = new FixedWidthIntType(Width, Signed);
874 return QualType(Entry, 0);
875}
Reid Spencer5f016e22007-07-11 17:01:13 +0000876
877/// getPointerType - Return the uniqued reference to the type for a pointer to
878/// the specified type.
879QualType ASTContext::getPointerType(QualType T) {
880 // Unique pointers, to guarantee there is only one pointer of a particular
881 // structure.
882 llvm::FoldingSetNodeID ID;
883 PointerType::Profile(ID, T);
884
885 void *InsertPos = 0;
886 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
887 return QualType(PT, 0);
888
889 // If the pointee type isn't canonical, this won't be a canonical type either,
890 // so fill in the canonical type field.
891 QualType Canonical;
892 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000893 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000894
895 // Get the new insert position for the node we care about.
896 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000897 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 }
Steve Narofff83820b2009-01-27 22:08:43 +0000899 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 Types.push_back(New);
901 PointerTypes.InsertNode(New, InsertPos);
902 return QualType(New, 0);
903}
904
Steve Naroff5618bd42008-08-27 16:04:49 +0000905/// getBlockPointerType - Return the uniqued reference to the type for
906/// a pointer to the specified block.
907QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000908 assert(T->isFunctionType() && "block of function types only");
909 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000910 // structure.
911 llvm::FoldingSetNodeID ID;
912 BlockPointerType::Profile(ID, T);
913
914 void *InsertPos = 0;
915 if (BlockPointerType *PT =
916 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
917 return QualType(PT, 0);
918
Steve Naroff296e8d52008-08-28 19:20:44 +0000919 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000920 // type either so fill in the canonical type field.
921 QualType Canonical;
922 if (!T->isCanonical()) {
923 Canonical = getBlockPointerType(getCanonicalType(T));
924
925 // Get the new insert position for the node we care about.
926 BlockPointerType *NewIP =
927 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000928 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000929 }
Steve Narofff83820b2009-01-27 22:08:43 +0000930 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000931 Types.push_back(New);
932 BlockPointerTypes.InsertNode(New, InsertPos);
933 return QualType(New, 0);
934}
935
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000936/// getLValueReferenceType - Return the uniqued reference to the type for an
937/// lvalue reference to the specified type.
938QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 // Unique pointers, to guarantee there is only one pointer of a particular
940 // structure.
941 llvm::FoldingSetNodeID ID;
942 ReferenceType::Profile(ID, T);
943
944 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000945 if (LValueReferenceType *RT =
946 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 // If the referencee type isn't canonical, this won't be a canonical type
950 // either, so fill in the canonical type field.
951 QualType Canonical;
952 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000953 Canonical = getLValueReferenceType(getCanonicalType(T));
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000956 LValueReferenceType *NewIP =
957 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000958 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 }
960
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000961 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000963 LValueReferenceTypes.InsertNode(New, InsertPos);
964 return QualType(New, 0);
965}
966
967/// getRValueReferenceType - Return the uniqued reference to the type for an
968/// rvalue reference to the specified type.
969QualType ASTContext::getRValueReferenceType(QualType T) {
970 // Unique pointers, to guarantee there is only one pointer of a particular
971 // structure.
972 llvm::FoldingSetNodeID ID;
973 ReferenceType::Profile(ID, T);
974
975 void *InsertPos = 0;
976 if (RValueReferenceType *RT =
977 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
978 return QualType(RT, 0);
979
980 // If the referencee type isn't canonical, this won't be a canonical type
981 // either, so fill in the canonical type field.
982 QualType Canonical;
983 if (!T->isCanonical()) {
984 Canonical = getRValueReferenceType(getCanonicalType(T));
985
986 // Get the new insert position for the node we care about.
987 RValueReferenceType *NewIP =
988 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
989 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
990 }
991
992 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
993 Types.push_back(New);
994 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 return QualType(New, 0);
996}
997
Sebastian Redlf30208a2009-01-24 21:16:55 +0000998/// getMemberPointerType - Return the uniqued reference to the type for a
999/// member pointer to the specified type, in the specified class.
1000QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1001{
1002 // Unique pointers, to guarantee there is only one pointer of a particular
1003 // structure.
1004 llvm::FoldingSetNodeID ID;
1005 MemberPointerType::Profile(ID, T, Cls);
1006
1007 void *InsertPos = 0;
1008 if (MemberPointerType *PT =
1009 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1010 return QualType(PT, 0);
1011
1012 // If the pointee or class type isn't canonical, this won't be a canonical
1013 // type either, so fill in the canonical type field.
1014 QualType Canonical;
1015 if (!T->isCanonical()) {
1016 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1017
1018 // Get the new insert position for the node we care about.
1019 MemberPointerType *NewIP =
1020 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1021 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1022 }
Steve Narofff83820b2009-01-27 22:08:43 +00001023 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001024 Types.push_back(New);
1025 MemberPointerTypes.InsertNode(New, InsertPos);
1026 return QualType(New, 0);
1027}
1028
Steve Narofffb22d962007-08-30 01:06:46 +00001029/// getConstantArrayType - Return the unique reference to the type for an
1030/// array of the specified element type.
1031QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001032 const llvm::APInt &ArySize,
1033 ArrayType::ArraySizeModifier ASM,
1034 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001036 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037
1038 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001039 if (ConstantArrayType *ATP =
1040 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 return QualType(ATP, 0);
1042
1043 // If the element type isn't canonical, this won't be a canonical type either,
1044 // so fill in the canonical type field.
1045 QualType Canonical;
1046 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001047 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001048 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001050 ConstantArrayType *NewIP =
1051 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001052 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 }
1054
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001055 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001056 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001057 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 Types.push_back(New);
1059 return QualType(New, 0);
1060}
1061
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001062/// getVariableArrayType - Returns a non-unique reference to the type for a
1063/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001064QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1065 ArrayType::ArraySizeModifier ASM,
1066 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001067 // Since we don't unique expressions, it isn't possible to unique VLA's
1068 // that have an expression provided for their size.
1069
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001070 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001071 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001072
1073 VariableArrayTypes.push_back(New);
1074 Types.push_back(New);
1075 return QualType(New, 0);
1076}
1077
Douglas Gregor898574e2008-12-05 23:32:09 +00001078/// getDependentSizedArrayType - Returns a non-unique reference to
1079/// the type for a dependently-sized array of the specified element
1080/// type. FIXME: We will need these to be uniqued, or at least
1081/// comparable, at some point.
1082QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1083 ArrayType::ArraySizeModifier ASM,
1084 unsigned EltTypeQuals) {
1085 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1086 "Size must be type- or value-dependent!");
1087
1088 // Since we don't unique expressions, it isn't possible to unique
1089 // dependently-sized array types.
1090
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001091 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001092 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1093 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001094
1095 DependentSizedArrayTypes.push_back(New);
1096 Types.push_back(New);
1097 return QualType(New, 0);
1098}
1099
Eli Friedmanc5773c42008-02-15 18:16:39 +00001100QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1101 ArrayType::ArraySizeModifier ASM,
1102 unsigned EltTypeQuals) {
1103 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001104 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001105
1106 void *InsertPos = 0;
1107 if (IncompleteArrayType *ATP =
1108 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1109 return QualType(ATP, 0);
1110
1111 // If the element type isn't canonical, this won't be a canonical type
1112 // either, so fill in the canonical type field.
1113 QualType Canonical;
1114
1115 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001116 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001117 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001118
1119 // Get the new insert position for the node we care about.
1120 IncompleteArrayType *NewIP =
1121 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001122 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001123 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001124
Steve Narofff83820b2009-01-27 22:08:43 +00001125 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001126 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001127
1128 IncompleteArrayTypes.InsertNode(New, InsertPos);
1129 Types.push_back(New);
1130 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001131}
1132
Steve Naroff73322922007-07-18 18:00:27 +00001133/// getVectorType - Return the unique reference to a vector type of
1134/// the specified element type and size. VectorType must be a built-in type.
1135QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 BuiltinType *baseType;
1137
Chris Lattnerf52ab252008-04-06 22:59:24 +00001138 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001139 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001140
1141 // Check if we've already instantiated a vector of this type.
1142 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001143 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 void *InsertPos = 0;
1145 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1146 return QualType(VTP, 0);
1147
1148 // If the element type isn't canonical, this won't be a canonical type either,
1149 // so fill in the canonical type field.
1150 QualType Canonical;
1151 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001152 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153
1154 // Get the new insert position for the node we care about.
1155 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001156 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
Steve Narofff83820b2009-01-27 22:08:43 +00001158 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 VectorTypes.InsertNode(New, InsertPos);
1160 Types.push_back(New);
1161 return QualType(New, 0);
1162}
1163
Nate Begeman213541a2008-04-18 23:10:10 +00001164/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001165/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001166QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001167 BuiltinType *baseType;
1168
Chris Lattnerf52ab252008-04-06 22:59:24 +00001169 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001170 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001171
1172 // Check if we've already instantiated a vector of this type.
1173 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001174 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001175 void *InsertPos = 0;
1176 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1177 return QualType(VTP, 0);
1178
1179 // If the element type isn't canonical, this won't be a canonical type either,
1180 // so fill in the canonical type field.
1181 QualType Canonical;
1182 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001183 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001184
1185 // Get the new insert position for the node we care about.
1186 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001187 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001188 }
Steve Narofff83820b2009-01-27 22:08:43 +00001189 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001190 VectorTypes.InsertNode(New, InsertPos);
1191 Types.push_back(New);
1192 return QualType(New, 0);
1193}
1194
Douglas Gregor72564e72009-02-26 23:50:07 +00001195/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001196///
Douglas Gregor72564e72009-02-26 23:50:07 +00001197QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 // Unique functions, to guarantee there is only one function of a particular
1199 // structure.
1200 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001201 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202
1203 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001204 if (FunctionNoProtoType *FT =
1205 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 return QualType(FT, 0);
1207
1208 QualType Canonical;
1209 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001210 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001211
1212 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 FunctionNoProtoType *NewIP =
1214 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 }
1217
Douglas Gregor72564e72009-02-26 23:50:07 +00001218 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001220 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 return QualType(New, 0);
1222}
1223
1224/// getFunctionType - Return a normal function type with a typed argument
1225/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001226QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001227 unsigned NumArgs, bool isVariadic,
1228 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // Unique functions, to guarantee there is only one function of a particular
1230 // structure.
1231 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001232 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001233 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001234
1235 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001236 if (FunctionProtoType *FTP =
1237 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 return QualType(FTP, 0);
1239
1240 // Determine whether the type being created is already canonical or not.
1241 bool isCanonical = ResultTy->isCanonical();
1242 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1243 if (!ArgArray[i]->isCanonical())
1244 isCanonical = false;
1245
1246 // If this type isn't canonical, get the canonical version of it.
1247 QualType Canonical;
1248 if (!isCanonical) {
1249 llvm::SmallVector<QualType, 16> CanonicalArgs;
1250 CanonicalArgs.reserve(NumArgs);
1251 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001252 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001253
Chris Lattnerf52ab252008-04-06 22:59:24 +00001254 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001256 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001257
1258 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001259 FunctionProtoType *NewIP =
1260 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001261 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 }
1263
Douglas Gregor72564e72009-02-26 23:50:07 +00001264 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001265 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001266 FunctionProtoType *FTP =
1267 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001268 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001269 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001270 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001272 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 return QualType(FTP, 0);
1274}
1275
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001276/// getTypeDeclType - Return the unique reference to the type for the
1277/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001278QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001279 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001280 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1281
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001282 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001283 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001284 else if (isa<TemplateTypeParmDecl>(Decl)) {
1285 assert(false && "Template type parameter types are always available.");
1286 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001287 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001288
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001289 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001290 if (PrevDecl)
1291 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001292 else
1293 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001294 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001295 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1296 if (PrevDecl)
1297 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001298 else
1299 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001300 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001301 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001302 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001303
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001304 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001305 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001306}
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308/// getTypedefType - Return the unique reference to the type for the
1309/// specified typename decl.
1310QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1311 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1312
Chris Lattnerf52ab252008-04-06 22:59:24 +00001313 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001314 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 Types.push_back(Decl->TypeForDecl);
1316 return QualType(Decl->TypeForDecl, 0);
1317}
1318
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001320/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001322 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1323
Steve Narofff83820b2009-01-27 22:08:43 +00001324 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001325 Types.push_back(Decl->TypeForDecl);
1326 return QualType(Decl->TypeForDecl, 0);
1327}
1328
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001329/// buildObjCInterfaceType - Returns a new type for the interface
1330/// declaration, regardless. It also removes any previously built
1331/// record declaration so caller can rebuild it.
1332QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1333 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1334 if (RD)
1335 RD = 0;
1336 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1337 Types.push_back(Decl->TypeForDecl);
1338 return QualType(Decl->TypeForDecl, 0);
1339}
1340
Douglas Gregorfab9d672009-02-05 23:33:38 +00001341/// \brief Retrieve the template type parameter type for a template
1342/// parameter with the given depth, index, and (optionally) name.
1343QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1344 IdentifierInfo *Name) {
1345 llvm::FoldingSetNodeID ID;
1346 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1347 void *InsertPos = 0;
1348 TemplateTypeParmType *TypeParm
1349 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1350
1351 if (TypeParm)
1352 return QualType(TypeParm, 0);
1353
1354 if (Name)
1355 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1356 getTemplateTypeParmType(Depth, Index));
1357 else
1358 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1359
1360 Types.push_back(TypeParm);
1361 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1362
1363 return QualType(TypeParm, 0);
1364}
1365
Douglas Gregor55f6b142009-02-09 18:46:07 +00001366QualType
1367ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001368 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001369 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001370 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001371 if (!Canon.isNull())
1372 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001373
Douglas Gregor55f6b142009-02-09 18:46:07 +00001374 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001375 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1376
Douglas Gregor55f6b142009-02-09 18:46:07 +00001377 void *InsertPos = 0;
1378 ClassTemplateSpecializationType *Spec
1379 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1380
1381 if (Spec)
1382 return QualType(Spec, 0);
1383
Douglas Gregor40808ce2009-03-09 23:48:35 +00001384 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1385 sizeof(TemplateArgument) * NumArgs),
1386 8);
1387 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1388 Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001389 Types.push_back(Spec);
1390 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1391
1392 return QualType(Spec, 0);
1393}
1394
Douglas Gregore4e5b052009-03-19 00:18:19 +00001395QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001396ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001397 QualType NamedType) {
1398 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001399 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001400
1401 void *InsertPos = 0;
1402 QualifiedNameType *T
1403 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1404 if (T)
1405 return QualType(T, 0);
1406
Douglas Gregorab452ba2009-03-26 23:50:42 +00001407 T = new (*this) QualifiedNameType(NNS, NamedType,
1408 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001409 Types.push_back(T);
1410 QualifiedNameTypes.InsertNode(T, InsertPos);
1411 return QualType(T, 0);
1412}
1413
Douglas Gregord57959a2009-03-27 23:10:48 +00001414QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1415 const IdentifierInfo *Name,
1416 QualType Canon) {
1417 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1418
1419 if (Canon.isNull()) {
1420 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1421 if (CanonNNS != NNS)
1422 Canon = getTypenameType(CanonNNS, Name);
1423 }
1424
1425 llvm::FoldingSetNodeID ID;
1426 TypenameType::Profile(ID, NNS, Name);
1427
1428 void *InsertPos = 0;
1429 TypenameType *T
1430 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1431 if (T)
1432 return QualType(T, 0);
1433
1434 T = new (*this) TypenameType(NNS, Name, Canon);
1435 Types.push_back(T);
1436 TypenameTypes.InsertNode(T, InsertPos);
1437 return QualType(T, 0);
1438}
1439
Chris Lattner88cb27a2008-04-07 04:56:42 +00001440/// CmpProtocolNames - Comparison predicate for sorting protocols
1441/// alphabetically.
1442static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1443 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001444 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001445}
1446
1447static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1448 unsigned &NumProtocols) {
1449 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1450
1451 // Sort protocols, keyed by name.
1452 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1453
1454 // Remove duplicates.
1455 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1456 NumProtocols = ProtocolsEnd-Protocols;
1457}
1458
1459
Chris Lattner065f0d72008-04-07 04:44:08 +00001460/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1461/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1463 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001464 // Sort the protocol list alphabetically to canonicalize it.
1465 SortAndUniqueProtocols(Protocols, NumProtocols);
1466
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001467 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001468 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001469
1470 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 if (ObjCQualifiedInterfaceType *QT =
1472 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001473 return QualType(QT, 0);
1474
1475 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001477 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001478
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001479 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001480 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001481 return QualType(QType, 0);
1482}
1483
Chris Lattner88cb27a2008-04-07 04:56:42 +00001484/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1485/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001486QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001487 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001488 // Sort the protocol list alphabetically to canonicalize it.
1489 SortAndUniqueProtocols(Protocols, NumProtocols);
1490
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001491 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001492 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001493
1494 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001496 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001497 return QualType(QT, 0);
1498
1499 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001500 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001501 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001502 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001503 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001504 return QualType(QType, 0);
1505}
1506
Douglas Gregor72564e72009-02-26 23:50:07 +00001507/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1508/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001509/// multiple declarations that refer to "typeof(x)" all contain different
1510/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1511/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001512QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001513 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001514 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001515 Types.push_back(toe);
1516 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001517}
1518
Steve Naroff9752f252007-08-01 18:02:17 +00001519/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1520/// TypeOfType AST's. The only motivation to unique these nodes would be
1521/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1522/// an issue. This doesn't effect the type checker, since it operates
1523/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001524QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001525 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001526 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001527 Types.push_back(tot);
1528 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001529}
1530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531/// getTagDeclType - Return the unique reference to the type for the
1532/// specified TagDecl (struct/union/class/enum) decl.
1533QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001534 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001535 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001536}
1537
1538/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1539/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1540/// needs to agree with the definition in <stddef.h>.
1541QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001542 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001543}
1544
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001545/// getSignedWCharType - Return the type of "signed wchar_t".
1546/// Used when in C++, as a GCC extension.
1547QualType ASTContext::getSignedWCharType() const {
1548 // FIXME: derive from "Target" ?
1549 return WCharTy;
1550}
1551
1552/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1553/// Used when in C++, as a GCC extension.
1554QualType ASTContext::getUnsignedWCharType() const {
1555 // FIXME: derive from "Target" ?
1556 return UnsignedIntTy;
1557}
1558
Chris Lattner8b9023b2007-07-13 03:05:23 +00001559/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1560/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1561QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001562 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001563}
1564
Chris Lattnere6327742008-04-02 05:18:44 +00001565//===----------------------------------------------------------------------===//
1566// Type Operators
1567//===----------------------------------------------------------------------===//
1568
Chris Lattner77c96472008-04-06 22:41:35 +00001569/// getCanonicalType - Return the canonical (structural) type corresponding to
1570/// the specified potentially non-canonical type. The non-canonical version
1571/// of a type may have many "decorated" versions of types. Decorators can
1572/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1573/// to be free of any of these, allowing two canonical types to be compared
1574/// for exact equality with a simple pointer comparison.
1575QualType ASTContext::getCanonicalType(QualType T) {
1576 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001577
1578 // If the result has type qualifiers, make sure to canonicalize them as well.
1579 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1580 if (TypeQuals == 0) return CanType;
1581
1582 // If the type qualifiers are on an array type, get the canonical type of the
1583 // array with the qualifiers applied to the element type.
1584 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1585 if (!AT)
1586 return CanType.getQualifiedType(TypeQuals);
1587
1588 // Get the canonical version of the element with the extra qualifiers on it.
1589 // This can recursively sink qualifiers through multiple levels of arrays.
1590 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1591 NewEltTy = getCanonicalType(NewEltTy);
1592
1593 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1594 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1595 CAT->getIndexTypeQualifier());
1596 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1597 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1598 IAT->getIndexTypeQualifier());
1599
Douglas Gregor898574e2008-12-05 23:32:09 +00001600 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1601 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1602 DSAT->getSizeModifier(),
1603 DSAT->getIndexTypeQualifier());
1604
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001605 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1606 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1607 VAT->getSizeModifier(),
1608 VAT->getIndexTypeQualifier());
1609}
1610
Douglas Gregord57959a2009-03-27 23:10:48 +00001611NestedNameSpecifier *
1612ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1613 if (!NNS)
1614 return 0;
1615
1616 switch (NNS->getKind()) {
1617 case NestedNameSpecifier::Identifier:
1618 // Canonicalize the prefix but keep the identifier the same.
1619 return NestedNameSpecifier::Create(*this,
1620 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1621 NNS->getAsIdentifier());
1622
1623 case NestedNameSpecifier::Namespace:
1624 // A namespace is canonical; build a nested-name-specifier with
1625 // this namespace and no prefix.
1626 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1627
1628 case NestedNameSpecifier::TypeSpec:
1629 case NestedNameSpecifier::TypeSpecWithTemplate: {
1630 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1631 NestedNameSpecifier *Prefix = 0;
1632
1633 // FIXME: This isn't the right check!
1634 if (T->isDependentType())
1635 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1636
1637 return NestedNameSpecifier::Create(*this, Prefix,
1638 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1639 T.getTypePtr());
1640 }
1641
1642 case NestedNameSpecifier::Global:
1643 // The global specifier is canonical and unique.
1644 return NNS;
1645 }
1646
1647 // Required to silence a GCC warning
1648 return 0;
1649}
1650
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001651
1652const ArrayType *ASTContext::getAsArrayType(QualType T) {
1653 // Handle the non-qualified case efficiently.
1654 if (T.getCVRQualifiers() == 0) {
1655 // Handle the common positive case fast.
1656 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1657 return AT;
1658 }
1659
1660 // Handle the common negative case fast, ignoring CVR qualifiers.
1661 QualType CType = T->getCanonicalTypeInternal();
1662
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001663 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001664 // test.
1665 if (!isa<ArrayType>(CType) &&
1666 !isa<ArrayType>(CType.getUnqualifiedType()))
1667 return 0;
1668
1669 // Apply any CVR qualifiers from the array type to the element type. This
1670 // implements C99 6.7.3p8: "If the specification of an array type includes
1671 // any type qualifiers, the element type is so qualified, not the array type."
1672
1673 // If we get here, we either have type qualifiers on the type, or we have
1674 // sugar such as a typedef in the way. If we have type qualifiers on the type
1675 // we must propagate them down into the elemeng type.
1676 unsigned CVRQuals = T.getCVRQualifiers();
1677 unsigned AddrSpace = 0;
1678 Type *Ty = T.getTypePtr();
1679
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001680 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001681 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001682 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1683 AddrSpace = EXTQT->getAddressSpace();
1684 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001685 } else {
1686 T = Ty->getDesugaredType();
1687 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1688 break;
1689 CVRQuals |= T.getCVRQualifiers();
1690 Ty = T.getTypePtr();
1691 }
1692 }
1693
1694 // If we have a simple case, just return now.
1695 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1696 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1697 return ATy;
1698
1699 // Otherwise, we have an array and we have qualifiers on it. Push the
1700 // qualifiers into the array element type and return a new array type.
1701 // Get the canonical version of the element with the extra qualifiers on it.
1702 // This can recursively sink qualifiers through multiple levels of arrays.
1703 QualType NewEltTy = ATy->getElementType();
1704 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001705 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001706 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1707
1708 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1709 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1710 CAT->getSizeModifier(),
1711 CAT->getIndexTypeQualifier()));
1712 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1713 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1714 IAT->getSizeModifier(),
1715 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001716
Douglas Gregor898574e2008-12-05 23:32:09 +00001717 if (const DependentSizedArrayType *DSAT
1718 = dyn_cast<DependentSizedArrayType>(ATy))
1719 return cast<ArrayType>(
1720 getDependentSizedArrayType(NewEltTy,
1721 DSAT->getSizeExpr(),
1722 DSAT->getSizeModifier(),
1723 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001724
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001725 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1726 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1727 VAT->getSizeModifier(),
1728 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001729}
1730
1731
Chris Lattnere6327742008-04-02 05:18:44 +00001732/// getArrayDecayedType - Return the properly qualified result of decaying the
1733/// specified array type to a pointer. This operation is non-trivial when
1734/// handling typedefs etc. The canonical type of "T" must be an array type,
1735/// this returns a pointer to a properly qualified element of the array.
1736///
1737/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1738QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001739 // Get the element type with 'getAsArrayType' so that we don't lose any
1740 // typedefs in the element type of the array. This also handles propagation
1741 // of type qualifiers from the array type into the element type if present
1742 // (C99 6.7.3p8).
1743 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1744 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001745
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001746 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001747
1748 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001749 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001750}
1751
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001752QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001753 QualType ElemTy = VAT->getElementType();
1754
1755 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1756 return getBaseElementType(VAT);
1757
1758 return ElemTy;
1759}
1760
Reid Spencer5f016e22007-07-11 17:01:13 +00001761/// getFloatingRank - Return a relative rank for floating point types.
1762/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001763static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001764 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001766
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001767 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001768 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001769 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 case BuiltinType::Float: return FloatRank;
1771 case BuiltinType::Double: return DoubleRank;
1772 case BuiltinType::LongDouble: return LongDoubleRank;
1773 }
1774}
1775
Steve Naroff716c7302007-08-27 01:41:48 +00001776/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1777/// point or a complex type (based on typeDomain/typeSize).
1778/// 'typeDomain' is a real floating point or complex type.
1779/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001780QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1781 QualType Domain) const {
1782 FloatingRank EltRank = getFloatingRank(Size);
1783 if (Domain->isComplexType()) {
1784 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001785 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001786 case FloatRank: return FloatComplexTy;
1787 case DoubleRank: return DoubleComplexTy;
1788 case LongDoubleRank: return LongDoubleComplexTy;
1789 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 }
Chris Lattner1361b112008-04-06 23:58:54 +00001791
1792 assert(Domain->isRealFloatingType() && "Unknown domain!");
1793 switch (EltRank) {
1794 default: assert(0 && "getFloatingRank(): illegal value for rank");
1795 case FloatRank: return FloatTy;
1796 case DoubleRank: return DoubleTy;
1797 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001798 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001799}
1800
Chris Lattner7cfeb082008-04-06 23:55:33 +00001801/// getFloatingTypeOrder - Compare the rank of the two specified floating
1802/// point types, ignoring the domain of the type (i.e. 'double' ==
1803/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1804/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001805int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1806 FloatingRank LHSR = getFloatingRank(LHS);
1807 FloatingRank RHSR = getFloatingRank(RHS);
1808
1809 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001810 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001811 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001812 return 1;
1813 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001814}
1815
Chris Lattnerf52ab252008-04-06 22:59:24 +00001816/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1817/// routine will assert if passed a built-in type that isn't an integer or enum,
1818/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001819unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001820 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001821 if (EnumType* ET = dyn_cast<EnumType>(T))
1822 T = ET->getDecl()->getIntegerType().getTypePtr();
1823
1824 // There are two things which impact the integer rank: the width, and
1825 // the ordering of builtins. The builtin ordering is encoded in the
1826 // bottom three bits; the width is encoded in the bits above that.
1827 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1828 return FWIT->getWidth() << 3;
1829 }
1830
Chris Lattnerf52ab252008-04-06 22:59:24 +00001831 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001832 default: assert(0 && "getIntegerRank(): not a built-in integer");
1833 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001834 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001835 case BuiltinType::Char_S:
1836 case BuiltinType::Char_U:
1837 case BuiltinType::SChar:
1838 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001839 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001840 case BuiltinType::Short:
1841 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001842 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001843 case BuiltinType::Int:
1844 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001845 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001846 case BuiltinType::Long:
1847 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001848 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001849 case BuiltinType::LongLong:
1850 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001851 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001852 }
1853}
1854
Chris Lattner7cfeb082008-04-06 23:55:33 +00001855/// getIntegerTypeOrder - Returns the highest ranked integer type:
1856/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1857/// LHS < RHS, return -1.
1858int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001859 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1860 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001861 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001862
Chris Lattnerf52ab252008-04-06 22:59:24 +00001863 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1864 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001865
Chris Lattner7cfeb082008-04-06 23:55:33 +00001866 unsigned LHSRank = getIntegerRank(LHSC);
1867 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001868
Chris Lattner7cfeb082008-04-06 23:55:33 +00001869 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1870 if (LHSRank == RHSRank) return 0;
1871 return LHSRank > RHSRank ? 1 : -1;
1872 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001873
Chris Lattner7cfeb082008-04-06 23:55:33 +00001874 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1875 if (LHSUnsigned) {
1876 // If the unsigned [LHS] type is larger, return it.
1877 if (LHSRank >= RHSRank)
1878 return 1;
1879
1880 // If the signed type can represent all values of the unsigned type, it
1881 // wins. Because we are dealing with 2's complement and types that are
1882 // powers of two larger than each other, this is always safe.
1883 return -1;
1884 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001885
Chris Lattner7cfeb082008-04-06 23:55:33 +00001886 // If the unsigned [RHS] type is larger, return it.
1887 if (RHSRank >= LHSRank)
1888 return -1;
1889
1890 // If the signed type can represent all values of the unsigned type, it
1891 // wins. Because we are dealing with 2's complement and types that are
1892 // powers of two larger than each other, this is always safe.
1893 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001894}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001895
1896// getCFConstantStringType - Return the type used for constant CFStrings.
1897QualType ASTContext::getCFConstantStringType() {
1898 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001899 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001900 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001901 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001902 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001903
1904 // const int *isa;
1905 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001906 // int flags;
1907 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001908 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001909 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001910 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001911 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001912
Anders Carlsson71993dd2007-08-17 05:31:46 +00001913 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001914 for (unsigned i = 0; i < 4; ++i) {
1915 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1916 SourceLocation(), 0,
1917 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001918 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001919 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001920 }
1921
1922 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001923 }
1924
1925 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001926}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001927
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001928QualType ASTContext::getObjCFastEnumerationStateType()
1929{
1930 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001931 ObjCFastEnumerationStateTypeDecl =
1932 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1933 &Idents.get("__objcFastEnumerationState"));
1934
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001935 QualType FieldTypes[] = {
1936 UnsignedLongTy,
1937 getPointerType(ObjCIdType),
1938 getPointerType(UnsignedLongTy),
1939 getConstantArrayType(UnsignedLongTy,
1940 llvm::APInt(32, 5), ArrayType::Normal, 0)
1941 };
1942
Douglas Gregor44b43212008-12-11 16:49:14 +00001943 for (size_t i = 0; i < 4; ++i) {
1944 FieldDecl *Field = FieldDecl::Create(*this,
1945 ObjCFastEnumerationStateTypeDecl,
1946 SourceLocation(), 0,
1947 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001948 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001949 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001950 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001951
Douglas Gregor44b43212008-12-11 16:49:14 +00001952 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001953 }
1954
1955 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1956}
1957
Anders Carlssone8c49532007-10-29 06:33:42 +00001958// This returns true if a type has been typedefed to BOOL:
1959// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001960static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001961 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001962 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1963 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001964
1965 return false;
1966}
1967
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001968/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001969/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001970int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001971 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001972
1973 // Make all integer and enum types at least as large as an int
1974 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001975 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001976 // Treat arrays as pointers, since that's how they're passed in.
1977 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001978 sz = getTypeSize(VoidPtrTy);
1979 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001980}
1981
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001982/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001983/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001984void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001985 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001986 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001987 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001988 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001989 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001990 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001991 // Compute size of all parameters.
1992 // Start with computing size of a pointer in number of bytes.
1993 // FIXME: There might(should) be a better way of doing this computation!
1994 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001995 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001996 // The first two arguments (self and _cmd) are pointers; account for
1997 // their size.
1998 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001999 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2000 E = Decl->param_end(); PI != E; ++PI) {
2001 QualType PType = (*PI)->getType();
2002 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002003 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002004 ParmOffset += sz;
2005 }
2006 S += llvm::utostr(ParmOffset);
2007 S += "@0:";
2008 S += llvm::utostr(PtrSize);
2009
2010 // Argument types.
2011 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002012 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2013 E = Decl->param_end(); PI != E; ++PI) {
2014 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002015 QualType PType = PVDecl->getOriginalType();
2016 if (const ArrayType *AT =
2017 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2018 // Use array's original type only if it has known number of
2019 // elements.
2020 if (!dyn_cast<ConstantArrayType>(AT))
2021 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002022 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002023 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002024 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002025 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002026 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002027 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002028 }
2029}
2030
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002031/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002032/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002033/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2034/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002035/// Property attributes are stored as a comma-delimited C string. The simple
2036/// attributes readonly and bycopy are encoded as single characters. The
2037/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2038/// encoded as single characters, followed by an identifier. Property types
2039/// are also encoded as a parametrized attribute. The characters used to encode
2040/// these attributes are defined by the following enumeration:
2041/// @code
2042/// enum PropertyAttributes {
2043/// kPropertyReadOnly = 'R', // property is read-only.
2044/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2045/// kPropertyByref = '&', // property is a reference to the value last assigned
2046/// kPropertyDynamic = 'D', // property is dynamic
2047/// kPropertyGetter = 'G', // followed by getter selector name
2048/// kPropertySetter = 'S', // followed by setter selector name
2049/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2050/// kPropertyType = 't' // followed by old-style type encoding.
2051/// kPropertyWeak = 'W' // 'weak' property
2052/// kPropertyStrong = 'P' // property GC'able
2053/// kPropertyNonAtomic = 'N' // property non-atomic
2054/// };
2055/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002056void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2057 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002058 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002059 // Collect information from the property implementation decl(s).
2060 bool Dynamic = false;
2061 ObjCPropertyImplDecl *SynthesizePID = 0;
2062
2063 // FIXME: Duplicated code due to poor abstraction.
2064 if (Container) {
2065 if (const ObjCCategoryImplDecl *CID =
2066 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2067 for (ObjCCategoryImplDecl::propimpl_iterator
2068 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2069 ObjCPropertyImplDecl *PID = *i;
2070 if (PID->getPropertyDecl() == PD) {
2071 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2072 Dynamic = true;
2073 } else {
2074 SynthesizePID = PID;
2075 }
2076 }
2077 }
2078 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002079 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002080 for (ObjCCategoryImplDecl::propimpl_iterator
2081 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2082 ObjCPropertyImplDecl *PID = *i;
2083 if (PID->getPropertyDecl() == PD) {
2084 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2085 Dynamic = true;
2086 } else {
2087 SynthesizePID = PID;
2088 }
2089 }
2090 }
2091 }
2092 }
2093
2094 // FIXME: This is not very efficient.
2095 S = "T";
2096
2097 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002098 // GCC has some special rules regarding encoding of properties which
2099 // closely resembles encoding of ivars.
2100 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2101 true /* outermost type */,
2102 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002103
2104 if (PD->isReadOnly()) {
2105 S += ",R";
2106 } else {
2107 switch (PD->getSetterKind()) {
2108 case ObjCPropertyDecl::Assign: break;
2109 case ObjCPropertyDecl::Copy: S += ",C"; break;
2110 case ObjCPropertyDecl::Retain: S += ",&"; break;
2111 }
2112 }
2113
2114 // It really isn't clear at all what this means, since properties
2115 // are "dynamic by default".
2116 if (Dynamic)
2117 S += ",D";
2118
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002119 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2120 S += ",N";
2121
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002122 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2123 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002124 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002125 }
2126
2127 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2128 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002129 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002130 }
2131
2132 if (SynthesizePID) {
2133 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2134 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002135 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002136 }
2137
2138 // FIXME: OBJCGC: weak & strong
2139}
2140
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002141/// getLegacyIntegralTypeEncoding -
2142/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002143/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002144/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2145///
2146void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2147 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2148 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002149 if (BT->getKind() == BuiltinType::ULong &&
2150 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002151 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002152 else
2153 if (BT->getKind() == BuiltinType::Long &&
2154 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002155 PointeeTy = IntTy;
2156 }
2157 }
2158}
2159
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002160void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002161 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002162 // We follow the behavior of gcc, expanding structures which are
2163 // directly pointed to, and expanding embedded structures. Note that
2164 // these rules are sufficient to prevent recursive encoding of the
2165 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002166 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2167 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002168}
2169
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002170static void EncodeBitField(const ASTContext *Context, std::string& S,
2171 FieldDecl *FD) {
2172 const Expr *E = FD->getBitWidth();
2173 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2174 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2175 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2176 S += 'b';
2177 S += llvm::utostr(N);
2178}
2179
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002180void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2181 bool ExpandPointedToStructures,
2182 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002183 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002184 bool OutermostType,
2185 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002186 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002187 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002188 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002189 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002190 else {
2191 char encoding;
2192 switch (BT->getKind()) {
2193 default: assert(0 && "Unhandled builtin type kind");
2194 case BuiltinType::Void: encoding = 'v'; break;
2195 case BuiltinType::Bool: encoding = 'B'; break;
2196 case BuiltinType::Char_U:
2197 case BuiltinType::UChar: encoding = 'C'; break;
2198 case BuiltinType::UShort: encoding = 'S'; break;
2199 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002200 case BuiltinType::ULong:
2201 encoding =
2202 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2203 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002204 case BuiltinType::ULongLong: encoding = 'Q'; break;
2205 case BuiltinType::Char_S:
2206 case BuiltinType::SChar: encoding = 'c'; break;
2207 case BuiltinType::Short: encoding = 's'; break;
2208 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002209 case BuiltinType::Long:
2210 encoding =
2211 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2212 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002213 case BuiltinType::LongLong: encoding = 'q'; break;
2214 case BuiltinType::Float: encoding = 'f'; break;
2215 case BuiltinType::Double: encoding = 'd'; break;
2216 case BuiltinType::LongDouble: encoding = 'd'; break;
2217 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002218
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002219 S += encoding;
2220 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002221 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002222 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002223 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2224 ExpandPointedToStructures,
2225 ExpandStructures, FD);
2226 if (FD || EncodingProperty) {
2227 // Note that we do extended encoding of protocol qualifer list
2228 // Only when doing ivar or property encoding.
2229 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2230 S += '"';
2231 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2232 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2233 S += '<';
2234 S += Proto->getNameAsString();
2235 S += '>';
2236 }
2237 S += '"';
2238 }
2239 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002240 }
2241 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002242 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002243 bool isReadOnly = false;
2244 // For historical/compatibility reasons, the read-only qualifier of the
2245 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2246 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2247 // Also, do not emit the 'r' for anything but the outermost type!
2248 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2249 if (OutermostType && T.isConstQualified()) {
2250 isReadOnly = true;
2251 S += 'r';
2252 }
2253 }
2254 else if (OutermostType) {
2255 QualType P = PointeeTy;
2256 while (P->getAsPointerType())
2257 P = P->getAsPointerType()->getPointeeType();
2258 if (P.isConstQualified()) {
2259 isReadOnly = true;
2260 S += 'r';
2261 }
2262 }
2263 if (isReadOnly) {
2264 // Another legacy compatibility encoding. Some ObjC qualifier and type
2265 // combinations need to be rearranged.
2266 // Rewrite "in const" from "nr" to "rn"
2267 const char * s = S.c_str();
2268 int len = S.length();
2269 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2270 std::string replace = "rn";
2271 S.replace(S.end()-2, S.end(), replace);
2272 }
2273 }
Steve Naroff389bf462009-02-12 17:52:19 +00002274 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002275 S += '@';
2276 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002277 }
2278 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002279 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002280 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002281 // Another historical/compatibility reason.
2282 // We encode the underlying type which comes out as
2283 // {...};
2284 S += '^';
2285 getObjCEncodingForTypeImpl(PointeeTy, S,
2286 false, ExpandPointedToStructures,
2287 NULL);
2288 return;
2289 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002290 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002291 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002292 const ObjCInterfaceType *OIT =
2293 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002294 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002295 S += '"';
2296 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002297 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2298 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2299 S += '<';
2300 S += Proto->getNameAsString();
2301 S += '>';
2302 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002303 S += '"';
2304 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002305 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002306 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002307 S += '#';
2308 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002309 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002310 S += ':';
2311 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002312 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002313
2314 if (PointeeTy->isCharType()) {
2315 // char pointer types should be encoded as '*' unless it is a
2316 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002317 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002318 S += '*';
2319 return;
2320 }
2321 }
2322
2323 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002324 getLegacyIntegralTypeEncoding(PointeeTy);
2325
2326 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002327 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002328 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002329 } else if (const ArrayType *AT =
2330 // Ignore type qualifiers etc.
2331 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002332 if (isa<IncompleteArrayType>(AT)) {
2333 // Incomplete arrays are encoded as a pointer to the array element.
2334 S += '^';
2335
2336 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2337 false, ExpandStructures, FD);
2338 } else {
2339 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002340
Anders Carlsson559a8332009-02-22 01:38:57 +00002341 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2342 S += llvm::utostr(CAT->getSize().getZExtValue());
2343 else {
2344 //Variable length arrays are encoded as a regular array with 0 elements.
2345 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2346 S += '0';
2347 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002348
Anders Carlsson559a8332009-02-22 01:38:57 +00002349 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2350 false, ExpandStructures, FD);
2351 S += ']';
2352 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002353 } else if (T->getAsFunctionType()) {
2354 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002355 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002356 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002357 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002358 // Anonymous structures print as '?'
2359 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2360 S += II->getName();
2361 } else {
2362 S += '?';
2363 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002364 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002365 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002366 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2367 FieldEnd = RDecl->field_end();
2368 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002369 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002370 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002371 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002372 S += '"';
2373 }
2374
2375 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002376 if (Field->isBitField()) {
2377 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2378 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002379 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002380 QualType qt = Field->getType();
2381 getLegacyIntegralTypeEncoding(qt);
2382 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002383 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002384 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002385 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002386 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002387 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002388 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002389 if (FD && FD->isBitField())
2390 EncodeBitField(this, S, FD);
2391 else
2392 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002393 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002394 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002395 } else if (T->isObjCInterfaceType()) {
2396 // @encode(class_name)
2397 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2398 S += '{';
2399 const IdentifierInfo *II = OI->getIdentifier();
2400 S += II->getName();
2401 S += '=';
2402 std::vector<FieldDecl*> RecFields;
2403 CollectObjCIvars(OI, RecFields);
2404 for (unsigned int i = 0; i != RecFields.size(); i++) {
2405 if (RecFields[i]->isBitField())
2406 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2407 RecFields[i]);
2408 else
2409 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2410 FD);
2411 }
2412 S += '}';
2413 }
2414 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002415 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002416}
2417
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002418void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002419 std::string& S) const {
2420 if (QT & Decl::OBJC_TQ_In)
2421 S += 'n';
2422 if (QT & Decl::OBJC_TQ_Inout)
2423 S += 'N';
2424 if (QT & Decl::OBJC_TQ_Out)
2425 S += 'o';
2426 if (QT & Decl::OBJC_TQ_Bycopy)
2427 S += 'O';
2428 if (QT & Decl::OBJC_TQ_Byref)
2429 S += 'R';
2430 if (QT & Decl::OBJC_TQ_Oneway)
2431 S += 'V';
2432}
2433
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002434void ASTContext::setBuiltinVaListType(QualType T)
2435{
2436 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2437
2438 BuiltinVaListType = T;
2439}
2440
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002441void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002442{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002443 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002444
2445 // typedef struct objc_object *id;
2446 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002447 // User error - caller will issue diagnostics.
2448 if (!ptr)
2449 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002450 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002451 // User error - caller will issue diagnostics.
2452 if (!rec)
2453 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002454 IdStructType = rec;
2455}
2456
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002457void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002458{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002459 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002460
2461 // typedef struct objc_selector *SEL;
2462 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002463 if (!ptr)
2464 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002465 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002466 if (!rec)
2467 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002468 SelStructType = rec;
2469}
2470
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002471void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002472{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002473 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002474}
2475
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002476void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002477{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002478 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002479
2480 // typedef struct objc_class *Class;
2481 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2482 assert(ptr && "'Class' incorrectly typed");
2483 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2484 assert(rec && "'Class' incorrectly typed");
2485 ClassStructType = rec;
2486}
2487
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002488void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2489 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002490 "'NSConstantString' type already set!");
2491
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002492 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002493}
2494
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002495/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002496/// TargetInfo, produce the corresponding type. The unsigned @p Type
2497/// is actually a value of type @c TargetInfo::IntType.
2498QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002499 switch (Type) {
2500 case TargetInfo::NoInt: return QualType();
2501 case TargetInfo::SignedShort: return ShortTy;
2502 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2503 case TargetInfo::SignedInt: return IntTy;
2504 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2505 case TargetInfo::SignedLong: return LongTy;
2506 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2507 case TargetInfo::SignedLongLong: return LongLongTy;
2508 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2509 }
2510
2511 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002512 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002513}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002514
2515//===----------------------------------------------------------------------===//
2516// Type Predicates.
2517//===----------------------------------------------------------------------===//
2518
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002519/// isObjCNSObjectType - Return true if this is an NSObject object using
2520/// NSObject attribute on a c-style pointer type.
2521/// FIXME - Make it work directly on types.
2522///
2523bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2524 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2525 if (TypedefDecl *TD = TDT->getDecl())
2526 if (TD->getAttr<ObjCNSObjectAttr>())
2527 return true;
2528 }
2529 return false;
2530}
2531
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002532/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2533/// to an object type. This includes "id" and "Class" (two 'special' pointers
2534/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2535/// ID type).
2536bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002537 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002538 return true;
2539
Steve Naroff6ae98502008-10-21 18:24:04 +00002540 // Blocks are objects.
2541 if (Ty->isBlockPointerType())
2542 return true;
2543
2544 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002545 if (!Ty->isPointerType())
2546 return false;
2547
2548 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2549 // pointer types. This looks for the typedef specifically, not for the
2550 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002551 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2552 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002553 return true;
2554
2555 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002556 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2557 return true;
2558
2559 // If is has NSObject attribute, OK as well.
2560 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002561}
2562
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002563/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2564/// garbage collection attribute.
2565///
2566QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002567 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002568 if (getLangOptions().ObjC1 &&
2569 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002570 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002571 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002572 // (or pointers to them) be treated as though they were declared
2573 // as __strong.
2574 if (GCAttrs == QualType::GCNone) {
2575 if (isObjCObjectPointerType(Ty))
2576 GCAttrs = QualType::Strong;
2577 else if (Ty->isPointerType())
2578 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2579 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002580 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002581 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002582}
2583
Chris Lattner6ac46a42008-04-07 06:51:04 +00002584//===----------------------------------------------------------------------===//
2585// Type Compatibility Testing
2586//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002587
Steve Naroff1c7d0672008-09-04 15:10:53 +00002588/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002589/// block types. Types must be strictly compatible here. For example,
2590/// C unfortunately doesn't produce an error for the following:
2591///
2592/// int (*emptyArgFunc)();
2593/// int (*intArgList)(int) = emptyArgFunc;
2594///
2595/// For blocks, we will produce an error for the following (similar to C++):
2596///
2597/// int (^emptyArgBlock)();
2598/// int (^intArgBlock)(int) = emptyArgBlock;
2599///
2600/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2601///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002602bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002603 const FunctionType *lbase = lhs->getAsFunctionType();
2604 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002605 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2606 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002607 if (lproto && rproto)
2608 return !mergeTypes(lhs, rhs).isNull();
2609 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002610}
2611
Chris Lattner6ac46a42008-04-07 06:51:04 +00002612/// areCompatVectorTypes - Return true if the two specified vector types are
2613/// compatible.
2614static bool areCompatVectorTypes(const VectorType *LHS,
2615 const VectorType *RHS) {
2616 assert(LHS->isCanonical() && RHS->isCanonical());
2617 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002618 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002619}
2620
Eli Friedman3d815e72008-08-22 00:56:42 +00002621/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002622/// compatible for assignment from RHS to LHS. This handles validation of any
2623/// protocol qualifiers on the LHS or RHS.
2624///
Eli Friedman3d815e72008-08-22 00:56:42 +00002625bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2626 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002627 // Verify that the base decls are compatible: the RHS must be a subclass of
2628 // the LHS.
2629 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2630 return false;
2631
2632 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2633 // protocol qualified at all, then we are good.
2634 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2635 return true;
2636
2637 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2638 // isn't a superset.
2639 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2640 return true; // FIXME: should return false!
2641
2642 // Finally, we must have two protocol-qualified interfaces.
2643 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2644 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002645
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002646 // All LHS protocols must have a presence on the RHS.
2647 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002648
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002649 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2650 LHSPE = LHSP->qual_end();
2651 LHSPI != LHSPE; LHSPI++) {
2652 bool RHSImplementsProtocol = false;
2653
2654 // If the RHS doesn't implement the protocol on the left, the types
2655 // are incompatible.
2656 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2657 RHSPE = RHSP->qual_end();
2658 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2659 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2660 RHSImplementsProtocol = true;
2661 }
2662 // FIXME: For better diagnostics, consider passing back the protocol name.
2663 if (!RHSImplementsProtocol)
2664 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002665 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002666 // The RHS implements all protocols listed on the LHS.
2667 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002668}
2669
Steve Naroff389bf462009-02-12 17:52:19 +00002670bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2671 // get the "pointed to" types
2672 const PointerType *LHSPT = LHS->getAsPointerType();
2673 const PointerType *RHSPT = RHS->getAsPointerType();
2674
2675 if (!LHSPT || !RHSPT)
2676 return false;
2677
2678 QualType lhptee = LHSPT->getPointeeType();
2679 QualType rhptee = RHSPT->getPointeeType();
2680 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2681 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2682 // ID acts sort of like void* for ObjC interfaces
2683 if (LHSIface && isObjCIdStructType(rhptee))
2684 return true;
2685 if (RHSIface && isObjCIdStructType(lhptee))
2686 return true;
2687 if (!LHSIface || !RHSIface)
2688 return false;
2689 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2690 canAssignObjCInterfaces(RHSIface, LHSIface);
2691}
2692
Steve Naroffec0550f2007-10-15 20:41:53 +00002693/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2694/// both shall have the identically qualified version of a compatible type.
2695/// C99 6.2.7p1: Two types have compatible types if their types are the
2696/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002697bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2698 return !mergeTypes(LHS, RHS).isNull();
2699}
2700
2701QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2702 const FunctionType *lbase = lhs->getAsFunctionType();
2703 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002704 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2705 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002706 bool allLTypes = true;
2707 bool allRTypes = true;
2708
2709 // Check return type
2710 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2711 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002712 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2713 allLTypes = false;
2714 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2715 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002716
2717 if (lproto && rproto) { // two C99 style function prototypes
2718 unsigned lproto_nargs = lproto->getNumArgs();
2719 unsigned rproto_nargs = rproto->getNumArgs();
2720
2721 // Compatible functions must have the same number of arguments
2722 if (lproto_nargs != rproto_nargs)
2723 return QualType();
2724
2725 // Variadic and non-variadic functions aren't compatible
2726 if (lproto->isVariadic() != rproto->isVariadic())
2727 return QualType();
2728
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002729 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2730 return QualType();
2731
Eli Friedman3d815e72008-08-22 00:56:42 +00002732 // Check argument compatibility
2733 llvm::SmallVector<QualType, 10> types;
2734 for (unsigned i = 0; i < lproto_nargs; i++) {
2735 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2736 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2737 QualType argtype = mergeTypes(largtype, rargtype);
2738 if (argtype.isNull()) return QualType();
2739 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002740 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2741 allLTypes = false;
2742 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2743 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002744 }
2745 if (allLTypes) return lhs;
2746 if (allRTypes) return rhs;
2747 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002748 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002749 }
2750
2751 if (lproto) allRTypes = false;
2752 if (rproto) allLTypes = false;
2753
Douglas Gregor72564e72009-02-26 23:50:07 +00002754 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002755 if (proto) {
2756 if (proto->isVariadic()) return QualType();
2757 // Check that the types are compatible with the types that
2758 // would result from default argument promotions (C99 6.7.5.3p15).
2759 // The only types actually affected are promotable integer
2760 // types and floats, which would be passed as a different
2761 // type depending on whether the prototype is visible.
2762 unsigned proto_nargs = proto->getNumArgs();
2763 for (unsigned i = 0; i < proto_nargs; ++i) {
2764 QualType argTy = proto->getArgType(i);
2765 if (argTy->isPromotableIntegerType() ||
2766 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2767 return QualType();
2768 }
2769
2770 if (allLTypes) return lhs;
2771 if (allRTypes) return rhs;
2772 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002773 proto->getNumArgs(), lproto->isVariadic(),
2774 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002775 }
2776
2777 if (allLTypes) return lhs;
2778 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002779 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002780}
2781
2782QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002783 // C++ [expr]: If an expression initially has the type "reference to T", the
2784 // type is adjusted to "T" prior to any further analysis, the expression
2785 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002786 // expression is an lvalue unless the reference is an rvalue reference and
2787 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002788 // FIXME: C++ shouldn't be going through here! The rules are different
2789 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002790 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2791 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002792 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002793 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002794 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002795 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002796
Eli Friedman3d815e72008-08-22 00:56:42 +00002797 QualType LHSCan = getCanonicalType(LHS),
2798 RHSCan = getCanonicalType(RHS);
2799
2800 // If two types are identical, they are compatible.
2801 if (LHSCan == RHSCan)
2802 return LHS;
2803
2804 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002805 // Note that we handle extended qualifiers later, in the
2806 // case for ExtQualType.
2807 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002808 return QualType();
2809
2810 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2811 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2812
Chris Lattner1adb8832008-01-14 05:45:46 +00002813 // We want to consider the two function types to be the same for these
2814 // comparisons, just force one to the other.
2815 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2816 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002817
2818 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002819 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2820 LHSClass = Type::ConstantArray;
2821 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2822 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002823
Nate Begeman213541a2008-04-18 23:10:10 +00002824 // Canonicalize ExtVector -> Vector.
2825 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2826 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002827
Chris Lattnerb0489812008-04-07 06:38:24 +00002828 // Consider qualified interfaces and interfaces the same.
2829 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2830 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002831
Chris Lattnera36a61f2008-04-07 05:43:21 +00002832 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002833 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002834 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2835 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2836
2837 // ID acts sort of like void* for ObjC interfaces
2838 if (LHSIface && isObjCIdStructType(RHS))
2839 return LHS;
2840 if (RHSIface && isObjCIdStructType(LHS))
2841 return RHS;
2842
Steve Naroffbc76dd02008-12-10 22:14:21 +00002843 // ID is compatible with all qualified id types.
2844 if (LHS->isObjCQualifiedIdType()) {
2845 if (const PointerType *PT = RHS->getAsPointerType()) {
2846 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002847 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002848 return LHS;
2849 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2850 // Unfortunately, this API is part of Sema (which we don't have access
2851 // to. Need to refactor. The following check is insufficient, since we
2852 // need to make sure the class implements the protocol.
2853 if (pType->isObjCInterfaceType())
2854 return LHS;
2855 }
2856 }
2857 if (RHS->isObjCQualifiedIdType()) {
2858 if (const PointerType *PT = LHS->getAsPointerType()) {
2859 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002860 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002861 return RHS;
2862 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2863 // Unfortunately, this API is part of Sema (which we don't have access
2864 // to. Need to refactor. The following check is insufficient, since we
2865 // need to make sure the class implements the protocol.
2866 if (pType->isObjCInterfaceType())
2867 return RHS;
2868 }
2869 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002870 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2871 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002872 if (const EnumType* ETy = LHS->getAsEnumType()) {
2873 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2874 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002875 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002876 if (const EnumType* ETy = RHS->getAsEnumType()) {
2877 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2878 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002879 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002880
Eli Friedman3d815e72008-08-22 00:56:42 +00002881 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002882 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002883
Steve Naroff4a746782008-01-09 22:43:08 +00002884 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002885 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002886#define TYPE(Class, Base)
2887#define ABSTRACT_TYPE(Class, Base)
2888#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2889#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2890#include "clang/AST/TypeNodes.def"
2891 assert(false && "Non-canonical and dependent types shouldn't get here");
2892 return QualType();
2893
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002894 case Type::LValueReference:
2895 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002896 case Type::MemberPointer:
2897 assert(false && "C++ should never be in mergeTypes");
2898 return QualType();
2899
2900 case Type::IncompleteArray:
2901 case Type::VariableArray:
2902 case Type::FunctionProto:
2903 case Type::ExtVector:
2904 case Type::ObjCQualifiedInterface:
2905 assert(false && "Types are eliminated above");
2906 return QualType();
2907
Chris Lattner1adb8832008-01-14 05:45:46 +00002908 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002909 {
2910 // Merge two pointer types, while trying to preserve typedef info
2911 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2912 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2913 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2914 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002915 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2916 return LHS;
2917 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2918 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002919 return getPointerType(ResultType);
2920 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002921 case Type::BlockPointer:
2922 {
2923 // Merge two block pointer types, while trying to preserve typedef info
2924 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2925 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2926 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2927 if (ResultType.isNull()) return QualType();
2928 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2929 return LHS;
2930 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2931 return RHS;
2932 return getBlockPointerType(ResultType);
2933 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002934 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002935 {
2936 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2937 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2938 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2939 return QualType();
2940
2941 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2942 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2943 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2944 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002945 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2946 return LHS;
2947 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2948 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002949 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2950 ArrayType::ArraySizeModifier(), 0);
2951 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2952 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002953 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2954 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002955 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2956 return LHS;
2957 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2958 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002959 if (LVAT) {
2960 // FIXME: This isn't correct! But tricky to implement because
2961 // the array's size has to be the size of LHS, but the type
2962 // has to be different.
2963 return LHS;
2964 }
2965 if (RVAT) {
2966 // FIXME: This isn't correct! But tricky to implement because
2967 // the array's size has to be the size of RHS, but the type
2968 // has to be different.
2969 return RHS;
2970 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002971 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2972 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002973 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002974 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002975 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002977 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002978 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002979 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002980 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2981 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002982 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002983 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002984 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002985 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002986 case Type::Complex:
2987 // Distinct complex types are incompatible.
2988 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002989 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002990 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002991 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2992 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002993 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002994 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002995 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002996 // FIXME: This should be type compatibility, e.g. whether
2997 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002998 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2999 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3000 if (LHSIface && RHSIface &&
3001 canAssignObjCInterfaces(LHSIface, RHSIface))
3002 return LHS;
3003
Eli Friedman3d815e72008-08-22 00:56:42 +00003004 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003005 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003006 case Type::ObjCQualifiedId:
3007 // Distinct qualified id's are not compatible.
3008 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003009 case Type::FixedWidthInt:
3010 // Distinct fixed-width integers are not compatible.
3011 return QualType();
3012 case Type::ObjCQualifiedClass:
3013 // Distinct qualified classes are not compatible.
3014 return QualType();
3015 case Type::ExtQual:
3016 // FIXME: ExtQual types can be compatible even if they're not
3017 // identical!
3018 return QualType();
3019 // First attempt at an implementation, but I'm not really sure it's
3020 // right...
3021#if 0
3022 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3023 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3024 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3025 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3026 return QualType();
3027 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3028 LHSBase = QualType(LQual->getBaseType(), 0);
3029 RHSBase = QualType(RQual->getBaseType(), 0);
3030 ResultType = mergeTypes(LHSBase, RHSBase);
3031 if (ResultType.isNull()) return QualType();
3032 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3033 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3034 return LHS;
3035 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3036 return RHS;
3037 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3038 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3039 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3040 return ResultType;
3041#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00003042 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003043
3044 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003045}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003046
Chris Lattner5426bf62008-04-07 07:01:58 +00003047//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003048// Integer Predicates
3049//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003050
Eli Friedmanad74a752008-06-28 06:23:08 +00003051unsigned ASTContext::getIntWidth(QualType T) {
3052 if (T == BoolTy)
3053 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003054 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3055 return FWIT->getWidth();
3056 }
3057 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003058 return (unsigned)getTypeSize(T);
3059}
3060
3061QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3062 assert(T->isSignedIntegerType() && "Unexpected type");
3063 if (const EnumType* ETy = T->getAsEnumType())
3064 T = ETy->getDecl()->getIntegerType();
3065 const BuiltinType* BTy = T->getAsBuiltinType();
3066 assert (BTy && "Unexpected signed integer type");
3067 switch (BTy->getKind()) {
3068 case BuiltinType::Char_S:
3069 case BuiltinType::SChar:
3070 return UnsignedCharTy;
3071 case BuiltinType::Short:
3072 return UnsignedShortTy;
3073 case BuiltinType::Int:
3074 return UnsignedIntTy;
3075 case BuiltinType::Long:
3076 return UnsignedLongTy;
3077 case BuiltinType::LongLong:
3078 return UnsignedLongLongTy;
3079 default:
3080 assert(0 && "Unexpected signed integer type");
3081 return QualType();
3082 }
3083}
3084
3085
3086//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003087// Serialization Support
3088//===----------------------------------------------------------------------===//
3089
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003090/// Emit - Serialize an ASTContext object to Bitcode.
3091void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003092 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003093 S.EmitRef(SourceMgr);
3094 S.EmitRef(Target);
3095 S.EmitRef(Idents);
3096 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003097
Ted Kremenekfee04522007-10-31 22:44:07 +00003098 // Emit the size of the type vector so that we can reserve that size
3099 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003100 S.EmitInt(Types.size());
3101
Ted Kremenek03ed4402007-11-13 22:02:55 +00003102 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3103 I!=E;++I)
3104 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003105
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003106 S.EmitOwnedPtr(TUDecl);
3107
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003108 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003109}
3110
Ted Kremenek0f84c002007-11-13 00:25:37 +00003111ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003112
3113 // Read the language options.
3114 LangOptions LOpts;
3115 LOpts.Read(D);
3116
Ted Kremenekfee04522007-10-31 22:44:07 +00003117 SourceManager &SM = D.ReadRef<SourceManager>();
3118 TargetInfo &t = D.ReadRef<TargetInfo>();
3119 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3120 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003121
Ted Kremenekfee04522007-10-31 22:44:07 +00003122 unsigned size_reserve = D.ReadInt();
3123
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003124 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3125 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003126
Ted Kremenek03ed4402007-11-13 22:02:55 +00003127 for (unsigned i = 0; i < size_reserve; ++i)
3128 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003129
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003130 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3131
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003132 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003133
3134 return A;
3135}