blob: 2662860af2fef98c7379b5fa7a46e17d238ed386 [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000023#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000024
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
27enum FloatingRank {
28 FloatRank, DoubleRank, LongDoubleRank
29};
30
Chris Lattner2fda0ed2008-10-05 17:34:18 +000031ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
32 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000033 IdentifierTable &idents, SelectorTable &sels,
34 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
36 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000037 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000038{
39 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
41 BuiltinInfo.InitializeBuiltins(idents, Target);
42 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Chris Lattner4b009652007-07-25 00:24:17 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000049 Types.pop_back();
50 }
Eli Friedman65489b72008-05-27 03:08:09 +000051
Nuno Lopes355a8682008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Eli Friedman65489b72008-05-27 03:08:09 +000079 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000080}
81
82void ASTContext::PrintStats() const {
83 fprintf(stderr, "*** AST Context Stats:\n");
84 fprintf(stderr, " %d types total.\n", (int)Types.size());
85 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000086 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000087 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000088 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000089
90 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000091 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
92 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000093 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000094
95 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
96 Type *T = Types[i];
97 if (isa<BuiltinType>(T))
98 ++NumBuiltin;
99 else if (isa<PointerType>(T))
100 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000101 else if (isa<BlockPointerType>(T))
102 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000103 else if (isa<ReferenceType>(T))
104 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000105 else if (isa<MemberPointerType>(T))
106 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000107 else if (isa<ComplexType>(T))
108 ++NumComplex;
109 else if (isa<ArrayType>(T))
110 ++NumArray;
111 else if (isa<VectorType>(T))
112 ++NumVector;
113 else if (isa<FunctionTypeNoProto>(T))
114 ++NumFunctionNP;
115 else if (isa<FunctionTypeProto>(T))
116 ++NumFunctionP;
117 else if (isa<TypedefType>(T))
118 ++NumTypeName;
119 else if (TagType *TT = dyn_cast<TagType>(T)) {
120 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000121 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000123 case TagDecl::TK_struct: ++NumTagStruct; break;
124 case TagDecl::TK_union: ++NumTagUnion; break;
125 case TagDecl::TK_class: ++NumTagClass; break;
126 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000127 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000128 } else if (isa<ObjCInterfaceType>(T))
129 ++NumObjCInterfaces;
130 else if (isa<ObjCQualifiedInterfaceType>(T))
131 ++NumObjCQualifiedInterfaces;
132 else if (isa<ObjCQualifiedIdType>(T))
133 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000134 else if (isa<TypeOfType>(T))
135 ++NumTypeOfTypes;
136 else if (isa<TypeOfExpr>(T))
137 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000138 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000139 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000140 assert(0 && "Unknown type!");
141 }
142 }
143
144 fprintf(stderr, " %d builtin types\n", NumBuiltin);
145 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000146 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000147 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000148 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 fprintf(stderr, " %d complex types\n", NumComplex);
150 fprintf(stderr, " %d array types\n", NumArray);
151 fprintf(stderr, " %d vector types\n", NumVector);
152 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
153 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
154 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
155 fprintf(stderr, " %d tagged types\n", NumTagged);
156 fprintf(stderr, " %d struct types\n", NumTagStruct);
157 fprintf(stderr, " %d union types\n", NumTagUnion);
158 fprintf(stderr, " %d class types\n", NumTagClass);
159 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000161 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000162 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000163 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000164 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000165 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
166 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
167
Chris Lattner4b009652007-07-25 00:24:17 +0000168 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
169 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
170 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000171 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000172 NumFunctionP*sizeof(FunctionTypeProto)+
173 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000174 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
175 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
178
179void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroffbd9375a2009-01-19 22:45:10 +0000180 void *Mem = Allocator.Allocate(sizeof(BuiltinType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000181 Types.push_back((R = QualType(new (Mem) 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.
266unsigned ASTContext::getDeclAlign(const Decl *D) {
267 // FIXME: If attribute(align) is specified on the decl, round up to it.
268
269 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
270 QualType T = VD->getType();
271 // Incomplete or function types default to 1.
272 if (T->isIncompleteType() || T->isFunctionType())
273 return 1;
274
275 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
276 T = cast<ArrayType>(T)->getElementType();
277
278 return getTypeAlign(T);
279 }
280
281 return 1;
282}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000283
Chris Lattner4b009652007-07-25 00:24:17 +0000284/// getTypeSize - Return the size of the specified type, in bits. This method
285/// does not work on incomplete types.
286std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000287ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000288 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000290 unsigned Align;
291 switch (T->getTypeClass()) {
292 case Type::TypeName: assert(0 && "Not a canonical type!");
293 case Type::FunctionNoProto:
294 case Type::FunctionProto:
295 default:
296 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000297 case Type::VariableArray:
298 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000299 case Type::DependentSizedArray:
300 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000301 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000302 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000303
Chris Lattner8cd0e932008-03-05 18:54:05 +0000304 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000305 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000306 Align = EltInfo.second;
307 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000308 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000309 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000310 case Type::Vector: {
311 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000312 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000313 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000314 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000315 // If the alignment is not a power of 2, round up to the next power of 2.
316 // This happens for non-power-of-2 length vectors.
317 // FIXME: this should probably be a target property.
318 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000319 break;
320 }
321
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000322 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000323 switch (cast<BuiltinType>(T)->getKind()) {
324 default: assert(0 && "Unknown builtin type!");
325 case BuiltinType::Void:
326 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000327 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000328 Width = Target.getBoolWidth();
329 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000330 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 case BuiltinType::Char_S:
332 case BuiltinType::Char_U:
333 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000334 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000335 Width = Target.getCharWidth();
336 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000337 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000338 case BuiltinType::WChar:
339 Width = Target.getWCharWidth();
340 Align = Target.getWCharAlign();
341 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000344 Width = Target.getShortWidth();
345 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000346 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000347 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000349 Width = Target.getIntWidth();
350 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000351 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000353 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000354 Width = Target.getLongWidth();
355 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000356 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000357 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = Target.getLongLongWidth();
360 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000361 break;
362 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000363 Width = Target.getFloatWidth();
364 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000365 break;
366 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000367 Width = Target.getDoubleWidth();
368 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000369 break;
370 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000371 Width = Target.getLongDoubleWidth();
372 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000374 }
375 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000376 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000377 // FIXME: Pointers into different addr spaces could have different sizes and
378 // alignment requirements: getPointerInfo should take an AddrSpace.
379 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000381 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000382 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000384 case Type::BlockPointer: {
385 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
386 Width = Target.getPointerWidth(AS);
387 Align = Target.getPointerAlign(AS);
388 break;
389 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000390 case Type::Pointer: {
391 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000392 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000393 Align = Target.getPointerAlign(AS);
394 break;
395 }
Chris Lattner4b009652007-07-25 00:24:17 +0000396 case Type::Reference:
397 // "When applied to a reference or a reference type, the result is the size
398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000403 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000404 // the GCC ABI, where pointers to data are one pointer large, pointers to
405 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000406 // other compilers too, we need to delegate this completely to TargetInfo
407 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000408 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
409 unsigned AS = Pointee.getAddressSpace();
410 Width = Target.getPointerWidth(AS);
411 if (Pointee->isFunctionType())
412 Width *= 2;
413 Align = Target.getPointerAlign(AS);
414 // GCC aligns at single pointer width.
415 }
Chris Lattner4b009652007-07-25 00:24:17 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000432 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000433 const TagType *TT = cast<TagType>(T);
434
435 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000436 Width = 1;
437 Align = 1;
438 break;
439 }
440
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000441 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000442 return getTypeInfo(ET->getDecl()->getIntegerType());
443
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000444 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000445 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000448 break;
449 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000450 }
Chris Lattner4b009652007-07-25 00:24:17 +0000451
452 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000453 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000454}
455
Devang Patelbfe323c2008-06-04 21:22:16 +0000456/// LayoutField - Field layout.
457void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000458 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000459 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000460 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000461 uint64_t FieldOffset = IsUnion ? 0 : Size;
462 uint64_t FieldSize;
463 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000464
465 // FIXME: Should this override struct packing? Probably we want to
466 // take the minimum?
467 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
468 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000469
470 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
471 // TODO: Need to check this algorithm on other targets!
472 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000473 FieldSize =
474 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000475
476 std::pair<uint64_t, unsigned> FieldInfo =
477 Context.getTypeInfo(FD->getType());
478 uint64_t TypeSize = FieldInfo.first;
479
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000480 // Determine the alignment of this bitfield. The packing
481 // attributes define a maximum and the alignment attribute defines
482 // a minimum.
483 // FIXME: What is the right behavior when the specified alignment
484 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000485 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000486 if (FieldPacking)
487 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000488 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
489 FieldAlign = std::max(FieldAlign, AA->getAlignment());
490
491 // Check if we need to add padding to give the field the correct
492 // alignment.
493 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
494 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
495
496 // Padding members don't affect overall alignment
497 if (!FD->getIdentifier())
498 FieldAlign = 1;
499 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000500 if (FD->getType()->isIncompleteArrayType()) {
501 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000502 // query getTypeInfo about these, so we figure it out here.
503 // Flexible array members don't have any size, but they
504 // have to be aligned appropriately for their element type.
505 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000506 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000507 FieldAlign = Context.getTypeAlign(ATy->getElementType());
508 } else {
509 std::pair<uint64_t, unsigned> FieldInfo =
510 Context.getTypeInfo(FD->getType());
511 FieldSize = FieldInfo.first;
512 FieldAlign = FieldInfo.second;
513 }
514
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000515 // Determine the alignment of this bitfield. The packing
516 // attributes define a maximum and the alignment attribute defines
517 // a minimum. Additionally, the packing alignment must be at least
518 // a byte for non-bitfields.
519 //
520 // FIXME: What is the right behavior when the specified alignment
521 // is smaller than the specified packing?
522 if (FieldPacking)
523 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000524 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
525 FieldAlign = std::max(FieldAlign, AA->getAlignment());
526
527 // Round up the current record size to the field's alignment boundary.
528 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
529 }
530
531 // Place this field at the current location.
532 FieldOffsets[FieldNo] = FieldOffset;
533
534 // Reserve space for this field.
535 if (IsUnion) {
536 Size = std::max(Size, FieldSize);
537 } else {
538 Size = FieldOffset + FieldSize;
539 }
540
541 // Remember max struct/class alignment.
542 Alignment = std::max(Alignment, FieldAlign);
543}
544
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000545static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
546 std::vector<FieldDecl*> &Fields) {
547 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
548 if (SuperClass)
549 CollectObjCIvars(SuperClass, Fields);
550 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
551 E = OI->ivar_end(); I != E; ++I) {
552 ObjCIvarDecl *IVDecl = (*I);
553 if (!IVDecl->isInvalidDecl())
554 Fields.push_back(cast<FieldDecl>(IVDecl));
555 }
556}
557
558/// addRecordToClass - produces record info. for the class for its
559/// ivars and all those inherited.
560///
561const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
562{
563 const RecordDecl *&RD = ASTRecordForInterface[D];
564 if (RD)
565 return RD;
566 std::vector<FieldDecl*> RecFields;
567 CollectObjCIvars(D, RecFields);
568 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
569 D->getLocation(),
570 D->getIdentifier());
571 /// FIXME! Can do collection of ivars and adding to the record while
572 /// doing it.
573 for (unsigned int i = 0; i != RecFields.size(); i++) {
574 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
575 RecFields[i]->getLocation(),
576 RecFields[i]->getIdentifier(),
577 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000578 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000579 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000580 }
581 NewRD->completeDefinition(*this);
582 RD = NewRD;
583 return RD;
584}
Devang Patel4b6bf702008-06-04 21:54:36 +0000585
Fariborz Jahanianea944842008-12-18 17:29:46 +0000586/// setFieldDecl - maps a field for the given Ivar reference node.
587//
588void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
589 const ObjCIvarDecl *Ivar,
590 const ObjCIvarRefExpr *MRef) {
591 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
592 lookupFieldDeclForIvar(*this, Ivar);
593 ASTFieldForIvarRef[MRef] = FD;
594}
595
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000596/// getASTObjcInterfaceLayout - Get or compute information about the layout of
597/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000598/// position information.
599const ASTRecordLayout &
600ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
601 // Look up this layout, if already laid out, return what we have.
602 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
603 if (Entry) return *Entry;
604
605 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
606 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000607 ASTRecordLayout *NewEntry = NULL;
608 unsigned FieldCount = D->ivar_size();
609 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
610 FieldCount++;
611 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
612 unsigned Alignment = SL.getAlignment();
613 uint64_t Size = SL.getSize();
614 NewEntry = new ASTRecordLayout(Size, Alignment);
615 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000616 // Super class is at the beginning of the layout.
617 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000618 } else {
619 NewEntry = new ASTRecordLayout();
620 NewEntry->InitializeLayout(FieldCount);
621 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000622 Entry = NewEntry;
623
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000624 unsigned StructPacking = 0;
625 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
626 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000627
628 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
629 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
630 AA->getAlignment()));
631
632 // Layout each ivar sequentially.
633 unsigned i = 0;
634 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
635 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
636 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000637 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000638 }
639
640 // Finally, round the size of the total struct up to the alignment of the
641 // struct itself.
642 NewEntry->FinalizeLayout();
643 return *NewEntry;
644}
645
Devang Patel7a78e432007-11-01 19:11:01 +0000646/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000647/// specified record (struct/union/class), which indicates its size and field
648/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000649const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000650 D = D->getDefinition(*this);
651 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000652
Chris Lattner4b009652007-07-25 00:24:17 +0000653 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000654 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000655 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000656
Devang Patel7a78e432007-11-01 19:11:01 +0000657 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
658 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
659 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000660 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000661
Douglas Gregor39677622008-12-11 20:41:00 +0000662 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000663 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000664 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000665
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000666 unsigned StructPacking = 0;
667 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
668 StructPacking = PA->getAlignment();
669
Eli Friedman5949a022008-05-30 09:31:38 +0000670 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000671 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
672 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000673
Eli Friedman5949a022008-05-30 09:31:38 +0000674 // Layout each field, for now, just sequentially, respecting alignment. In
675 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000676 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000677 for (RecordDecl::field_iterator Field = D->field_begin(),
678 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000679 Field != FieldEnd; (void)++Field, ++FieldIdx)
680 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000681
682 // Finally, round the size of the total struct up to the alignment of the
683 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000684 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000685 return *NewEntry;
686}
687
Chris Lattner4b009652007-07-25 00:24:17 +0000688//===----------------------------------------------------------------------===//
689// Type creation/memoization methods
690//===----------------------------------------------------------------------===//
691
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000692QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000693 QualType CanT = getCanonicalType(T);
694 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000695 return T;
696
697 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
698 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000699 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000700 "Type is already address space qualified");
701
702 // Check if we've already instantiated an address space qual'd type of this
703 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000704 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000705 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000706 void *InsertPos = 0;
707 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
708 return QualType(ASQy, 0);
709
710 // If the base type isn't canonical, this won't be a canonical type either,
711 // so fill in the canonical type field.
712 QualType Canonical;
713 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000714 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000715
716 // Get the new insert position for the node we care about.
717 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000718 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000719 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000720 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000721 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000722 ASQualTypes.InsertNode(New, InsertPos);
723 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000724 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000725}
726
Chris Lattner4b009652007-07-25 00:24:17 +0000727
728/// getComplexType - Return the uniqued reference to the type for a complex
729/// number with the specified element type.
730QualType ASTContext::getComplexType(QualType T) {
731 // Unique pointers, to guarantee there is only one pointer of a particular
732 // structure.
733 llvm::FoldingSetNodeID ID;
734 ComplexType::Profile(ID, T);
735
736 void *InsertPos = 0;
737 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
738 return QualType(CT, 0);
739
740 // If the pointee type isn't canonical, this won't be a canonical type either,
741 // so fill in the canonical type field.
742 QualType Canonical;
743 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000744 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000745
746 // Get the new insert position for the node we care about.
747 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000748 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000749 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000750 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000751 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000752 Types.push_back(New);
753 ComplexTypes.InsertNode(New, InsertPos);
754 return QualType(New, 0);
755}
756
757
758/// getPointerType - Return the uniqued reference to the type for a pointer to
759/// the specified type.
760QualType ASTContext::getPointerType(QualType T) {
761 // Unique pointers, to guarantee there is only one pointer of a particular
762 // structure.
763 llvm::FoldingSetNodeID ID;
764 PointerType::Profile(ID, T);
765
766 void *InsertPos = 0;
767 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
768 return QualType(PT, 0);
769
770 // If the pointee type isn't canonical, this won't be a canonical type either,
771 // so fill in the canonical type field.
772 QualType Canonical;
773 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000774 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000775
776 // Get the new insert position for the node we care about.
777 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000778 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000779 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000780 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000781 PointerType *New = new (Mem) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000782 Types.push_back(New);
783 PointerTypes.InsertNode(New, InsertPos);
784 return QualType(New, 0);
785}
786
Steve Naroff7aa54752008-08-27 16:04:49 +0000787/// getBlockPointerType - Return the uniqued reference to the type for
788/// a pointer to the specified block.
789QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000790 assert(T->isFunctionType() && "block of function types only");
791 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000792 // structure.
793 llvm::FoldingSetNodeID ID;
794 BlockPointerType::Profile(ID, T);
795
796 void *InsertPos = 0;
797 if (BlockPointerType *PT =
798 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
799 return QualType(PT, 0);
800
Steve Narofffd5b19d2008-08-28 19:20:44 +0000801 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000802 // type either so fill in the canonical type field.
803 QualType Canonical;
804 if (!T->isCanonical()) {
805 Canonical = getBlockPointerType(getCanonicalType(T));
806
807 // Get the new insert position for the node we care about.
808 BlockPointerType *NewIP =
809 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000810 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000811 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000812 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000813 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000814 Types.push_back(New);
815 BlockPointerTypes.InsertNode(New, InsertPos);
816 return QualType(New, 0);
817}
818
Chris Lattner4b009652007-07-25 00:24:17 +0000819/// getReferenceType - Return the uniqued reference to the type for a reference
820/// to the specified type.
821QualType ASTContext::getReferenceType(QualType T) {
822 // Unique pointers, to guarantee there is only one pointer of a particular
823 // structure.
824 llvm::FoldingSetNodeID ID;
825 ReferenceType::Profile(ID, T);
826
827 void *InsertPos = 0;
828 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
829 return QualType(RT, 0);
830
831 // If the referencee type isn't canonical, this won't be a canonical type
832 // either, so fill in the canonical type field.
833 QualType Canonical;
834 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000835 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000836
837 // Get the new insert position for the node we care about.
838 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000839 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000840 }
841
Steve Naroffbd9375a2009-01-19 22:45:10 +0000842 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000843 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000844 Types.push_back(New);
845 ReferenceTypes.InsertNode(New, InsertPos);
846 return QualType(New, 0);
847}
848
Sebastian Redl75555032009-01-24 21:16:55 +0000849/// getMemberPointerType - Return the uniqued reference to the type for a
850/// member pointer to the specified type, in the specified class.
851QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
852{
853 // Unique pointers, to guarantee there is only one pointer of a particular
854 // structure.
855 llvm::FoldingSetNodeID ID;
856 MemberPointerType::Profile(ID, T, Cls);
857
858 void *InsertPos = 0;
859 if (MemberPointerType *PT =
860 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
861 return QualType(PT, 0);
862
863 // If the pointee or class type isn't canonical, this won't be a canonical
864 // type either, so fill in the canonical type field.
865 QualType Canonical;
866 if (!T->isCanonical()) {
867 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
868
869 // Get the new insert position for the node we care about.
870 MemberPointerType *NewIP =
871 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
872 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
873 }
874 void *Mem = Allocator.Allocate(sizeof(MemberPointerType), 8);
875 MemberPointerType *New = new (Mem) MemberPointerType(T, Cls, Canonical);
876 Types.push_back(New);
877 MemberPointerTypes.InsertNode(New, InsertPos);
878 return QualType(New, 0);
879}
880
Steve Naroff83c13012007-08-30 01:06:46 +0000881/// getConstantArrayType - Return the unique reference to the type for an
882/// array of the specified element type.
883QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000884 const llvm::APInt &ArySize,
885 ArrayType::ArraySizeModifier ASM,
886 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000887 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000888 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000889
890 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000891 if (ConstantArrayType *ATP =
892 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000893 return QualType(ATP, 0);
894
895 // If the element type isn't canonical, this won't be a canonical type either,
896 // so fill in the canonical type field.
897 QualType Canonical;
898 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000899 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000900 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000901 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000902 ConstantArrayType *NewIP =
903 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000904 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000905 }
906
Steve Naroffbd9375a2009-01-19 22:45:10 +0000907 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000908 ConstantArrayType *New =
909 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000910 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000911 Types.push_back(New);
912 return QualType(New, 0);
913}
914
Steve Naroffe2579e32007-08-30 18:14:25 +0000915/// getVariableArrayType - Returns a non-unique reference to the type for a
916/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000917QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
918 ArrayType::ArraySizeModifier ASM,
919 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000920 // Since we don't unique expressions, it isn't possible to unique VLA's
921 // that have an expression provided for their size.
922
Steve Naroffbd9375a2009-01-19 22:45:10 +0000923 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000924 VariableArrayType *New =
925 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000926
927 VariableArrayTypes.push_back(New);
928 Types.push_back(New);
929 return QualType(New, 0);
930}
931
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000932/// getDependentSizedArrayType - Returns a non-unique reference to
933/// the type for a dependently-sized array of the specified element
934/// type. FIXME: We will need these to be uniqued, or at least
935/// comparable, at some point.
936QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
937 ArrayType::ArraySizeModifier ASM,
938 unsigned EltTypeQuals) {
939 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
940 "Size must be type- or value-dependent!");
941
942 // Since we don't unique expressions, it isn't possible to unique
943 // dependently-sized array types.
944
Steve Naroffbd9375a2009-01-19 22:45:10 +0000945 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000946 DependentSizedArrayType *New =
947 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
948 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000949
950 DependentSizedArrayTypes.push_back(New);
951 Types.push_back(New);
952 return QualType(New, 0);
953}
954
Eli Friedman8ff07782008-02-15 18:16:39 +0000955QualType ASTContext::getIncompleteArrayType(QualType EltTy,
956 ArrayType::ArraySizeModifier ASM,
957 unsigned EltTypeQuals) {
958 llvm::FoldingSetNodeID ID;
959 IncompleteArrayType::Profile(ID, EltTy);
960
961 void *InsertPos = 0;
962 if (IncompleteArrayType *ATP =
963 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
964 return QualType(ATP, 0);
965
966 // If the element type isn't canonical, this won't be a canonical type
967 // either, so fill in the canonical type field.
968 QualType Canonical;
969
970 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000971 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000972 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000973
974 // Get the new insert position for the node we care about.
975 IncompleteArrayType *NewIP =
976 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000977 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000978 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000979
Steve Naroffbd9375a2009-01-19 22:45:10 +0000980 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000981 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
982 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000983
984 IncompleteArrayTypes.InsertNode(New, InsertPos);
985 Types.push_back(New);
986 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000987}
988
Chris Lattner4b009652007-07-25 00:24:17 +0000989/// getVectorType - Return the unique reference to a vector type of
990/// the specified element type and size. VectorType must be a built-in type.
991QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
992 BuiltinType *baseType;
993
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000994 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000995 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
996
997 // Check if we've already instantiated a vector of this type.
998 llvm::FoldingSetNodeID ID;
999 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1000 void *InsertPos = 0;
1001 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1002 return QualType(VTP, 0);
1003
1004 // If the element type isn't canonical, this won't be a canonical type either,
1005 // so fill in the canonical type field.
1006 QualType Canonical;
1007 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001008 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001009
1010 // Get the new insert position for the node we care about.
1011 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001012 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001013 }
Steve Naroffbd9375a2009-01-19 22:45:10 +00001014 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001015 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001016 VectorTypes.InsertNode(New, InsertPos);
1017 Types.push_back(New);
1018 return QualType(New, 0);
1019}
1020
Nate Begemanaf6ed502008-04-18 23:10:10 +00001021/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001022/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001023QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001024 BuiltinType *baseType;
1025
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001026 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001027 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001028
1029 // Check if we've already instantiated a vector of this type.
1030 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001031 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001032 void *InsertPos = 0;
1033 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1034 return QualType(VTP, 0);
1035
1036 // If the element type isn't canonical, this won't be a canonical type either,
1037 // so fill in the canonical type field.
1038 QualType Canonical;
1039 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001040 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001041
1042 // Get the new insert position for the node we care about.
1043 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001044 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001045 }
Steve Naroffbd9375a2009-01-19 22:45:10 +00001046 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001047 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001048 VectorTypes.InsertNode(New, InsertPos);
1049 Types.push_back(New);
1050 return QualType(New, 0);
1051}
1052
1053/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1054///
1055QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1056 // Unique functions, to guarantee there is only one function of a particular
1057 // structure.
1058 llvm::FoldingSetNodeID ID;
1059 FunctionTypeNoProto::Profile(ID, ResultTy);
1060
1061 void *InsertPos = 0;
1062 if (FunctionTypeNoProto *FT =
1063 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1064 return QualType(FT, 0);
1065
1066 QualType Canonical;
1067 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001068 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001069
1070 // Get the new insert position for the node we care about.
1071 FunctionTypeNoProto *NewIP =
1072 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001073 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001074 }
1075
Douglas Gregor5c561212009-01-20 21:02:13 +00001076 void *Mem = Allocator.Allocate(sizeof(FunctionTypeNoProto), 8);
1077 FunctionTypeNoProto *New = new (Mem) FunctionTypeNoProto(ResultTy, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001078 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001079 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001080 return QualType(New, 0);
1081}
1082
1083/// getFunctionType - Return a normal function type with a typed argument
1084/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001085QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001086 unsigned NumArgs, bool isVariadic,
1087 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001088 // Unique functions, to guarantee there is only one function of a particular
1089 // structure.
1090 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001091 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1092 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001093
1094 void *InsertPos = 0;
1095 if (FunctionTypeProto *FTP =
1096 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1097 return QualType(FTP, 0);
1098
1099 // Determine whether the type being created is already canonical or not.
1100 bool isCanonical = ResultTy->isCanonical();
1101 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1102 if (!ArgArray[i]->isCanonical())
1103 isCanonical = false;
1104
1105 // If this type isn't canonical, get the canonical version of it.
1106 QualType Canonical;
1107 if (!isCanonical) {
1108 llvm::SmallVector<QualType, 16> CanonicalArgs;
1109 CanonicalArgs.reserve(NumArgs);
1110 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001111 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001112
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001113 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001114 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001115 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001116
1117 // Get the new insert position for the node we care about.
1118 FunctionTypeProto *NewIP =
1119 FunctionTypeProtos.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 }
1122
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001123 // FunctionTypeProto objects are allocated with extra bytes after them
1124 // for a variable size array (for parameter types) at the end of them.
1125 // FIXME: Can we do better than forcing a 16-byte alignment?
Chris Lattner4b009652007-07-25 00:24:17 +00001126 FunctionTypeProto *FTP =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001127 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1128 NumArgs*sizeof(QualType), 16);
Chris Lattner4b009652007-07-25 00:24:17 +00001129 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001130 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001131 Types.push_back(FTP);
1132 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1133 return QualType(FTP, 0);
1134}
1135
Douglas Gregor1d661552008-04-13 21:07:44 +00001136/// getTypeDeclType - Return the unique reference to the type for the
1137/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001138QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001139 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001140 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1141
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001142 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001143 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +00001144 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1145 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001146 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001147 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001148
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001149 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001150 if (PrevDecl)
1151 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1152 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001153 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001154 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1155 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001156 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001157 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001158 if (PrevDecl)
1159 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1160 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001161 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001162 Decl->TypeForDecl = new (Mem) RecordType(Record);
1163 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001164 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001165 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1166 if (PrevDecl)
1167 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1168 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001169 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001170 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1171 }
1172 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001173 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001174 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001175
Ted Kremenek46a837c2008-09-05 17:16:31 +00001176 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001177 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001178}
1179
Chris Lattner4b009652007-07-25 00:24:17 +00001180/// getTypedefType - Return the unique reference to the type for the
1181/// specified typename decl.
1182QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1183 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1184
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001185 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroffbd9375a2009-01-19 22:45:10 +00001186 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001187 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001188 Types.push_back(Decl->TypeForDecl);
1189 return QualType(Decl->TypeForDecl, 0);
1190}
1191
Douglas Gregordd861062008-12-05 18:15:24 +00001192/// getTemplateTypeParmType - Return the unique reference to the type
1193/// for the specified template type parameter declaration.
1194QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1195 if (!Decl->TypeForDecl) {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001196 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001197 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregordd861062008-12-05 18:15:24 +00001198 Types.push_back(Decl->TypeForDecl);
1199 }
1200 return QualType(Decl->TypeForDecl, 0);
1201}
1202
Ted Kremenek42730c52008-01-07 19:49:32 +00001203/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001204/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001205QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001206 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1207
Steve Naroffbd9375a2009-01-19 22:45:10 +00001208 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001209 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001210 Types.push_back(Decl->TypeForDecl);
1211 return QualType(Decl->TypeForDecl, 0);
1212}
1213
Chris Lattnere1352302008-04-07 04:56:42 +00001214/// CmpProtocolNames - Comparison predicate for sorting protocols
1215/// alphabetically.
1216static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1217 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001218 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001219}
1220
1221static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1222 unsigned &NumProtocols) {
1223 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1224
1225 // Sort protocols, keyed by name.
1226 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1227
1228 // Remove duplicates.
1229 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1230 NumProtocols = ProtocolsEnd-Protocols;
1231}
1232
1233
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001234/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1235/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001236QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1237 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001238 // Sort the protocol list alphabetically to canonicalize it.
1239 SortAndUniqueProtocols(Protocols, NumProtocols);
1240
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001241 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001242 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001243
1244 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001245 if (ObjCQualifiedInterfaceType *QT =
1246 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001247 return QualType(QT, 0);
1248
1249 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001250 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremenek42730c52008-01-07 19:49:32 +00001251 ObjCQualifiedInterfaceType *QType =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001252 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1253
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001254 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001255 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001256 return QualType(QType, 0);
1257}
1258
Chris Lattnere1352302008-04-07 04:56:42 +00001259/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1260/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001261QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001262 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001263 // Sort the protocol list alphabetically to canonicalize it.
1264 SortAndUniqueProtocols(Protocols, NumProtocols);
1265
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001266 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001268
1269 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001270 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001271 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001272 return QualType(QT, 0);
1273
1274 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001275 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001276 ObjCQualifiedIdType *QType =
1277 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001278 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001279 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001280 return QualType(QType, 0);
1281}
1282
Steve Naroff0604dd92007-08-01 18:02:17 +00001283/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1284/// TypeOfExpr AST's (since expression's are never shared). For example,
1285/// multiple declarations that refer to "typeof(x)" all contain different
1286/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1287/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001288QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001289 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor5c561212009-01-20 21:02:13 +00001290 void *Mem = Allocator.Allocate(sizeof(TypeOfExpr), 8);
1291 TypeOfExpr *toe = new (Mem) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001292 Types.push_back(toe);
1293 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001294}
1295
Steve Naroff0604dd92007-08-01 18:02:17 +00001296/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1297/// TypeOfType AST's. The only motivation to unique these nodes would be
1298/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1299/// an issue. This doesn't effect the type checker, since it operates
1300/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001301QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001302 QualType Canonical = getCanonicalType(tofType);
Steve Naroffbd9375a2009-01-19 22:45:10 +00001303 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001304 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001305 Types.push_back(tot);
1306 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001307}
1308
Chris Lattner4b009652007-07-25 00:24:17 +00001309/// getTagDeclType - Return the unique reference to the type for the
1310/// specified TagDecl (struct/union/class/enum) decl.
1311QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001312 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001313 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001314}
1315
1316/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1317/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1318/// needs to agree with the definition in <stddef.h>.
1319QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001320 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001321}
1322
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001323/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001324/// width of characters in wide strings, The value is target dependent and
1325/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001326QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001327 if (LangOpts.CPlusPlus)
1328 return WCharTy;
1329
Douglas Gregorc6507e42008-11-03 14:12:49 +00001330 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1331 // wide-character type?
1332 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001333}
1334
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001335/// getSignedWCharType - Return the type of "signed wchar_t".
1336/// Used when in C++, as a GCC extension.
1337QualType ASTContext::getSignedWCharType() const {
1338 // FIXME: derive from "Target" ?
1339 return WCharTy;
1340}
1341
1342/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1343/// Used when in C++, as a GCC extension.
1344QualType ASTContext::getUnsignedWCharType() const {
1345 // FIXME: derive from "Target" ?
1346 return UnsignedIntTy;
1347}
1348
Chris Lattner4b009652007-07-25 00:24:17 +00001349/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1350/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1351QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001352 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001353}
1354
Chris Lattner19eb97e2008-04-02 05:18:44 +00001355//===----------------------------------------------------------------------===//
1356// Type Operators
1357//===----------------------------------------------------------------------===//
1358
Chris Lattner3dae6f42008-04-06 22:41:35 +00001359/// getCanonicalType - Return the canonical (structural) type corresponding to
1360/// the specified potentially non-canonical type. The non-canonical version
1361/// of a type may have many "decorated" versions of types. Decorators can
1362/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1363/// to be free of any of these, allowing two canonical types to be compared
1364/// for exact equality with a simple pointer comparison.
1365QualType ASTContext::getCanonicalType(QualType T) {
1366 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001367
1368 // If the result has type qualifiers, make sure to canonicalize them as well.
1369 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1370 if (TypeQuals == 0) return CanType;
1371
1372 // If the type qualifiers are on an array type, get the canonical type of the
1373 // array with the qualifiers applied to the element type.
1374 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1375 if (!AT)
1376 return CanType.getQualifiedType(TypeQuals);
1377
1378 // Get the canonical version of the element with the extra qualifiers on it.
1379 // This can recursively sink qualifiers through multiple levels of arrays.
1380 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1381 NewEltTy = getCanonicalType(NewEltTy);
1382
1383 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1384 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1385 CAT->getIndexTypeQualifier());
1386 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1387 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1388 IAT->getIndexTypeQualifier());
1389
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001390 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1391 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1392 DSAT->getSizeModifier(),
1393 DSAT->getIndexTypeQualifier());
1394
Chris Lattnera1923f62008-08-04 07:31:14 +00001395 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1396 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1397 VAT->getSizeModifier(),
1398 VAT->getIndexTypeQualifier());
1399}
1400
1401
1402const ArrayType *ASTContext::getAsArrayType(QualType T) {
1403 // Handle the non-qualified case efficiently.
1404 if (T.getCVRQualifiers() == 0) {
1405 // Handle the common positive case fast.
1406 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1407 return AT;
1408 }
1409
1410 // Handle the common negative case fast, ignoring CVR qualifiers.
1411 QualType CType = T->getCanonicalTypeInternal();
1412
1413 // Make sure to look through type qualifiers (like ASQuals) for the negative
1414 // test.
1415 if (!isa<ArrayType>(CType) &&
1416 !isa<ArrayType>(CType.getUnqualifiedType()))
1417 return 0;
1418
1419 // Apply any CVR qualifiers from the array type to the element type. This
1420 // implements C99 6.7.3p8: "If the specification of an array type includes
1421 // any type qualifiers, the element type is so qualified, not the array type."
1422
1423 // If we get here, we either have type qualifiers on the type, or we have
1424 // sugar such as a typedef in the way. If we have type qualifiers on the type
1425 // we must propagate them down into the elemeng type.
1426 unsigned CVRQuals = T.getCVRQualifiers();
1427 unsigned AddrSpace = 0;
1428 Type *Ty = T.getTypePtr();
1429
1430 // Rip through ASQualType's and typedefs to get to a concrete type.
1431 while (1) {
1432 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1433 AddrSpace = ASQT->getAddressSpace();
1434 Ty = ASQT->getBaseType();
1435 } else {
1436 T = Ty->getDesugaredType();
1437 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1438 break;
1439 CVRQuals |= T.getCVRQualifiers();
1440 Ty = T.getTypePtr();
1441 }
1442 }
1443
1444 // If we have a simple case, just return now.
1445 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1446 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1447 return ATy;
1448
1449 // Otherwise, we have an array and we have qualifiers on it. Push the
1450 // qualifiers into the array element type and return a new array type.
1451 // Get the canonical version of the element with the extra qualifiers on it.
1452 // This can recursively sink qualifiers through multiple levels of arrays.
1453 QualType NewEltTy = ATy->getElementType();
1454 if (AddrSpace)
1455 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1456 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1457
1458 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1459 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1460 CAT->getSizeModifier(),
1461 CAT->getIndexTypeQualifier()));
1462 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1463 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1464 IAT->getSizeModifier(),
1465 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001466
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001467 if (const DependentSizedArrayType *DSAT
1468 = dyn_cast<DependentSizedArrayType>(ATy))
1469 return cast<ArrayType>(
1470 getDependentSizedArrayType(NewEltTy,
1471 DSAT->getSizeExpr(),
1472 DSAT->getSizeModifier(),
1473 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001474
Chris Lattnera1923f62008-08-04 07:31:14 +00001475 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1476 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1477 VAT->getSizeModifier(),
1478 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001479}
1480
1481
Chris Lattner19eb97e2008-04-02 05:18:44 +00001482/// getArrayDecayedType - Return the properly qualified result of decaying the
1483/// specified array type to a pointer. This operation is non-trivial when
1484/// handling typedefs etc. The canonical type of "T" must be an array type,
1485/// this returns a pointer to a properly qualified element of the array.
1486///
1487/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1488QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001489 // Get the element type with 'getAsArrayType' so that we don't lose any
1490 // typedefs in the element type of the array. This also handles propagation
1491 // of type qualifiers from the array type into the element type if present
1492 // (C99 6.7.3p8).
1493 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1494 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001495
Chris Lattnera1923f62008-08-04 07:31:14 +00001496 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001497
1498 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001499 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001500}
1501
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001502QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001503 QualType ElemTy = VAT->getElementType();
1504
1505 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1506 return getBaseElementType(VAT);
1507
1508 return ElemTy;
1509}
1510
Chris Lattner4b009652007-07-25 00:24:17 +00001511/// getFloatingRank - Return a relative rank for floating point types.
1512/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001513static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001514 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001515 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001516
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001517 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001518 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001519 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001520 case BuiltinType::Float: return FloatRank;
1521 case BuiltinType::Double: return DoubleRank;
1522 case BuiltinType::LongDouble: return LongDoubleRank;
1523 }
1524}
1525
Steve Narofffa0c4532007-08-27 01:41:48 +00001526/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1527/// point or a complex type (based on typeDomain/typeSize).
1528/// 'typeDomain' is a real floating point or complex type.
1529/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001530QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1531 QualType Domain) const {
1532 FloatingRank EltRank = getFloatingRank(Size);
1533 if (Domain->isComplexType()) {
1534 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001535 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001536 case FloatRank: return FloatComplexTy;
1537 case DoubleRank: return DoubleComplexTy;
1538 case LongDoubleRank: return LongDoubleComplexTy;
1539 }
Chris Lattner4b009652007-07-25 00:24:17 +00001540 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001541
1542 assert(Domain->isRealFloatingType() && "Unknown domain!");
1543 switch (EltRank) {
1544 default: assert(0 && "getFloatingRank(): illegal value for rank");
1545 case FloatRank: return FloatTy;
1546 case DoubleRank: return DoubleTy;
1547 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001548 }
Chris Lattner4b009652007-07-25 00:24:17 +00001549}
1550
Chris Lattner51285d82008-04-06 23:55:33 +00001551/// getFloatingTypeOrder - Compare the rank of the two specified floating
1552/// point types, ignoring the domain of the type (i.e. 'double' ==
1553/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1554/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001555int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1556 FloatingRank LHSR = getFloatingRank(LHS);
1557 FloatingRank RHSR = getFloatingRank(RHS);
1558
1559 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001560 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001561 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001562 return 1;
1563 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001564}
1565
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001566/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1567/// routine will assert if passed a built-in type that isn't an integer or enum,
1568/// or if it is not canonicalized.
1569static unsigned getIntegerRank(Type *T) {
1570 assert(T->isCanonical() && "T should be canonicalized");
1571 if (isa<EnumType>(T))
1572 return 4;
1573
1574 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001575 default: assert(0 && "getIntegerRank(): not a built-in integer");
1576 case BuiltinType::Bool:
1577 return 1;
1578 case BuiltinType::Char_S:
1579 case BuiltinType::Char_U:
1580 case BuiltinType::SChar:
1581 case BuiltinType::UChar:
1582 return 2;
1583 case BuiltinType::Short:
1584 case BuiltinType::UShort:
1585 return 3;
1586 case BuiltinType::Int:
1587 case BuiltinType::UInt:
1588 return 4;
1589 case BuiltinType::Long:
1590 case BuiltinType::ULong:
1591 return 5;
1592 case BuiltinType::LongLong:
1593 case BuiltinType::ULongLong:
1594 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001595 }
1596}
1597
Chris Lattner51285d82008-04-06 23:55:33 +00001598/// getIntegerTypeOrder - Returns the highest ranked integer type:
1599/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1600/// LHS < RHS, return -1.
1601int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001602 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1603 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001604 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001605
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001606 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1607 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001608
Chris Lattner51285d82008-04-06 23:55:33 +00001609 unsigned LHSRank = getIntegerRank(LHSC);
1610 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001611
Chris Lattner51285d82008-04-06 23:55:33 +00001612 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1613 if (LHSRank == RHSRank) return 0;
1614 return LHSRank > RHSRank ? 1 : -1;
1615 }
Chris Lattner4b009652007-07-25 00:24:17 +00001616
Chris Lattner51285d82008-04-06 23:55:33 +00001617 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1618 if (LHSUnsigned) {
1619 // If the unsigned [LHS] type is larger, return it.
1620 if (LHSRank >= RHSRank)
1621 return 1;
1622
1623 // If the signed type can represent all values of the unsigned type, it
1624 // wins. Because we are dealing with 2's complement and types that are
1625 // powers of two larger than each other, this is always safe.
1626 return -1;
1627 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001628
Chris Lattner51285d82008-04-06 23:55:33 +00001629 // If the unsigned [RHS] type is larger, return it.
1630 if (RHSRank >= LHSRank)
1631 return -1;
1632
1633 // If the signed type can represent all values of the unsigned type, it
1634 // wins. Because we are dealing with 2's complement and types that are
1635 // powers of two larger than each other, this is always safe.
1636 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001637}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001638
1639// getCFConstantStringType - Return the type used for constant CFStrings.
1640QualType ASTContext::getCFConstantStringType() {
1641 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001642 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001643 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001644 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001645 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001646
1647 // const int *isa;
1648 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001649 // int flags;
1650 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001651 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001652 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001653 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001654 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001655
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001656 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001657 for (unsigned i = 0; i < 4; ++i) {
1658 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1659 SourceLocation(), 0,
1660 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001661 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001662 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001663 }
1664
1665 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001666 }
1667
1668 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001669}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001670
Anders Carlssonf58cac72008-08-30 19:34:46 +00001671QualType ASTContext::getObjCFastEnumerationStateType()
1672{
1673 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001674 ObjCFastEnumerationStateTypeDecl =
1675 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1676 &Idents.get("__objcFastEnumerationState"));
1677
Anders Carlssonf58cac72008-08-30 19:34:46 +00001678 QualType FieldTypes[] = {
1679 UnsignedLongTy,
1680 getPointerType(ObjCIdType),
1681 getPointerType(UnsignedLongTy),
1682 getConstantArrayType(UnsignedLongTy,
1683 llvm::APInt(32, 5), ArrayType::Normal, 0)
1684 };
1685
Douglas Gregor8acb7272008-12-11 16:49:14 +00001686 for (size_t i = 0; i < 4; ++i) {
1687 FieldDecl *Field = FieldDecl::Create(*this,
1688 ObjCFastEnumerationStateTypeDecl,
1689 SourceLocation(), 0,
1690 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001691 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001692 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001693 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001694
Douglas Gregor8acb7272008-12-11 16:49:14 +00001695 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001696 }
1697
1698 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1699}
1700
Anders Carlssone3f02572007-10-29 06:33:42 +00001701// This returns true if a type has been typedefed to BOOL:
1702// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001703static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001704 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001705 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1706 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001707
1708 return false;
1709}
1710
Ted Kremenek42730c52008-01-07 19:49:32 +00001711/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001712/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001713int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001714 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001715
1716 // Make all integer and enum types at least as large as an int
1717 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001718 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001719 // Treat arrays as pointers, since that's how they're passed in.
1720 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001721 sz = getTypeSize(VoidPtrTy);
1722 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001723}
1724
Ted Kremenek42730c52008-01-07 19:49:32 +00001725/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001726/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001727void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001728 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001729 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001730 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001731 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001732 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001733 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001734 // Compute size of all parameters.
1735 // Start with computing size of a pointer in number of bytes.
1736 // FIXME: There might(should) be a better way of doing this computation!
1737 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001738 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001739 // The first two arguments (self and _cmd) are pointers; account for
1740 // their size.
1741 int ParmOffset = 2 * PtrSize;
1742 int NumOfParams = Decl->getNumParams();
1743 for (int i = 0; i < NumOfParams; i++) {
1744 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001745 int sz = getObjCEncodingTypeSize (PType);
1746 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001747 ParmOffset += sz;
1748 }
1749 S += llvm::utostr(ParmOffset);
1750 S += "@0:";
1751 S += llvm::utostr(PtrSize);
1752
1753 // Argument types.
1754 ParmOffset = 2 * PtrSize;
1755 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001756 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1757 QualType PType = PVDecl->getOriginalType();
1758 if (const ArrayType *AT =
1759 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1760 // Use array's original type only if it has known number of
1761 // elements.
1762 if (!dyn_cast<ConstantArrayType>(AT))
1763 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001764 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001765 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001766 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001767 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001768 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001769 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001770 }
1771}
1772
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001773/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001774/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001775/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1776/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001777/// Property attributes are stored as a comma-delimited C string. The simple
1778/// attributes readonly and bycopy are encoded as single characters. The
1779/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1780/// encoded as single characters, followed by an identifier. Property types
1781/// are also encoded as a parametrized attribute. The characters used to encode
1782/// these attributes are defined by the following enumeration:
1783/// @code
1784/// enum PropertyAttributes {
1785/// kPropertyReadOnly = 'R', // property is read-only.
1786/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1787/// kPropertyByref = '&', // property is a reference to the value last assigned
1788/// kPropertyDynamic = 'D', // property is dynamic
1789/// kPropertyGetter = 'G', // followed by getter selector name
1790/// kPropertySetter = 'S', // followed by setter selector name
1791/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1792/// kPropertyType = 't' // followed by old-style type encoding.
1793/// kPropertyWeak = 'W' // 'weak' property
1794/// kPropertyStrong = 'P' // property GC'able
1795/// kPropertyNonAtomic = 'N' // property non-atomic
1796/// };
1797/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001798void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1799 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001800 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001801 // Collect information from the property implementation decl(s).
1802 bool Dynamic = false;
1803 ObjCPropertyImplDecl *SynthesizePID = 0;
1804
1805 // FIXME: Duplicated code due to poor abstraction.
1806 if (Container) {
1807 if (const ObjCCategoryImplDecl *CID =
1808 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1809 for (ObjCCategoryImplDecl::propimpl_iterator
1810 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1811 ObjCPropertyImplDecl *PID = *i;
1812 if (PID->getPropertyDecl() == PD) {
1813 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1814 Dynamic = true;
1815 } else {
1816 SynthesizePID = PID;
1817 }
1818 }
1819 }
1820 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001821 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001822 for (ObjCCategoryImplDecl::propimpl_iterator
1823 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1824 ObjCPropertyImplDecl *PID = *i;
1825 if (PID->getPropertyDecl() == PD) {
1826 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1827 Dynamic = true;
1828 } else {
1829 SynthesizePID = PID;
1830 }
1831 }
1832 }
1833 }
1834 }
1835
1836 // FIXME: This is not very efficient.
1837 S = "T";
1838
1839 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001840 // GCC has some special rules regarding encoding of properties which
1841 // closely resembles encoding of ivars.
1842 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1843 true /* outermost type */,
1844 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001845
1846 if (PD->isReadOnly()) {
1847 S += ",R";
1848 } else {
1849 switch (PD->getSetterKind()) {
1850 case ObjCPropertyDecl::Assign: break;
1851 case ObjCPropertyDecl::Copy: S += ",C"; break;
1852 case ObjCPropertyDecl::Retain: S += ",&"; break;
1853 }
1854 }
1855
1856 // It really isn't clear at all what this means, since properties
1857 // are "dynamic by default".
1858 if (Dynamic)
1859 S += ",D";
1860
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001861 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1862 S += ",N";
1863
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001864 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1865 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001866 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001867 }
1868
1869 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1870 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001871 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001872 }
1873
1874 if (SynthesizePID) {
1875 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1876 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001877 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001878 }
1879
1880 // FIXME: OBJCGC: weak & strong
1881}
1882
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001883/// getLegacyIntegralTypeEncoding -
1884/// Another legacy compatibility encoding: 32-bit longs are encoded as
1885/// 'l' or 'L', but not always. For typedefs, we need to use
1886/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1887///
1888void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1889 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1890 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1891 if (BT->getKind() == BuiltinType::ULong)
1892 PointeeTy = UnsignedIntTy;
1893 else if (BT->getKind() == BuiltinType::Long)
1894 PointeeTy = IntTy;
1895 }
1896 }
1897}
1898
Fariborz Jahanian248db262008-01-22 22:44:46 +00001899void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001900 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001901 // We follow the behavior of gcc, expanding structures which are
1902 // directly pointed to, and expanding embedded structures. Note that
1903 // these rules are sufficient to prevent recursive encoding of the
1904 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001905 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1906 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001907}
1908
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001909static void EncodeBitField(const ASTContext *Context, std::string& S,
1910 FieldDecl *FD) {
1911 const Expr *E = FD->getBitWidth();
1912 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1913 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1914 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1915 S += 'b';
1916 S += llvm::utostr(N);
1917}
1918
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001919void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1920 bool ExpandPointedToStructures,
1921 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001922 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001923 bool OutermostType,
1924 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001925 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001926 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001927 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001928 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001929 else {
1930 char encoding;
1931 switch (BT->getKind()) {
1932 default: assert(0 && "Unhandled builtin type kind");
1933 case BuiltinType::Void: encoding = 'v'; break;
1934 case BuiltinType::Bool: encoding = 'B'; break;
1935 case BuiltinType::Char_U:
1936 case BuiltinType::UChar: encoding = 'C'; break;
1937 case BuiltinType::UShort: encoding = 'S'; break;
1938 case BuiltinType::UInt: encoding = 'I'; break;
1939 case BuiltinType::ULong: encoding = 'L'; break;
1940 case BuiltinType::ULongLong: encoding = 'Q'; break;
1941 case BuiltinType::Char_S:
1942 case BuiltinType::SChar: encoding = 'c'; break;
1943 case BuiltinType::Short: encoding = 's'; break;
1944 case BuiltinType::Int: encoding = 'i'; break;
1945 case BuiltinType::Long: encoding = 'l'; break;
1946 case BuiltinType::LongLong: encoding = 'q'; break;
1947 case BuiltinType::Float: encoding = 'f'; break;
1948 case BuiltinType::Double: encoding = 'd'; break;
1949 case BuiltinType::LongDouble: encoding = 'd'; break;
1950 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001951
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001952 S += encoding;
1953 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001954 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001955 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001956 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1957 ExpandPointedToStructures,
1958 ExpandStructures, FD);
1959 if (FD || EncodingProperty) {
1960 // Note that we do extended encoding of protocol qualifer list
1961 // Only when doing ivar or property encoding.
1962 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1963 S += '"';
1964 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1965 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1966 S += '<';
1967 S += Proto->getNameAsString();
1968 S += '>';
1969 }
1970 S += '"';
1971 }
1972 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001973 }
1974 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001975 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001976 bool isReadOnly = false;
1977 // For historical/compatibility reasons, the read-only qualifier of the
1978 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1979 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1980 // Also, do not emit the 'r' for anything but the outermost type!
1981 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1982 if (OutermostType && T.isConstQualified()) {
1983 isReadOnly = true;
1984 S += 'r';
1985 }
1986 }
1987 else if (OutermostType) {
1988 QualType P = PointeeTy;
1989 while (P->getAsPointerType())
1990 P = P->getAsPointerType()->getPointeeType();
1991 if (P.isConstQualified()) {
1992 isReadOnly = true;
1993 S += 'r';
1994 }
1995 }
1996 if (isReadOnly) {
1997 // Another legacy compatibility encoding. Some ObjC qualifier and type
1998 // combinations need to be rearranged.
1999 // Rewrite "in const" from "nr" to "rn"
2000 const char * s = S.c_str();
2001 int len = S.length();
2002 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2003 std::string replace = "rn";
2004 S.replace(S.end()-2, S.end(), replace);
2005 }
2006 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002007 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002008 S += '@';
2009 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002010 }
2011 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002012 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2013 // Another historical/compatibility reason.
2014 // We encode the underlying type which comes out as
2015 // {...};
2016 S += '^';
2017 getObjCEncodingForTypeImpl(PointeeTy, S,
2018 false, ExpandPointedToStructures,
2019 NULL);
2020 return;
2021 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002022 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002023 if (FD || EncodingProperty) {
2024 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2025 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002026 S += '"';
2027 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002028 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2029 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2030 S += '<';
2031 S += Proto->getNameAsString();
2032 S += '>';
2033 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002034 S += '"';
2035 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002036 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002037 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002038 S += '#';
2039 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002040 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002041 S += ':';
2042 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002043 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002044
2045 if (PointeeTy->isCharType()) {
2046 // char pointer types should be encoded as '*' unless it is a
2047 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002048 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002049 S += '*';
2050 return;
2051 }
2052 }
2053
2054 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002055 getLegacyIntegralTypeEncoding(PointeeTy);
2056
2057 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002058 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002059 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002060 } else if (const ArrayType *AT =
2061 // Ignore type qualifiers etc.
2062 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002063 S += '[';
2064
2065 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2066 S += llvm::utostr(CAT->getSize().getZExtValue());
2067 else
2068 assert(0 && "Unhandled array type!");
2069
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002070 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002071 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002072 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002073 } else if (T->getAsFunctionType()) {
2074 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002075 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002076 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002077 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002078 // Anonymous structures print as '?'
2079 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2080 S += II->getName();
2081 } else {
2082 S += '?';
2083 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002084 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002085 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002086 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2087 FieldEnd = RDecl->field_end();
2088 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002089 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002090 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002091 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002092 S += '"';
2093 }
2094
2095 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002096 if (Field->isBitField()) {
2097 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2098 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002099 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002100 QualType qt = Field->getType();
2101 getLegacyIntegralTypeEncoding(qt);
2102 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002103 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002104 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002105 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002106 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002107 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002108 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002109 if (FD && FD->isBitField())
2110 EncodeBitField(this, S, FD);
2111 else
2112 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002113 } else if (T->isBlockPointerType()) {
2114 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002115 } else if (T->isObjCInterfaceType()) {
2116 // @encode(class_name)
2117 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2118 S += '{';
2119 const IdentifierInfo *II = OI->getIdentifier();
2120 S += II->getName();
2121 S += '=';
2122 std::vector<FieldDecl*> RecFields;
2123 CollectObjCIvars(OI, RecFields);
2124 for (unsigned int i = 0; i != RecFields.size(); i++) {
2125 if (RecFields[i]->isBitField())
2126 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2127 RecFields[i]);
2128 else
2129 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2130 FD);
2131 }
2132 S += '}';
2133 }
2134 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002135 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002136}
2137
Ted Kremenek42730c52008-01-07 19:49:32 +00002138void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002139 std::string& S) const {
2140 if (QT & Decl::OBJC_TQ_In)
2141 S += 'n';
2142 if (QT & Decl::OBJC_TQ_Inout)
2143 S += 'N';
2144 if (QT & Decl::OBJC_TQ_Out)
2145 S += 'o';
2146 if (QT & Decl::OBJC_TQ_Bycopy)
2147 S += 'O';
2148 if (QT & Decl::OBJC_TQ_Byref)
2149 S += 'R';
2150 if (QT & Decl::OBJC_TQ_Oneway)
2151 S += 'V';
2152}
2153
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002154void ASTContext::setBuiltinVaListType(QualType T)
2155{
2156 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2157
2158 BuiltinVaListType = T;
2159}
2160
Ted Kremenek42730c52008-01-07 19:49:32 +00002161void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002162{
Ted Kremenek42730c52008-01-07 19:49:32 +00002163 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002164
2165 // typedef struct objc_object *id;
2166 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002167 // User error - caller will issue diagnostics.
2168 if (!ptr)
2169 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002170 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002171 // User error - caller will issue diagnostics.
2172 if (!rec)
2173 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002174 IdStructType = rec;
2175}
2176
Ted Kremenek42730c52008-01-07 19:49:32 +00002177void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002178{
Ted Kremenek42730c52008-01-07 19:49:32 +00002179 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002180
2181 // typedef struct objc_selector *SEL;
2182 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002183 if (!ptr)
2184 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002185 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002186 if (!rec)
2187 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002188 SelStructType = rec;
2189}
2190
Ted Kremenek42730c52008-01-07 19:49:32 +00002191void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002192{
Ted Kremenek42730c52008-01-07 19:49:32 +00002193 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002194}
2195
Ted Kremenek42730c52008-01-07 19:49:32 +00002196void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002197{
Ted Kremenek42730c52008-01-07 19:49:32 +00002198 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002199
2200 // typedef struct objc_class *Class;
2201 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2202 assert(ptr && "'Class' incorrectly typed");
2203 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2204 assert(rec && "'Class' incorrectly typed");
2205 ClassStructType = rec;
2206}
2207
Ted Kremenek42730c52008-01-07 19:49:32 +00002208void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2209 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002210 "'NSConstantString' type already set!");
2211
Ted Kremenek42730c52008-01-07 19:49:32 +00002212 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002213}
2214
Douglas Gregorc6507e42008-11-03 14:12:49 +00002215/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002216/// TargetInfo, produce the corresponding type. The unsigned @p Type
2217/// is actually a value of type @c TargetInfo::IntType.
2218QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002219 switch (Type) {
2220 case TargetInfo::NoInt: return QualType();
2221 case TargetInfo::SignedShort: return ShortTy;
2222 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2223 case TargetInfo::SignedInt: return IntTy;
2224 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2225 case TargetInfo::SignedLong: return LongTy;
2226 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2227 case TargetInfo::SignedLongLong: return LongLongTy;
2228 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2229 }
2230
2231 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002232 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002233}
Ted Kremenek118930e2008-07-24 23:58:27 +00002234
2235//===----------------------------------------------------------------------===//
2236// Type Predicates.
2237//===----------------------------------------------------------------------===//
2238
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002239/// isObjCNSObjectType - Return true if this is an NSObject object using
2240/// NSObject attribute on a c-style pointer type.
2241/// FIXME - Make it work directly on types.
2242///
2243bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2244 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2245 if (TypedefDecl *TD = TDT->getDecl())
2246 if (TD->getAttr<ObjCNSObjectAttr>())
2247 return true;
2248 }
2249 return false;
2250}
2251
Ted Kremenek118930e2008-07-24 23:58:27 +00002252/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2253/// to an object type. This includes "id" and "Class" (two 'special' pointers
2254/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2255/// ID type).
2256bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2257 if (Ty->isObjCQualifiedIdType())
2258 return true;
2259
Steve Naroffd9e00802008-10-21 18:24:04 +00002260 // Blocks are objects.
2261 if (Ty->isBlockPointerType())
2262 return true;
2263
2264 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002265 if (!Ty->isPointerType())
2266 return false;
2267
2268 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2269 // pointer types. This looks for the typedef specifically, not for the
2270 // underlying type.
2271 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2272 return true;
2273
2274 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002275 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2276 return true;
2277
2278 // If is has NSObject attribute, OK as well.
2279 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002280}
2281
Chris Lattner6ff358b2008-04-07 06:51:04 +00002282//===----------------------------------------------------------------------===//
2283// Type Compatibility Testing
2284//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002285
Steve Naroff3454b6c2008-09-04 15:10:53 +00002286/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002287/// block types. Types must be strictly compatible here. For example,
2288/// C unfortunately doesn't produce an error for the following:
2289///
2290/// int (*emptyArgFunc)();
2291/// int (*intArgList)(int) = emptyArgFunc;
2292///
2293/// For blocks, we will produce an error for the following (similar to C++):
2294///
2295/// int (^emptyArgBlock)();
2296/// int (^intArgBlock)(int) = emptyArgBlock;
2297///
2298/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2299///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002300bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002301 const FunctionType *lbase = lhs->getAsFunctionType();
2302 const FunctionType *rbase = rhs->getAsFunctionType();
2303 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2304 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2305 if (lproto && rproto)
2306 return !mergeTypes(lhs, rhs).isNull();
2307 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002308}
2309
Chris Lattner6ff358b2008-04-07 06:51:04 +00002310/// areCompatVectorTypes - Return true if the two specified vector types are
2311/// compatible.
2312static bool areCompatVectorTypes(const VectorType *LHS,
2313 const VectorType *RHS) {
2314 assert(LHS->isCanonical() && RHS->isCanonical());
2315 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002316 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002317}
2318
Eli Friedman0d9549b2008-08-22 00:56:42 +00002319/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002320/// compatible for assignment from RHS to LHS. This handles validation of any
2321/// protocol qualifiers on the LHS or RHS.
2322///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002323bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2324 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002325 // Verify that the base decls are compatible: the RHS must be a subclass of
2326 // the LHS.
2327 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2328 return false;
2329
2330 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2331 // protocol qualified at all, then we are good.
2332 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2333 return true;
2334
2335 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2336 // isn't a superset.
2337 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2338 return true; // FIXME: should return false!
2339
2340 // Finally, we must have two protocol-qualified interfaces.
2341 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2342 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2343 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2344 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2345 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2346 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2347
2348 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2349 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2350 // LHS in a single parallel scan until we run out of elements in LHS.
2351 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2352 ObjCProtocolDecl *LHSProto = *LHSPI;
2353
2354 while (RHSPI != RHSPE) {
2355 ObjCProtocolDecl *RHSProto = *RHSPI++;
2356 // If the RHS has a protocol that the LHS doesn't, ignore it.
2357 if (RHSProto != LHSProto)
2358 continue;
2359
2360 // Otherwise, the RHS does have this element.
2361 ++LHSPI;
2362 if (LHSPI == LHSPE)
2363 return true; // All protocols in LHS exist in RHS.
2364
2365 LHSProto = *LHSPI;
2366 }
2367
2368 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2369 return false;
2370}
2371
Steve Naroff85f0dc52007-10-15 20:41:53 +00002372/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2373/// both shall have the identically qualified version of a compatible type.
2374/// C99 6.2.7p1: Two types have compatible types if their types are the
2375/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002376bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2377 return !mergeTypes(LHS, RHS).isNull();
2378}
2379
2380QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2381 const FunctionType *lbase = lhs->getAsFunctionType();
2382 const FunctionType *rbase = rhs->getAsFunctionType();
2383 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2384 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2385 bool allLTypes = true;
2386 bool allRTypes = true;
2387
2388 // Check return type
2389 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2390 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002391 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2392 allLTypes = false;
2393 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2394 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002395
2396 if (lproto && rproto) { // two C99 style function prototypes
2397 unsigned lproto_nargs = lproto->getNumArgs();
2398 unsigned rproto_nargs = rproto->getNumArgs();
2399
2400 // Compatible functions must have the same number of arguments
2401 if (lproto_nargs != rproto_nargs)
2402 return QualType();
2403
2404 // Variadic and non-variadic functions aren't compatible
2405 if (lproto->isVariadic() != rproto->isVariadic())
2406 return QualType();
2407
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002408 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2409 return QualType();
2410
Eli Friedman0d9549b2008-08-22 00:56:42 +00002411 // Check argument compatibility
2412 llvm::SmallVector<QualType, 10> types;
2413 for (unsigned i = 0; i < lproto_nargs; i++) {
2414 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2415 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2416 QualType argtype = mergeTypes(largtype, rargtype);
2417 if (argtype.isNull()) return QualType();
2418 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002419 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2420 allLTypes = false;
2421 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2422 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002423 }
2424 if (allLTypes) return lhs;
2425 if (allRTypes) return rhs;
2426 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002427 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002428 }
2429
2430 if (lproto) allRTypes = false;
2431 if (rproto) allLTypes = false;
2432
2433 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2434 if (proto) {
2435 if (proto->isVariadic()) return QualType();
2436 // Check that the types are compatible with the types that
2437 // would result from default argument promotions (C99 6.7.5.3p15).
2438 // The only types actually affected are promotable integer
2439 // types and floats, which would be passed as a different
2440 // type depending on whether the prototype is visible.
2441 unsigned proto_nargs = proto->getNumArgs();
2442 for (unsigned i = 0; i < proto_nargs; ++i) {
2443 QualType argTy = proto->getArgType(i);
2444 if (argTy->isPromotableIntegerType() ||
2445 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2446 return QualType();
2447 }
2448
2449 if (allLTypes) return lhs;
2450 if (allRTypes) return rhs;
2451 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002452 proto->getNumArgs(), lproto->isVariadic(),
2453 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002454 }
2455
2456 if (allLTypes) return lhs;
2457 if (allRTypes) return rhs;
2458 return getFunctionTypeNoProto(retType);
2459}
2460
2461QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002462 // C++ [expr]: If an expression initially has the type "reference to T", the
2463 // type is adjusted to "T" prior to any further analysis, the expression
2464 // designates the object or function denoted by the reference, and the
2465 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002466 // FIXME: C++ shouldn't be going through here! The rules are different
2467 // enough that they should be handled separately.
2468 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002469 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002470 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002471 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002472
Eli Friedman0d9549b2008-08-22 00:56:42 +00002473 QualType LHSCan = getCanonicalType(LHS),
2474 RHSCan = getCanonicalType(RHS);
2475
2476 // If two types are identical, they are compatible.
2477 if (LHSCan == RHSCan)
2478 return LHS;
2479
2480 // If the qualifiers are different, the types aren't compatible
2481 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2482 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2483 return QualType();
2484
2485 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2486 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2487
Chris Lattnerc38d4522008-01-14 05:45:46 +00002488 // We want to consider the two function types to be the same for these
2489 // comparisons, just force one to the other.
2490 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2491 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002492
2493 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002494 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2495 LHSClass = Type::ConstantArray;
2496 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2497 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002498
Nate Begemanaf6ed502008-04-18 23:10:10 +00002499 // Canonicalize ExtVector -> Vector.
2500 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2501 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002502
Chris Lattner7cdcb252008-04-07 06:38:24 +00002503 // Consider qualified interfaces and interfaces the same.
2504 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2505 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002506
Chris Lattnerb5709e22008-04-07 05:43:21 +00002507 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002508 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002509 // ID is compatible with all qualified id types.
2510 if (LHS->isObjCQualifiedIdType()) {
2511 if (const PointerType *PT = RHS->getAsPointerType()) {
2512 QualType pType = PT->getPointeeType();
2513 if (isObjCIdType(pType))
2514 return LHS;
2515 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2516 // Unfortunately, this API is part of Sema (which we don't have access
2517 // to. Need to refactor. The following check is insufficient, since we
2518 // need to make sure the class implements the protocol.
2519 if (pType->isObjCInterfaceType())
2520 return LHS;
2521 }
2522 }
2523 if (RHS->isObjCQualifiedIdType()) {
2524 if (const PointerType *PT = LHS->getAsPointerType()) {
2525 QualType pType = PT->getPointeeType();
2526 if (isObjCIdType(pType))
2527 return RHS;
2528 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2529 // Unfortunately, this API is part of Sema (which we don't have access
2530 // to. Need to refactor. The following check is insufficient, since we
2531 // need to make sure the class implements the protocol.
2532 if (pType->isObjCInterfaceType())
2533 return RHS;
2534 }
2535 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002536 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2537 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002538 if (const EnumType* ETy = LHS->getAsEnumType()) {
2539 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2540 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002541 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002542 if (const EnumType* ETy = RHS->getAsEnumType()) {
2543 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2544 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002545 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002546
Eli Friedman0d9549b2008-08-22 00:56:42 +00002547 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002548 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002549
Steve Naroffc88babe2008-01-09 22:43:08 +00002550 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002551 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002552 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002553 {
2554 // Merge two pointer types, while trying to preserve typedef info
2555 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2556 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2557 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2558 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002559 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2560 return LHS;
2561 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2562 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002563 return getPointerType(ResultType);
2564 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002565 case Type::BlockPointer:
2566 {
2567 // Merge two block pointer types, while trying to preserve typedef info
2568 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2569 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2570 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2571 if (ResultType.isNull()) return QualType();
2572 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2573 return LHS;
2574 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2575 return RHS;
2576 return getBlockPointerType(ResultType);
2577 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002578 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002579 {
2580 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2581 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2582 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2583 return QualType();
2584
2585 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2586 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2587 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2588 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002589 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2590 return LHS;
2591 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2592 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002593 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2594 ArrayType::ArraySizeModifier(), 0);
2595 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2596 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002597 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2598 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002599 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2600 return LHS;
2601 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2602 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002603 if (LVAT) {
2604 // FIXME: This isn't correct! But tricky to implement because
2605 // the array's size has to be the size of LHS, but the type
2606 // has to be different.
2607 return LHS;
2608 }
2609 if (RVAT) {
2610 // FIXME: This isn't correct! But tricky to implement because
2611 // the array's size has to be the size of RHS, but the type
2612 // has to be different.
2613 return RHS;
2614 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002615 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2616 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002617 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002618 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002619 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002620 return mergeFunctionTypes(LHS, RHS);
2621 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002622 // FIXME: Why are these compatible?
2623 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2624 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2625 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002626 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002627 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002628 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002629 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002630 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2631 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002632 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002633 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002634 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2635 // for checking assignment/comparison safety
2636 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002637 case Type::ObjCQualifiedId:
2638 // Distinct qualified id's are not compatible.
2639 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002640 default:
2641 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002642 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002643 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002644}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002645
Chris Lattner1d78a862008-04-07 07:01:58 +00002646//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002647// Integer Predicates
2648//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002649
Eli Friedman0832dbc2008-06-28 06:23:08 +00002650unsigned ASTContext::getIntWidth(QualType T) {
2651 if (T == BoolTy)
2652 return 1;
2653 // At the moment, only bool has padding bits
2654 return (unsigned)getTypeSize(T);
2655}
2656
2657QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2658 assert(T->isSignedIntegerType() && "Unexpected type");
2659 if (const EnumType* ETy = T->getAsEnumType())
2660 T = ETy->getDecl()->getIntegerType();
2661 const BuiltinType* BTy = T->getAsBuiltinType();
2662 assert (BTy && "Unexpected signed integer type");
2663 switch (BTy->getKind()) {
2664 case BuiltinType::Char_S:
2665 case BuiltinType::SChar:
2666 return UnsignedCharTy;
2667 case BuiltinType::Short:
2668 return UnsignedShortTy;
2669 case BuiltinType::Int:
2670 return UnsignedIntTy;
2671 case BuiltinType::Long:
2672 return UnsignedLongTy;
2673 case BuiltinType::LongLong:
2674 return UnsignedLongLongTy;
2675 default:
2676 assert(0 && "Unexpected signed integer type");
2677 return QualType();
2678 }
2679}
2680
2681
2682//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002683// Serialization Support
2684//===----------------------------------------------------------------------===//
2685
Ted Kremenek738e6c02007-10-31 17:10:13 +00002686/// Emit - Serialize an ASTContext object to Bitcode.
2687void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002688 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002689 S.EmitRef(SourceMgr);
2690 S.EmitRef(Target);
2691 S.EmitRef(Idents);
2692 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002693
Ted Kremenek68228a92007-10-31 22:44:07 +00002694 // Emit the size of the type vector so that we can reserve that size
2695 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002696 S.EmitInt(Types.size());
2697
Ted Kremenek034a78c2007-11-13 22:02:55 +00002698 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2699 I!=E;++I)
2700 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002701
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002702 S.EmitOwnedPtr(TUDecl);
2703
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002704 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002705}
2706
Ted Kremenekacba3612007-11-13 00:25:37 +00002707ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002708
2709 // Read the language options.
2710 LangOptions LOpts;
2711 LOpts.Read(D);
2712
Ted Kremenek68228a92007-10-31 22:44:07 +00002713 SourceManager &SM = D.ReadRef<SourceManager>();
2714 TargetInfo &t = D.ReadRef<TargetInfo>();
2715 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2716 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002717
Ted Kremenek68228a92007-10-31 22:44:07 +00002718 unsigned size_reserve = D.ReadInt();
2719
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002720 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2721 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002722
Ted Kremenek034a78c2007-11-13 22:02:55 +00002723 for (unsigned i = 0; i < size_reserve; ++i)
2724 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002725
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002726 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2727
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002728 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002729
2730 return A;
2731}