blob: e433769a9ce882cff1c42e534384dccd719303f7 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-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 Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregor1e589cc2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
81 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Eli Friedman37eb4a42009-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 Gregor1e589cc2009-03-26 23:50:42 +000092
93 if (GlobalNestedNameSpecifier)
94 GlobalNestedNameSpecifier->Destroy(*this);
95
Eli Friedman65489b72008-05-27 03:08:09 +000096 TUDecl->Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000097
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000104 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000105 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
106 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
107
Chris Lattner4b009652007-07-25 00:24:17 +0000108 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
110 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000111 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000119 else if (isa<BlockPointerType>(T))
120 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000121 else if (isa<LValueReferenceType>(T))
122 ++NumLValueReference;
123 else if (isa<RValueReferenceType>(T))
124 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000125 else if (isa<MemberPointerType>(T))
126 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000127 else if (isa<ComplexType>(T))
128 ++NumComplex;
129 else if (isa<ArrayType>(T))
130 ++NumArray;
131 else if (isa<VectorType>(T))
132 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000133 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000135 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000136 ++NumFunctionP;
137 else if (isa<TypedefType>(T))
138 ++NumTypeName;
139 else if (TagType *TT = dyn_cast<TagType>(T)) {
140 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000141 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000147 }
Ted Kremenek42730c52008-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 Naroffe0430632008-05-21 15:59:22 +0000154 else if (isa<TypeOfType>(T))
155 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000156 else if (isa<TypeOfExprType>(T))
157 ++NumTypeOfExprTypes;
Steve Naroff948fd372007-09-17 14:16:13 +0000158 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
171 fprintf(stderr, " %d array types\n", NumArray);
172 fprintf(stderr, " %d vector types\n", NumVector);
173 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 Kremenek42730c52008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000188
Chris Lattner4b009652007-07-25 00:24:17 +0000189 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
190 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
191 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000192 NumLValueReference*sizeof(LValueReferenceType)+
193 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000194 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000195 NumFunctionP*sizeof(FunctionProtoType)+
196 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000197 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000198 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Chris Lattner4b009652007-07-25 00:24:17 +0000199}
200
201
202void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000203 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000204}
205
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000215 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +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);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000237
Chris Lattnere1dafe72009-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());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000242
Douglas Gregord2baafd2008-10-21 16:13:35 +0000243 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-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 Gregord2baafd2008-10-21 16:13:35 +0000252
Chris Lattner4b009652007-07-25 00:24:17 +0000253 // C99 6.2.5p11.
254 FloatComplexTy = getComplexType(FloatTy);
255 DoubleComplexTy = getComplexType(DoubleTy);
256 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000257
Steve Naroff9d12c902007-10-15 14:41:52 +0000258 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000259 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000260 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000261 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000262 ClassStructType = 0;
263
Ted Kremenek42730c52008-01-07 19:49:32 +0000264 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000265
266 // void * type
267 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000268}
269
270//===----------------------------------------------------------------------===//
271// Type Sizing and Analysis
272//===----------------------------------------------------------------------===//
273
Chris Lattner2a674dc2008-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 Lattnerbd3153e2009-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 Dunbar96d1f1b2009-02-17 22:16:19 +0000290unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-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 Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000305 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000306
307 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000308}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000309
Chris Lattner4b009652007-07-25 00:24:17 +0000310/// getTypeSize - Return the size of the specified type, in bits. This method
311/// does not work on incomplete types.
312std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000313ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000314 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000315 uint64_t Width=0;
316 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-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 Lattner4b009652007-07-25 00:24:17 +0000326 case Type::FunctionNoProto:
327 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000328 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000329 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000330 case Type::VariableArray:
331 assert(0 && "VLAs not implemented yet!");
332 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000333 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000334
Chris Lattner8cd0e932008-03-05 18:54:05 +0000335 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000336 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000337 Align = EltInfo.second;
338 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000339 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000340 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000341 case Type::Vector: {
342 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000343 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000344 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000345 Align = Width;
Nate Begeman7903d052009-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 Lattner4b009652007-07-25 00:24:17 +0000350 break;
351 }
352
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000353 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000354 switch (cast<BuiltinType>(T)->getKind()) {
355 default: assert(0 && "Unknown builtin type!");
356 case BuiltinType::Void:
357 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = Target.getBoolWidth();
360 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000361 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000362 case BuiltinType::Char_S:
363 case BuiltinType::Char_U:
364 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000365 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000366 Width = Target.getCharWidth();
367 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000368 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000369 case BuiltinType::WChar:
370 Width = Target.getWCharWidth();
371 Align = Target.getWCharAlign();
372 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000373 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000374 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000375 Width = Target.getShortWidth();
376 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000377 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000378 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000379 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000380 Width = Target.getIntWidth();
381 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000382 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000383 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000384 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000385 Width = Target.getLongWidth();
386 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000387 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000388 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000389 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000390 Width = Target.getLongLongWidth();
391 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000392 break;
393 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000394 Width = Target.getFloatWidth();
395 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000396 break;
397 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000398 Width = Target.getDoubleWidth();
399 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000400 break;
401 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000402 Width = Target.getLongDoubleWidth();
403 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000404 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000405 }
406 break;
Eli Friedmanff3fcdf2009-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 Lattnere9174982009-02-15 21:20:13 +0000411 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000412 Align = Width;
413 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000414 case Type::ExtQual:
Chris Lattner8cd0e932008-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 Jahanianb60352a2009-02-17 18:27:45 +0000417 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000418 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000419 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000420 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000421 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000422 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000423 break;
Steve Naroff62f09f52008-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 Lattner461a6c52008-03-08 08:34:58 +0000430 case Type::Pointer: {
431 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000432 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000433 Align = Target.getPointerAlign(AS);
434 break;
435 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000436 case Type::LValueReference:
437 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000438 // "When applied to a reference or a reference type, the result is the size
439 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000440 // FIXME: This is wrong for struct layout: a reference in a struct has
441 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000442 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000443 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000444 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-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 Redl18cffee2009-01-24 23:29:36 +0000447 // other compilers too, we need to delegate this completely to TargetInfo
448 // or some ABI abstraction layer.
Sebastian Redl75555032009-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 Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000461 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000462 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000463 Align = EltInfo.second;
464 break;
465 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000466 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000467 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000468 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
469 Width = Layout.getSize();
470 Align = Layout.getAlignment();
471 break;
472 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000473 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000474 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000475 const TagType *TT = cast<TagType>(T);
476
477 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000478 Width = 1;
479 Align = 1;
480 break;
481 }
482
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000483 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000484 return getTypeInfo(ET->getDecl()->getIntegerType());
485
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000486 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000487 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
488 Width = Layout.getSize();
489 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000490 break;
491 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000492 }
Chris Lattner4b009652007-07-25 00:24:17 +0000493
494 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000495 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000496}
497
Chris Lattner83165b52009-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 Dunbarc61a8002009-02-18 19:59:32 +0000506 if (T->isSpecificBuiltinType(BuiltinType::Double))
507 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000508
509 return ABIAlign;
510}
511
512
Devang Patelbfe323c2008-06-04 21:22:16 +0000513/// LayoutField - Field layout.
514void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000515 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000516 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000517 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000518 uint64_t FieldOffset = IsUnion ? 0 : Size;
519 uint64_t FieldSize;
520 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar7cbcbf42008-08-13 23:47:13 +0000530 FieldSize =
531 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-06-04 21:22:16 +0000542 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000543 if (FieldPacking)
544 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-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 Lattnerfd799692008-08-09 21:35:13 +0000557 if (FD->getType()->isIncompleteArrayType()) {
558 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-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 Lattnera1923f62008-08-04 07:31:14 +0000563 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-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 Jahaniana2c97df2009-03-05 20:08:48 +0000602void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
603 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahanian0556b152008-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000635 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000636 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000637 }
638 NewRD->completeDefinition(*this);
639 RD = NewRD;
640 return RD;
641}
Devang Patel4b6bf702008-06-04 21:54:36 +0000642
Fariborz Jahanianea944842008-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 Lattner2fda0ed2008-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 Patel4b6bf702008-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 Patel8682d882008-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 Lattner2fda0ed2008-10-05 17:34:18 +0000673 // Super class is at the beginning of the layout.
674 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000675 } else {
676 NewEntry = new ASTRecordLayout();
677 NewEntry->InitializeLayout(FieldCount);
678 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000679 Entry = NewEntry;
680
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000681 unsigned StructPacking = 0;
682 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
683 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-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 Dunbar2cb762f2008-10-16 02:34:03 +0000694 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-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 Patel7a78e432007-11-01 19:11:01 +0000703/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000704/// specified record (struct/union/class), which indicates its size and field
705/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000706const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000707 D = D->getDefinition(*this);
708 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000709
Chris Lattner4b009652007-07-25 00:24:17 +0000710 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000711 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000712 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000713
Devang Patel7a78e432007-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 Lattner4b009652007-07-25 00:24:17 +0000717 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000718
Douglas Gregor39677622008-12-11 20:41:00 +0000719 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000720 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000721 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000722
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000723 unsigned StructPacking = 0;
724 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
725 StructPacking = PA->getAlignment();
726
Eli Friedman5949a022008-05-30 09:31:38 +0000727 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000728 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
729 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000730
Eli Friedman5949a022008-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 Gregor8acb7272008-12-11 16:49:14 +0000733 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000734 for (RecordDecl::field_iterator Field = D->field_begin(),
735 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000736 Field != FieldEnd; (void)++Field, ++FieldIdx)
737 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-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 Patelbfe323c2008-06-04 21:22:16 +0000741 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000742 return *NewEntry;
743}
744
Chris Lattner4b009652007-07-25 00:24:17 +0000745//===----------------------------------------------------------------------===//
746// Type creation/memoization methods
747//===----------------------------------------------------------------------===//
748
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000749QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000750 QualType CanT = getCanonicalType(T);
751 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000752 return T;
Chris Lattner18b5a9a2009-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 Lattner35fef522008-02-20 20:55:12 +0000759
Chris Lattner18b5a9a2009-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 Lattner35fef522008-02-20 20:55:12 +0000768
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000769 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000770 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000771 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000772 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000773 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000774 return QualType(EXTQy, CVRQuals);
775
Christopher Lamb2a72bb32008-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 Lattner18b5a9a2009-02-18 22:53:11 +0000779 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000780 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000781
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000782 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000783 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000784 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000785 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000786 ExtQualType *New =
787 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000788 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000789 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000790 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000791}
792
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793QualType ASTContext::getObjCGCQualType(QualType T,
794 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000795 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000796 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000797 return T;
798
Chris Lattner18b5a9a2009-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 Jahanianaf238092009-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 Lattner18b5a9a2009-02-18 22:53:11 +0000816 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000817 void *InsertPos = 0;
818 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000819 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-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 Friedman94fcc9a2009-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 Jahanianaf238092009-02-18 05:09:49 +0000825 QualType Canonical;
826 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000827 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000828
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000829 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-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 Lattner18b5a9a2009-02-18 22:53:11 +0000833 ExtQualType *New =
834 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000835 ExtQualTypes.InsertNode(New, InsertPos);
836 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000837 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000838}
Chris Lattner4b009652007-07-25 00:24:17 +0000839
840/// 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 Lattnerc1b68db2008-04-06 22:59:24 +0000856 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000857
858 // Get the new insert position for the node we care about.
859 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000860 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000861 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000862 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000863 Types.push_back(New);
864 ComplexTypes.InsertNode(New, InsertPos);
865 return QualType(New, 0);
866}
867
Eli Friedmanff3fcdf2009-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}
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000893 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000894
895 // Get the new insert position for the node we care about.
896 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000897 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000898 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000899 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000900 Types.push_back(New);
901 PointerTypes.InsertNode(New, InsertPos);
902 return QualType(New, 0);
903}
904
Steve Naroff7aa54752008-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 Narofffd5b19d2008-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 Naroff7aa54752008-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 Narofffd5b19d2008-08-28 19:20:44 +0000919 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-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 Lattner578a37e2008-10-12 00:26:57 +0000928 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000929 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000930 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000931 Types.push_back(New);
932 BlockPointerTypes.InsertNode(New, InsertPos);
933 return QualType(New, 0);
934}
935
Sebastian Redlce6fff02009-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) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlce6fff02009-03-16 23:22:08 +0000945 if (LValueReferenceType *RT =
946 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000947 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000948
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlce6fff02009-03-16 23:22:08 +0000953 Canonical = getLValueReferenceType(getCanonicalType(T));
954
Chris Lattner4b009652007-07-25 00:24:17 +0000955 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000956 LValueReferenceType *NewIP =
957 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000958 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000959 }
960
Sebastian Redlce6fff02009-03-16 23:22:08 +0000961 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 Types.push_back(New);
Sebastian Redlce6fff02009-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000995 return QualType(New, 0);
996}
997
Sebastian Redl75555032009-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 Naroff93fd2112009-01-27 22:08:43 +00001023 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001024 Types.push_back(New);
1025 MemberPointerTypes.InsertNode(New, InsertPos);
1026 return QualType(New, 0);
1027}
1028
Steve Naroff83c13012007-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 Naroff24c9b982007-08-30 18:10:14 +00001032 const llvm::APInt &ArySize,
1033 ArrayType::ArraySizeModifier ASM,
1034 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001035 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001036 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001037
1038 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001039 if (ConstantArrayType *ATP =
1040 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001047 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001048 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001049 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001050 ConstantArrayType *NewIP =
1051 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001052 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001053 }
1054
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001055 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001056 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001057 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001058 Types.push_back(New);
1059 return QualType(New, 0);
1060}
1061
Steve Naroffe2579e32007-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 Naroff24c9b982007-08-30 18:10:14 +00001064QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1065 ArrayType::ArraySizeModifier ASM,
1066 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-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 Kremenekc70e7d02009-01-19 21:31:22 +00001070 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001071 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001072
1073 VariableArrayTypes.push_back(New);
1074 Types.push_back(New);
1075 return QualType(New, 0);
1076}
1077
Douglas Gregor1b21c7f2008-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 Kremenekc70e7d02009-01-19 21:31:22 +00001091 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001092 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1093 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001094
1095 DependentSizedArrayTypes.push_back(New);
1096 Types.push_back(New);
1097 return QualType(New, 0);
1098}
1099
Eli Friedman8ff07782008-02-15 18:16:39 +00001100QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1101 ArrayType::ArraySizeModifier ASM,
1102 unsigned EltTypeQuals) {
1103 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001104 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-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 Lattnerc1b68db2008-04-06 22:59:24 +00001116 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001117 ASM, EltTypeQuals);
Eli Friedman8ff07782008-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 Lattner578a37e2008-10-12 00:26:57 +00001122 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001123 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001124
Steve Naroff93fd2112009-01-27 22:08:43 +00001125 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001126 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001127
1128 IncompleteArrayTypes.InsertNode(New, InsertPos);
1129 Types.push_back(New);
1130 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001131}
1132
Chris Lattner4b009652007-07-25 00:24:17 +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) {
1136 BuiltinType *baseType;
1137
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001138 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001139 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1140
1141 // Check if we've already instantiated a vector of this type.
1142 llvm::FoldingSetNodeID ID;
1143 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1144 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 Lattnerc1b68db2008-04-06 22:59:24 +00001152 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001153
1154 // Get the new insert position for the node we care about.
1155 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001156 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001157 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001158 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001159 VectorTypes.InsertNode(New, InsertPos);
1160 Types.push_back(New);
1161 return QualType(New, 0);
1162}
1163
Nate Begemanaf6ed502008-04-18 23:10:10 +00001164/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001165/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001166QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001167 BuiltinType *baseType;
1168
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001169 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001170 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001171
1172 // Check if we've already instantiated a vector of this type.
1173 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001174 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +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 Begemanaf6ed502008-04-18 23:10:10 +00001183 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001184
1185 // Get the new insert position for the node we care about.
1186 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001187 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001188 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001189 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001190 VectorTypes.InsertNode(New, InsertPos);
1191 Types.push_back(New);
1192 return QualType(New, 0);
1193}
1194
Douglas Gregor4fa58902009-02-26 23:50:07 +00001195/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001196///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001197QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001198 // Unique functions, to guarantee there is only one function of a particular
1199 // structure.
1200 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001201 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001202
1203 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001204 if (FunctionNoProtoType *FT =
1205 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001206 return QualType(FT, 0);
1207
1208 QualType Canonical;
1209 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001210 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001211
1212 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001213 FunctionNoProtoType *NewIP =
1214 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001216 }
1217
Douglas Gregor4fa58902009-02-26 23:50:07 +00001218 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001219 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001220 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner2fda0ed2008-10-05 17:34:18 +00001226QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001227 unsigned NumArgs, bool isVariadic,
1228 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001229 // Unique functions, to guarantee there is only one function of a particular
1230 // structure.
1231 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001232 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001233 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001234
1235 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001236 if (FunctionProtoType *FTP =
1237 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001252 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001253
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001254 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001255 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001256 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001257
1258 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001259 FunctionProtoType *NewIP =
1260 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001261 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001262 }
1263
Douglas Gregor4fa58902009-02-26 23:50:07 +00001264 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001265 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001266 FunctionProtoType *FTP =
1267 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001268 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001270 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001271 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001272 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001273 return QualType(FTP, 0);
1274}
1275
Douglas Gregor1d661552008-04-13 21:07:44 +00001276/// getTypeDeclType - Return the unique reference to the type for the
1277/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001278QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001279 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001280 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1281
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001282 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001283 return getTypedefType(Typedef);
Douglas Gregora4918772009-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 Gregor1d661552008-04-13 21:07:44 +00001287 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001288
Douglas Gregor2e047592009-02-28 01:32:25 +00001289 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001290 if (PrevDecl)
1291 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001292 else
1293 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001294 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001295 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1296 if (PrevDecl)
1297 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001298 else
1299 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001300 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001301 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001302 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001303
Ted Kremenek46a837c2008-09-05 17:16:31 +00001304 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001305 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001306}
1307
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001313 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001314 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001315 Types.push_back(Decl->TypeForDecl);
1316 return QualType(Decl->TypeForDecl, 0);
1317}
1318
Ted Kremenek42730c52008-01-07 19:49:32 +00001319/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001320/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001321QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001322 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1323
Steve Naroff93fd2112009-01-27 22:08:43 +00001324 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001325 Types.push_back(Decl->TypeForDecl);
1326 return QualType(Decl->TypeForDecl, 0);
1327}
1328
Fariborz Jahanian27ecc672009-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 Gregora4918772009-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 Gregor8e458f42009-02-09 18:46:07 +00001366QualType
1367ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001368 const TemplateArgument *Args,
Douglas Gregor8e458f42009-02-09 18:46:07 +00001369 unsigned NumArgs,
Douglas Gregor8e458f42009-02-09 18:46:07 +00001370 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001371 if (!Canon.isNull())
1372 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001373
Douglas Gregor8e458f42009-02-09 18:46:07 +00001374 llvm::FoldingSetNodeID ID;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001375 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1376
Douglas Gregor8e458f42009-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 Gregorf9ff4b12009-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 Gregor8e458f42009-02-09 18:46:07 +00001389 Types.push_back(Spec);
1390 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1391
1392 return QualType(Spec, 0);
1393}
1394
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001395QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001396ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001397 QualType NamedType) {
1398 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001399 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-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 Gregor1e589cc2009-03-26 23:50:42 +00001407 T = new (*this) QualifiedNameType(NNS, NamedType,
1408 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001409 Types.push_back(T);
1410 QualifiedNameTypes.InsertNode(T, InsertPos);
1411 return QualType(T, 0);
1412}
1413
Chris Lattnere1352302008-04-07 04:56:42 +00001414/// CmpProtocolNames - Comparison predicate for sorting protocols
1415/// alphabetically.
1416static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1417 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001418 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001419}
1420
1421static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1422 unsigned &NumProtocols) {
1423 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1424
1425 // Sort protocols, keyed by name.
1426 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1427
1428 // Remove duplicates.
1429 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1430 NumProtocols = ProtocolsEnd-Protocols;
1431}
1432
1433
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001434/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1435/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001436QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1437 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001438 // Sort the protocol list alphabetically to canonicalize it.
1439 SortAndUniqueProtocols(Protocols, NumProtocols);
1440
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001441 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001442 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001443
1444 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001445 if (ObjCQualifiedInterfaceType *QT =
1446 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001447 return QualType(QT, 0);
1448
1449 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001450 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001451 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001452
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001453 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001454 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001455 return QualType(QType, 0);
1456}
1457
Chris Lattnere1352302008-04-07 04:56:42 +00001458/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1459/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001460QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001461 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001462 // Sort the protocol list alphabetically to canonicalize it.
1463 SortAndUniqueProtocols(Protocols, NumProtocols);
1464
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001465 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001466 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001467
1468 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001469 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001470 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001471 return QualType(QT, 0);
1472
1473 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001474 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001475 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001476 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001477 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001478 return QualType(QType, 0);
1479}
1480
Douglas Gregor4fa58902009-02-26 23:50:07 +00001481/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1482/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001483/// multiple declarations that refer to "typeof(x)" all contain different
1484/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1485/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001486QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001487 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001488 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001489 Types.push_back(toe);
1490 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001491}
1492
Steve Naroff0604dd92007-08-01 18:02:17 +00001493/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1494/// TypeOfType AST's. The only motivation to unique these nodes would be
1495/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1496/// an issue. This doesn't effect the type checker, since it operates
1497/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001498QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001499 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001500 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001501 Types.push_back(tot);
1502 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001503}
1504
Chris Lattner4b009652007-07-25 00:24:17 +00001505/// getTagDeclType - Return the unique reference to the type for the
1506/// specified TagDecl (struct/union/class/enum) decl.
1507QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001508 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001509 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001510}
1511
1512/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1513/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1514/// needs to agree with the definition in <stddef.h>.
1515QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001516 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001517}
1518
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001519/// getSignedWCharType - Return the type of "signed wchar_t".
1520/// Used when in C++, as a GCC extension.
1521QualType ASTContext::getSignedWCharType() const {
1522 // FIXME: derive from "Target" ?
1523 return WCharTy;
1524}
1525
1526/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1527/// Used when in C++, as a GCC extension.
1528QualType ASTContext::getUnsignedWCharType() const {
1529 // FIXME: derive from "Target" ?
1530 return UnsignedIntTy;
1531}
1532
Chris Lattner4b009652007-07-25 00:24:17 +00001533/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1534/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1535QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001536 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001537}
1538
Chris Lattner19eb97e2008-04-02 05:18:44 +00001539//===----------------------------------------------------------------------===//
1540// Type Operators
1541//===----------------------------------------------------------------------===//
1542
Chris Lattner3dae6f42008-04-06 22:41:35 +00001543/// getCanonicalType - Return the canonical (structural) type corresponding to
1544/// the specified potentially non-canonical type. The non-canonical version
1545/// of a type may have many "decorated" versions of types. Decorators can
1546/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1547/// to be free of any of these, allowing two canonical types to be compared
1548/// for exact equality with a simple pointer comparison.
1549QualType ASTContext::getCanonicalType(QualType T) {
1550 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001551
1552 // If the result has type qualifiers, make sure to canonicalize them as well.
1553 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1554 if (TypeQuals == 0) return CanType;
1555
1556 // If the type qualifiers are on an array type, get the canonical type of the
1557 // array with the qualifiers applied to the element type.
1558 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1559 if (!AT)
1560 return CanType.getQualifiedType(TypeQuals);
1561
1562 // Get the canonical version of the element with the extra qualifiers on it.
1563 // This can recursively sink qualifiers through multiple levels of arrays.
1564 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1565 NewEltTy = getCanonicalType(NewEltTy);
1566
1567 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1568 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1569 CAT->getIndexTypeQualifier());
1570 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1571 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1572 IAT->getIndexTypeQualifier());
1573
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001574 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1575 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1576 DSAT->getSizeModifier(),
1577 DSAT->getIndexTypeQualifier());
1578
Chris Lattnera1923f62008-08-04 07:31:14 +00001579 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1580 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1581 VAT->getSizeModifier(),
1582 VAT->getIndexTypeQualifier());
1583}
1584
1585
1586const ArrayType *ASTContext::getAsArrayType(QualType T) {
1587 // Handle the non-qualified case efficiently.
1588 if (T.getCVRQualifiers() == 0) {
1589 // Handle the common positive case fast.
1590 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1591 return AT;
1592 }
1593
1594 // Handle the common negative case fast, ignoring CVR qualifiers.
1595 QualType CType = T->getCanonicalTypeInternal();
1596
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001597 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001598 // test.
1599 if (!isa<ArrayType>(CType) &&
1600 !isa<ArrayType>(CType.getUnqualifiedType()))
1601 return 0;
1602
1603 // Apply any CVR qualifiers from the array type to the element type. This
1604 // implements C99 6.7.3p8: "If the specification of an array type includes
1605 // any type qualifiers, the element type is so qualified, not the array type."
1606
1607 // If we get here, we either have type qualifiers on the type, or we have
1608 // sugar such as a typedef in the way. If we have type qualifiers on the type
1609 // we must propagate them down into the elemeng type.
1610 unsigned CVRQuals = T.getCVRQualifiers();
1611 unsigned AddrSpace = 0;
1612 Type *Ty = T.getTypePtr();
1613
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001614 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001615 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001616 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1617 AddrSpace = EXTQT->getAddressSpace();
1618 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001619 } else {
1620 T = Ty->getDesugaredType();
1621 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1622 break;
1623 CVRQuals |= T.getCVRQualifiers();
1624 Ty = T.getTypePtr();
1625 }
1626 }
1627
1628 // If we have a simple case, just return now.
1629 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1630 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1631 return ATy;
1632
1633 // Otherwise, we have an array and we have qualifiers on it. Push the
1634 // qualifiers into the array element type and return a new array type.
1635 // Get the canonical version of the element with the extra qualifiers on it.
1636 // This can recursively sink qualifiers through multiple levels of arrays.
1637 QualType NewEltTy = ATy->getElementType();
1638 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001639 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001640 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1641
1642 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1643 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1644 CAT->getSizeModifier(),
1645 CAT->getIndexTypeQualifier()));
1646 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1647 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1648 IAT->getSizeModifier(),
1649 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001650
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001651 if (const DependentSizedArrayType *DSAT
1652 = dyn_cast<DependentSizedArrayType>(ATy))
1653 return cast<ArrayType>(
1654 getDependentSizedArrayType(NewEltTy,
1655 DSAT->getSizeExpr(),
1656 DSAT->getSizeModifier(),
1657 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001658
Chris Lattnera1923f62008-08-04 07:31:14 +00001659 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1660 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1661 VAT->getSizeModifier(),
1662 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001663}
1664
1665
Chris Lattner19eb97e2008-04-02 05:18:44 +00001666/// getArrayDecayedType - Return the properly qualified result of decaying the
1667/// specified array type to a pointer. This operation is non-trivial when
1668/// handling typedefs etc. The canonical type of "T" must be an array type,
1669/// this returns a pointer to a properly qualified element of the array.
1670///
1671/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1672QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001673 // Get the element type with 'getAsArrayType' so that we don't lose any
1674 // typedefs in the element type of the array. This also handles propagation
1675 // of type qualifiers from the array type into the element type if present
1676 // (C99 6.7.3p8).
1677 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1678 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001679
Chris Lattnera1923f62008-08-04 07:31:14 +00001680 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001681
1682 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001683 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001684}
1685
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001686QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001687 QualType ElemTy = VAT->getElementType();
1688
1689 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1690 return getBaseElementType(VAT);
1691
1692 return ElemTy;
1693}
1694
Chris Lattner4b009652007-07-25 00:24:17 +00001695/// getFloatingRank - Return a relative rank for floating point types.
1696/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001697static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001698 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001699 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001700
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001701 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001702 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001703 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001704 case BuiltinType::Float: return FloatRank;
1705 case BuiltinType::Double: return DoubleRank;
1706 case BuiltinType::LongDouble: return LongDoubleRank;
1707 }
1708}
1709
Steve Narofffa0c4532007-08-27 01:41:48 +00001710/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1711/// point or a complex type (based on typeDomain/typeSize).
1712/// 'typeDomain' is a real floating point or complex type.
1713/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001714QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1715 QualType Domain) const {
1716 FloatingRank EltRank = getFloatingRank(Size);
1717 if (Domain->isComplexType()) {
1718 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001719 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001720 case FloatRank: return FloatComplexTy;
1721 case DoubleRank: return DoubleComplexTy;
1722 case LongDoubleRank: return LongDoubleComplexTy;
1723 }
Chris Lattner4b009652007-07-25 00:24:17 +00001724 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001725
1726 assert(Domain->isRealFloatingType() && "Unknown domain!");
1727 switch (EltRank) {
1728 default: assert(0 && "getFloatingRank(): illegal value for rank");
1729 case FloatRank: return FloatTy;
1730 case DoubleRank: return DoubleTy;
1731 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001732 }
Chris Lattner4b009652007-07-25 00:24:17 +00001733}
1734
Chris Lattner51285d82008-04-06 23:55:33 +00001735/// getFloatingTypeOrder - Compare the rank of the two specified floating
1736/// point types, ignoring the domain of the type (i.e. 'double' ==
1737/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1738/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001739int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1740 FloatingRank LHSR = getFloatingRank(LHS);
1741 FloatingRank RHSR = getFloatingRank(RHS);
1742
1743 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001744 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001745 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001746 return 1;
1747 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001748}
1749
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001750/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1751/// routine will assert if passed a built-in type that isn't an integer or enum,
1752/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001753unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001754 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001755 if (EnumType* ET = dyn_cast<EnumType>(T))
1756 T = ET->getDecl()->getIntegerType().getTypePtr();
1757
1758 // There are two things which impact the integer rank: the width, and
1759 // the ordering of builtins. The builtin ordering is encoded in the
1760 // bottom three bits; the width is encoded in the bits above that.
1761 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1762 return FWIT->getWidth() << 3;
1763 }
1764
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001765 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001766 default: assert(0 && "getIntegerRank(): not a built-in integer");
1767 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001768 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001769 case BuiltinType::Char_S:
1770 case BuiltinType::Char_U:
1771 case BuiltinType::SChar:
1772 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001773 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001774 case BuiltinType::Short:
1775 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001776 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001777 case BuiltinType::Int:
1778 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001779 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001780 case BuiltinType::Long:
1781 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001782 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001783 case BuiltinType::LongLong:
1784 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001785 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001786 }
1787}
1788
Chris Lattner51285d82008-04-06 23:55:33 +00001789/// getIntegerTypeOrder - Returns the highest ranked integer type:
1790/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1791/// LHS < RHS, return -1.
1792int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001793 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1794 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001795 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001796
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001797 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1798 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001799
Chris Lattner51285d82008-04-06 23:55:33 +00001800 unsigned LHSRank = getIntegerRank(LHSC);
1801 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001802
Chris Lattner51285d82008-04-06 23:55:33 +00001803 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1804 if (LHSRank == RHSRank) return 0;
1805 return LHSRank > RHSRank ? 1 : -1;
1806 }
Chris Lattner4b009652007-07-25 00:24:17 +00001807
Chris Lattner51285d82008-04-06 23:55:33 +00001808 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1809 if (LHSUnsigned) {
1810 // If the unsigned [LHS] type is larger, return it.
1811 if (LHSRank >= RHSRank)
1812 return 1;
1813
1814 // If the signed type can represent all values of the unsigned type, it
1815 // wins. Because we are dealing with 2's complement and types that are
1816 // powers of two larger than each other, this is always safe.
1817 return -1;
1818 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001819
Chris Lattner51285d82008-04-06 23:55:33 +00001820 // If the unsigned [RHS] type is larger, return it.
1821 if (RHSRank >= LHSRank)
1822 return -1;
1823
1824 // If the signed type can represent all values of the unsigned type, it
1825 // wins. Because we are dealing with 2's complement and types that are
1826 // powers of two larger than each other, this is always safe.
1827 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001828}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001829
1830// getCFConstantStringType - Return the type used for constant CFStrings.
1831QualType ASTContext::getCFConstantStringType() {
1832 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001833 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001834 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001835 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001836 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001837
1838 // const int *isa;
1839 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001840 // int flags;
1841 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001842 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001843 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001844 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001845 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001846
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001847 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001848 for (unsigned i = 0; i < 4; ++i) {
1849 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1850 SourceLocation(), 0,
1851 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001852 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001853 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001854 }
1855
1856 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001857 }
1858
1859 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001860}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001861
Anders Carlssonf58cac72008-08-30 19:34:46 +00001862QualType ASTContext::getObjCFastEnumerationStateType()
1863{
1864 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001865 ObjCFastEnumerationStateTypeDecl =
1866 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1867 &Idents.get("__objcFastEnumerationState"));
1868
Anders Carlssonf58cac72008-08-30 19:34:46 +00001869 QualType FieldTypes[] = {
1870 UnsignedLongTy,
1871 getPointerType(ObjCIdType),
1872 getPointerType(UnsignedLongTy),
1873 getConstantArrayType(UnsignedLongTy,
1874 llvm::APInt(32, 5), ArrayType::Normal, 0)
1875 };
1876
Douglas Gregor8acb7272008-12-11 16:49:14 +00001877 for (size_t i = 0; i < 4; ++i) {
1878 FieldDecl *Field = FieldDecl::Create(*this,
1879 ObjCFastEnumerationStateTypeDecl,
1880 SourceLocation(), 0,
1881 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001882 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001883 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001884 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001885
Douglas Gregor8acb7272008-12-11 16:49:14 +00001886 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001887 }
1888
1889 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1890}
1891
Anders Carlssone3f02572007-10-29 06:33:42 +00001892// This returns true if a type has been typedefed to BOOL:
1893// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001894static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001895 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001896 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1897 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001898
1899 return false;
1900}
1901
Ted Kremenek42730c52008-01-07 19:49:32 +00001902/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001903/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001904int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001905 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001906
1907 // Make all integer and enum types at least as large as an int
1908 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001909 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001910 // Treat arrays as pointers, since that's how they're passed in.
1911 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001912 sz = getTypeSize(VoidPtrTy);
1913 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001914}
1915
Ted Kremenek42730c52008-01-07 19:49:32 +00001916/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001917/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001918void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001919 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001920 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001921 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001922 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001923 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001924 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001925 // Compute size of all parameters.
1926 // Start with computing size of a pointer in number of bytes.
1927 // FIXME: There might(should) be a better way of doing this computation!
1928 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001929 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001930 // The first two arguments (self and _cmd) are pointers; account for
1931 // their size.
1932 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001933 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1934 E = Decl->param_end(); PI != E; ++PI) {
1935 QualType PType = (*PI)->getType();
1936 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001937 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001938 ParmOffset += sz;
1939 }
1940 S += llvm::utostr(ParmOffset);
1941 S += "@0:";
1942 S += llvm::utostr(PtrSize);
1943
1944 // Argument types.
1945 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001946 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1947 E = Decl->param_end(); PI != E; ++PI) {
1948 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001949 QualType PType = PVDecl->getOriginalType();
1950 if (const ArrayType *AT =
1951 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1952 // Use array's original type only if it has known number of
1953 // elements.
1954 if (!dyn_cast<ConstantArrayType>(AT))
1955 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001956 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001957 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001958 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001959 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001960 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001961 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001962 }
1963}
1964
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001965/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001966/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001967/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1968/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001969/// Property attributes are stored as a comma-delimited C string. The simple
1970/// attributes readonly and bycopy are encoded as single characters. The
1971/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1972/// encoded as single characters, followed by an identifier. Property types
1973/// are also encoded as a parametrized attribute. The characters used to encode
1974/// these attributes are defined by the following enumeration:
1975/// @code
1976/// enum PropertyAttributes {
1977/// kPropertyReadOnly = 'R', // property is read-only.
1978/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1979/// kPropertyByref = '&', // property is a reference to the value last assigned
1980/// kPropertyDynamic = 'D', // property is dynamic
1981/// kPropertyGetter = 'G', // followed by getter selector name
1982/// kPropertySetter = 'S', // followed by setter selector name
1983/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1984/// kPropertyType = 't' // followed by old-style type encoding.
1985/// kPropertyWeak = 'W' // 'weak' property
1986/// kPropertyStrong = 'P' // property GC'able
1987/// kPropertyNonAtomic = 'N' // property non-atomic
1988/// };
1989/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001990void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1991 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001992 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001993 // Collect information from the property implementation decl(s).
1994 bool Dynamic = false;
1995 ObjCPropertyImplDecl *SynthesizePID = 0;
1996
1997 // FIXME: Duplicated code due to poor abstraction.
1998 if (Container) {
1999 if (const ObjCCategoryImplDecl *CID =
2000 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2001 for (ObjCCategoryImplDecl::propimpl_iterator
2002 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2003 ObjCPropertyImplDecl *PID = *i;
2004 if (PID->getPropertyDecl() == PD) {
2005 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2006 Dynamic = true;
2007 } else {
2008 SynthesizePID = PID;
2009 }
2010 }
2011 }
2012 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002013 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002014 for (ObjCCategoryImplDecl::propimpl_iterator
2015 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2016 ObjCPropertyImplDecl *PID = *i;
2017 if (PID->getPropertyDecl() == PD) {
2018 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2019 Dynamic = true;
2020 } else {
2021 SynthesizePID = PID;
2022 }
2023 }
2024 }
2025 }
2026 }
2027
2028 // FIXME: This is not very efficient.
2029 S = "T";
2030
2031 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002032 // GCC has some special rules regarding encoding of properties which
2033 // closely resembles encoding of ivars.
2034 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2035 true /* outermost type */,
2036 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002037
2038 if (PD->isReadOnly()) {
2039 S += ",R";
2040 } else {
2041 switch (PD->getSetterKind()) {
2042 case ObjCPropertyDecl::Assign: break;
2043 case ObjCPropertyDecl::Copy: S += ",C"; break;
2044 case ObjCPropertyDecl::Retain: S += ",&"; break;
2045 }
2046 }
2047
2048 // It really isn't clear at all what this means, since properties
2049 // are "dynamic by default".
2050 if (Dynamic)
2051 S += ",D";
2052
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002053 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2054 S += ",N";
2055
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002056 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2057 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002058 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002059 }
2060
2061 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2062 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002063 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002064 }
2065
2066 if (SynthesizePID) {
2067 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2068 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002069 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002070 }
2071
2072 // FIXME: OBJCGC: weak & strong
2073}
2074
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002075/// getLegacyIntegralTypeEncoding -
2076/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002077/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002078/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2079///
2080void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2081 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2082 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002083 if (BT->getKind() == BuiltinType::ULong &&
2084 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002085 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002086 else
2087 if (BT->getKind() == BuiltinType::Long &&
2088 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002089 PointeeTy = IntTy;
2090 }
2091 }
2092}
2093
Fariborz Jahanian248db262008-01-22 22:44:46 +00002094void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002095 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002096 // We follow the behavior of gcc, expanding structures which are
2097 // directly pointed to, and expanding embedded structures. Note that
2098 // these rules are sufficient to prevent recursive encoding of the
2099 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002100 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2101 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002102}
2103
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002104static void EncodeBitField(const ASTContext *Context, std::string& S,
2105 FieldDecl *FD) {
2106 const Expr *E = FD->getBitWidth();
2107 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2108 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2109 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2110 S += 'b';
2111 S += llvm::utostr(N);
2112}
2113
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002114void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2115 bool ExpandPointedToStructures,
2116 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002117 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002118 bool OutermostType,
2119 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002120 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002121 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002122 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002123 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002124 else {
2125 char encoding;
2126 switch (BT->getKind()) {
2127 default: assert(0 && "Unhandled builtin type kind");
2128 case BuiltinType::Void: encoding = 'v'; break;
2129 case BuiltinType::Bool: encoding = 'B'; break;
2130 case BuiltinType::Char_U:
2131 case BuiltinType::UChar: encoding = 'C'; break;
2132 case BuiltinType::UShort: encoding = 'S'; break;
2133 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002134 case BuiltinType::ULong:
2135 encoding =
2136 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2137 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002138 case BuiltinType::ULongLong: encoding = 'Q'; break;
2139 case BuiltinType::Char_S:
2140 case BuiltinType::SChar: encoding = 'c'; break;
2141 case BuiltinType::Short: encoding = 's'; break;
2142 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002143 case BuiltinType::Long:
2144 encoding =
2145 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2146 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002147 case BuiltinType::LongLong: encoding = 'q'; break;
2148 case BuiltinType::Float: encoding = 'f'; break;
2149 case BuiltinType::Double: encoding = 'd'; break;
2150 case BuiltinType::LongDouble: encoding = 'd'; break;
2151 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002152
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002153 S += encoding;
2154 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002155 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002156 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002157 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2158 ExpandPointedToStructures,
2159 ExpandStructures, FD);
2160 if (FD || EncodingProperty) {
2161 // Note that we do extended encoding of protocol qualifer list
2162 // Only when doing ivar or property encoding.
2163 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2164 S += '"';
2165 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2166 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2167 S += '<';
2168 S += Proto->getNameAsString();
2169 S += '>';
2170 }
2171 S += '"';
2172 }
2173 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002174 }
2175 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002176 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002177 bool isReadOnly = false;
2178 // For historical/compatibility reasons, the read-only qualifier of the
2179 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2180 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2181 // Also, do not emit the 'r' for anything but the outermost type!
2182 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2183 if (OutermostType && T.isConstQualified()) {
2184 isReadOnly = true;
2185 S += 'r';
2186 }
2187 }
2188 else if (OutermostType) {
2189 QualType P = PointeeTy;
2190 while (P->getAsPointerType())
2191 P = P->getAsPointerType()->getPointeeType();
2192 if (P.isConstQualified()) {
2193 isReadOnly = true;
2194 S += 'r';
2195 }
2196 }
2197 if (isReadOnly) {
2198 // Another legacy compatibility encoding. Some ObjC qualifier and type
2199 // combinations need to be rearranged.
2200 // Rewrite "in const" from "nr" to "rn"
2201 const char * s = S.c_str();
2202 int len = S.length();
2203 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2204 std::string replace = "rn";
2205 S.replace(S.end()-2, S.end(), replace);
2206 }
2207 }
Steve Naroff17c03822009-02-12 17:52:19 +00002208 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002209 S += '@';
2210 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002211 }
2212 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002213 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002214 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002215 // Another historical/compatibility reason.
2216 // We encode the underlying type which comes out as
2217 // {...};
2218 S += '^';
2219 getObjCEncodingForTypeImpl(PointeeTy, S,
2220 false, ExpandPointedToStructures,
2221 NULL);
2222 return;
2223 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002224 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002225 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002226 const ObjCInterfaceType *OIT =
2227 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002228 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002229 S += '"';
2230 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002231 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2232 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2233 S += '<';
2234 S += Proto->getNameAsString();
2235 S += '>';
2236 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002237 S += '"';
2238 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002239 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002240 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002241 S += '#';
2242 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002243 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002244 S += ':';
2245 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002246 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002247
2248 if (PointeeTy->isCharType()) {
2249 // char pointer types should be encoded as '*' unless it is a
2250 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002251 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002252 S += '*';
2253 return;
2254 }
2255 }
2256
2257 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002258 getLegacyIntegralTypeEncoding(PointeeTy);
2259
2260 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002261 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002262 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002263 } else if (const ArrayType *AT =
2264 // Ignore type qualifiers etc.
2265 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002266 if (isa<IncompleteArrayType>(AT)) {
2267 // Incomplete arrays are encoded as a pointer to the array element.
2268 S += '^';
2269
2270 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2271 false, ExpandStructures, FD);
2272 } else {
2273 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002274
Anders Carlsson858c64d2009-02-22 01:38:57 +00002275 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2276 S += llvm::utostr(CAT->getSize().getZExtValue());
2277 else {
2278 //Variable length arrays are encoded as a regular array with 0 elements.
2279 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2280 S += '0';
2281 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002282
Anders Carlsson858c64d2009-02-22 01:38:57 +00002283 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2284 false, ExpandStructures, FD);
2285 S += ']';
2286 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002287 } else if (T->getAsFunctionType()) {
2288 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002289 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002290 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002291 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002292 // Anonymous structures print as '?'
2293 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2294 S += II->getName();
2295 } else {
2296 S += '?';
2297 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002298 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002299 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002300 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2301 FieldEnd = RDecl->field_end();
2302 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002303 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002304 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002305 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002306 S += '"';
2307 }
2308
2309 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002310 if (Field->isBitField()) {
2311 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2312 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002313 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002314 QualType qt = Field->getType();
2315 getLegacyIntegralTypeEncoding(qt);
2316 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002317 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002318 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002319 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002320 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002321 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002322 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002323 if (FD && FD->isBitField())
2324 EncodeBitField(this, S, FD);
2325 else
2326 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002327 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002328 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002329 } else if (T->isObjCInterfaceType()) {
2330 // @encode(class_name)
2331 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2332 S += '{';
2333 const IdentifierInfo *II = OI->getIdentifier();
2334 S += II->getName();
2335 S += '=';
2336 std::vector<FieldDecl*> RecFields;
2337 CollectObjCIvars(OI, RecFields);
2338 for (unsigned int i = 0; i != RecFields.size(); i++) {
2339 if (RecFields[i]->isBitField())
2340 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2341 RecFields[i]);
2342 else
2343 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2344 FD);
2345 }
2346 S += '}';
2347 }
2348 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002349 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002350}
2351
Ted Kremenek42730c52008-01-07 19:49:32 +00002352void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002353 std::string& S) const {
2354 if (QT & Decl::OBJC_TQ_In)
2355 S += 'n';
2356 if (QT & Decl::OBJC_TQ_Inout)
2357 S += 'N';
2358 if (QT & Decl::OBJC_TQ_Out)
2359 S += 'o';
2360 if (QT & Decl::OBJC_TQ_Bycopy)
2361 S += 'O';
2362 if (QT & Decl::OBJC_TQ_Byref)
2363 S += 'R';
2364 if (QT & Decl::OBJC_TQ_Oneway)
2365 S += 'V';
2366}
2367
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002368void ASTContext::setBuiltinVaListType(QualType T)
2369{
2370 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2371
2372 BuiltinVaListType = T;
2373}
2374
Ted Kremenek42730c52008-01-07 19:49:32 +00002375void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002376{
Ted Kremenek42730c52008-01-07 19:49:32 +00002377 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002378
2379 // typedef struct objc_object *id;
2380 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002381 // User error - caller will issue diagnostics.
2382 if (!ptr)
2383 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002384 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002385 // User error - caller will issue diagnostics.
2386 if (!rec)
2387 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002388 IdStructType = rec;
2389}
2390
Ted Kremenek42730c52008-01-07 19:49:32 +00002391void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002392{
Ted Kremenek42730c52008-01-07 19:49:32 +00002393 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002394
2395 // typedef struct objc_selector *SEL;
2396 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002397 if (!ptr)
2398 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002399 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002400 if (!rec)
2401 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002402 SelStructType = rec;
2403}
2404
Ted Kremenek42730c52008-01-07 19:49:32 +00002405void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002406{
Ted Kremenek42730c52008-01-07 19:49:32 +00002407 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002408}
2409
Ted Kremenek42730c52008-01-07 19:49:32 +00002410void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002411{
Ted Kremenek42730c52008-01-07 19:49:32 +00002412 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002413
2414 // typedef struct objc_class *Class;
2415 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2416 assert(ptr && "'Class' incorrectly typed");
2417 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2418 assert(rec && "'Class' incorrectly typed");
2419 ClassStructType = rec;
2420}
2421
Ted Kremenek42730c52008-01-07 19:49:32 +00002422void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2423 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002424 "'NSConstantString' type already set!");
2425
Ted Kremenek42730c52008-01-07 19:49:32 +00002426 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002427}
2428
Douglas Gregorc6507e42008-11-03 14:12:49 +00002429/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002430/// TargetInfo, produce the corresponding type. The unsigned @p Type
2431/// is actually a value of type @c TargetInfo::IntType.
2432QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002433 switch (Type) {
2434 case TargetInfo::NoInt: return QualType();
2435 case TargetInfo::SignedShort: return ShortTy;
2436 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2437 case TargetInfo::SignedInt: return IntTy;
2438 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2439 case TargetInfo::SignedLong: return LongTy;
2440 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2441 case TargetInfo::SignedLongLong: return LongLongTy;
2442 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2443 }
2444
2445 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002446 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002447}
Ted Kremenek118930e2008-07-24 23:58:27 +00002448
2449//===----------------------------------------------------------------------===//
2450// Type Predicates.
2451//===----------------------------------------------------------------------===//
2452
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002453/// isObjCNSObjectType - Return true if this is an NSObject object using
2454/// NSObject attribute on a c-style pointer type.
2455/// FIXME - Make it work directly on types.
2456///
2457bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2458 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2459 if (TypedefDecl *TD = TDT->getDecl())
2460 if (TD->getAttr<ObjCNSObjectAttr>())
2461 return true;
2462 }
2463 return false;
2464}
2465
Ted Kremenek118930e2008-07-24 23:58:27 +00002466/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2467/// to an object type. This includes "id" and "Class" (two 'special' pointers
2468/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2469/// ID type).
2470bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002471 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002472 return true;
2473
Steve Naroffd9e00802008-10-21 18:24:04 +00002474 // Blocks are objects.
2475 if (Ty->isBlockPointerType())
2476 return true;
2477
2478 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002479 if (!Ty->isPointerType())
2480 return false;
2481
2482 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2483 // pointer types. This looks for the typedef specifically, not for the
2484 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002485 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2486 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002487 return true;
2488
2489 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002490 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2491 return true;
2492
2493 // If is has NSObject attribute, OK as well.
2494 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002495}
2496
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002497/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2498/// garbage collection attribute.
2499///
2500QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002501 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002502 if (getLangOptions().ObjC1 &&
2503 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002504 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002505 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002506 // (or pointers to them) be treated as though they were declared
2507 // as __strong.
2508 if (GCAttrs == QualType::GCNone) {
2509 if (isObjCObjectPointerType(Ty))
2510 GCAttrs = QualType::Strong;
2511 else if (Ty->isPointerType())
2512 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2513 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002514 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002515 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002516}
2517
Chris Lattner6ff358b2008-04-07 06:51:04 +00002518//===----------------------------------------------------------------------===//
2519// Type Compatibility Testing
2520//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002521
Steve Naroff3454b6c2008-09-04 15:10:53 +00002522/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002523/// block types. Types must be strictly compatible here. For example,
2524/// C unfortunately doesn't produce an error for the following:
2525///
2526/// int (*emptyArgFunc)();
2527/// int (*intArgList)(int) = emptyArgFunc;
2528///
2529/// For blocks, we will produce an error for the following (similar to C++):
2530///
2531/// int (^emptyArgBlock)();
2532/// int (^intArgBlock)(int) = emptyArgBlock;
2533///
2534/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2535///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002536bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002537 const FunctionType *lbase = lhs->getAsFunctionType();
2538 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002539 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2540 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002541 if (lproto && rproto)
2542 return !mergeTypes(lhs, rhs).isNull();
2543 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002544}
2545
Chris Lattner6ff358b2008-04-07 06:51:04 +00002546/// areCompatVectorTypes - Return true if the two specified vector types are
2547/// compatible.
2548static bool areCompatVectorTypes(const VectorType *LHS,
2549 const VectorType *RHS) {
2550 assert(LHS->isCanonical() && RHS->isCanonical());
2551 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002552 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002553}
2554
Eli Friedman0d9549b2008-08-22 00:56:42 +00002555/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002556/// compatible for assignment from RHS to LHS. This handles validation of any
2557/// protocol qualifiers on the LHS or RHS.
2558///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002559bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2560 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002561 // Verify that the base decls are compatible: the RHS must be a subclass of
2562 // the LHS.
2563 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2564 return false;
2565
2566 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2567 // protocol qualified at all, then we are good.
2568 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2569 return true;
2570
2571 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2572 // isn't a superset.
2573 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2574 return true; // FIXME: should return false!
2575
2576 // Finally, we must have two protocol-qualified interfaces.
2577 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2578 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002579
Steve Naroff98e71b82009-03-01 16:12:44 +00002580 // All LHS protocols must have a presence on the RHS.
2581 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002582
Steve Naroff98e71b82009-03-01 16:12:44 +00002583 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2584 LHSPE = LHSP->qual_end();
2585 LHSPI != LHSPE; LHSPI++) {
2586 bool RHSImplementsProtocol = false;
2587
2588 // If the RHS doesn't implement the protocol on the left, the types
2589 // are incompatible.
2590 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2591 RHSPE = RHSP->qual_end();
2592 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2593 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2594 RHSImplementsProtocol = true;
2595 }
2596 // FIXME: For better diagnostics, consider passing back the protocol name.
2597 if (!RHSImplementsProtocol)
2598 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002599 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002600 // The RHS implements all protocols listed on the LHS.
2601 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002602}
2603
Steve Naroff17c03822009-02-12 17:52:19 +00002604bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2605 // get the "pointed to" types
2606 const PointerType *LHSPT = LHS->getAsPointerType();
2607 const PointerType *RHSPT = RHS->getAsPointerType();
2608
2609 if (!LHSPT || !RHSPT)
2610 return false;
2611
2612 QualType lhptee = LHSPT->getPointeeType();
2613 QualType rhptee = RHSPT->getPointeeType();
2614 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2615 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2616 // ID acts sort of like void* for ObjC interfaces
2617 if (LHSIface && isObjCIdStructType(rhptee))
2618 return true;
2619 if (RHSIface && isObjCIdStructType(lhptee))
2620 return true;
2621 if (!LHSIface || !RHSIface)
2622 return false;
2623 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2624 canAssignObjCInterfaces(RHSIface, LHSIface);
2625}
2626
Steve Naroff85f0dc52007-10-15 20:41:53 +00002627/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2628/// both shall have the identically qualified version of a compatible type.
2629/// C99 6.2.7p1: Two types have compatible types if their types are the
2630/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002631bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2632 return !mergeTypes(LHS, RHS).isNull();
2633}
2634
2635QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2636 const FunctionType *lbase = lhs->getAsFunctionType();
2637 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002638 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2639 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002640 bool allLTypes = true;
2641 bool allRTypes = true;
2642
2643 // Check return type
2644 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2645 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002646 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2647 allLTypes = false;
2648 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2649 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002650
2651 if (lproto && rproto) { // two C99 style function prototypes
2652 unsigned lproto_nargs = lproto->getNumArgs();
2653 unsigned rproto_nargs = rproto->getNumArgs();
2654
2655 // Compatible functions must have the same number of arguments
2656 if (lproto_nargs != rproto_nargs)
2657 return QualType();
2658
2659 // Variadic and non-variadic functions aren't compatible
2660 if (lproto->isVariadic() != rproto->isVariadic())
2661 return QualType();
2662
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002663 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2664 return QualType();
2665
Eli Friedman0d9549b2008-08-22 00:56:42 +00002666 // Check argument compatibility
2667 llvm::SmallVector<QualType, 10> types;
2668 for (unsigned i = 0; i < lproto_nargs; i++) {
2669 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2670 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2671 QualType argtype = mergeTypes(largtype, rargtype);
2672 if (argtype.isNull()) return QualType();
2673 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002674 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2675 allLTypes = false;
2676 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2677 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002678 }
2679 if (allLTypes) return lhs;
2680 if (allRTypes) return rhs;
2681 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002682 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002683 }
2684
2685 if (lproto) allRTypes = false;
2686 if (rproto) allLTypes = false;
2687
Douglas Gregor4fa58902009-02-26 23:50:07 +00002688 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002689 if (proto) {
2690 if (proto->isVariadic()) return QualType();
2691 // Check that the types are compatible with the types that
2692 // would result from default argument promotions (C99 6.7.5.3p15).
2693 // The only types actually affected are promotable integer
2694 // types and floats, which would be passed as a different
2695 // type depending on whether the prototype is visible.
2696 unsigned proto_nargs = proto->getNumArgs();
2697 for (unsigned i = 0; i < proto_nargs; ++i) {
2698 QualType argTy = proto->getArgType(i);
2699 if (argTy->isPromotableIntegerType() ||
2700 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2701 return QualType();
2702 }
2703
2704 if (allLTypes) return lhs;
2705 if (allRTypes) return rhs;
2706 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002707 proto->getNumArgs(), lproto->isVariadic(),
2708 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002709 }
2710
2711 if (allLTypes) return lhs;
2712 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002713 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002714}
2715
2716QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002717 // C++ [expr]: If an expression initially has the type "reference to T", the
2718 // type is adjusted to "T" prior to any further analysis, the expression
2719 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002720 // expression is an lvalue unless the reference is an rvalue reference and
2721 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002722 // FIXME: C++ shouldn't be going through here! The rules are different
2723 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002724 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2725 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002726 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002727 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002728 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002729 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002730
Eli Friedman0d9549b2008-08-22 00:56:42 +00002731 QualType LHSCan = getCanonicalType(LHS),
2732 RHSCan = getCanonicalType(RHS);
2733
2734 // If two types are identical, they are compatible.
2735 if (LHSCan == RHSCan)
2736 return LHS;
2737
2738 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002739 // Note that we handle extended qualifiers later, in the
2740 // case for ExtQualType.
2741 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002742 return QualType();
2743
2744 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2745 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2746
Chris Lattnerc38d4522008-01-14 05:45:46 +00002747 // We want to consider the two function types to be the same for these
2748 // comparisons, just force one to the other.
2749 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2750 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002751
2752 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002753 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2754 LHSClass = Type::ConstantArray;
2755 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2756 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002757
Nate Begemanaf6ed502008-04-18 23:10:10 +00002758 // Canonicalize ExtVector -> Vector.
2759 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2760 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002761
Chris Lattner7cdcb252008-04-07 06:38:24 +00002762 // Consider qualified interfaces and interfaces the same.
2763 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2764 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002765
Chris Lattnerb5709e22008-04-07 05:43:21 +00002766 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002767 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002768 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2769 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2770
2771 // ID acts sort of like void* for ObjC interfaces
2772 if (LHSIface && isObjCIdStructType(RHS))
2773 return LHS;
2774 if (RHSIface && isObjCIdStructType(LHS))
2775 return RHS;
2776
Steve Naroff28ceff72008-12-10 22:14:21 +00002777 // ID is compatible with all qualified id types.
2778 if (LHS->isObjCQualifiedIdType()) {
2779 if (const PointerType *PT = RHS->getAsPointerType()) {
2780 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002781 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002782 return LHS;
2783 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2784 // Unfortunately, this API is part of Sema (which we don't have access
2785 // to. Need to refactor. The following check is insufficient, since we
2786 // need to make sure the class implements the protocol.
2787 if (pType->isObjCInterfaceType())
2788 return LHS;
2789 }
2790 }
2791 if (RHS->isObjCQualifiedIdType()) {
2792 if (const PointerType *PT = LHS->getAsPointerType()) {
2793 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002794 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002795 return RHS;
2796 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2797 // Unfortunately, this API is part of Sema (which we don't have access
2798 // to. Need to refactor. The following check is insufficient, since we
2799 // need to make sure the class implements the protocol.
2800 if (pType->isObjCInterfaceType())
2801 return RHS;
2802 }
2803 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002804 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2805 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002806 if (const EnumType* ETy = LHS->getAsEnumType()) {
2807 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2808 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002809 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002810 if (const EnumType* ETy = RHS->getAsEnumType()) {
2811 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2812 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002813 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002814
Eli Friedman0d9549b2008-08-22 00:56:42 +00002815 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002816 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002817
Steve Naroffc88babe2008-01-09 22:43:08 +00002818 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002819 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002820#define TYPE(Class, Base)
2821#define ABSTRACT_TYPE(Class, Base)
2822#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2823#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2824#include "clang/AST/TypeNodes.def"
2825 assert(false && "Non-canonical and dependent types shouldn't get here");
2826 return QualType();
2827
Sebastian Redlce6fff02009-03-16 23:22:08 +00002828 case Type::LValueReference:
2829 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002830 case Type::MemberPointer:
2831 assert(false && "C++ should never be in mergeTypes");
2832 return QualType();
2833
2834 case Type::IncompleteArray:
2835 case Type::VariableArray:
2836 case Type::FunctionProto:
2837 case Type::ExtVector:
2838 case Type::ObjCQualifiedInterface:
2839 assert(false && "Types are eliminated above");
2840 return QualType();
2841
Chris Lattnerc38d4522008-01-14 05:45:46 +00002842 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002843 {
2844 // Merge two pointer types, while trying to preserve typedef info
2845 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2846 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2847 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2848 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002849 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2850 return LHS;
2851 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2852 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002853 return getPointerType(ResultType);
2854 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002855 case Type::BlockPointer:
2856 {
2857 // Merge two block pointer types, while trying to preserve typedef info
2858 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2859 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2860 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2861 if (ResultType.isNull()) return QualType();
2862 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2863 return LHS;
2864 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2865 return RHS;
2866 return getBlockPointerType(ResultType);
2867 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002868 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002869 {
2870 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2871 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2872 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2873 return QualType();
2874
2875 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2876 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2877 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2878 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002879 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2880 return LHS;
2881 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2882 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002883 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2884 ArrayType::ArraySizeModifier(), 0);
2885 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2886 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002887 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2888 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002889 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2890 return LHS;
2891 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2892 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002893 if (LVAT) {
2894 // FIXME: This isn't correct! But tricky to implement because
2895 // the array's size has to be the size of LHS, but the type
2896 // has to be different.
2897 return LHS;
2898 }
2899 if (RVAT) {
2900 // FIXME: This isn't correct! But tricky to implement because
2901 // the array's size has to be the size of RHS, but the type
2902 // has to be different.
2903 return RHS;
2904 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002905 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2906 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002907 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002908 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002909 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002910 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00002911 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002912 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002913 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002914 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2915 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002916 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002917 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002918 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002919 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002920 case Type::Complex:
2921 // Distinct complex types are incompatible.
2922 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002923 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002924 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002925 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2926 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002927 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002928 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002929 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002930 // FIXME: This should be type compatibility, e.g. whether
2931 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00002932 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2933 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2934 if (LHSIface && RHSIface &&
2935 canAssignObjCInterfaces(LHSIface, RHSIface))
2936 return LHS;
2937
Eli Friedman0d9549b2008-08-22 00:56:42 +00002938 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002939 }
Steve Naroff28ceff72008-12-10 22:14:21 +00002940 case Type::ObjCQualifiedId:
2941 // Distinct qualified id's are not compatible.
2942 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002943 case Type::FixedWidthInt:
2944 // Distinct fixed-width integers are not compatible.
2945 return QualType();
2946 case Type::ObjCQualifiedClass:
2947 // Distinct qualified classes are not compatible.
2948 return QualType();
2949 case Type::ExtQual:
2950 // FIXME: ExtQual types can be compatible even if they're not
2951 // identical!
2952 return QualType();
2953 // First attempt at an implementation, but I'm not really sure it's
2954 // right...
2955#if 0
2956 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2957 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2958 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2959 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2960 return QualType();
2961 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2962 LHSBase = QualType(LQual->getBaseType(), 0);
2963 RHSBase = QualType(RQual->getBaseType(), 0);
2964 ResultType = mergeTypes(LHSBase, RHSBase);
2965 if (ResultType.isNull()) return QualType();
2966 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2967 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2968 return LHS;
2969 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2970 return RHS;
2971 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2972 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2973 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2974 return ResultType;
2975#endif
Steve Naroff85f0dc52007-10-15 20:41:53 +00002976 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00002977
2978 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002979}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002980
Chris Lattner1d78a862008-04-07 07:01:58 +00002981//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002982// Integer Predicates
2983//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002984
Eli Friedman0832dbc2008-06-28 06:23:08 +00002985unsigned ASTContext::getIntWidth(QualType T) {
2986 if (T == BoolTy)
2987 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002988 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2989 return FWIT->getWidth();
2990 }
2991 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002992 return (unsigned)getTypeSize(T);
2993}
2994
2995QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2996 assert(T->isSignedIntegerType() && "Unexpected type");
2997 if (const EnumType* ETy = T->getAsEnumType())
2998 T = ETy->getDecl()->getIntegerType();
2999 const BuiltinType* BTy = T->getAsBuiltinType();
3000 assert (BTy && "Unexpected signed integer type");
3001 switch (BTy->getKind()) {
3002 case BuiltinType::Char_S:
3003 case BuiltinType::SChar:
3004 return UnsignedCharTy;
3005 case BuiltinType::Short:
3006 return UnsignedShortTy;
3007 case BuiltinType::Int:
3008 return UnsignedIntTy;
3009 case BuiltinType::Long:
3010 return UnsignedLongTy;
3011 case BuiltinType::LongLong:
3012 return UnsignedLongLongTy;
3013 default:
3014 assert(0 && "Unexpected signed integer type");
3015 return QualType();
3016 }
3017}
3018
3019
3020//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003021// Serialization Support
3022//===----------------------------------------------------------------------===//
3023
Ted Kremenek738e6c02007-10-31 17:10:13 +00003024/// Emit - Serialize an ASTContext object to Bitcode.
3025void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003026 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003027 S.EmitRef(SourceMgr);
3028 S.EmitRef(Target);
3029 S.EmitRef(Idents);
3030 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003031
Ted Kremenek68228a92007-10-31 22:44:07 +00003032 // Emit the size of the type vector so that we can reserve that size
3033 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003034 S.EmitInt(Types.size());
3035
Ted Kremenek034a78c2007-11-13 22:02:55 +00003036 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3037 I!=E;++I)
3038 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003039
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003040 S.EmitOwnedPtr(TUDecl);
3041
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003042 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003043}
3044
Ted Kremenekacba3612007-11-13 00:25:37 +00003045ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003046
3047 // Read the language options.
3048 LangOptions LOpts;
3049 LOpts.Read(D);
3050
Ted Kremenek68228a92007-10-31 22:44:07 +00003051 SourceManager &SM = D.ReadRef<SourceManager>();
3052 TargetInfo &t = D.ReadRef<TargetInfo>();
3053 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3054 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003055
Ted Kremenek68228a92007-10-31 22:44:07 +00003056 unsigned size_reserve = D.ReadInt();
3057
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003058 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3059 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003060
Ted Kremenek034a78c2007-11-13 22:02:55 +00003061 for (unsigned i = 0; i < size_reserve; ++i)
3062 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003063
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003064 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3065
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003066 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003067
3068 return A;
3069}