blob: 63e4ccc24157b04e1bdbe974303cc5b79d43a168 [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: {
403 // Note that this is not only platform- but also ABI-dependent. We follow
404 // 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
406 // other compilers too, we need to delegate this completely to TargetInfo.
407 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
408 unsigned AS = Pointee.getAddressSpace();
409 Width = Target.getPointerWidth(AS);
410 if (Pointee->isFunctionType())
411 Width *= 2;
412 Align = Target.getPointerAlign(AS);
413 // GCC aligns at single pointer width.
414 }
Chris Lattner4b009652007-07-25 00:24:17 +0000415 case Type::Complex: {
416 // Complex types have the same alignment as their elements, but twice the
417 // size.
418 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000419 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000420 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000421 Align = EltInfo.second;
422 break;
423 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000424 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000425 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000426 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
427 Width = Layout.getSize();
428 Align = Layout.getAlignment();
429 break;
430 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000431 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000432 const TagType *TT = cast<TagType>(T);
433
434 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000435 Width = 1;
436 Align = 1;
437 break;
438 }
439
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000440 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000441 return getTypeInfo(ET->getDecl()->getIntegerType());
442
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000443 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000444 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
445 Width = Layout.getSize();
446 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000447 break;
448 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000449 }
Chris Lattner4b009652007-07-25 00:24:17 +0000450
451 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000452 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000453}
454
Devang Patelbfe323c2008-06-04 21:22:16 +0000455/// LayoutField - Field layout.
456void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000457 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000458 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000459 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000460 uint64_t FieldOffset = IsUnion ? 0 : Size;
461 uint64_t FieldSize;
462 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000463
464 // FIXME: Should this override struct packing? Probably we want to
465 // take the minimum?
466 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
467 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000468
469 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
470 // TODO: Need to check this algorithm on other targets!
471 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000472 FieldSize =
473 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000474
475 std::pair<uint64_t, unsigned> FieldInfo =
476 Context.getTypeInfo(FD->getType());
477 uint64_t TypeSize = FieldInfo.first;
478
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000479 // Determine the alignment of this bitfield. The packing
480 // attributes define a maximum and the alignment attribute defines
481 // a minimum.
482 // FIXME: What is the right behavior when the specified alignment
483 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000484 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000485 if (FieldPacking)
486 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000487 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
488 FieldAlign = std::max(FieldAlign, AA->getAlignment());
489
490 // Check if we need to add padding to give the field the correct
491 // alignment.
492 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
493 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
494
495 // Padding members don't affect overall alignment
496 if (!FD->getIdentifier())
497 FieldAlign = 1;
498 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000499 if (FD->getType()->isIncompleteArrayType()) {
500 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000501 // query getTypeInfo about these, so we figure it out here.
502 // Flexible array members don't have any size, but they
503 // have to be aligned appropriately for their element type.
504 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000505 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000506 FieldAlign = Context.getTypeAlign(ATy->getElementType());
507 } else {
508 std::pair<uint64_t, unsigned> FieldInfo =
509 Context.getTypeInfo(FD->getType());
510 FieldSize = FieldInfo.first;
511 FieldAlign = FieldInfo.second;
512 }
513
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000514 // Determine the alignment of this bitfield. The packing
515 // attributes define a maximum and the alignment attribute defines
516 // a minimum. Additionally, the packing alignment must be at least
517 // a byte for non-bitfields.
518 //
519 // FIXME: What is the right behavior when the specified alignment
520 // is smaller than the specified packing?
521 if (FieldPacking)
522 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000523 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
524 FieldAlign = std::max(FieldAlign, AA->getAlignment());
525
526 // Round up the current record size to the field's alignment boundary.
527 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
528 }
529
530 // Place this field at the current location.
531 FieldOffsets[FieldNo] = FieldOffset;
532
533 // Reserve space for this field.
534 if (IsUnion) {
535 Size = std::max(Size, FieldSize);
536 } else {
537 Size = FieldOffset + FieldSize;
538 }
539
540 // Remember max struct/class alignment.
541 Alignment = std::max(Alignment, FieldAlign);
542}
543
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000544static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
545 std::vector<FieldDecl*> &Fields) {
546 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
547 if (SuperClass)
548 CollectObjCIvars(SuperClass, Fields);
549 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
550 E = OI->ivar_end(); I != E; ++I) {
551 ObjCIvarDecl *IVDecl = (*I);
552 if (!IVDecl->isInvalidDecl())
553 Fields.push_back(cast<FieldDecl>(IVDecl));
554 }
555}
556
557/// addRecordToClass - produces record info. for the class for its
558/// ivars and all those inherited.
559///
560const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
561{
562 const RecordDecl *&RD = ASTRecordForInterface[D];
563 if (RD)
564 return RD;
565 std::vector<FieldDecl*> RecFields;
566 CollectObjCIvars(D, RecFields);
567 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
568 D->getLocation(),
569 D->getIdentifier());
570 /// FIXME! Can do collection of ivars and adding to the record while
571 /// doing it.
572 for (unsigned int i = 0; i != RecFields.size(); i++) {
573 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
574 RecFields[i]->getLocation(),
575 RecFields[i]->getIdentifier(),
576 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000577 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000578 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000579 }
580 NewRD->completeDefinition(*this);
581 RD = NewRD;
582 return RD;
583}
Devang Patel4b6bf702008-06-04 21:54:36 +0000584
Fariborz Jahanianea944842008-12-18 17:29:46 +0000585/// setFieldDecl - maps a field for the given Ivar reference node.
586//
587void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
588 const ObjCIvarDecl *Ivar,
589 const ObjCIvarRefExpr *MRef) {
590 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
591 lookupFieldDeclForIvar(*this, Ivar);
592 ASTFieldForIvarRef[MRef] = FD;
593}
594
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000595/// getASTObjcInterfaceLayout - Get or compute information about the layout of
596/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000597/// position information.
598const ASTRecordLayout &
599ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
600 // Look up this layout, if already laid out, return what we have.
601 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
602 if (Entry) return *Entry;
603
604 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
605 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000606 ASTRecordLayout *NewEntry = NULL;
607 unsigned FieldCount = D->ivar_size();
608 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
609 FieldCount++;
610 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
611 unsigned Alignment = SL.getAlignment();
612 uint64_t Size = SL.getSize();
613 NewEntry = new ASTRecordLayout(Size, Alignment);
614 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000615 // Super class is at the beginning of the layout.
616 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000617 } else {
618 NewEntry = new ASTRecordLayout();
619 NewEntry->InitializeLayout(FieldCount);
620 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000621 Entry = NewEntry;
622
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000623 unsigned StructPacking = 0;
624 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
625 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000626
627 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
628 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
629 AA->getAlignment()));
630
631 // Layout each ivar sequentially.
632 unsigned i = 0;
633 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
634 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
635 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000636 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000637 }
638
639 // Finally, round the size of the total struct up to the alignment of the
640 // struct itself.
641 NewEntry->FinalizeLayout();
642 return *NewEntry;
643}
644
Devang Patel7a78e432007-11-01 19:11:01 +0000645/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000646/// specified record (struct/union/class), which indicates its size and field
647/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000648const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000649 D = D->getDefinition(*this);
650 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000651
Chris Lattner4b009652007-07-25 00:24:17 +0000652 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000653 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000654 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000655
Devang Patel7a78e432007-11-01 19:11:01 +0000656 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
657 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
658 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000659 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000660
Douglas Gregor39677622008-12-11 20:41:00 +0000661 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000662 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000663 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000664
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000665 unsigned StructPacking = 0;
666 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
667 StructPacking = PA->getAlignment();
668
Eli Friedman5949a022008-05-30 09:31:38 +0000669 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000670 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
671 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000672
Eli Friedman5949a022008-05-30 09:31:38 +0000673 // Layout each field, for now, just sequentially, respecting alignment. In
674 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000675 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000676 for (RecordDecl::field_iterator Field = D->field_begin(),
677 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000678 Field != FieldEnd; (void)++Field, ++FieldIdx)
679 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000680
681 // Finally, round the size of the total struct up to the alignment of the
682 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000683 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000684 return *NewEntry;
685}
686
Chris Lattner4b009652007-07-25 00:24:17 +0000687//===----------------------------------------------------------------------===//
688// Type creation/memoization methods
689//===----------------------------------------------------------------------===//
690
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000691QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000692 QualType CanT = getCanonicalType(T);
693 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000694 return T;
695
696 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
697 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000698 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000699 "Type is already address space qualified");
700
701 // Check if we've already instantiated an address space qual'd type of this
702 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000703 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000704 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000705 void *InsertPos = 0;
706 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
707 return QualType(ASQy, 0);
708
709 // If the base type isn't canonical, this won't be a canonical type either,
710 // so fill in the canonical type field.
711 QualType Canonical;
712 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000713 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000714
715 // Get the new insert position for the node we care about.
716 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000717 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000718 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000719 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000720 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000721 ASQualTypes.InsertNode(New, InsertPos);
722 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000723 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000724}
725
Chris Lattner4b009652007-07-25 00:24:17 +0000726
727/// getComplexType - Return the uniqued reference to the type for a complex
728/// number with the specified element type.
729QualType ASTContext::getComplexType(QualType T) {
730 // Unique pointers, to guarantee there is only one pointer of a particular
731 // structure.
732 llvm::FoldingSetNodeID ID;
733 ComplexType::Profile(ID, T);
734
735 void *InsertPos = 0;
736 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
737 return QualType(CT, 0);
738
739 // If the pointee type isn't canonical, this won't be a canonical type either,
740 // so fill in the canonical type field.
741 QualType Canonical;
742 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000743 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000744
745 // Get the new insert position for the node we care about.
746 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000747 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000748 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000749 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000750 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000751 Types.push_back(New);
752 ComplexTypes.InsertNode(New, InsertPos);
753 return QualType(New, 0);
754}
755
756
757/// getPointerType - Return the uniqued reference to the type for a pointer to
758/// the specified type.
759QualType ASTContext::getPointerType(QualType T) {
760 // Unique pointers, to guarantee there is only one pointer of a particular
761 // structure.
762 llvm::FoldingSetNodeID ID;
763 PointerType::Profile(ID, T);
764
765 void *InsertPos = 0;
766 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
767 return QualType(PT, 0);
768
769 // If the pointee type isn't canonical, this won't be a canonical type either,
770 // so fill in the canonical type field.
771 QualType Canonical;
772 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000773 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000774
775 // Get the new insert position for the node we care about.
776 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000777 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000778 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000779 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000780 PointerType *New = new (Mem) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000781 Types.push_back(New);
782 PointerTypes.InsertNode(New, InsertPos);
783 return QualType(New, 0);
784}
785
Steve Naroff7aa54752008-08-27 16:04:49 +0000786/// getBlockPointerType - Return the uniqued reference to the type for
787/// a pointer to the specified block.
788QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000789 assert(T->isFunctionType() && "block of function types only");
790 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000791 // structure.
792 llvm::FoldingSetNodeID ID;
793 BlockPointerType::Profile(ID, T);
794
795 void *InsertPos = 0;
796 if (BlockPointerType *PT =
797 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
798 return QualType(PT, 0);
799
Steve Narofffd5b19d2008-08-28 19:20:44 +0000800 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000801 // type either so fill in the canonical type field.
802 QualType Canonical;
803 if (!T->isCanonical()) {
804 Canonical = getBlockPointerType(getCanonicalType(T));
805
806 // Get the new insert position for the node we care about.
807 BlockPointerType *NewIP =
808 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000809 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000810 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000811 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000812 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000813 Types.push_back(New);
814 BlockPointerTypes.InsertNode(New, InsertPos);
815 return QualType(New, 0);
816}
817
Chris Lattner4b009652007-07-25 00:24:17 +0000818/// getReferenceType - Return the uniqued reference to the type for a reference
819/// to the specified type.
820QualType ASTContext::getReferenceType(QualType T) {
821 // Unique pointers, to guarantee there is only one pointer of a particular
822 // structure.
823 llvm::FoldingSetNodeID ID;
824 ReferenceType::Profile(ID, T);
825
826 void *InsertPos = 0;
827 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
828 return QualType(RT, 0);
829
830 // If the referencee type isn't canonical, this won't be a canonical type
831 // either, so fill in the canonical type field.
832 QualType Canonical;
833 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000834 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000835
836 // Get the new insert position for the node we care about.
837 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000838 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000839 }
840
Steve Naroffbd9375a2009-01-19 22:45:10 +0000841 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000842 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000843 Types.push_back(New);
844 ReferenceTypes.InsertNode(New, InsertPos);
845 return QualType(New, 0);
846}
847
Sebastian Redl75555032009-01-24 21:16:55 +0000848/// getMemberPointerType - Return the uniqued reference to the type for a
849/// member pointer to the specified type, in the specified class.
850QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
851{
852 // Unique pointers, to guarantee there is only one pointer of a particular
853 // structure.
854 llvm::FoldingSetNodeID ID;
855 MemberPointerType::Profile(ID, T, Cls);
856
857 void *InsertPos = 0;
858 if (MemberPointerType *PT =
859 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
860 return QualType(PT, 0);
861
862 // If the pointee or class type isn't canonical, this won't be a canonical
863 // type either, so fill in the canonical type field.
864 QualType Canonical;
865 if (!T->isCanonical()) {
866 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
867
868 // Get the new insert position for the node we care about.
869 MemberPointerType *NewIP =
870 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
871 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
872 }
873 void *Mem = Allocator.Allocate(sizeof(MemberPointerType), 8);
874 MemberPointerType *New = new (Mem) MemberPointerType(T, Cls, Canonical);
875 Types.push_back(New);
876 MemberPointerTypes.InsertNode(New, InsertPos);
877 return QualType(New, 0);
878}
879
Steve Naroff83c13012007-08-30 01:06:46 +0000880/// getConstantArrayType - Return the unique reference to the type for an
881/// array of the specified element type.
882QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000883 const llvm::APInt &ArySize,
884 ArrayType::ArraySizeModifier ASM,
885 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000886 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000887 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000888
889 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000890 if (ConstantArrayType *ATP =
891 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000892 return QualType(ATP, 0);
893
894 // If the element type isn't canonical, this won't be a canonical type either,
895 // so fill in the canonical type field.
896 QualType Canonical;
897 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000898 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000899 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000900 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000901 ConstantArrayType *NewIP =
902 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000903 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000904 }
905
Steve Naroffbd9375a2009-01-19 22:45:10 +0000906 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000907 ConstantArrayType *New =
908 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000909 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000910 Types.push_back(New);
911 return QualType(New, 0);
912}
913
Steve Naroffe2579e32007-08-30 18:14:25 +0000914/// getVariableArrayType - Returns a non-unique reference to the type for a
915/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000916QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
917 ArrayType::ArraySizeModifier ASM,
918 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000919 // Since we don't unique expressions, it isn't possible to unique VLA's
920 // that have an expression provided for their size.
921
Steve Naroffbd9375a2009-01-19 22:45:10 +0000922 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000923 VariableArrayType *New =
924 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000925
926 VariableArrayTypes.push_back(New);
927 Types.push_back(New);
928 return QualType(New, 0);
929}
930
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000931/// getDependentSizedArrayType - Returns a non-unique reference to
932/// the type for a dependently-sized array of the specified element
933/// type. FIXME: We will need these to be uniqued, or at least
934/// comparable, at some point.
935QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
936 ArrayType::ArraySizeModifier ASM,
937 unsigned EltTypeQuals) {
938 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
939 "Size must be type- or value-dependent!");
940
941 // Since we don't unique expressions, it isn't possible to unique
942 // dependently-sized array types.
943
Steve Naroffbd9375a2009-01-19 22:45:10 +0000944 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000945 DependentSizedArrayType *New =
946 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
947 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000948
949 DependentSizedArrayTypes.push_back(New);
950 Types.push_back(New);
951 return QualType(New, 0);
952}
953
Eli Friedman8ff07782008-02-15 18:16:39 +0000954QualType ASTContext::getIncompleteArrayType(QualType EltTy,
955 ArrayType::ArraySizeModifier ASM,
956 unsigned EltTypeQuals) {
957 llvm::FoldingSetNodeID ID;
958 IncompleteArrayType::Profile(ID, EltTy);
959
960 void *InsertPos = 0;
961 if (IncompleteArrayType *ATP =
962 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
963 return QualType(ATP, 0);
964
965 // If the element type isn't canonical, this won't be a canonical type
966 // either, so fill in the canonical type field.
967 QualType Canonical;
968
969 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000970 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000971 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000972
973 // Get the new insert position for the node we care about.
974 IncompleteArrayType *NewIP =
975 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000976 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000977 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000978
Steve Naroffbd9375a2009-01-19 22:45:10 +0000979 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000980 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
981 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000982
983 IncompleteArrayTypes.InsertNode(New, InsertPos);
984 Types.push_back(New);
985 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000986}
987
Chris Lattner4b009652007-07-25 00:24:17 +0000988/// getVectorType - Return the unique reference to a vector type of
989/// the specified element type and size. VectorType must be a built-in type.
990QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
991 BuiltinType *baseType;
992
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000993 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000994 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
995
996 // Check if we've already instantiated a vector of this type.
997 llvm::FoldingSetNodeID ID;
998 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
999 void *InsertPos = 0;
1000 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1001 return QualType(VTP, 0);
1002
1003 // If the element type isn't canonical, this won't be a canonical type either,
1004 // so fill in the canonical type field.
1005 QualType Canonical;
1006 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001007 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001008
1009 // Get the new insert position for the node we care about.
1010 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001011 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001012 }
Steve Naroffbd9375a2009-01-19 22:45:10 +00001013 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001014 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001015 VectorTypes.InsertNode(New, InsertPos);
1016 Types.push_back(New);
1017 return QualType(New, 0);
1018}
1019
Nate Begemanaf6ed502008-04-18 23:10:10 +00001020/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001021/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001022QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001023 BuiltinType *baseType;
1024
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001025 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001026 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001027
1028 // Check if we've already instantiated a vector of this type.
1029 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001030 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001031 void *InsertPos = 0;
1032 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1033 return QualType(VTP, 0);
1034
1035 // If the element type isn't canonical, this won't be a canonical type either,
1036 // so fill in the canonical type field.
1037 QualType Canonical;
1038 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001039 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001040
1041 // Get the new insert position for the node we care about.
1042 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001043 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001044 }
Steve Naroffbd9375a2009-01-19 22:45:10 +00001045 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001046 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001047 VectorTypes.InsertNode(New, InsertPos);
1048 Types.push_back(New);
1049 return QualType(New, 0);
1050}
1051
1052/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1053///
1054QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1055 // Unique functions, to guarantee there is only one function of a particular
1056 // structure.
1057 llvm::FoldingSetNodeID ID;
1058 FunctionTypeNoProto::Profile(ID, ResultTy);
1059
1060 void *InsertPos = 0;
1061 if (FunctionTypeNoProto *FT =
1062 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1063 return QualType(FT, 0);
1064
1065 QualType Canonical;
1066 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001067 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001068
1069 // Get the new insert position for the node we care about.
1070 FunctionTypeNoProto *NewIP =
1071 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001072 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001073 }
1074
Douglas Gregor5c561212009-01-20 21:02:13 +00001075 void *Mem = Allocator.Allocate(sizeof(FunctionTypeNoProto), 8);
1076 FunctionTypeNoProto *New = new (Mem) FunctionTypeNoProto(ResultTy, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001077 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001078 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001079 return QualType(New, 0);
1080}
1081
1082/// getFunctionType - Return a normal function type with a typed argument
1083/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001084QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001085 unsigned NumArgs, bool isVariadic,
1086 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001087 // Unique functions, to guarantee there is only one function of a particular
1088 // structure.
1089 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001090 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1091 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001092
1093 void *InsertPos = 0;
1094 if (FunctionTypeProto *FTP =
1095 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1096 return QualType(FTP, 0);
1097
1098 // Determine whether the type being created is already canonical or not.
1099 bool isCanonical = ResultTy->isCanonical();
1100 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1101 if (!ArgArray[i]->isCanonical())
1102 isCanonical = false;
1103
1104 // If this type isn't canonical, get the canonical version of it.
1105 QualType Canonical;
1106 if (!isCanonical) {
1107 llvm::SmallVector<QualType, 16> CanonicalArgs;
1108 CanonicalArgs.reserve(NumArgs);
1109 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001110 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001111
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001112 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001113 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001114 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001115
1116 // Get the new insert position for the node we care about.
1117 FunctionTypeProto *NewIP =
1118 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001119 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001120 }
1121
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001122 // FunctionTypeProto objects are allocated with extra bytes after them
1123 // for a variable size array (for parameter types) at the end of them.
1124 // FIXME: Can we do better than forcing a 16-byte alignment?
Chris Lattner4b009652007-07-25 00:24:17 +00001125 FunctionTypeProto *FTP =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001126 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1127 NumArgs*sizeof(QualType), 16);
Chris Lattner4b009652007-07-25 00:24:17 +00001128 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001129 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001130 Types.push_back(FTP);
1131 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1132 return QualType(FTP, 0);
1133}
1134
Douglas Gregor1d661552008-04-13 21:07:44 +00001135/// getTypeDeclType - Return the unique reference to the type for the
1136/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001137QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001138 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001139 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1140
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001141 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001142 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +00001143 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1144 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001145 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001146 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001147
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001148 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001149 if (PrevDecl)
1150 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1151 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001152 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001153 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1154 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001155 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001156 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001157 if (PrevDecl)
1158 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1159 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001160 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001161 Decl->TypeForDecl = new (Mem) RecordType(Record);
1162 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001163 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001164 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1165 if (PrevDecl)
1166 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1167 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001168 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001169 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1170 }
1171 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001172 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001173 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001174
Ted Kremenek46a837c2008-09-05 17:16:31 +00001175 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001176 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001177}
1178
Chris Lattner4b009652007-07-25 00:24:17 +00001179/// getTypedefType - Return the unique reference to the type for the
1180/// specified typename decl.
1181QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1182 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1183
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001184 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroffbd9375a2009-01-19 22:45:10 +00001185 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001186 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001187 Types.push_back(Decl->TypeForDecl);
1188 return QualType(Decl->TypeForDecl, 0);
1189}
1190
Douglas Gregordd861062008-12-05 18:15:24 +00001191/// getTemplateTypeParmType - Return the unique reference to the type
1192/// for the specified template type parameter declaration.
1193QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1194 if (!Decl->TypeForDecl) {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001195 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001196 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregordd861062008-12-05 18:15:24 +00001197 Types.push_back(Decl->TypeForDecl);
1198 }
1199 return QualType(Decl->TypeForDecl, 0);
1200}
1201
Ted Kremenek42730c52008-01-07 19:49:32 +00001202/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001203/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001204QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001205 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1206
Steve Naroffbd9375a2009-01-19 22:45:10 +00001207 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001208 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001209 Types.push_back(Decl->TypeForDecl);
1210 return QualType(Decl->TypeForDecl, 0);
1211}
1212
Chris Lattnere1352302008-04-07 04:56:42 +00001213/// CmpProtocolNames - Comparison predicate for sorting protocols
1214/// alphabetically.
1215static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1216 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001217 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001218}
1219
1220static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1221 unsigned &NumProtocols) {
1222 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1223
1224 // Sort protocols, keyed by name.
1225 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1226
1227 // Remove duplicates.
1228 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1229 NumProtocols = ProtocolsEnd-Protocols;
1230}
1231
1232
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001233/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1234/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001235QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1236 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001237 // Sort the protocol list alphabetically to canonicalize it.
1238 SortAndUniqueProtocols(Protocols, NumProtocols);
1239
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001240 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001241 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001242
1243 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001244 if (ObjCQualifiedInterfaceType *QT =
1245 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001246 return QualType(QT, 0);
1247
1248 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001249 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremenek42730c52008-01-07 19:49:32 +00001250 ObjCQualifiedInterfaceType *QType =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001251 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1252
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001253 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001254 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001255 return QualType(QType, 0);
1256}
1257
Chris Lattnere1352302008-04-07 04:56:42 +00001258/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1259/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001260QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001261 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001262 // Sort the protocol list alphabetically to canonicalize it.
1263 SortAndUniqueProtocols(Protocols, NumProtocols);
1264
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001265 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001266 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001267
1268 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001269 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001270 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001271 return QualType(QT, 0);
1272
1273 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001274 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001275 ObjCQualifiedIdType *QType =
1276 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001277 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001278 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001279 return QualType(QType, 0);
1280}
1281
Steve Naroff0604dd92007-08-01 18:02:17 +00001282/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1283/// TypeOfExpr AST's (since expression's are never shared). For example,
1284/// multiple declarations that refer to "typeof(x)" all contain different
1285/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1286/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001287QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001288 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor5c561212009-01-20 21:02:13 +00001289 void *Mem = Allocator.Allocate(sizeof(TypeOfExpr), 8);
1290 TypeOfExpr *toe = new (Mem) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001291 Types.push_back(toe);
1292 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001293}
1294
Steve Naroff0604dd92007-08-01 18:02:17 +00001295/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1296/// TypeOfType AST's. The only motivation to unique these nodes would be
1297/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1298/// an issue. This doesn't effect the type checker, since it operates
1299/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001300QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001301 QualType Canonical = getCanonicalType(tofType);
Steve Naroffbd9375a2009-01-19 22:45:10 +00001302 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001303 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001304 Types.push_back(tot);
1305 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001306}
1307
Chris Lattner4b009652007-07-25 00:24:17 +00001308/// getTagDeclType - Return the unique reference to the type for the
1309/// specified TagDecl (struct/union/class/enum) decl.
1310QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001311 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001312 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001313}
1314
1315/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1316/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1317/// needs to agree with the definition in <stddef.h>.
1318QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001319 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001320}
1321
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001322/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001323/// width of characters in wide strings, The value is target dependent and
1324/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001325QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001326 if (LangOpts.CPlusPlus)
1327 return WCharTy;
1328
Douglas Gregorc6507e42008-11-03 14:12:49 +00001329 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1330 // wide-character type?
1331 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001332}
1333
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001334/// getSignedWCharType - Return the type of "signed wchar_t".
1335/// Used when in C++, as a GCC extension.
1336QualType ASTContext::getSignedWCharType() const {
1337 // FIXME: derive from "Target" ?
1338 return WCharTy;
1339}
1340
1341/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1342/// Used when in C++, as a GCC extension.
1343QualType ASTContext::getUnsignedWCharType() const {
1344 // FIXME: derive from "Target" ?
1345 return UnsignedIntTy;
1346}
1347
Chris Lattner4b009652007-07-25 00:24:17 +00001348/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1349/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1350QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001351 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001352}
1353
Chris Lattner19eb97e2008-04-02 05:18:44 +00001354//===----------------------------------------------------------------------===//
1355// Type Operators
1356//===----------------------------------------------------------------------===//
1357
Chris Lattner3dae6f42008-04-06 22:41:35 +00001358/// getCanonicalType - Return the canonical (structural) type corresponding to
1359/// the specified potentially non-canonical type. The non-canonical version
1360/// of a type may have many "decorated" versions of types. Decorators can
1361/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1362/// to be free of any of these, allowing two canonical types to be compared
1363/// for exact equality with a simple pointer comparison.
1364QualType ASTContext::getCanonicalType(QualType T) {
1365 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001366
1367 // If the result has type qualifiers, make sure to canonicalize them as well.
1368 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1369 if (TypeQuals == 0) return CanType;
1370
1371 // If the type qualifiers are on an array type, get the canonical type of the
1372 // array with the qualifiers applied to the element type.
1373 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1374 if (!AT)
1375 return CanType.getQualifiedType(TypeQuals);
1376
1377 // Get the canonical version of the element with the extra qualifiers on it.
1378 // This can recursively sink qualifiers through multiple levels of arrays.
1379 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1380 NewEltTy = getCanonicalType(NewEltTy);
1381
1382 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1383 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1384 CAT->getIndexTypeQualifier());
1385 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1386 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1387 IAT->getIndexTypeQualifier());
1388
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001389 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1390 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1391 DSAT->getSizeModifier(),
1392 DSAT->getIndexTypeQualifier());
1393
Chris Lattnera1923f62008-08-04 07:31:14 +00001394 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1395 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1396 VAT->getSizeModifier(),
1397 VAT->getIndexTypeQualifier());
1398}
1399
1400
1401const ArrayType *ASTContext::getAsArrayType(QualType T) {
1402 // Handle the non-qualified case efficiently.
1403 if (T.getCVRQualifiers() == 0) {
1404 // Handle the common positive case fast.
1405 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1406 return AT;
1407 }
1408
1409 // Handle the common negative case fast, ignoring CVR qualifiers.
1410 QualType CType = T->getCanonicalTypeInternal();
1411
1412 // Make sure to look through type qualifiers (like ASQuals) for the negative
1413 // test.
1414 if (!isa<ArrayType>(CType) &&
1415 !isa<ArrayType>(CType.getUnqualifiedType()))
1416 return 0;
1417
1418 // Apply any CVR qualifiers from the array type to the element type. This
1419 // implements C99 6.7.3p8: "If the specification of an array type includes
1420 // any type qualifiers, the element type is so qualified, not the array type."
1421
1422 // If we get here, we either have type qualifiers on the type, or we have
1423 // sugar such as a typedef in the way. If we have type qualifiers on the type
1424 // we must propagate them down into the elemeng type.
1425 unsigned CVRQuals = T.getCVRQualifiers();
1426 unsigned AddrSpace = 0;
1427 Type *Ty = T.getTypePtr();
1428
1429 // Rip through ASQualType's and typedefs to get to a concrete type.
1430 while (1) {
1431 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1432 AddrSpace = ASQT->getAddressSpace();
1433 Ty = ASQT->getBaseType();
1434 } else {
1435 T = Ty->getDesugaredType();
1436 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1437 break;
1438 CVRQuals |= T.getCVRQualifiers();
1439 Ty = T.getTypePtr();
1440 }
1441 }
1442
1443 // If we have a simple case, just return now.
1444 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1445 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1446 return ATy;
1447
1448 // Otherwise, we have an array and we have qualifiers on it. Push the
1449 // qualifiers into the array element type and return a new array type.
1450 // Get the canonical version of the element with the extra qualifiers on it.
1451 // This can recursively sink qualifiers through multiple levels of arrays.
1452 QualType NewEltTy = ATy->getElementType();
1453 if (AddrSpace)
1454 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1455 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1456
1457 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1458 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1459 CAT->getSizeModifier(),
1460 CAT->getIndexTypeQualifier()));
1461 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1462 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1463 IAT->getSizeModifier(),
1464 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001465
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001466 if (const DependentSizedArrayType *DSAT
1467 = dyn_cast<DependentSizedArrayType>(ATy))
1468 return cast<ArrayType>(
1469 getDependentSizedArrayType(NewEltTy,
1470 DSAT->getSizeExpr(),
1471 DSAT->getSizeModifier(),
1472 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001473
Chris Lattnera1923f62008-08-04 07:31:14 +00001474 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1475 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1476 VAT->getSizeModifier(),
1477 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001478}
1479
1480
Chris Lattner19eb97e2008-04-02 05:18:44 +00001481/// getArrayDecayedType - Return the properly qualified result of decaying the
1482/// specified array type to a pointer. This operation is non-trivial when
1483/// handling typedefs etc. The canonical type of "T" must be an array type,
1484/// this returns a pointer to a properly qualified element of the array.
1485///
1486/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1487QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001488 // Get the element type with 'getAsArrayType' so that we don't lose any
1489 // typedefs in the element type of the array. This also handles propagation
1490 // of type qualifiers from the array type into the element type if present
1491 // (C99 6.7.3p8).
1492 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1493 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001494
Chris Lattnera1923f62008-08-04 07:31:14 +00001495 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001496
1497 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001498 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001499}
1500
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001501QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001502 QualType ElemTy = VAT->getElementType();
1503
1504 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1505 return getBaseElementType(VAT);
1506
1507 return ElemTy;
1508}
1509
Chris Lattner4b009652007-07-25 00:24:17 +00001510/// getFloatingRank - Return a relative rank for floating point types.
1511/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001512static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001513 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001514 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001515
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001516 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001517 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001518 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001519 case BuiltinType::Float: return FloatRank;
1520 case BuiltinType::Double: return DoubleRank;
1521 case BuiltinType::LongDouble: return LongDoubleRank;
1522 }
1523}
1524
Steve Narofffa0c4532007-08-27 01:41:48 +00001525/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1526/// point or a complex type (based on typeDomain/typeSize).
1527/// 'typeDomain' is a real floating point or complex type.
1528/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001529QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1530 QualType Domain) const {
1531 FloatingRank EltRank = getFloatingRank(Size);
1532 if (Domain->isComplexType()) {
1533 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001534 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001535 case FloatRank: return FloatComplexTy;
1536 case DoubleRank: return DoubleComplexTy;
1537 case LongDoubleRank: return LongDoubleComplexTy;
1538 }
Chris Lattner4b009652007-07-25 00:24:17 +00001539 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001540
1541 assert(Domain->isRealFloatingType() && "Unknown domain!");
1542 switch (EltRank) {
1543 default: assert(0 && "getFloatingRank(): illegal value for rank");
1544 case FloatRank: return FloatTy;
1545 case DoubleRank: return DoubleTy;
1546 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001547 }
Chris Lattner4b009652007-07-25 00:24:17 +00001548}
1549
Chris Lattner51285d82008-04-06 23:55:33 +00001550/// getFloatingTypeOrder - Compare the rank of the two specified floating
1551/// point types, ignoring the domain of the type (i.e. 'double' ==
1552/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1553/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001554int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1555 FloatingRank LHSR = getFloatingRank(LHS);
1556 FloatingRank RHSR = getFloatingRank(RHS);
1557
1558 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001559 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001560 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001561 return 1;
1562 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001563}
1564
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001565/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1566/// routine will assert if passed a built-in type that isn't an integer or enum,
1567/// or if it is not canonicalized.
1568static unsigned getIntegerRank(Type *T) {
1569 assert(T->isCanonical() && "T should be canonicalized");
1570 if (isa<EnumType>(T))
1571 return 4;
1572
1573 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001574 default: assert(0 && "getIntegerRank(): not a built-in integer");
1575 case BuiltinType::Bool:
1576 return 1;
1577 case BuiltinType::Char_S:
1578 case BuiltinType::Char_U:
1579 case BuiltinType::SChar:
1580 case BuiltinType::UChar:
1581 return 2;
1582 case BuiltinType::Short:
1583 case BuiltinType::UShort:
1584 return 3;
1585 case BuiltinType::Int:
1586 case BuiltinType::UInt:
1587 return 4;
1588 case BuiltinType::Long:
1589 case BuiltinType::ULong:
1590 return 5;
1591 case BuiltinType::LongLong:
1592 case BuiltinType::ULongLong:
1593 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001594 }
1595}
1596
Chris Lattner51285d82008-04-06 23:55:33 +00001597/// getIntegerTypeOrder - Returns the highest ranked integer type:
1598/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1599/// LHS < RHS, return -1.
1600int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001601 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1602 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001603 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001604
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001605 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1606 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001607
Chris Lattner51285d82008-04-06 23:55:33 +00001608 unsigned LHSRank = getIntegerRank(LHSC);
1609 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001610
Chris Lattner51285d82008-04-06 23:55:33 +00001611 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1612 if (LHSRank == RHSRank) return 0;
1613 return LHSRank > RHSRank ? 1 : -1;
1614 }
Chris Lattner4b009652007-07-25 00:24:17 +00001615
Chris Lattner51285d82008-04-06 23:55:33 +00001616 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1617 if (LHSUnsigned) {
1618 // If the unsigned [LHS] type is larger, return it.
1619 if (LHSRank >= RHSRank)
1620 return 1;
1621
1622 // If the signed type can represent all values of the unsigned type, it
1623 // wins. Because we are dealing with 2's complement and types that are
1624 // powers of two larger than each other, this is always safe.
1625 return -1;
1626 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001627
Chris Lattner51285d82008-04-06 23:55:33 +00001628 // If the unsigned [RHS] type is larger, return it.
1629 if (RHSRank >= LHSRank)
1630 return -1;
1631
1632 // If the signed type can represent all values of the unsigned type, it
1633 // wins. Because we are dealing with 2's complement and types that are
1634 // powers of two larger than each other, this is always safe.
1635 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001636}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001637
1638// getCFConstantStringType - Return the type used for constant CFStrings.
1639QualType ASTContext::getCFConstantStringType() {
1640 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001641 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001642 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001643 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001644 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001645
1646 // const int *isa;
1647 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001648 // int flags;
1649 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001650 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001651 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001652 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001653 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001654
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001655 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001656 for (unsigned i = 0; i < 4; ++i) {
1657 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1658 SourceLocation(), 0,
1659 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001660 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001661 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001662 }
1663
1664 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001665 }
1666
1667 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001668}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001669
Anders Carlssonf58cac72008-08-30 19:34:46 +00001670QualType ASTContext::getObjCFastEnumerationStateType()
1671{
1672 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001673 ObjCFastEnumerationStateTypeDecl =
1674 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1675 &Idents.get("__objcFastEnumerationState"));
1676
Anders Carlssonf58cac72008-08-30 19:34:46 +00001677 QualType FieldTypes[] = {
1678 UnsignedLongTy,
1679 getPointerType(ObjCIdType),
1680 getPointerType(UnsignedLongTy),
1681 getConstantArrayType(UnsignedLongTy,
1682 llvm::APInt(32, 5), ArrayType::Normal, 0)
1683 };
1684
Douglas Gregor8acb7272008-12-11 16:49:14 +00001685 for (size_t i = 0; i < 4; ++i) {
1686 FieldDecl *Field = FieldDecl::Create(*this,
1687 ObjCFastEnumerationStateTypeDecl,
1688 SourceLocation(), 0,
1689 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001690 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001691 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001692 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001693
Douglas Gregor8acb7272008-12-11 16:49:14 +00001694 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001695 }
1696
1697 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1698}
1699
Anders Carlssone3f02572007-10-29 06:33:42 +00001700// This returns true if a type has been typedefed to BOOL:
1701// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001702static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001703 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001704 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1705 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001706
1707 return false;
1708}
1709
Ted Kremenek42730c52008-01-07 19:49:32 +00001710/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001711/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001712int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001713 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001714
1715 // Make all integer and enum types at least as large as an int
1716 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001717 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001718 // Treat arrays as pointers, since that's how they're passed in.
1719 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001720 sz = getTypeSize(VoidPtrTy);
1721 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001722}
1723
Ted Kremenek42730c52008-01-07 19:49:32 +00001724/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001725/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001726void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001727 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001728 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001729 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001730 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001731 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001732 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001733 // Compute size of all parameters.
1734 // Start with computing size of a pointer in number of bytes.
1735 // FIXME: There might(should) be a better way of doing this computation!
1736 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001737 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001738 // The first two arguments (self and _cmd) are pointers; account for
1739 // their size.
1740 int ParmOffset = 2 * PtrSize;
1741 int NumOfParams = Decl->getNumParams();
1742 for (int i = 0; i < NumOfParams; i++) {
1743 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001744 int sz = getObjCEncodingTypeSize (PType);
1745 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001746 ParmOffset += sz;
1747 }
1748 S += llvm::utostr(ParmOffset);
1749 S += "@0:";
1750 S += llvm::utostr(PtrSize);
1751
1752 // Argument types.
1753 ParmOffset = 2 * PtrSize;
1754 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001755 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1756 QualType PType = PVDecl->getOriginalType();
1757 if (const ArrayType *AT =
1758 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1759 // Use array's original type only if it has known number of
1760 // elements.
1761 if (!dyn_cast<ConstantArrayType>(AT))
1762 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001763 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001764 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001765 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001766 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001767 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001768 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001769 }
1770}
1771
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001772/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001773/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001774/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1775/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001776/// Property attributes are stored as a comma-delimited C string. The simple
1777/// attributes readonly and bycopy are encoded as single characters. The
1778/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1779/// encoded as single characters, followed by an identifier. Property types
1780/// are also encoded as a parametrized attribute. The characters used to encode
1781/// these attributes are defined by the following enumeration:
1782/// @code
1783/// enum PropertyAttributes {
1784/// kPropertyReadOnly = 'R', // property is read-only.
1785/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1786/// kPropertyByref = '&', // property is a reference to the value last assigned
1787/// kPropertyDynamic = 'D', // property is dynamic
1788/// kPropertyGetter = 'G', // followed by getter selector name
1789/// kPropertySetter = 'S', // followed by setter selector name
1790/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1791/// kPropertyType = 't' // followed by old-style type encoding.
1792/// kPropertyWeak = 'W' // 'weak' property
1793/// kPropertyStrong = 'P' // property GC'able
1794/// kPropertyNonAtomic = 'N' // property non-atomic
1795/// };
1796/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001797void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1798 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001799 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001800 // Collect information from the property implementation decl(s).
1801 bool Dynamic = false;
1802 ObjCPropertyImplDecl *SynthesizePID = 0;
1803
1804 // FIXME: Duplicated code due to poor abstraction.
1805 if (Container) {
1806 if (const ObjCCategoryImplDecl *CID =
1807 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1808 for (ObjCCategoryImplDecl::propimpl_iterator
1809 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1810 ObjCPropertyImplDecl *PID = *i;
1811 if (PID->getPropertyDecl() == PD) {
1812 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1813 Dynamic = true;
1814 } else {
1815 SynthesizePID = PID;
1816 }
1817 }
1818 }
1819 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001820 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001821 for (ObjCCategoryImplDecl::propimpl_iterator
1822 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1823 ObjCPropertyImplDecl *PID = *i;
1824 if (PID->getPropertyDecl() == PD) {
1825 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1826 Dynamic = true;
1827 } else {
1828 SynthesizePID = PID;
1829 }
1830 }
1831 }
1832 }
1833 }
1834
1835 // FIXME: This is not very efficient.
1836 S = "T";
1837
1838 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001839 // GCC has some special rules regarding encoding of properties which
1840 // closely resembles encoding of ivars.
1841 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1842 true /* outermost type */,
1843 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001844
1845 if (PD->isReadOnly()) {
1846 S += ",R";
1847 } else {
1848 switch (PD->getSetterKind()) {
1849 case ObjCPropertyDecl::Assign: break;
1850 case ObjCPropertyDecl::Copy: S += ",C"; break;
1851 case ObjCPropertyDecl::Retain: S += ",&"; break;
1852 }
1853 }
1854
1855 // It really isn't clear at all what this means, since properties
1856 // are "dynamic by default".
1857 if (Dynamic)
1858 S += ",D";
1859
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001860 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1861 S += ",N";
1862
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001863 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1864 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001865 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001866 }
1867
1868 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1869 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001870 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001871 }
1872
1873 if (SynthesizePID) {
1874 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1875 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001876 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001877 }
1878
1879 // FIXME: OBJCGC: weak & strong
1880}
1881
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001882/// getLegacyIntegralTypeEncoding -
1883/// Another legacy compatibility encoding: 32-bit longs are encoded as
1884/// 'l' or 'L', but not always. For typedefs, we need to use
1885/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1886///
1887void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1888 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1889 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1890 if (BT->getKind() == BuiltinType::ULong)
1891 PointeeTy = UnsignedIntTy;
1892 else if (BT->getKind() == BuiltinType::Long)
1893 PointeeTy = IntTy;
1894 }
1895 }
1896}
1897
Fariborz Jahanian248db262008-01-22 22:44:46 +00001898void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001899 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001900 // We follow the behavior of gcc, expanding structures which are
1901 // directly pointed to, and expanding embedded structures. Note that
1902 // these rules are sufficient to prevent recursive encoding of the
1903 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001904 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1905 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001906}
1907
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001908static void EncodeBitField(const ASTContext *Context, std::string& S,
1909 FieldDecl *FD) {
1910 const Expr *E = FD->getBitWidth();
1911 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1912 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1913 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1914 S += 'b';
1915 S += llvm::utostr(N);
1916}
1917
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001918void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1919 bool ExpandPointedToStructures,
1920 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001921 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001922 bool OutermostType,
1923 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001924 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001925 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001926 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001927 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001928 else {
1929 char encoding;
1930 switch (BT->getKind()) {
1931 default: assert(0 && "Unhandled builtin type kind");
1932 case BuiltinType::Void: encoding = 'v'; break;
1933 case BuiltinType::Bool: encoding = 'B'; break;
1934 case BuiltinType::Char_U:
1935 case BuiltinType::UChar: encoding = 'C'; break;
1936 case BuiltinType::UShort: encoding = 'S'; break;
1937 case BuiltinType::UInt: encoding = 'I'; break;
1938 case BuiltinType::ULong: encoding = 'L'; break;
1939 case BuiltinType::ULongLong: encoding = 'Q'; break;
1940 case BuiltinType::Char_S:
1941 case BuiltinType::SChar: encoding = 'c'; break;
1942 case BuiltinType::Short: encoding = 's'; break;
1943 case BuiltinType::Int: encoding = 'i'; break;
1944 case BuiltinType::Long: encoding = 'l'; break;
1945 case BuiltinType::LongLong: encoding = 'q'; break;
1946 case BuiltinType::Float: encoding = 'f'; break;
1947 case BuiltinType::Double: encoding = 'd'; break;
1948 case BuiltinType::LongDouble: encoding = 'd'; break;
1949 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001950
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001951 S += encoding;
1952 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001953 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001954 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001955 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1956 ExpandPointedToStructures,
1957 ExpandStructures, FD);
1958 if (FD || EncodingProperty) {
1959 // Note that we do extended encoding of protocol qualifer list
1960 // Only when doing ivar or property encoding.
1961 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1962 S += '"';
1963 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1964 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1965 S += '<';
1966 S += Proto->getNameAsString();
1967 S += '>';
1968 }
1969 S += '"';
1970 }
1971 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001972 }
1973 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001974 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001975 bool isReadOnly = false;
1976 // For historical/compatibility reasons, the read-only qualifier of the
1977 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1978 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1979 // Also, do not emit the 'r' for anything but the outermost type!
1980 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1981 if (OutermostType && T.isConstQualified()) {
1982 isReadOnly = true;
1983 S += 'r';
1984 }
1985 }
1986 else if (OutermostType) {
1987 QualType P = PointeeTy;
1988 while (P->getAsPointerType())
1989 P = P->getAsPointerType()->getPointeeType();
1990 if (P.isConstQualified()) {
1991 isReadOnly = true;
1992 S += 'r';
1993 }
1994 }
1995 if (isReadOnly) {
1996 // Another legacy compatibility encoding. Some ObjC qualifier and type
1997 // combinations need to be rearranged.
1998 // Rewrite "in const" from "nr" to "rn"
1999 const char * s = S.c_str();
2000 int len = S.length();
2001 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2002 std::string replace = "rn";
2003 S.replace(S.end()-2, S.end(), replace);
2004 }
2005 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002006 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002007 S += '@';
2008 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002009 }
2010 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002011 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2012 // Another historical/compatibility reason.
2013 // We encode the underlying type which comes out as
2014 // {...};
2015 S += '^';
2016 getObjCEncodingForTypeImpl(PointeeTy, S,
2017 false, ExpandPointedToStructures,
2018 NULL);
2019 return;
2020 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002021 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002022 if (FD || EncodingProperty) {
2023 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2024 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002025 S += '"';
2026 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002027 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2028 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2029 S += '<';
2030 S += Proto->getNameAsString();
2031 S += '>';
2032 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002033 S += '"';
2034 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002035 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002036 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002037 S += '#';
2038 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002039 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002040 S += ':';
2041 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002042 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002043
2044 if (PointeeTy->isCharType()) {
2045 // char pointer types should be encoded as '*' unless it is a
2046 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002047 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002048 S += '*';
2049 return;
2050 }
2051 }
2052
2053 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002054 getLegacyIntegralTypeEncoding(PointeeTy);
2055
2056 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002057 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002058 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002059 } else if (const ArrayType *AT =
2060 // Ignore type qualifiers etc.
2061 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002062 S += '[';
2063
2064 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2065 S += llvm::utostr(CAT->getSize().getZExtValue());
2066 else
2067 assert(0 && "Unhandled array type!");
2068
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002069 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002070 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002071 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002072 } else if (T->getAsFunctionType()) {
2073 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002074 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002075 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002076 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002077 // Anonymous structures print as '?'
2078 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2079 S += II->getName();
2080 } else {
2081 S += '?';
2082 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002083 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002084 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002085 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2086 FieldEnd = RDecl->field_end();
2087 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002088 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002089 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002090 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002091 S += '"';
2092 }
2093
2094 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002095 if (Field->isBitField()) {
2096 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2097 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002098 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002099 QualType qt = Field->getType();
2100 getLegacyIntegralTypeEncoding(qt);
2101 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002102 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002103 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002104 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002105 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002106 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002107 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002108 if (FD && FD->isBitField())
2109 EncodeBitField(this, S, FD);
2110 else
2111 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002112 } else if (T->isBlockPointerType()) {
2113 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002114 } else if (T->isObjCInterfaceType()) {
2115 // @encode(class_name)
2116 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2117 S += '{';
2118 const IdentifierInfo *II = OI->getIdentifier();
2119 S += II->getName();
2120 S += '=';
2121 std::vector<FieldDecl*> RecFields;
2122 CollectObjCIvars(OI, RecFields);
2123 for (unsigned int i = 0; i != RecFields.size(); i++) {
2124 if (RecFields[i]->isBitField())
2125 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2126 RecFields[i]);
2127 else
2128 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2129 FD);
2130 }
2131 S += '}';
2132 }
2133 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002134 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002135}
2136
Ted Kremenek42730c52008-01-07 19:49:32 +00002137void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002138 std::string& S) const {
2139 if (QT & Decl::OBJC_TQ_In)
2140 S += 'n';
2141 if (QT & Decl::OBJC_TQ_Inout)
2142 S += 'N';
2143 if (QT & Decl::OBJC_TQ_Out)
2144 S += 'o';
2145 if (QT & Decl::OBJC_TQ_Bycopy)
2146 S += 'O';
2147 if (QT & Decl::OBJC_TQ_Byref)
2148 S += 'R';
2149 if (QT & Decl::OBJC_TQ_Oneway)
2150 S += 'V';
2151}
2152
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002153void ASTContext::setBuiltinVaListType(QualType T)
2154{
2155 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2156
2157 BuiltinVaListType = T;
2158}
2159
Ted Kremenek42730c52008-01-07 19:49:32 +00002160void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002161{
Ted Kremenek42730c52008-01-07 19:49:32 +00002162 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002163
2164 // typedef struct objc_object *id;
2165 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002166 // User error - caller will issue diagnostics.
2167 if (!ptr)
2168 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002169 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002170 // User error - caller will issue diagnostics.
2171 if (!rec)
2172 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002173 IdStructType = rec;
2174}
2175
Ted Kremenek42730c52008-01-07 19:49:32 +00002176void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002177{
Ted Kremenek42730c52008-01-07 19:49:32 +00002178 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002179
2180 // typedef struct objc_selector *SEL;
2181 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002182 if (!ptr)
2183 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002184 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002185 if (!rec)
2186 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002187 SelStructType = rec;
2188}
2189
Ted Kremenek42730c52008-01-07 19:49:32 +00002190void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002191{
Ted Kremenek42730c52008-01-07 19:49:32 +00002192 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002193}
2194
Ted Kremenek42730c52008-01-07 19:49:32 +00002195void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002196{
Ted Kremenek42730c52008-01-07 19:49:32 +00002197 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002198
2199 // typedef struct objc_class *Class;
2200 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2201 assert(ptr && "'Class' incorrectly typed");
2202 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2203 assert(rec && "'Class' incorrectly typed");
2204 ClassStructType = rec;
2205}
2206
Ted Kremenek42730c52008-01-07 19:49:32 +00002207void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2208 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002209 "'NSConstantString' type already set!");
2210
Ted Kremenek42730c52008-01-07 19:49:32 +00002211 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002212}
2213
Douglas Gregorc6507e42008-11-03 14:12:49 +00002214/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002215/// TargetInfo, produce the corresponding type. The unsigned @p Type
2216/// is actually a value of type @c TargetInfo::IntType.
2217QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002218 switch (Type) {
2219 case TargetInfo::NoInt: return QualType();
2220 case TargetInfo::SignedShort: return ShortTy;
2221 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2222 case TargetInfo::SignedInt: return IntTy;
2223 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2224 case TargetInfo::SignedLong: return LongTy;
2225 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2226 case TargetInfo::SignedLongLong: return LongLongTy;
2227 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2228 }
2229
2230 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002231 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002232}
Ted Kremenek118930e2008-07-24 23:58:27 +00002233
2234//===----------------------------------------------------------------------===//
2235// Type Predicates.
2236//===----------------------------------------------------------------------===//
2237
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002238/// isObjCNSObjectType - Return true if this is an NSObject object using
2239/// NSObject attribute on a c-style pointer type.
2240/// FIXME - Make it work directly on types.
2241///
2242bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2243 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2244 if (TypedefDecl *TD = TDT->getDecl())
2245 if (TD->getAttr<ObjCNSObjectAttr>())
2246 return true;
2247 }
2248 return false;
2249}
2250
Ted Kremenek118930e2008-07-24 23:58:27 +00002251/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2252/// to an object type. This includes "id" and "Class" (two 'special' pointers
2253/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2254/// ID type).
2255bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2256 if (Ty->isObjCQualifiedIdType())
2257 return true;
2258
Steve Naroffd9e00802008-10-21 18:24:04 +00002259 // Blocks are objects.
2260 if (Ty->isBlockPointerType())
2261 return true;
2262
2263 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002264 if (!Ty->isPointerType())
2265 return false;
2266
2267 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2268 // pointer types. This looks for the typedef specifically, not for the
2269 // underlying type.
2270 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2271 return true;
2272
2273 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002274 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2275 return true;
2276
2277 // If is has NSObject attribute, OK as well.
2278 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002279}
2280
Chris Lattner6ff358b2008-04-07 06:51:04 +00002281//===----------------------------------------------------------------------===//
2282// Type Compatibility Testing
2283//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002284
Steve Naroff3454b6c2008-09-04 15:10:53 +00002285/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002286/// block types. Types must be strictly compatible here. For example,
2287/// C unfortunately doesn't produce an error for the following:
2288///
2289/// int (*emptyArgFunc)();
2290/// int (*intArgList)(int) = emptyArgFunc;
2291///
2292/// For blocks, we will produce an error for the following (similar to C++):
2293///
2294/// int (^emptyArgBlock)();
2295/// int (^intArgBlock)(int) = emptyArgBlock;
2296///
2297/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2298///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002299bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002300 const FunctionType *lbase = lhs->getAsFunctionType();
2301 const FunctionType *rbase = rhs->getAsFunctionType();
2302 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2303 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2304 if (lproto && rproto)
2305 return !mergeTypes(lhs, rhs).isNull();
2306 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002307}
2308
Chris Lattner6ff358b2008-04-07 06:51:04 +00002309/// areCompatVectorTypes - Return true if the two specified vector types are
2310/// compatible.
2311static bool areCompatVectorTypes(const VectorType *LHS,
2312 const VectorType *RHS) {
2313 assert(LHS->isCanonical() && RHS->isCanonical());
2314 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002315 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002316}
2317
Eli Friedman0d9549b2008-08-22 00:56:42 +00002318/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002319/// compatible for assignment from RHS to LHS. This handles validation of any
2320/// protocol qualifiers on the LHS or RHS.
2321///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002322bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2323 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002324 // Verify that the base decls are compatible: the RHS must be a subclass of
2325 // the LHS.
2326 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2327 return false;
2328
2329 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2330 // protocol qualified at all, then we are good.
2331 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2332 return true;
2333
2334 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2335 // isn't a superset.
2336 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2337 return true; // FIXME: should return false!
2338
2339 // Finally, we must have two protocol-qualified interfaces.
2340 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2341 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2342 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2343 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2344 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2345 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2346
2347 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2348 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2349 // LHS in a single parallel scan until we run out of elements in LHS.
2350 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2351 ObjCProtocolDecl *LHSProto = *LHSPI;
2352
2353 while (RHSPI != RHSPE) {
2354 ObjCProtocolDecl *RHSProto = *RHSPI++;
2355 // If the RHS has a protocol that the LHS doesn't, ignore it.
2356 if (RHSProto != LHSProto)
2357 continue;
2358
2359 // Otherwise, the RHS does have this element.
2360 ++LHSPI;
2361 if (LHSPI == LHSPE)
2362 return true; // All protocols in LHS exist in RHS.
2363
2364 LHSProto = *LHSPI;
2365 }
2366
2367 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2368 return false;
2369}
2370
Steve Naroff85f0dc52007-10-15 20:41:53 +00002371/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2372/// both shall have the identically qualified version of a compatible type.
2373/// C99 6.2.7p1: Two types have compatible types if their types are the
2374/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002375bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2376 return !mergeTypes(LHS, RHS).isNull();
2377}
2378
2379QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2380 const FunctionType *lbase = lhs->getAsFunctionType();
2381 const FunctionType *rbase = rhs->getAsFunctionType();
2382 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2383 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2384 bool allLTypes = true;
2385 bool allRTypes = true;
2386
2387 // Check return type
2388 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2389 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002390 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2391 allLTypes = false;
2392 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2393 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002394
2395 if (lproto && rproto) { // two C99 style function prototypes
2396 unsigned lproto_nargs = lproto->getNumArgs();
2397 unsigned rproto_nargs = rproto->getNumArgs();
2398
2399 // Compatible functions must have the same number of arguments
2400 if (lproto_nargs != rproto_nargs)
2401 return QualType();
2402
2403 // Variadic and non-variadic functions aren't compatible
2404 if (lproto->isVariadic() != rproto->isVariadic())
2405 return QualType();
2406
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002407 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2408 return QualType();
2409
Eli Friedman0d9549b2008-08-22 00:56:42 +00002410 // Check argument compatibility
2411 llvm::SmallVector<QualType, 10> types;
2412 for (unsigned i = 0; i < lproto_nargs; i++) {
2413 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2414 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2415 QualType argtype = mergeTypes(largtype, rargtype);
2416 if (argtype.isNull()) return QualType();
2417 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002418 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2419 allLTypes = false;
2420 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2421 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002422 }
2423 if (allLTypes) return lhs;
2424 if (allRTypes) return rhs;
2425 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002426 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002427 }
2428
2429 if (lproto) allRTypes = false;
2430 if (rproto) allLTypes = false;
2431
2432 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2433 if (proto) {
2434 if (proto->isVariadic()) return QualType();
2435 // Check that the types are compatible with the types that
2436 // would result from default argument promotions (C99 6.7.5.3p15).
2437 // The only types actually affected are promotable integer
2438 // types and floats, which would be passed as a different
2439 // type depending on whether the prototype is visible.
2440 unsigned proto_nargs = proto->getNumArgs();
2441 for (unsigned i = 0; i < proto_nargs; ++i) {
2442 QualType argTy = proto->getArgType(i);
2443 if (argTy->isPromotableIntegerType() ||
2444 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2445 return QualType();
2446 }
2447
2448 if (allLTypes) return lhs;
2449 if (allRTypes) return rhs;
2450 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002451 proto->getNumArgs(), lproto->isVariadic(),
2452 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002453 }
2454
2455 if (allLTypes) return lhs;
2456 if (allRTypes) return rhs;
2457 return getFunctionTypeNoProto(retType);
2458}
2459
2460QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002461 // C++ [expr]: If an expression initially has the type "reference to T", the
2462 // type is adjusted to "T" prior to any further analysis, the expression
2463 // designates the object or function denoted by the reference, and the
2464 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002465 // FIXME: C++ shouldn't be going through here! The rules are different
2466 // enough that they should be handled separately.
2467 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002468 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002469 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002470 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002471
Eli Friedman0d9549b2008-08-22 00:56:42 +00002472 QualType LHSCan = getCanonicalType(LHS),
2473 RHSCan = getCanonicalType(RHS);
2474
2475 // If two types are identical, they are compatible.
2476 if (LHSCan == RHSCan)
2477 return LHS;
2478
2479 // If the qualifiers are different, the types aren't compatible
2480 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2481 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2482 return QualType();
2483
2484 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2485 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2486
Chris Lattnerc38d4522008-01-14 05:45:46 +00002487 // We want to consider the two function types to be the same for these
2488 // comparisons, just force one to the other.
2489 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2490 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002491
2492 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002493 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2494 LHSClass = Type::ConstantArray;
2495 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2496 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002497
Nate Begemanaf6ed502008-04-18 23:10:10 +00002498 // Canonicalize ExtVector -> Vector.
2499 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2500 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002501
Chris Lattner7cdcb252008-04-07 06:38:24 +00002502 // Consider qualified interfaces and interfaces the same.
2503 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2504 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002505
Chris Lattnerb5709e22008-04-07 05:43:21 +00002506 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002507 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002508 // ID is compatible with all qualified id types.
2509 if (LHS->isObjCQualifiedIdType()) {
2510 if (const PointerType *PT = RHS->getAsPointerType()) {
2511 QualType pType = PT->getPointeeType();
2512 if (isObjCIdType(pType))
2513 return LHS;
2514 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2515 // Unfortunately, this API is part of Sema (which we don't have access
2516 // to. Need to refactor. The following check is insufficient, since we
2517 // need to make sure the class implements the protocol.
2518 if (pType->isObjCInterfaceType())
2519 return LHS;
2520 }
2521 }
2522 if (RHS->isObjCQualifiedIdType()) {
2523 if (const PointerType *PT = LHS->getAsPointerType()) {
2524 QualType pType = PT->getPointeeType();
2525 if (isObjCIdType(pType))
2526 return RHS;
2527 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2528 // Unfortunately, this API is part of Sema (which we don't have access
2529 // to. Need to refactor. The following check is insufficient, since we
2530 // need to make sure the class implements the protocol.
2531 if (pType->isObjCInterfaceType())
2532 return RHS;
2533 }
2534 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002535 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2536 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002537 if (const EnumType* ETy = LHS->getAsEnumType()) {
2538 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2539 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002540 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002541 if (const EnumType* ETy = RHS->getAsEnumType()) {
2542 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2543 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002544 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002545
Eli Friedman0d9549b2008-08-22 00:56:42 +00002546 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002547 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002548
Steve Naroffc88babe2008-01-09 22:43:08 +00002549 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002550 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002551 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002552 {
2553 // Merge two pointer types, while trying to preserve typedef info
2554 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2555 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2556 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2557 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002558 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2559 return LHS;
2560 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2561 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002562 return getPointerType(ResultType);
2563 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002564 case Type::BlockPointer:
2565 {
2566 // Merge two block pointer types, while trying to preserve typedef info
2567 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2568 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2569 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2570 if (ResultType.isNull()) return QualType();
2571 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2572 return LHS;
2573 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2574 return RHS;
2575 return getBlockPointerType(ResultType);
2576 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002577 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002578 {
2579 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2580 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2581 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2582 return QualType();
2583
2584 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2585 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2586 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2587 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002588 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2589 return LHS;
2590 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2591 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002592 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2593 ArrayType::ArraySizeModifier(), 0);
2594 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2595 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002596 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2597 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002598 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2599 return LHS;
2600 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2601 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002602 if (LVAT) {
2603 // FIXME: This isn't correct! But tricky to implement because
2604 // the array's size has to be the size of LHS, but the type
2605 // has to be different.
2606 return LHS;
2607 }
2608 if (RVAT) {
2609 // FIXME: This isn't correct! But tricky to implement because
2610 // the array's size has to be the size of RHS, but the type
2611 // has to be different.
2612 return RHS;
2613 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002614 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2615 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002616 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002617 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002618 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002619 return mergeFunctionTypes(LHS, RHS);
2620 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002621 // FIXME: Why are these compatible?
2622 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2623 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2624 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002625 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002626 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002627 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002628 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002629 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2630 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002631 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002632 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002633 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2634 // for checking assignment/comparison safety
2635 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002636 case Type::ObjCQualifiedId:
2637 // Distinct qualified id's are not compatible.
2638 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002639 default:
2640 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002641 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002642 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002643}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002644
Chris Lattner1d78a862008-04-07 07:01:58 +00002645//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002646// Integer Predicates
2647//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002648
Eli Friedman0832dbc2008-06-28 06:23:08 +00002649unsigned ASTContext::getIntWidth(QualType T) {
2650 if (T == BoolTy)
2651 return 1;
2652 // At the moment, only bool has padding bits
2653 return (unsigned)getTypeSize(T);
2654}
2655
2656QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2657 assert(T->isSignedIntegerType() && "Unexpected type");
2658 if (const EnumType* ETy = T->getAsEnumType())
2659 T = ETy->getDecl()->getIntegerType();
2660 const BuiltinType* BTy = T->getAsBuiltinType();
2661 assert (BTy && "Unexpected signed integer type");
2662 switch (BTy->getKind()) {
2663 case BuiltinType::Char_S:
2664 case BuiltinType::SChar:
2665 return UnsignedCharTy;
2666 case BuiltinType::Short:
2667 return UnsignedShortTy;
2668 case BuiltinType::Int:
2669 return UnsignedIntTy;
2670 case BuiltinType::Long:
2671 return UnsignedLongTy;
2672 case BuiltinType::LongLong:
2673 return UnsignedLongLongTy;
2674 default:
2675 assert(0 && "Unexpected signed integer type");
2676 return QualType();
2677 }
2678}
2679
2680
2681//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002682// Serialization Support
2683//===----------------------------------------------------------------------===//
2684
Ted Kremenek738e6c02007-10-31 17:10:13 +00002685/// Emit - Serialize an ASTContext object to Bitcode.
2686void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002687 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002688 S.EmitRef(SourceMgr);
2689 S.EmitRef(Target);
2690 S.EmitRef(Idents);
2691 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002692
Ted Kremenek68228a92007-10-31 22:44:07 +00002693 // Emit the size of the type vector so that we can reserve that size
2694 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002695 S.EmitInt(Types.size());
2696
Ted Kremenek034a78c2007-11-13 22:02:55 +00002697 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2698 I!=E;++I)
2699 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002700
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002701 S.EmitOwnedPtr(TUDecl);
2702
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002703 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002704}
2705
Ted Kremenekacba3612007-11-13 00:25:37 +00002706ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002707
2708 // Read the language options.
2709 LangOptions LOpts;
2710 LOpts.Read(D);
2711
Ted Kremenek68228a92007-10-31 22:44:07 +00002712 SourceManager &SM = D.ReadRef<SourceManager>();
2713 TargetInfo &t = D.ReadRef<TargetInfo>();
2714 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2715 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002716
Ted Kremenek68228a92007-10-31 22:44:07 +00002717 unsigned size_reserve = D.ReadInt();
2718
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002719 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2720 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002721
Ted Kremenek034a78c2007-11-13 22:02:55 +00002722 for (unsigned i = 0; i < size_reserve; ++i)
2723 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002724
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002725 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2726
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002727 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002728
2729 return A;
2730}