blob: 78f2a8a9ab5c7ca9bc3d33c0f221181a72481611 [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) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor23d23262009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
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
Eli Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
110 else if (isa<ArrayType>(T))
111 ++NumArray;
112 else if (isa<VectorType>(T))
113 ++NumVector;
114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000139 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
151 fprintf(stderr, " %d array types\n", NumArray);
152 fprintf(stderr, " %d vector types\n", NumVector);
153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregord2baafd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000233
Steve Naroff9d12c902007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
249
Chris Lattner2a674dc2008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000266unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000267 unsigned Align = Target.getCharWidth();
268
269 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
270 Align = std::max(Align, AA->getAlignment());
271
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000272 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
273 QualType T = VD->getType();
274 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000275 if (!T->isIncompleteType() && !T->isFunctionType()) {
276 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
277 T = cast<ArrayType>(T)->getElementType();
278
279 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
280 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000281 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000282
283 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000284}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000285
Chris Lattner4b009652007-07-25 00:24:17 +0000286/// getTypeSize - Return the size of the specified type, in bits. This method
287/// does not work on incomplete types.
288std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000289ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000290 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000291 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000292 unsigned Align;
293 switch (T->getTypeClass()) {
294 case Type::TypeName: assert(0 && "Not a canonical type!");
295 case Type::FunctionNoProto:
296 case Type::FunctionProto:
297 default:
298 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000299 case Type::VariableArray:
300 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000301 case Type::DependentSizedArray:
302 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000303 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000304 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000305
Chris Lattner8cd0e932008-03-05 18:54:05 +0000306 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000307 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000308 Align = EltInfo.second;
309 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000310 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000311 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000312 case Type::Vector: {
313 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000314 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000315 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000316 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000317 // If the alignment is not a power of 2, round up to the next power of 2.
318 // This happens for non-power-of-2 length vectors.
319 // FIXME: this should probably be a target property.
320 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000321 break;
322 }
323
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000324 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000325 switch (cast<BuiltinType>(T)->getKind()) {
326 default: assert(0 && "Unknown builtin type!");
327 case BuiltinType::Void:
328 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000329 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000330 Width = Target.getBoolWidth();
331 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000332 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000333 case BuiltinType::Char_S:
334 case BuiltinType::Char_U:
335 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000336 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000337 Width = Target.getCharWidth();
338 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000339 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000340 case BuiltinType::WChar:
341 Width = Target.getWCharWidth();
342 Align = Target.getWCharAlign();
343 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000344 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000345 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000346 Width = Target.getShortWidth();
347 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000349 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000350 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000351 Width = Target.getIntWidth();
352 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000353 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000354 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000355 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000356 Width = Target.getLongWidth();
357 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000359 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000360 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000361 Width = Target.getLongLongWidth();
362 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000363 break;
364 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000365 Width = Target.getFloatWidth();
366 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000367 break;
368 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000369 Width = Target.getDoubleWidth();
370 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000371 break;
372 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 Width = Target.getLongDoubleWidth();
374 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000375 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000376 }
377 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000378 case Type::FixedWidthInt:
379 // FIXME: This isn't precisely correct; the width/alignment should depend
380 // on the available types for the target
381 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000382 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000383 Align = Width;
384 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000385 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000386 // FIXME: Pointers into different addr spaces could have different sizes and
387 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000388 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000389 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000390 case Type::ObjCQualifiedClass:
Chris Lattner1d78a862008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000393 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000394 case Type::BlockPointer: {
395 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
396 Width = Target.getPointerWidth(AS);
397 Align = Target.getPointerAlign(AS);
398 break;
399 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000400 case Type::Pointer: {
401 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000402 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000403 Align = Target.getPointerAlign(AS);
404 break;
405 }
Chris Lattner4b009652007-07-25 00:24:17 +0000406 case Type::Reference:
407 // "When applied to a reference or a reference type, the result is the size
408 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000409 // FIXME: This is wrong for struct layout: a reference in a struct has
410 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000411 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000412 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000413 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000414 // the GCC ABI, where pointers to data are one pointer large, pointers to
415 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000416 // other compilers too, we need to delegate this completely to TargetInfo
417 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000418 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
419 unsigned AS = Pointee.getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 if (Pointee->isFunctionType())
422 Width *= 2;
423 Align = Target.getPointerAlign(AS);
424 // GCC aligns at single pointer width.
425 }
Chris Lattner4b009652007-07-25 00:24:17 +0000426 case Type::Complex: {
427 // Complex types have the same alignment as their elements, but twice the
428 // size.
429 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000430 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000431 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000432 Align = EltInfo.second;
433 break;
434 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000435 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000436 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000437 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
438 Width = Layout.getSize();
439 Align = Layout.getAlignment();
440 break;
441 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000442 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000443 const TagType *TT = cast<TagType>(T);
444
445 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000446 Width = 1;
447 Align = 1;
448 break;
449 }
450
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000451 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000452 return getTypeInfo(ET->getDecl()->getIntegerType());
453
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000454 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000455 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
456 Width = Layout.getSize();
457 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000458 break;
459 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000460 }
Chris Lattner4b009652007-07-25 00:24:17 +0000461
462 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000463 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000464}
465
Chris Lattner83165b52009-01-27 18:08:34 +0000466/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
467/// type for the current target in bits. This can be different than the ABI
468/// alignment in cases where it is beneficial for performance to overalign
469/// a data type.
470unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
471 unsigned ABIAlign = getTypeAlign(T);
472
473 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000474 if (T->isSpecificBuiltinType(BuiltinType::Double))
475 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000476
477 return ABIAlign;
478}
479
480
Devang Patelbfe323c2008-06-04 21:22:16 +0000481/// LayoutField - Field layout.
482void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000483 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000484 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000485 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000486 uint64_t FieldOffset = IsUnion ? 0 : Size;
487 uint64_t FieldSize;
488 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000489
490 // FIXME: Should this override struct packing? Probably we want to
491 // take the minimum?
492 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
493 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000494
495 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
496 // TODO: Need to check this algorithm on other targets!
497 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000498 FieldSize =
499 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000500
501 std::pair<uint64_t, unsigned> FieldInfo =
502 Context.getTypeInfo(FD->getType());
503 uint64_t TypeSize = FieldInfo.first;
504
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000505 // Determine the alignment of this bitfield. The packing
506 // attributes define a maximum and the alignment attribute defines
507 // a minimum.
508 // FIXME: What is the right behavior when the specified alignment
509 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000510 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000511 if (FieldPacking)
512 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000513 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
514 FieldAlign = std::max(FieldAlign, AA->getAlignment());
515
516 // Check if we need to add padding to give the field the correct
517 // alignment.
518 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
519 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
520
521 // Padding members don't affect overall alignment
522 if (!FD->getIdentifier())
523 FieldAlign = 1;
524 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000525 if (FD->getType()->isIncompleteArrayType()) {
526 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000527 // query getTypeInfo about these, so we figure it out here.
528 // Flexible array members don't have any size, but they
529 // have to be aligned appropriately for their element type.
530 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000531 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000532 FieldAlign = Context.getTypeAlign(ATy->getElementType());
533 } else {
534 std::pair<uint64_t, unsigned> FieldInfo =
535 Context.getTypeInfo(FD->getType());
536 FieldSize = FieldInfo.first;
537 FieldAlign = FieldInfo.second;
538 }
539
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000540 // Determine the alignment of this bitfield. The packing
541 // attributes define a maximum and the alignment attribute defines
542 // a minimum. Additionally, the packing alignment must be at least
543 // a byte for non-bitfields.
544 //
545 // FIXME: What is the right behavior when the specified alignment
546 // is smaller than the specified packing?
547 if (FieldPacking)
548 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000549 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
550 FieldAlign = std::max(FieldAlign, AA->getAlignment());
551
552 // Round up the current record size to the field's alignment boundary.
553 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
554 }
555
556 // Place this field at the current location.
557 FieldOffsets[FieldNo] = FieldOffset;
558
559 // Reserve space for this field.
560 if (IsUnion) {
561 Size = std::max(Size, FieldSize);
562 } else {
563 Size = FieldOffset + FieldSize;
564 }
565
566 // Remember max struct/class alignment.
567 Alignment = std::max(Alignment, FieldAlign);
568}
569
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000570static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
571 std::vector<FieldDecl*> &Fields) {
572 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
573 if (SuperClass)
574 CollectObjCIvars(SuperClass, Fields);
575 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
576 E = OI->ivar_end(); I != E; ++I) {
577 ObjCIvarDecl *IVDecl = (*I);
578 if (!IVDecl->isInvalidDecl())
579 Fields.push_back(cast<FieldDecl>(IVDecl));
580 }
581}
582
583/// addRecordToClass - produces record info. for the class for its
584/// ivars and all those inherited.
585///
586const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
587{
588 const RecordDecl *&RD = ASTRecordForInterface[D];
589 if (RD)
590 return RD;
591 std::vector<FieldDecl*> RecFields;
592 CollectObjCIvars(D, RecFields);
593 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
594 D->getLocation(),
595 D->getIdentifier());
596 /// FIXME! Can do collection of ivars and adding to the record while
597 /// doing it.
598 for (unsigned int i = 0; i != RecFields.size(); i++) {
599 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
600 RecFields[i]->getLocation(),
601 RecFields[i]->getIdentifier(),
602 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000603 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000604 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000605 }
606 NewRD->completeDefinition(*this);
607 RD = NewRD;
608 return RD;
609}
Devang Patel4b6bf702008-06-04 21:54:36 +0000610
Fariborz Jahanianea944842008-12-18 17:29:46 +0000611/// setFieldDecl - maps a field for the given Ivar reference node.
612//
613void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
614 const ObjCIvarDecl *Ivar,
615 const ObjCIvarRefExpr *MRef) {
616 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
617 lookupFieldDeclForIvar(*this, Ivar);
618 ASTFieldForIvarRef[MRef] = FD;
619}
620
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000621/// getASTObjcInterfaceLayout - Get or compute information about the layout of
622/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000623/// position information.
624const ASTRecordLayout &
625ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
626 // Look up this layout, if already laid out, return what we have.
627 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
628 if (Entry) return *Entry;
629
630 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
631 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000632 ASTRecordLayout *NewEntry = NULL;
633 unsigned FieldCount = D->ivar_size();
634 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
635 FieldCount++;
636 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
637 unsigned Alignment = SL.getAlignment();
638 uint64_t Size = SL.getSize();
639 NewEntry = new ASTRecordLayout(Size, Alignment);
640 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000641 // Super class is at the beginning of the layout.
642 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000643 } else {
644 NewEntry = new ASTRecordLayout();
645 NewEntry->InitializeLayout(FieldCount);
646 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000647 Entry = NewEntry;
648
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000649 unsigned StructPacking = 0;
650 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
651 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000652
653 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
654 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
655 AA->getAlignment()));
656
657 // Layout each ivar sequentially.
658 unsigned i = 0;
659 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
660 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
661 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000662 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000663 }
664
665 // Finally, round the size of the total struct up to the alignment of the
666 // struct itself.
667 NewEntry->FinalizeLayout();
668 return *NewEntry;
669}
670
Devang Patel7a78e432007-11-01 19:11:01 +0000671/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000672/// specified record (struct/union/class), which indicates its size and field
673/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000674const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000675 D = D->getDefinition(*this);
676 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000677
Chris Lattner4b009652007-07-25 00:24:17 +0000678 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000679 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000680 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000681
Devang Patel7a78e432007-11-01 19:11:01 +0000682 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
683 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
684 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000685 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000686
Douglas Gregor39677622008-12-11 20:41:00 +0000687 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000688 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000689 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000690
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000691 unsigned StructPacking = 0;
692 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
693 StructPacking = PA->getAlignment();
694
Eli Friedman5949a022008-05-30 09:31:38 +0000695 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000696 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
697 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000698
Eli Friedman5949a022008-05-30 09:31:38 +0000699 // Layout each field, for now, just sequentially, respecting alignment. In
700 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000701 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000702 for (RecordDecl::field_iterator Field = D->field_begin(),
703 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000704 Field != FieldEnd; (void)++Field, ++FieldIdx)
705 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000706
707 // Finally, round the size of the total struct up to the alignment of the
708 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000709 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000710 return *NewEntry;
711}
712
Chris Lattner4b009652007-07-25 00:24:17 +0000713//===----------------------------------------------------------------------===//
714// Type creation/memoization methods
715//===----------------------------------------------------------------------===//
716
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000717QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000718 QualType CanT = getCanonicalType(T);
719 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000720 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000721
722 // If we are composing extended qualifiers together, merge together into one
723 // ExtQualType node.
724 unsigned CVRQuals = T.getCVRQualifiers();
725 QualType::GCAttrTypes GCAttr = QualType::GCNone;
726 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000727
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000728 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
729 // If this type already has an address space specified, it cannot get
730 // another one.
731 assert(EQT->getAddressSpace() == 0 &&
732 "Type cannot be in multiple addr spaces!");
733 GCAttr = EQT->getObjCGCAttr();
734 TypeNode = EQT->getBaseType();
735 }
Chris Lattner35fef522008-02-20 20:55:12 +0000736
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000737 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000738 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000739 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000740 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000741 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000742 return QualType(EXTQy, CVRQuals);
743
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000744 // If the base type isn't canonical, this won't be a canonical type either,
745 // so fill in the canonical type field.
746 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000747 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000748 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000749
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000750 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000751 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000752 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000753 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000754 ExtQualType *New =
755 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000756 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000757 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000758 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000759}
760
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000761QualType ASTContext::getObjCGCQualType(QualType T,
762 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000763 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000764 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000765 return T;
766
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000767 // If we are composing extended qualifiers together, merge together into one
768 // ExtQualType node.
769 unsigned CVRQuals = T.getCVRQualifiers();
770 Type *TypeNode = T.getTypePtr();
771 unsigned AddressSpace = 0;
772
773 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
774 // If this type already has an address space specified, it cannot get
775 // another one.
776 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
777 "Type cannot be in multiple addr spaces!");
778 AddressSpace = EQT->getAddressSpace();
779 TypeNode = EQT->getBaseType();
780 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000781
782 // Check if we've already instantiated an gc qual'd type of this type.
783 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000784 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000785 void *InsertPos = 0;
786 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000787 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000788
789 // If the base type isn't canonical, this won't be a canonical type either,
790 // so fill in the canonical type field.
791 QualType Canonical;
792 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000794
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000795 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000796 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
797 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
798 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000799 ExtQualType *New =
800 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000801 ExtQualTypes.InsertNode(New, InsertPos);
802 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000803 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000804}
Chris Lattner4b009652007-07-25 00:24:17 +0000805
806/// getComplexType - Return the uniqued reference to the type for a complex
807/// number with the specified element type.
808QualType ASTContext::getComplexType(QualType T) {
809 // Unique pointers, to guarantee there is only one pointer of a particular
810 // structure.
811 llvm::FoldingSetNodeID ID;
812 ComplexType::Profile(ID, T);
813
814 void *InsertPos = 0;
815 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
816 return QualType(CT, 0);
817
818 // If the pointee type isn't canonical, this won't be a canonical type either,
819 // so fill in the canonical type field.
820 QualType Canonical;
821 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000822 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000823
824 // Get the new insert position for the node we care about.
825 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000826 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000827 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000828 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000829 Types.push_back(New);
830 ComplexTypes.InsertNode(New, InsertPos);
831 return QualType(New, 0);
832}
833
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000834QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
835 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
836 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
837 FixedWidthIntType *&Entry = Map[Width];
838 if (!Entry)
839 Entry = new FixedWidthIntType(Width, Signed);
840 return QualType(Entry, 0);
841}
Chris Lattner4b009652007-07-25 00:24:17 +0000842
843/// getPointerType - Return the uniqued reference to the type for a pointer to
844/// the specified type.
845QualType ASTContext::getPointerType(QualType T) {
846 // Unique pointers, to guarantee there is only one pointer of a particular
847 // structure.
848 llvm::FoldingSetNodeID ID;
849 PointerType::Profile(ID, T);
850
851 void *InsertPos = 0;
852 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
853 return QualType(PT, 0);
854
855 // If the pointee type isn't canonical, this won't be a canonical type either,
856 // so fill in the canonical type field.
857 QualType Canonical;
858 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000859 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000860
861 // Get the new insert position for the node we care about.
862 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000863 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000864 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000865 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000866 Types.push_back(New);
867 PointerTypes.InsertNode(New, InsertPos);
868 return QualType(New, 0);
869}
870
Steve Naroff7aa54752008-08-27 16:04:49 +0000871/// getBlockPointerType - Return the uniqued reference to the type for
872/// a pointer to the specified block.
873QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000874 assert(T->isFunctionType() && "block of function types only");
875 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000876 // structure.
877 llvm::FoldingSetNodeID ID;
878 BlockPointerType::Profile(ID, T);
879
880 void *InsertPos = 0;
881 if (BlockPointerType *PT =
882 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
883 return QualType(PT, 0);
884
Steve Narofffd5b19d2008-08-28 19:20:44 +0000885 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000886 // type either so fill in the canonical type field.
887 QualType Canonical;
888 if (!T->isCanonical()) {
889 Canonical = getBlockPointerType(getCanonicalType(T));
890
891 // Get the new insert position for the node we care about.
892 BlockPointerType *NewIP =
893 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000894 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000895 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000896 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000897 Types.push_back(New);
898 BlockPointerTypes.InsertNode(New, InsertPos);
899 return QualType(New, 0);
900}
901
Chris Lattner4b009652007-07-25 00:24:17 +0000902/// getReferenceType - Return the uniqued reference to the type for a reference
903/// to the specified type.
904QualType ASTContext::getReferenceType(QualType T) {
905 // Unique pointers, to guarantee there is only one pointer of a particular
906 // structure.
907 llvm::FoldingSetNodeID ID;
908 ReferenceType::Profile(ID, T);
909
910 void *InsertPos = 0;
911 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
912 return QualType(RT, 0);
913
914 // If the referencee type isn't canonical, this won't be a canonical type
915 // either, so fill in the canonical type field.
916 QualType Canonical;
917 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000918 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000919
920 // Get the new insert position for the node we care about.
921 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000922 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000923 }
924
Steve Naroff93fd2112009-01-27 22:08:43 +0000925 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000926 Types.push_back(New);
927 ReferenceTypes.InsertNode(New, InsertPos);
928 return QualType(New, 0);
929}
930
Sebastian Redl75555032009-01-24 21:16:55 +0000931/// getMemberPointerType - Return the uniqued reference to the type for a
932/// member pointer to the specified type, in the specified class.
933QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
934{
935 // Unique pointers, to guarantee there is only one pointer of a particular
936 // structure.
937 llvm::FoldingSetNodeID ID;
938 MemberPointerType::Profile(ID, T, Cls);
939
940 void *InsertPos = 0;
941 if (MemberPointerType *PT =
942 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
943 return QualType(PT, 0);
944
945 // If the pointee or class type isn't canonical, this won't be a canonical
946 // type either, so fill in the canonical type field.
947 QualType Canonical;
948 if (!T->isCanonical()) {
949 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
950
951 // Get the new insert position for the node we care about.
952 MemberPointerType *NewIP =
953 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
954 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
955 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000956 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000957 Types.push_back(New);
958 MemberPointerTypes.InsertNode(New, InsertPos);
959 return QualType(New, 0);
960}
961
Steve Naroff83c13012007-08-30 01:06:46 +0000962/// getConstantArrayType - Return the unique reference to the type for an
963/// array of the specified element type.
964QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000965 const llvm::APInt &ArySize,
966 ArrayType::ArraySizeModifier ASM,
967 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000968 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +0000969 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000970
971 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000972 if (ConstantArrayType *ATP =
973 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000974 return QualType(ATP, 0);
975
976 // If the element type isn't canonical, this won't be a canonical type either,
977 // so fill in the canonical type field.
978 QualType Canonical;
979 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000980 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000981 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000982 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000983 ConstantArrayType *NewIP =
984 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000985 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000986 }
987
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000988 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000989 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000990 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000991 Types.push_back(New);
992 return QualType(New, 0);
993}
994
Steve Naroffe2579e32007-08-30 18:14:25 +0000995/// getVariableArrayType - Returns a non-unique reference to the type for a
996/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000997QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
998 ArrayType::ArraySizeModifier ASM,
999 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001000 // Since we don't unique expressions, it isn't possible to unique VLA's
1001 // that have an expression provided for their size.
1002
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001003 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001004 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001005
1006 VariableArrayTypes.push_back(New);
1007 Types.push_back(New);
1008 return QualType(New, 0);
1009}
1010
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001011/// getDependentSizedArrayType - Returns a non-unique reference to
1012/// the type for a dependently-sized array of the specified element
1013/// type. FIXME: We will need these to be uniqued, or at least
1014/// comparable, at some point.
1015QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1016 ArrayType::ArraySizeModifier ASM,
1017 unsigned EltTypeQuals) {
1018 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1019 "Size must be type- or value-dependent!");
1020
1021 // Since we don't unique expressions, it isn't possible to unique
1022 // dependently-sized array types.
1023
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001024 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001025 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1026 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001027
1028 DependentSizedArrayTypes.push_back(New);
1029 Types.push_back(New);
1030 return QualType(New, 0);
1031}
1032
Eli Friedman8ff07782008-02-15 18:16:39 +00001033QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1034 ArrayType::ArraySizeModifier ASM,
1035 unsigned EltTypeQuals) {
1036 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001037 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001038
1039 void *InsertPos = 0;
1040 if (IncompleteArrayType *ATP =
1041 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1042 return QualType(ATP, 0);
1043
1044 // If the element type isn't canonical, this won't be a canonical type
1045 // either, so fill in the canonical type field.
1046 QualType Canonical;
1047
1048 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001049 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001050 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001051
1052 // Get the new insert position for the node we care about.
1053 IncompleteArrayType *NewIP =
1054 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001055 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001056 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001057
Steve Naroff93fd2112009-01-27 22:08:43 +00001058 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001059 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001060
1061 IncompleteArrayTypes.InsertNode(New, InsertPos);
1062 Types.push_back(New);
1063 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001064}
1065
Chris Lattner4b009652007-07-25 00:24:17 +00001066/// getVectorType - Return the unique reference to a vector type of
1067/// the specified element type and size. VectorType must be a built-in type.
1068QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1069 BuiltinType *baseType;
1070
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001071 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001072 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1073
1074 // Check if we've already instantiated a vector of this type.
1075 llvm::FoldingSetNodeID ID;
1076 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1077 void *InsertPos = 0;
1078 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1079 return QualType(VTP, 0);
1080
1081 // If the element type isn't canonical, this won't be a canonical type either,
1082 // so fill in the canonical type field.
1083 QualType Canonical;
1084 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001085 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001086
1087 // Get the new insert position for the node we care about.
1088 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001089 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001090 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001091 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001092 VectorTypes.InsertNode(New, InsertPos);
1093 Types.push_back(New);
1094 return QualType(New, 0);
1095}
1096
Nate Begemanaf6ed502008-04-18 23:10:10 +00001097/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001098/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001099QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001100 BuiltinType *baseType;
1101
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001102 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001103 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001104
1105 // Check if we've already instantiated a vector of this type.
1106 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001107 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001108 void *InsertPos = 0;
1109 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1110 return QualType(VTP, 0);
1111
1112 // If the element type isn't canonical, this won't be a canonical type either,
1113 // so fill in the canonical type field.
1114 QualType Canonical;
1115 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001116 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001117
1118 // Get the new insert position for the node we care about.
1119 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001120 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001121 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001122 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001123 VectorTypes.InsertNode(New, InsertPos);
1124 Types.push_back(New);
1125 return QualType(New, 0);
1126}
1127
1128/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1129///
1130QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1131 // Unique functions, to guarantee there is only one function of a particular
1132 // structure.
1133 llvm::FoldingSetNodeID ID;
1134 FunctionTypeNoProto::Profile(ID, ResultTy);
1135
1136 void *InsertPos = 0;
1137 if (FunctionTypeNoProto *FT =
1138 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1139 return QualType(FT, 0);
1140
1141 QualType Canonical;
1142 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001143 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001144
1145 // Get the new insert position for the node we care about.
1146 FunctionTypeNoProto *NewIP =
1147 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001148 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001149 }
1150
Steve Naroff93fd2112009-01-27 22:08:43 +00001151 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001152 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001153 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001154 return QualType(New, 0);
1155}
1156
1157/// getFunctionType - Return a normal function type with a typed argument
1158/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001159QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001160 unsigned NumArgs, bool isVariadic,
1161 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001162 // Unique functions, to guarantee there is only one function of a particular
1163 // structure.
1164 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001165 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1166 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001167
1168 void *InsertPos = 0;
1169 if (FunctionTypeProto *FTP =
1170 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1171 return QualType(FTP, 0);
1172
1173 // Determine whether the type being created is already canonical or not.
1174 bool isCanonical = ResultTy->isCanonical();
1175 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1176 if (!ArgArray[i]->isCanonical())
1177 isCanonical = false;
1178
1179 // If this type isn't canonical, get the canonical version of it.
1180 QualType Canonical;
1181 if (!isCanonical) {
1182 llvm::SmallVector<QualType, 16> CanonicalArgs;
1183 CanonicalArgs.reserve(NumArgs);
1184 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001185 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001186
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001187 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001188 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001189 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001190
1191 // Get the new insert position for the node we care about.
1192 FunctionTypeProto *NewIP =
1193 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001194 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001195 }
1196
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001197 // FunctionTypeProto objects are allocated with extra bytes after them
1198 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001199 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001200 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1201 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001202 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001203 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001204 Types.push_back(FTP);
1205 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1206 return QualType(FTP, 0);
1207}
1208
Douglas Gregor1d661552008-04-13 21:07:44 +00001209/// getTypeDeclType - Return the unique reference to the type for the
1210/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001211QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001212 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001213 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1214
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001215 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001216 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001217 else if (isa<TemplateTypeParmDecl>(Decl)) {
1218 assert(false && "Template type parameter types are always available.");
1219 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001220 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001221
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001222 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001223 if (PrevDecl)
1224 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001225 else
1226 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001227 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001228 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001229 if (PrevDecl)
1230 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001231 else
1232 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001233 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001234 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1235 if (PrevDecl)
1236 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001237 else
1238 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001239 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001240 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001241 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001242
Ted Kremenek46a837c2008-09-05 17:16:31 +00001243 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001244 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001245}
1246
Chris Lattner4b009652007-07-25 00:24:17 +00001247/// getTypedefType - Return the unique reference to the type for the
1248/// specified typename decl.
1249QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1250 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1251
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001252 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001253 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001254 Types.push_back(Decl->TypeForDecl);
1255 return QualType(Decl->TypeForDecl, 0);
1256}
1257
Ted Kremenek42730c52008-01-07 19:49:32 +00001258/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001259/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001260QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001261 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1262
Steve Naroff93fd2112009-01-27 22:08:43 +00001263 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001264 Types.push_back(Decl->TypeForDecl);
1265 return QualType(Decl->TypeForDecl, 0);
1266}
1267
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001268/// buildObjCInterfaceType - Returns a new type for the interface
1269/// declaration, regardless. It also removes any previously built
1270/// record declaration so caller can rebuild it.
1271QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1272 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1273 if (RD)
1274 RD = 0;
1275 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1276 Types.push_back(Decl->TypeForDecl);
1277 return QualType(Decl->TypeForDecl, 0);
1278}
1279
Douglas Gregora4918772009-02-05 23:33:38 +00001280/// \brief Retrieve the template type parameter type for a template
1281/// parameter with the given depth, index, and (optionally) name.
1282QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1283 IdentifierInfo *Name) {
1284 llvm::FoldingSetNodeID ID;
1285 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1286 void *InsertPos = 0;
1287 TemplateTypeParmType *TypeParm
1288 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1289
1290 if (TypeParm)
1291 return QualType(TypeParm, 0);
1292
1293 if (Name)
1294 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1295 getTemplateTypeParmType(Depth, Index));
1296 else
1297 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1298
1299 Types.push_back(TypeParm);
1300 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1301
1302 return QualType(TypeParm, 0);
1303}
1304
Douglas Gregor8e458f42009-02-09 18:46:07 +00001305QualType
1306ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1307 unsigned NumArgs,
1308 uintptr_t *Args, bool *ArgIsType,
1309 QualType Canon) {
1310 llvm::FoldingSetNodeID ID;
1311 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1312 ArgIsType);
1313 void *InsertPos = 0;
1314 ClassTemplateSpecializationType *Spec
1315 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1316
1317 if (Spec)
1318 return QualType(Spec, 0);
1319
1320 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1321 (sizeof(uintptr_t) *
1322 (ClassTemplateSpecializationType::
1323 getNumPackedWords(NumArgs) +
1324 NumArgs)), 8);
1325 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1326 ArgIsType, Canon);
1327 Types.push_back(Spec);
1328 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1329
1330 return QualType(Spec, 0);
1331}
1332
Chris Lattnere1352302008-04-07 04:56:42 +00001333/// CmpProtocolNames - Comparison predicate for sorting protocols
1334/// alphabetically.
1335static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1336 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001337 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001338}
1339
1340static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1341 unsigned &NumProtocols) {
1342 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1343
1344 // Sort protocols, keyed by name.
1345 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1346
1347 // Remove duplicates.
1348 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1349 NumProtocols = ProtocolsEnd-Protocols;
1350}
1351
1352
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001353/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1354/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001355QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1356 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001357 // Sort the protocol list alphabetically to canonicalize it.
1358 SortAndUniqueProtocols(Protocols, NumProtocols);
1359
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001360 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001361 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001362
1363 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001364 if (ObjCQualifiedInterfaceType *QT =
1365 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001366 return QualType(QT, 0);
1367
1368 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001369 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001370 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001371
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001372 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001373 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001374 return QualType(QType, 0);
1375}
1376
Chris Lattnere1352302008-04-07 04:56:42 +00001377/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1378/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001379QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001380 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001381 // Sort the protocol list alphabetically to canonicalize it.
1382 SortAndUniqueProtocols(Protocols, NumProtocols);
1383
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001384 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001385 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001386
1387 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001388 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001389 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001390 return QualType(QT, 0);
1391
1392 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001393 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001394 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001395 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001397 return QualType(QType, 0);
1398}
1399
Steve Naroff262a5dd2009-02-21 20:17:11 +00001400/// getObjCQualifiedClassType - Return an ObjCQualifiedIdType for the 'Class'
1401/// decl and the conforming protocol list.
1402QualType ASTContext::getObjCQualifiedClassType(ObjCProtocolDecl **Protocols,
1403 unsigned NumProtocols) {
1404 // Sort the protocol list alphabetically to canonicalize it.
1405 SortAndUniqueProtocols(Protocols, NumProtocols);
1406
1407 llvm::FoldingSetNodeID ID;
1408 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
1409
1410 void *InsertPos = 0;
1411 if (ObjCQualifiedClassType *QT =
1412 ObjCQualifiedClassTypes.FindNodeOrInsertPos(ID, InsertPos))
1413 return QualType(QT, 0);
1414
1415 // No Match;
1416 ObjCQualifiedClassType *QType =
1417 new (*this,8) ObjCQualifiedClassType(Protocols, NumProtocols);
1418 Types.push_back(QType);
1419 ObjCQualifiedClassTypes.InsertNode(QType, InsertPos);
1420 return QualType(QType, 0);
1421}
1422
Steve Naroff0604dd92007-08-01 18:02:17 +00001423/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1424/// TypeOfExpr AST's (since expression's are never shared). For example,
1425/// multiple declarations that refer to "typeof(x)" all contain different
1426/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1427/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001428QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001429 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001430 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001431 Types.push_back(toe);
1432 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001433}
1434
Steve Naroff0604dd92007-08-01 18:02:17 +00001435/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1436/// TypeOfType AST's. The only motivation to unique these nodes would be
1437/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1438/// an issue. This doesn't effect the type checker, since it operates
1439/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001440QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001441 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001442 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001443 Types.push_back(tot);
1444 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001445}
1446
Chris Lattner4b009652007-07-25 00:24:17 +00001447/// getTagDeclType - Return the unique reference to the type for the
1448/// specified TagDecl (struct/union/class/enum) decl.
1449QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001450 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001451 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001452}
1453
1454/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1455/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1456/// needs to agree with the definition in <stddef.h>.
1457QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001458 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001459}
1460
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001461/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001462/// width of characters in wide strings, The value is target dependent and
1463/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001464QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001465 if (LangOpts.CPlusPlus)
1466 return WCharTy;
1467
Douglas Gregorc6507e42008-11-03 14:12:49 +00001468 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1469 // wide-character type?
1470 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001471}
1472
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001473/// getSignedWCharType - Return the type of "signed wchar_t".
1474/// Used when in C++, as a GCC extension.
1475QualType ASTContext::getSignedWCharType() const {
1476 // FIXME: derive from "Target" ?
1477 return WCharTy;
1478}
1479
1480/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1481/// Used when in C++, as a GCC extension.
1482QualType ASTContext::getUnsignedWCharType() const {
1483 // FIXME: derive from "Target" ?
1484 return UnsignedIntTy;
1485}
1486
Chris Lattner4b009652007-07-25 00:24:17 +00001487/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1488/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1489QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001490 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001491}
1492
Chris Lattner19eb97e2008-04-02 05:18:44 +00001493//===----------------------------------------------------------------------===//
1494// Type Operators
1495//===----------------------------------------------------------------------===//
1496
Chris Lattner3dae6f42008-04-06 22:41:35 +00001497/// getCanonicalType - Return the canonical (structural) type corresponding to
1498/// the specified potentially non-canonical type. The non-canonical version
1499/// of a type may have many "decorated" versions of types. Decorators can
1500/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1501/// to be free of any of these, allowing two canonical types to be compared
1502/// for exact equality with a simple pointer comparison.
1503QualType ASTContext::getCanonicalType(QualType T) {
1504 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001505
1506 // If the result has type qualifiers, make sure to canonicalize them as well.
1507 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1508 if (TypeQuals == 0) return CanType;
1509
1510 // If the type qualifiers are on an array type, get the canonical type of the
1511 // array with the qualifiers applied to the element type.
1512 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1513 if (!AT)
1514 return CanType.getQualifiedType(TypeQuals);
1515
1516 // Get the canonical version of the element with the extra qualifiers on it.
1517 // This can recursively sink qualifiers through multiple levels of arrays.
1518 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1519 NewEltTy = getCanonicalType(NewEltTy);
1520
1521 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1522 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1523 CAT->getIndexTypeQualifier());
1524 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1525 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1526 IAT->getIndexTypeQualifier());
1527
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001528 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1529 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1530 DSAT->getSizeModifier(),
1531 DSAT->getIndexTypeQualifier());
1532
Chris Lattnera1923f62008-08-04 07:31:14 +00001533 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1534 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1535 VAT->getSizeModifier(),
1536 VAT->getIndexTypeQualifier());
1537}
1538
1539
1540const ArrayType *ASTContext::getAsArrayType(QualType T) {
1541 // Handle the non-qualified case efficiently.
1542 if (T.getCVRQualifiers() == 0) {
1543 // Handle the common positive case fast.
1544 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1545 return AT;
1546 }
1547
1548 // Handle the common negative case fast, ignoring CVR qualifiers.
1549 QualType CType = T->getCanonicalTypeInternal();
1550
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001551 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001552 // test.
1553 if (!isa<ArrayType>(CType) &&
1554 !isa<ArrayType>(CType.getUnqualifiedType()))
1555 return 0;
1556
1557 // Apply any CVR qualifiers from the array type to the element type. This
1558 // implements C99 6.7.3p8: "If the specification of an array type includes
1559 // any type qualifiers, the element type is so qualified, not the array type."
1560
1561 // If we get here, we either have type qualifiers on the type, or we have
1562 // sugar such as a typedef in the way. If we have type qualifiers on the type
1563 // we must propagate them down into the elemeng type.
1564 unsigned CVRQuals = T.getCVRQualifiers();
1565 unsigned AddrSpace = 0;
1566 Type *Ty = T.getTypePtr();
1567
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001568 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001569 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001570 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1571 AddrSpace = EXTQT->getAddressSpace();
1572 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001573 } else {
1574 T = Ty->getDesugaredType();
1575 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1576 break;
1577 CVRQuals |= T.getCVRQualifiers();
1578 Ty = T.getTypePtr();
1579 }
1580 }
1581
1582 // If we have a simple case, just return now.
1583 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1584 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1585 return ATy;
1586
1587 // Otherwise, we have an array and we have qualifiers on it. Push the
1588 // qualifiers into the array element type and return a new array type.
1589 // Get the canonical version of the element with the extra qualifiers on it.
1590 // This can recursively sink qualifiers through multiple levels of arrays.
1591 QualType NewEltTy = ATy->getElementType();
1592 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001593 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001594 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1595
1596 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1597 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1598 CAT->getSizeModifier(),
1599 CAT->getIndexTypeQualifier()));
1600 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1601 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1602 IAT->getSizeModifier(),
1603 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001604
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001605 if (const DependentSizedArrayType *DSAT
1606 = dyn_cast<DependentSizedArrayType>(ATy))
1607 return cast<ArrayType>(
1608 getDependentSizedArrayType(NewEltTy,
1609 DSAT->getSizeExpr(),
1610 DSAT->getSizeModifier(),
1611 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001612
Chris Lattnera1923f62008-08-04 07:31:14 +00001613 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1614 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1615 VAT->getSizeModifier(),
1616 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001617}
1618
1619
Chris Lattner19eb97e2008-04-02 05:18:44 +00001620/// getArrayDecayedType - Return the properly qualified result of decaying the
1621/// specified array type to a pointer. This operation is non-trivial when
1622/// handling typedefs etc. The canonical type of "T" must be an array type,
1623/// this returns a pointer to a properly qualified element of the array.
1624///
1625/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1626QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001627 // Get the element type with 'getAsArrayType' so that we don't lose any
1628 // typedefs in the element type of the array. This also handles propagation
1629 // of type qualifiers from the array type into the element type if present
1630 // (C99 6.7.3p8).
1631 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1632 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001633
Chris Lattnera1923f62008-08-04 07:31:14 +00001634 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001635
1636 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001637 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001638}
1639
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001640QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001641 QualType ElemTy = VAT->getElementType();
1642
1643 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1644 return getBaseElementType(VAT);
1645
1646 return ElemTy;
1647}
1648
Chris Lattner4b009652007-07-25 00:24:17 +00001649/// getFloatingRank - Return a relative rank for floating point types.
1650/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001651static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001652 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001653 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001654
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001655 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001656 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001657 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001658 case BuiltinType::Float: return FloatRank;
1659 case BuiltinType::Double: return DoubleRank;
1660 case BuiltinType::LongDouble: return LongDoubleRank;
1661 }
1662}
1663
Steve Narofffa0c4532007-08-27 01:41:48 +00001664/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1665/// point or a complex type (based on typeDomain/typeSize).
1666/// 'typeDomain' is a real floating point or complex type.
1667/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001668QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1669 QualType Domain) const {
1670 FloatingRank EltRank = getFloatingRank(Size);
1671 if (Domain->isComplexType()) {
1672 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001673 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001674 case FloatRank: return FloatComplexTy;
1675 case DoubleRank: return DoubleComplexTy;
1676 case LongDoubleRank: return LongDoubleComplexTy;
1677 }
Chris Lattner4b009652007-07-25 00:24:17 +00001678 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001679
1680 assert(Domain->isRealFloatingType() && "Unknown domain!");
1681 switch (EltRank) {
1682 default: assert(0 && "getFloatingRank(): illegal value for rank");
1683 case FloatRank: return FloatTy;
1684 case DoubleRank: return DoubleTy;
1685 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001686 }
Chris Lattner4b009652007-07-25 00:24:17 +00001687}
1688
Chris Lattner51285d82008-04-06 23:55:33 +00001689/// getFloatingTypeOrder - Compare the rank of the two specified floating
1690/// point types, ignoring the domain of the type (i.e. 'double' ==
1691/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1692/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001693int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1694 FloatingRank LHSR = getFloatingRank(LHS);
1695 FloatingRank RHSR = getFloatingRank(RHS);
1696
1697 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001698 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001699 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001700 return 1;
1701 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001702}
1703
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001704/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1705/// routine will assert if passed a built-in type that isn't an integer or enum,
1706/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001707unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001708 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001709 if (EnumType* ET = dyn_cast<EnumType>(T))
1710 T = ET->getDecl()->getIntegerType().getTypePtr();
1711
1712 // There are two things which impact the integer rank: the width, and
1713 // the ordering of builtins. The builtin ordering is encoded in the
1714 // bottom three bits; the width is encoded in the bits above that.
1715 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1716 return FWIT->getWidth() << 3;
1717 }
1718
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001719 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001720 default: assert(0 && "getIntegerRank(): not a built-in integer");
1721 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001722 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001723 case BuiltinType::Char_S:
1724 case BuiltinType::Char_U:
1725 case BuiltinType::SChar:
1726 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001727 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001728 case BuiltinType::Short:
1729 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001730 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001731 case BuiltinType::Int:
1732 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001733 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001734 case BuiltinType::Long:
1735 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001736 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001737 case BuiltinType::LongLong:
1738 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001739 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001740 }
1741}
1742
Chris Lattner51285d82008-04-06 23:55:33 +00001743/// getIntegerTypeOrder - Returns the highest ranked integer type:
1744/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1745/// LHS < RHS, return -1.
1746int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001747 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1748 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001749 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001750
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001751 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1752 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001753
Chris Lattner51285d82008-04-06 23:55:33 +00001754 unsigned LHSRank = getIntegerRank(LHSC);
1755 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001756
Chris Lattner51285d82008-04-06 23:55:33 +00001757 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1758 if (LHSRank == RHSRank) return 0;
1759 return LHSRank > RHSRank ? 1 : -1;
1760 }
Chris Lattner4b009652007-07-25 00:24:17 +00001761
Chris Lattner51285d82008-04-06 23:55:33 +00001762 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1763 if (LHSUnsigned) {
1764 // If the unsigned [LHS] type is larger, return it.
1765 if (LHSRank >= RHSRank)
1766 return 1;
1767
1768 // If the signed type can represent all values of the unsigned type, it
1769 // wins. Because we are dealing with 2's complement and types that are
1770 // powers of two larger than each other, this is always safe.
1771 return -1;
1772 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001773
Chris Lattner51285d82008-04-06 23:55:33 +00001774 // If the unsigned [RHS] type is larger, return it.
1775 if (RHSRank >= LHSRank)
1776 return -1;
1777
1778 // If the signed type can represent all values of the unsigned type, it
1779 // wins. Because we are dealing with 2's complement and types that are
1780 // powers of two larger than each other, this is always safe.
1781 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001782}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001783
1784// getCFConstantStringType - Return the type used for constant CFStrings.
1785QualType ASTContext::getCFConstantStringType() {
1786 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001787 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001788 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001789 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001790 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001791
1792 // const int *isa;
1793 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001794 // int flags;
1795 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001796 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001797 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001798 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001799 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001800
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001801 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001802 for (unsigned i = 0; i < 4; ++i) {
1803 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1804 SourceLocation(), 0,
1805 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001806 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001807 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001808 }
1809
1810 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001811 }
1812
1813 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001814}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001815
Anders Carlssonf58cac72008-08-30 19:34:46 +00001816QualType ASTContext::getObjCFastEnumerationStateType()
1817{
1818 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001819 ObjCFastEnumerationStateTypeDecl =
1820 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1821 &Idents.get("__objcFastEnumerationState"));
1822
Anders Carlssonf58cac72008-08-30 19:34:46 +00001823 QualType FieldTypes[] = {
1824 UnsignedLongTy,
1825 getPointerType(ObjCIdType),
1826 getPointerType(UnsignedLongTy),
1827 getConstantArrayType(UnsignedLongTy,
1828 llvm::APInt(32, 5), ArrayType::Normal, 0)
1829 };
1830
Douglas Gregor8acb7272008-12-11 16:49:14 +00001831 for (size_t i = 0; i < 4; ++i) {
1832 FieldDecl *Field = FieldDecl::Create(*this,
1833 ObjCFastEnumerationStateTypeDecl,
1834 SourceLocation(), 0,
1835 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001836 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001837 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001838 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001839
Douglas Gregor8acb7272008-12-11 16:49:14 +00001840 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001841 }
1842
1843 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1844}
1845
Anders Carlssone3f02572007-10-29 06:33:42 +00001846// This returns true if a type has been typedefed to BOOL:
1847// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001848static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001849 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001850 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1851 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001852
1853 return false;
1854}
1855
Ted Kremenek42730c52008-01-07 19:49:32 +00001856/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001857/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001858int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001859 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001860
1861 // Make all integer and enum types at least as large as an int
1862 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001863 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001864 // Treat arrays as pointers, since that's how they're passed in.
1865 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001866 sz = getTypeSize(VoidPtrTy);
1867 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001868}
1869
Ted Kremenek42730c52008-01-07 19:49:32 +00001870/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001871/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001872void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001873 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001874 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001875 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001876 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001877 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001878 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001879 // Compute size of all parameters.
1880 // Start with computing size of a pointer in number of bytes.
1881 // FIXME: There might(should) be a better way of doing this computation!
1882 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001883 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001884 // The first two arguments (self and _cmd) are pointers; account for
1885 // their size.
1886 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001887 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1888 E = Decl->param_end(); PI != E; ++PI) {
1889 QualType PType = (*PI)->getType();
1890 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001891 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001892 ParmOffset += sz;
1893 }
1894 S += llvm::utostr(ParmOffset);
1895 S += "@0:";
1896 S += llvm::utostr(PtrSize);
1897
1898 // Argument types.
1899 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001900 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1901 E = Decl->param_end(); PI != E; ++PI) {
1902 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001903 QualType PType = PVDecl->getOriginalType();
1904 if (const ArrayType *AT =
1905 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1906 // Use array's original type only if it has known number of
1907 // elements.
1908 if (!dyn_cast<ConstantArrayType>(AT))
1909 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001910 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001911 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001912 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001913 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001914 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001915 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001916 }
1917}
1918
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001919/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001920/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001921/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1922/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001923/// Property attributes are stored as a comma-delimited C string. The simple
1924/// attributes readonly and bycopy are encoded as single characters. The
1925/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1926/// encoded as single characters, followed by an identifier. Property types
1927/// are also encoded as a parametrized attribute. The characters used to encode
1928/// these attributes are defined by the following enumeration:
1929/// @code
1930/// enum PropertyAttributes {
1931/// kPropertyReadOnly = 'R', // property is read-only.
1932/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1933/// kPropertyByref = '&', // property is a reference to the value last assigned
1934/// kPropertyDynamic = 'D', // property is dynamic
1935/// kPropertyGetter = 'G', // followed by getter selector name
1936/// kPropertySetter = 'S', // followed by setter selector name
1937/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1938/// kPropertyType = 't' // followed by old-style type encoding.
1939/// kPropertyWeak = 'W' // 'weak' property
1940/// kPropertyStrong = 'P' // property GC'able
1941/// kPropertyNonAtomic = 'N' // property non-atomic
1942/// };
1943/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001944void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1945 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001946 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001947 // Collect information from the property implementation decl(s).
1948 bool Dynamic = false;
1949 ObjCPropertyImplDecl *SynthesizePID = 0;
1950
1951 // FIXME: Duplicated code due to poor abstraction.
1952 if (Container) {
1953 if (const ObjCCategoryImplDecl *CID =
1954 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1955 for (ObjCCategoryImplDecl::propimpl_iterator
1956 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1957 ObjCPropertyImplDecl *PID = *i;
1958 if (PID->getPropertyDecl() == PD) {
1959 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1960 Dynamic = true;
1961 } else {
1962 SynthesizePID = PID;
1963 }
1964 }
1965 }
1966 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001967 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001968 for (ObjCCategoryImplDecl::propimpl_iterator
1969 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1970 ObjCPropertyImplDecl *PID = *i;
1971 if (PID->getPropertyDecl() == PD) {
1972 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1973 Dynamic = true;
1974 } else {
1975 SynthesizePID = PID;
1976 }
1977 }
1978 }
1979 }
1980 }
1981
1982 // FIXME: This is not very efficient.
1983 S = "T";
1984
1985 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001986 // GCC has some special rules regarding encoding of properties which
1987 // closely resembles encoding of ivars.
1988 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1989 true /* outermost type */,
1990 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001991
1992 if (PD->isReadOnly()) {
1993 S += ",R";
1994 } else {
1995 switch (PD->getSetterKind()) {
1996 case ObjCPropertyDecl::Assign: break;
1997 case ObjCPropertyDecl::Copy: S += ",C"; break;
1998 case ObjCPropertyDecl::Retain: S += ",&"; break;
1999 }
2000 }
2001
2002 // It really isn't clear at all what this means, since properties
2003 // are "dynamic by default".
2004 if (Dynamic)
2005 S += ",D";
2006
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002007 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2008 S += ",N";
2009
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002010 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2011 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002012 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002013 }
2014
2015 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2016 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002017 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002018 }
2019
2020 if (SynthesizePID) {
2021 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2022 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002023 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002024 }
2025
2026 // FIXME: OBJCGC: weak & strong
2027}
2028
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002029/// getLegacyIntegralTypeEncoding -
2030/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002031/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002032/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2033///
2034void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2035 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2036 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002037 if (BT->getKind() == BuiltinType::ULong &&
2038 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002039 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002040 else
2041 if (BT->getKind() == BuiltinType::Long &&
2042 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002043 PointeeTy = IntTy;
2044 }
2045 }
2046}
2047
Fariborz Jahanian248db262008-01-22 22:44:46 +00002048void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002049 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002050 // We follow the behavior of gcc, expanding structures which are
2051 // directly pointed to, and expanding embedded structures. Note that
2052 // these rules are sufficient to prevent recursive encoding of the
2053 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002054 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2055 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002056}
2057
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002058static void EncodeBitField(const ASTContext *Context, std::string& S,
2059 FieldDecl *FD) {
2060 const Expr *E = FD->getBitWidth();
2061 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2062 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2063 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2064 S += 'b';
2065 S += llvm::utostr(N);
2066}
2067
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002068void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2069 bool ExpandPointedToStructures,
2070 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002071 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002072 bool OutermostType,
2073 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002074 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002075 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002076 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002077 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002078 else {
2079 char encoding;
2080 switch (BT->getKind()) {
2081 default: assert(0 && "Unhandled builtin type kind");
2082 case BuiltinType::Void: encoding = 'v'; break;
2083 case BuiltinType::Bool: encoding = 'B'; break;
2084 case BuiltinType::Char_U:
2085 case BuiltinType::UChar: encoding = 'C'; break;
2086 case BuiltinType::UShort: encoding = 'S'; break;
2087 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002088 case BuiltinType::ULong:
2089 encoding =
2090 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2091 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002092 case BuiltinType::ULongLong: encoding = 'Q'; break;
2093 case BuiltinType::Char_S:
2094 case BuiltinType::SChar: encoding = 'c'; break;
2095 case BuiltinType::Short: encoding = 's'; break;
2096 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002097 case BuiltinType::Long:
2098 encoding =
2099 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2100 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002101 case BuiltinType::LongLong: encoding = 'q'; break;
2102 case BuiltinType::Float: encoding = 'f'; break;
2103 case BuiltinType::Double: encoding = 'd'; break;
2104 case BuiltinType::LongDouble: encoding = 'd'; break;
2105 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002106
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002107 S += encoding;
2108 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002109 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002110 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002111 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2112 ExpandPointedToStructures,
2113 ExpandStructures, FD);
2114 if (FD || EncodingProperty) {
2115 // Note that we do extended encoding of protocol qualifer list
2116 // Only when doing ivar or property encoding.
2117 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2118 S += '"';
2119 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2120 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2121 S += '<';
2122 S += Proto->getNameAsString();
2123 S += '>';
2124 }
2125 S += '"';
2126 }
2127 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002128 }
2129 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002130 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002131 bool isReadOnly = false;
2132 // For historical/compatibility reasons, the read-only qualifier of the
2133 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2134 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2135 // Also, do not emit the 'r' for anything but the outermost type!
2136 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2137 if (OutermostType && T.isConstQualified()) {
2138 isReadOnly = true;
2139 S += 'r';
2140 }
2141 }
2142 else if (OutermostType) {
2143 QualType P = PointeeTy;
2144 while (P->getAsPointerType())
2145 P = P->getAsPointerType()->getPointeeType();
2146 if (P.isConstQualified()) {
2147 isReadOnly = true;
2148 S += 'r';
2149 }
2150 }
2151 if (isReadOnly) {
2152 // Another legacy compatibility encoding. Some ObjC qualifier and type
2153 // combinations need to be rearranged.
2154 // Rewrite "in const" from "nr" to "rn"
2155 const char * s = S.c_str();
2156 int len = S.length();
2157 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2158 std::string replace = "rn";
2159 S.replace(S.end()-2, S.end(), replace);
2160 }
2161 }
Steve Naroff17c03822009-02-12 17:52:19 +00002162 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002163 S += '@';
2164 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002165 }
2166 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002167 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002168 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002169 // Another historical/compatibility reason.
2170 // We encode the underlying type which comes out as
2171 // {...};
2172 S += '^';
2173 getObjCEncodingForTypeImpl(PointeeTy, S,
2174 false, ExpandPointedToStructures,
2175 NULL);
2176 return;
2177 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002178 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002179 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002180 const ObjCInterfaceType *OIT =
2181 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002182 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002183 S += '"';
2184 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002185 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2186 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2187 S += '<';
2188 S += Proto->getNameAsString();
2189 S += '>';
2190 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002191 S += '"';
2192 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002193 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002194 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002195 S += '#';
2196 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002197 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002198 S += ':';
2199 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002200 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002201
2202 if (PointeeTy->isCharType()) {
2203 // char pointer types should be encoded as '*' unless it is a
2204 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002205 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002206 S += '*';
2207 return;
2208 }
2209 }
2210
2211 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002212 getLegacyIntegralTypeEncoding(PointeeTy);
2213
2214 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002215 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002216 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002217 } else if (const ArrayType *AT =
2218 // Ignore type qualifiers etc.
2219 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002220 if (isa<IncompleteArrayType>(AT)) {
2221 // Incomplete arrays are encoded as a pointer to the array element.
2222 S += '^';
2223
2224 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2225 false, ExpandStructures, FD);
2226 } else {
2227 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002228
Anders Carlsson858c64d2009-02-22 01:38:57 +00002229 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2230 S += llvm::utostr(CAT->getSize().getZExtValue());
2231 else {
2232 //Variable length arrays are encoded as a regular array with 0 elements.
2233 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2234 S += '0';
2235 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002236
Anders Carlsson858c64d2009-02-22 01:38:57 +00002237 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2238 false, ExpandStructures, FD);
2239 S += ']';
2240 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002241 } else if (T->getAsFunctionType()) {
2242 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002243 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002244 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002245 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002246 // Anonymous structures print as '?'
2247 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2248 S += II->getName();
2249 } else {
2250 S += '?';
2251 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002252 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002253 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002254 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2255 FieldEnd = RDecl->field_end();
2256 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002257 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002258 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002259 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002260 S += '"';
2261 }
2262
2263 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002264 if (Field->isBitField()) {
2265 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2266 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002267 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002268 QualType qt = Field->getType();
2269 getLegacyIntegralTypeEncoding(qt);
2270 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002271 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002272 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002273 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002274 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002275 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002276 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002277 if (FD && FD->isBitField())
2278 EncodeBitField(this, S, FD);
2279 else
2280 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002281 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002282 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002283 } else if (T->isObjCInterfaceType()) {
2284 // @encode(class_name)
2285 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2286 S += '{';
2287 const IdentifierInfo *II = OI->getIdentifier();
2288 S += II->getName();
2289 S += '=';
2290 std::vector<FieldDecl*> RecFields;
2291 CollectObjCIvars(OI, RecFields);
2292 for (unsigned int i = 0; i != RecFields.size(); i++) {
2293 if (RecFields[i]->isBitField())
2294 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2295 RecFields[i]);
2296 else
2297 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2298 FD);
2299 }
2300 S += '}';
2301 }
2302 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002303 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002304}
2305
Ted Kremenek42730c52008-01-07 19:49:32 +00002306void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002307 std::string& S) const {
2308 if (QT & Decl::OBJC_TQ_In)
2309 S += 'n';
2310 if (QT & Decl::OBJC_TQ_Inout)
2311 S += 'N';
2312 if (QT & Decl::OBJC_TQ_Out)
2313 S += 'o';
2314 if (QT & Decl::OBJC_TQ_Bycopy)
2315 S += 'O';
2316 if (QT & Decl::OBJC_TQ_Byref)
2317 S += 'R';
2318 if (QT & Decl::OBJC_TQ_Oneway)
2319 S += 'V';
2320}
2321
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002322void ASTContext::setBuiltinVaListType(QualType T)
2323{
2324 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2325
2326 BuiltinVaListType = T;
2327}
2328
Ted Kremenek42730c52008-01-07 19:49:32 +00002329void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002330{
Ted Kremenek42730c52008-01-07 19:49:32 +00002331 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002332
2333 // typedef struct objc_object *id;
2334 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002335 // User error - caller will issue diagnostics.
2336 if (!ptr)
2337 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002338 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002339 // User error - caller will issue diagnostics.
2340 if (!rec)
2341 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002342 IdStructType = rec;
2343}
2344
Ted Kremenek42730c52008-01-07 19:49:32 +00002345void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002346{
Ted Kremenek42730c52008-01-07 19:49:32 +00002347 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002348
2349 // typedef struct objc_selector *SEL;
2350 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002351 if (!ptr)
2352 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002353 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002354 if (!rec)
2355 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002356 SelStructType = rec;
2357}
2358
Ted Kremenek42730c52008-01-07 19:49:32 +00002359void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002360{
Ted Kremenek42730c52008-01-07 19:49:32 +00002361 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002362}
2363
Ted Kremenek42730c52008-01-07 19:49:32 +00002364void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002365{
Ted Kremenek42730c52008-01-07 19:49:32 +00002366 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002367
2368 // typedef struct objc_class *Class;
2369 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2370 assert(ptr && "'Class' incorrectly typed");
2371 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2372 assert(rec && "'Class' incorrectly typed");
2373 ClassStructType = rec;
2374}
2375
Ted Kremenek42730c52008-01-07 19:49:32 +00002376void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2377 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002378 "'NSConstantString' type already set!");
2379
Ted Kremenek42730c52008-01-07 19:49:32 +00002380 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002381}
2382
Douglas Gregorc6507e42008-11-03 14:12:49 +00002383/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002384/// TargetInfo, produce the corresponding type. The unsigned @p Type
2385/// is actually a value of type @c TargetInfo::IntType.
2386QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002387 switch (Type) {
2388 case TargetInfo::NoInt: return QualType();
2389 case TargetInfo::SignedShort: return ShortTy;
2390 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2391 case TargetInfo::SignedInt: return IntTy;
2392 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2393 case TargetInfo::SignedLong: return LongTy;
2394 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2395 case TargetInfo::SignedLongLong: return LongLongTy;
2396 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2397 }
2398
2399 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002400 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002401}
Ted Kremenek118930e2008-07-24 23:58:27 +00002402
2403//===----------------------------------------------------------------------===//
2404// Type Predicates.
2405//===----------------------------------------------------------------------===//
2406
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002407/// isObjCNSObjectType - Return true if this is an NSObject object using
2408/// NSObject attribute on a c-style pointer type.
2409/// FIXME - Make it work directly on types.
2410///
2411bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2412 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2413 if (TypedefDecl *TD = TDT->getDecl())
2414 if (TD->getAttr<ObjCNSObjectAttr>())
2415 return true;
2416 }
2417 return false;
2418}
2419
Ted Kremenek118930e2008-07-24 23:58:27 +00002420/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2421/// to an object type. This includes "id" and "Class" (two 'special' pointers
2422/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2423/// ID type).
2424bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd305a862009-02-21 21:17:01 +00002425 if (Ty->isObjCQualifiedIdType() || Ty->isObjCQualifiedClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002426 return true;
2427
Steve Naroffd9e00802008-10-21 18:24:04 +00002428 // Blocks are objects.
2429 if (Ty->isBlockPointerType())
2430 return true;
2431
2432 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002433 if (!Ty->isPointerType())
2434 return false;
2435
2436 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2437 // pointer types. This looks for the typedef specifically, not for the
2438 // underlying type.
2439 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2440 return true;
2441
2442 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002443 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2444 return true;
2445
2446 // If is has NSObject attribute, OK as well.
2447 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002448}
2449
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002450/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2451/// garbage collection attribute.
2452///
2453QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002454 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002455 if (getLangOptions().ObjC1 &&
2456 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002457 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002458 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002459 // (or pointers to them) be treated as though they were declared
2460 // as __strong.
2461 if (GCAttrs == QualType::GCNone) {
2462 if (isObjCObjectPointerType(Ty))
2463 GCAttrs = QualType::Strong;
2464 else if (Ty->isPointerType())
2465 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2466 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002467 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002468 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002469}
2470
Chris Lattner6ff358b2008-04-07 06:51:04 +00002471//===----------------------------------------------------------------------===//
2472// Type Compatibility Testing
2473//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002474
Steve Naroff3454b6c2008-09-04 15:10:53 +00002475/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002476/// block types. Types must be strictly compatible here. For example,
2477/// C unfortunately doesn't produce an error for the following:
2478///
2479/// int (*emptyArgFunc)();
2480/// int (*intArgList)(int) = emptyArgFunc;
2481///
2482/// For blocks, we will produce an error for the following (similar to C++):
2483///
2484/// int (^emptyArgBlock)();
2485/// int (^intArgBlock)(int) = emptyArgBlock;
2486///
2487/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2488///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002489bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002490 const FunctionType *lbase = lhs->getAsFunctionType();
2491 const FunctionType *rbase = rhs->getAsFunctionType();
2492 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2493 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2494 if (lproto && rproto)
2495 return !mergeTypes(lhs, rhs).isNull();
2496 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002497}
2498
Chris Lattner6ff358b2008-04-07 06:51:04 +00002499/// areCompatVectorTypes - Return true if the two specified vector types are
2500/// compatible.
2501static bool areCompatVectorTypes(const VectorType *LHS,
2502 const VectorType *RHS) {
2503 assert(LHS->isCanonical() && RHS->isCanonical());
2504 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002505 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002506}
2507
Eli Friedman0d9549b2008-08-22 00:56:42 +00002508/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002509/// compatible for assignment from RHS to LHS. This handles validation of any
2510/// protocol qualifiers on the LHS or RHS.
2511///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002512bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2513 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002514 // Verify that the base decls are compatible: the RHS must be a subclass of
2515 // the LHS.
2516 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2517 return false;
2518
2519 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2520 // protocol qualified at all, then we are good.
2521 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2522 return true;
2523
2524 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2525 // isn't a superset.
2526 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2527 return true; // FIXME: should return false!
2528
2529 // Finally, we must have two protocol-qualified interfaces.
2530 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2531 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2532 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2533 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2534 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2535 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2536
2537 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2538 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2539 // LHS in a single parallel scan until we run out of elements in LHS.
2540 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2541 ObjCProtocolDecl *LHSProto = *LHSPI;
2542
2543 while (RHSPI != RHSPE) {
2544 ObjCProtocolDecl *RHSProto = *RHSPI++;
2545 // If the RHS has a protocol that the LHS doesn't, ignore it.
2546 if (RHSProto != LHSProto)
2547 continue;
2548
2549 // Otherwise, the RHS does have this element.
2550 ++LHSPI;
2551 if (LHSPI == LHSPE)
2552 return true; // All protocols in LHS exist in RHS.
2553
2554 LHSProto = *LHSPI;
2555 }
2556
2557 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2558 return false;
2559}
2560
Steve Naroff17c03822009-02-12 17:52:19 +00002561bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2562 // get the "pointed to" types
2563 const PointerType *LHSPT = LHS->getAsPointerType();
2564 const PointerType *RHSPT = RHS->getAsPointerType();
2565
2566 if (!LHSPT || !RHSPT)
2567 return false;
2568
2569 QualType lhptee = LHSPT->getPointeeType();
2570 QualType rhptee = RHSPT->getPointeeType();
2571 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2572 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2573 // ID acts sort of like void* for ObjC interfaces
2574 if (LHSIface && isObjCIdStructType(rhptee))
2575 return true;
2576 if (RHSIface && isObjCIdStructType(lhptee))
2577 return true;
2578 if (!LHSIface || !RHSIface)
2579 return false;
2580 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2581 canAssignObjCInterfaces(RHSIface, LHSIface);
2582}
2583
Steve Naroff85f0dc52007-10-15 20:41:53 +00002584/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2585/// both shall have the identically qualified version of a compatible type.
2586/// C99 6.2.7p1: Two types have compatible types if their types are the
2587/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002588bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2589 return !mergeTypes(LHS, RHS).isNull();
2590}
2591
2592QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2593 const FunctionType *lbase = lhs->getAsFunctionType();
2594 const FunctionType *rbase = rhs->getAsFunctionType();
2595 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2596 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2597 bool allLTypes = true;
2598 bool allRTypes = true;
2599
2600 // Check return type
2601 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2602 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002603 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2604 allLTypes = false;
2605 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2606 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002607
2608 if (lproto && rproto) { // two C99 style function prototypes
2609 unsigned lproto_nargs = lproto->getNumArgs();
2610 unsigned rproto_nargs = rproto->getNumArgs();
2611
2612 // Compatible functions must have the same number of arguments
2613 if (lproto_nargs != rproto_nargs)
2614 return QualType();
2615
2616 // Variadic and non-variadic functions aren't compatible
2617 if (lproto->isVariadic() != rproto->isVariadic())
2618 return QualType();
2619
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002620 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2621 return QualType();
2622
Eli Friedman0d9549b2008-08-22 00:56:42 +00002623 // Check argument compatibility
2624 llvm::SmallVector<QualType, 10> types;
2625 for (unsigned i = 0; i < lproto_nargs; i++) {
2626 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2627 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2628 QualType argtype = mergeTypes(largtype, rargtype);
2629 if (argtype.isNull()) return QualType();
2630 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002631 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2632 allLTypes = false;
2633 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2634 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002635 }
2636 if (allLTypes) return lhs;
2637 if (allRTypes) return rhs;
2638 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002639 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002640 }
2641
2642 if (lproto) allRTypes = false;
2643 if (rproto) allLTypes = false;
2644
2645 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2646 if (proto) {
2647 if (proto->isVariadic()) return QualType();
2648 // Check that the types are compatible with the types that
2649 // would result from default argument promotions (C99 6.7.5.3p15).
2650 // The only types actually affected are promotable integer
2651 // types and floats, which would be passed as a different
2652 // type depending on whether the prototype is visible.
2653 unsigned proto_nargs = proto->getNumArgs();
2654 for (unsigned i = 0; i < proto_nargs; ++i) {
2655 QualType argTy = proto->getArgType(i);
2656 if (argTy->isPromotableIntegerType() ||
2657 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2658 return QualType();
2659 }
2660
2661 if (allLTypes) return lhs;
2662 if (allRTypes) return rhs;
2663 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002664 proto->getNumArgs(), lproto->isVariadic(),
2665 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002666 }
2667
2668 if (allLTypes) return lhs;
2669 if (allRTypes) return rhs;
2670 return getFunctionTypeNoProto(retType);
2671}
2672
2673QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002674 // C++ [expr]: If an expression initially has the type "reference to T", the
2675 // type is adjusted to "T" prior to any further analysis, the expression
2676 // designates the object or function denoted by the reference, and the
2677 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002678 // FIXME: C++ shouldn't be going through here! The rules are different
2679 // enough that they should be handled separately.
2680 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002681 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002682 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002683 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002684
Eli Friedman0d9549b2008-08-22 00:56:42 +00002685 QualType LHSCan = getCanonicalType(LHS),
2686 RHSCan = getCanonicalType(RHS);
2687
2688 // If two types are identical, they are compatible.
2689 if (LHSCan == RHSCan)
2690 return LHS;
2691
2692 // If the qualifiers are different, the types aren't compatible
2693 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2694 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2695 return QualType();
2696
2697 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2698 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2699
Chris Lattnerc38d4522008-01-14 05:45:46 +00002700 // We want to consider the two function types to be the same for these
2701 // comparisons, just force one to the other.
2702 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2703 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002704
2705 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002706 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2707 LHSClass = Type::ConstantArray;
2708 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2709 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002710
Nate Begemanaf6ed502008-04-18 23:10:10 +00002711 // Canonicalize ExtVector -> Vector.
2712 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2713 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002714
Chris Lattner7cdcb252008-04-07 06:38:24 +00002715 // Consider qualified interfaces and interfaces the same.
2716 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2717 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002718
Chris Lattnerb5709e22008-04-07 05:43:21 +00002719 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002720 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002721 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2722 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2723
2724 // ID acts sort of like void* for ObjC interfaces
2725 if (LHSIface && isObjCIdStructType(RHS))
2726 return LHS;
2727 if (RHSIface && isObjCIdStructType(LHS))
2728 return RHS;
2729
Steve Naroff28ceff72008-12-10 22:14:21 +00002730 // ID is compatible with all qualified id types.
2731 if (LHS->isObjCQualifiedIdType()) {
2732 if (const PointerType *PT = RHS->getAsPointerType()) {
2733 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002734 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002735 return LHS;
2736 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2737 // Unfortunately, this API is part of Sema (which we don't have access
2738 // to. Need to refactor. The following check is insufficient, since we
2739 // need to make sure the class implements the protocol.
2740 if (pType->isObjCInterfaceType())
2741 return LHS;
2742 }
2743 }
2744 if (RHS->isObjCQualifiedIdType()) {
2745 if (const PointerType *PT = LHS->getAsPointerType()) {
2746 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002747 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002748 return RHS;
2749 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2750 // Unfortunately, this API is part of Sema (which we don't have access
2751 // to. Need to refactor. The following check is insufficient, since we
2752 // need to make sure the class implements the protocol.
2753 if (pType->isObjCInterfaceType())
2754 return RHS;
2755 }
2756 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002757 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2758 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002759 if (const EnumType* ETy = LHS->getAsEnumType()) {
2760 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2761 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002762 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002763 if (const EnumType* ETy = RHS->getAsEnumType()) {
2764 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2765 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002766 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002767
Eli Friedman0d9549b2008-08-22 00:56:42 +00002768 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002769 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002770
Steve Naroffc88babe2008-01-09 22:43:08 +00002771 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002772 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002773 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002774 {
2775 // Merge two pointer types, while trying to preserve typedef info
2776 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2777 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2778 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2779 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002780 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2781 return LHS;
2782 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2783 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002784 return getPointerType(ResultType);
2785 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002786 case Type::BlockPointer:
2787 {
2788 // Merge two block pointer types, while trying to preserve typedef info
2789 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2790 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2791 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2792 if (ResultType.isNull()) return QualType();
2793 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2794 return LHS;
2795 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2796 return RHS;
2797 return getBlockPointerType(ResultType);
2798 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002799 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002800 {
2801 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2802 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2803 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2804 return QualType();
2805
2806 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2807 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2808 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2809 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002810 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2811 return LHS;
2812 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2813 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002814 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2815 ArrayType::ArraySizeModifier(), 0);
2816 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2817 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002818 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2819 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002820 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2821 return LHS;
2822 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2823 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002824 if (LVAT) {
2825 // FIXME: This isn't correct! But tricky to implement because
2826 // the array's size has to be the size of LHS, but the type
2827 // has to be different.
2828 return LHS;
2829 }
2830 if (RVAT) {
2831 // FIXME: This isn't correct! But tricky to implement because
2832 // the array's size has to be the size of RHS, but the type
2833 // has to be different.
2834 return RHS;
2835 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002836 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2837 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002838 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002839 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002840 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002841 return mergeFunctionTypes(LHS, RHS);
2842 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002843 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002844 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2845 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002847 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002848 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002849 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002850 case Type::Complex:
2851 // Distinct complex types are incompatible.
2852 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002853 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002854 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2855 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002856 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002857 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002858 // Check if the interfaces are assignment compatible.
2859 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2860 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2861 if (LHSIface && RHSIface &&
2862 canAssignObjCInterfaces(LHSIface, RHSIface))
2863 return LHS;
2864
Eli Friedman0d9549b2008-08-22 00:56:42 +00002865 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002866 }
Steve Naroff28ceff72008-12-10 22:14:21 +00002867 case Type::ObjCQualifiedId:
2868 // Distinct qualified id's are not compatible.
2869 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002870 default:
2871 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002872 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002873 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002874}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002875
Chris Lattner1d78a862008-04-07 07:01:58 +00002876//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002877// Integer Predicates
2878//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002879
Eli Friedman0832dbc2008-06-28 06:23:08 +00002880unsigned ASTContext::getIntWidth(QualType T) {
2881 if (T == BoolTy)
2882 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002883 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2884 return FWIT->getWidth();
2885 }
2886 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002887 return (unsigned)getTypeSize(T);
2888}
2889
2890QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2891 assert(T->isSignedIntegerType() && "Unexpected type");
2892 if (const EnumType* ETy = T->getAsEnumType())
2893 T = ETy->getDecl()->getIntegerType();
2894 const BuiltinType* BTy = T->getAsBuiltinType();
2895 assert (BTy && "Unexpected signed integer type");
2896 switch (BTy->getKind()) {
2897 case BuiltinType::Char_S:
2898 case BuiltinType::SChar:
2899 return UnsignedCharTy;
2900 case BuiltinType::Short:
2901 return UnsignedShortTy;
2902 case BuiltinType::Int:
2903 return UnsignedIntTy;
2904 case BuiltinType::Long:
2905 return UnsignedLongTy;
2906 case BuiltinType::LongLong:
2907 return UnsignedLongLongTy;
2908 default:
2909 assert(0 && "Unexpected signed integer type");
2910 return QualType();
2911 }
2912}
2913
2914
2915//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002916// Serialization Support
2917//===----------------------------------------------------------------------===//
2918
Ted Kremenek738e6c02007-10-31 17:10:13 +00002919/// Emit - Serialize an ASTContext object to Bitcode.
2920void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002921 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002922 S.EmitRef(SourceMgr);
2923 S.EmitRef(Target);
2924 S.EmitRef(Idents);
2925 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002926
Ted Kremenek68228a92007-10-31 22:44:07 +00002927 // Emit the size of the type vector so that we can reserve that size
2928 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002929 S.EmitInt(Types.size());
2930
Ted Kremenek034a78c2007-11-13 22:02:55 +00002931 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2932 I!=E;++I)
2933 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002934
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002935 S.EmitOwnedPtr(TUDecl);
2936
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002937 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002938}
2939
Ted Kremenekacba3612007-11-13 00:25:37 +00002940ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002941
2942 // Read the language options.
2943 LangOptions LOpts;
2944 LOpts.Read(D);
2945
Ted Kremenek68228a92007-10-31 22:44:07 +00002946 SourceManager &SM = D.ReadRef<SourceManager>();
2947 TargetInfo &t = D.ReadRef<TargetInfo>();
2948 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2949 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002950
Ted Kremenek68228a92007-10-31 22:44:07 +00002951 unsigned size_reserve = D.ReadInt();
2952
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002953 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2954 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002955
Ted Kremenek034a78c2007-11-13 22:02:55 +00002956 for (unsigned i = 0; i < size_reserve; ++i)
2957 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002958
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002959 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2960
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002961 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002962
2963 return A;
2964}