blob: 46b88df153a9c345fff06ac323712701970e2ec5 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
42 BuiltinInfo.InitializeBuiltins(idents, Target);
43 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
110 else if (isa<ArrayType>(T))
111 ++NumArray;
112 else if (isa<VectorType>(T))
113 ++NumVector;
114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000139 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
151 fprintf(stderr, " %d array types\n", NumArray);
152 fprintf(stderr, " %d vector types\n", NumVector);
153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregord2baafd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000233
Steve Naroff9d12c902007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
249
Chris Lattner2a674dc2008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
266unsigned ASTContext::getDeclAlign(const Decl *D) {
267 // FIXME: If attribute(align) is specified on the decl, round up to it.
268
269 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
270 QualType T = VD->getType();
271 // Incomplete or function types default to 1.
272 if (T->isIncompleteType() || T->isFunctionType())
273 return 1;
274
275 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
276 T = cast<ArrayType>(T)->getElementType();
277
278 return getTypeAlign(T);
279 }
280
281 return 1;
282}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000283
Chris Lattner4b009652007-07-25 00:24:17 +0000284/// getTypeSize - Return the size of the specified type, in bits. This method
285/// does not work on incomplete types.
286std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000287ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000288 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000290 unsigned Align;
291 switch (T->getTypeClass()) {
292 case Type::TypeName: assert(0 && "Not a canonical type!");
293 case Type::FunctionNoProto:
294 case Type::FunctionProto:
295 default:
296 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000297 case Type::VariableArray:
298 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000299 case Type::DependentSizedArray:
300 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000301 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000302 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000303
Chris Lattner8cd0e932008-03-05 18:54:05 +0000304 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000305 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000306 Align = EltInfo.second;
307 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000308 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000309 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000310 case Type::Vector: {
311 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000312 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000313 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000314 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000315 // If the alignment is not a power of 2, round up to the next power of 2.
316 // This happens for non-power-of-2 length vectors.
317 // FIXME: this should probably be a target property.
318 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000319 break;
320 }
321
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000322 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000323 switch (cast<BuiltinType>(T)->getKind()) {
324 default: assert(0 && "Unknown builtin type!");
325 case BuiltinType::Void:
326 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000327 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000328 Width = Target.getBoolWidth();
329 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000330 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 case BuiltinType::Char_S:
332 case BuiltinType::Char_U:
333 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000334 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000335 Width = Target.getCharWidth();
336 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000337 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000338 case BuiltinType::WChar:
339 Width = Target.getWCharWidth();
340 Align = Target.getWCharAlign();
341 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000344 Width = Target.getShortWidth();
345 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000346 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000347 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000349 Width = Target.getIntWidth();
350 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000351 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000353 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000354 Width = Target.getLongWidth();
355 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000356 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000357 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = Target.getLongLongWidth();
360 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000361 break;
362 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000363 Width = Target.getFloatWidth();
364 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000365 break;
366 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000367 Width = Target.getDoubleWidth();
368 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000369 break;
370 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000371 Width = Target.getLongDoubleWidth();
372 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000374 }
375 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000376 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000377 // FIXME: Pointers into different addr spaces could have different sizes and
378 // alignment requirements: getPointerInfo should take an AddrSpace.
379 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000381 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000382 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000384 case Type::BlockPointer: {
385 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
386 Width = Target.getPointerWidth(AS);
387 Align = Target.getPointerAlign(AS);
388 break;
389 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000390 case Type::Pointer: {
391 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000392 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000393 Align = Target.getPointerAlign(AS);
394 break;
395 }
Chris Lattner4b009652007-07-25 00:24:17 +0000396 case Type::Reference:
397 // "When applied to a reference or a reference type, the result is the size
398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000403 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000404 // the GCC ABI, where pointers to data are one pointer large, pointers to
405 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000406 // other compilers too, we need to delegate this completely to TargetInfo
407 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000408 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
409 unsigned AS = Pointee.getAddressSpace();
410 Width = Target.getPointerWidth(AS);
411 if (Pointee->isFunctionType())
412 Width *= 2;
413 Align = Target.getPointerAlign(AS);
414 // GCC aligns at single pointer width.
415 }
Chris Lattner4b009652007-07-25 00:24:17 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000432 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000433 const TagType *TT = cast<TagType>(T);
434
435 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000436 Width = 1;
437 Align = 1;
438 break;
439 }
440
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000441 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000442 return getTypeInfo(ET->getDecl()->getIntegerType());
443
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000444 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000445 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000448 break;
449 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000450 }
Chris Lattner4b009652007-07-25 00:24:17 +0000451
452 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000453 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000454}
455
Chris Lattner83165b52009-01-27 18:08:34 +0000456/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
457/// type for the current target in bits. This can be different than the ABI
458/// alignment in cases where it is beneficial for performance to overalign
459/// a data type.
460unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
461 unsigned ABIAlign = getTypeAlign(T);
462
463 // Doubles should be naturally aligned if possible.
464 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
465 if (BT->getKind() == BuiltinType::Double)
Fariborz Jahanianbaeeb752009-01-27 18:55:00 +0000466 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000467
468 return ABIAlign;
469}
470
471
Devang Patelbfe323c2008-06-04 21:22:16 +0000472/// LayoutField - Field layout.
473void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000474 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000475 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000476 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000477 uint64_t FieldOffset = IsUnion ? 0 : Size;
478 uint64_t FieldSize;
479 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000480
481 // FIXME: Should this override struct packing? Probably we want to
482 // take the minimum?
483 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
484 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000485
486 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
487 // TODO: Need to check this algorithm on other targets!
488 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000489 FieldSize =
490 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000491
492 std::pair<uint64_t, unsigned> FieldInfo =
493 Context.getTypeInfo(FD->getType());
494 uint64_t TypeSize = FieldInfo.first;
495
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000496 // Determine the alignment of this bitfield. The packing
497 // attributes define a maximum and the alignment attribute defines
498 // a minimum.
499 // FIXME: What is the right behavior when the specified alignment
500 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000501 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000502 if (FieldPacking)
503 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000504 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
505 FieldAlign = std::max(FieldAlign, AA->getAlignment());
506
507 // Check if we need to add padding to give the field the correct
508 // alignment.
509 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
510 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
511
512 // Padding members don't affect overall alignment
513 if (!FD->getIdentifier())
514 FieldAlign = 1;
515 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000516 if (FD->getType()->isIncompleteArrayType()) {
517 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000518 // query getTypeInfo about these, so we figure it out here.
519 // Flexible array members don't have any size, but they
520 // have to be aligned appropriately for their element type.
521 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000522 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000523 FieldAlign = Context.getTypeAlign(ATy->getElementType());
524 } else {
525 std::pair<uint64_t, unsigned> FieldInfo =
526 Context.getTypeInfo(FD->getType());
527 FieldSize = FieldInfo.first;
528 FieldAlign = FieldInfo.second;
529 }
530
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000531 // Determine the alignment of this bitfield. The packing
532 // attributes define a maximum and the alignment attribute defines
533 // a minimum. Additionally, the packing alignment must be at least
534 // a byte for non-bitfields.
535 //
536 // FIXME: What is the right behavior when the specified alignment
537 // is smaller than the specified packing?
538 if (FieldPacking)
539 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000540 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
541 FieldAlign = std::max(FieldAlign, AA->getAlignment());
542
543 // Round up the current record size to the field's alignment boundary.
544 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
545 }
546
547 // Place this field at the current location.
548 FieldOffsets[FieldNo] = FieldOffset;
549
550 // Reserve space for this field.
551 if (IsUnion) {
552 Size = std::max(Size, FieldSize);
553 } else {
554 Size = FieldOffset + FieldSize;
555 }
556
557 // Remember max struct/class alignment.
558 Alignment = std::max(Alignment, FieldAlign);
559}
560
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000561static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
562 std::vector<FieldDecl*> &Fields) {
563 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
564 if (SuperClass)
565 CollectObjCIvars(SuperClass, Fields);
566 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
567 E = OI->ivar_end(); I != E; ++I) {
568 ObjCIvarDecl *IVDecl = (*I);
569 if (!IVDecl->isInvalidDecl())
570 Fields.push_back(cast<FieldDecl>(IVDecl));
571 }
572}
573
574/// addRecordToClass - produces record info. for the class for its
575/// ivars and all those inherited.
576///
577const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
578{
579 const RecordDecl *&RD = ASTRecordForInterface[D];
580 if (RD)
581 return RD;
582 std::vector<FieldDecl*> RecFields;
583 CollectObjCIvars(D, RecFields);
584 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
585 D->getLocation(),
586 D->getIdentifier());
587 /// FIXME! Can do collection of ivars and adding to the record while
588 /// doing it.
589 for (unsigned int i = 0; i != RecFields.size(); i++) {
590 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
591 RecFields[i]->getLocation(),
592 RecFields[i]->getIdentifier(),
593 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000594 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000595 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000596 }
597 NewRD->completeDefinition(*this);
598 RD = NewRD;
599 return RD;
600}
Devang Patel4b6bf702008-06-04 21:54:36 +0000601
Fariborz Jahanianea944842008-12-18 17:29:46 +0000602/// setFieldDecl - maps a field for the given Ivar reference node.
603//
604void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
605 const ObjCIvarDecl *Ivar,
606 const ObjCIvarRefExpr *MRef) {
607 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
608 lookupFieldDeclForIvar(*this, Ivar);
609 ASTFieldForIvarRef[MRef] = FD;
610}
611
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000612/// getASTObjcInterfaceLayout - Get or compute information about the layout of
613/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000614/// position information.
615const ASTRecordLayout &
616ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
617 // Look up this layout, if already laid out, return what we have.
618 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
619 if (Entry) return *Entry;
620
621 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
622 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000623 ASTRecordLayout *NewEntry = NULL;
624 unsigned FieldCount = D->ivar_size();
625 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
626 FieldCount++;
627 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
628 unsigned Alignment = SL.getAlignment();
629 uint64_t Size = SL.getSize();
630 NewEntry = new ASTRecordLayout(Size, Alignment);
631 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000632 // Super class is at the beginning of the layout.
633 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000634 } else {
635 NewEntry = new ASTRecordLayout();
636 NewEntry->InitializeLayout(FieldCount);
637 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000638 Entry = NewEntry;
639
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000640 unsigned StructPacking = 0;
641 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
642 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000643
644 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
645 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
646 AA->getAlignment()));
647
648 // Layout each ivar sequentially.
649 unsigned i = 0;
650 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
651 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
652 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000653 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000654 }
655
656 // Finally, round the size of the total struct up to the alignment of the
657 // struct itself.
658 NewEntry->FinalizeLayout();
659 return *NewEntry;
660}
661
Devang Patel7a78e432007-11-01 19:11:01 +0000662/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000663/// specified record (struct/union/class), which indicates its size and field
664/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000665const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000666 D = D->getDefinition(*this);
667 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000668
Chris Lattner4b009652007-07-25 00:24:17 +0000669 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000670 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000671 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000672
Devang Patel7a78e432007-11-01 19:11:01 +0000673 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
674 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
675 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000676 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000677
Douglas Gregor39677622008-12-11 20:41:00 +0000678 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000679 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000680 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000681
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000682 unsigned StructPacking = 0;
683 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
684 StructPacking = PA->getAlignment();
685
Eli Friedman5949a022008-05-30 09:31:38 +0000686 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000687 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
688 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000689
Eli Friedman5949a022008-05-30 09:31:38 +0000690 // Layout each field, for now, just sequentially, respecting alignment. In
691 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000692 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000693 for (RecordDecl::field_iterator Field = D->field_begin(),
694 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000695 Field != FieldEnd; (void)++Field, ++FieldIdx)
696 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000697
698 // Finally, round the size of the total struct up to the alignment of the
699 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000700 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000701 return *NewEntry;
702}
703
Chris Lattner4b009652007-07-25 00:24:17 +0000704//===----------------------------------------------------------------------===//
705// Type creation/memoization methods
706//===----------------------------------------------------------------------===//
707
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000708QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000709 QualType CanT = getCanonicalType(T);
710 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000711 return T;
712
713 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
714 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000715 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000716 "Type is already address space qualified");
717
718 // Check if we've already instantiated an address space qual'd type of this
719 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000720 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000721 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000722 void *InsertPos = 0;
723 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(ASQy, 0);
725
726 // If the base type isn't canonical, this won't be a canonical type either,
727 // so fill in the canonical type field.
728 QualType Canonical;
729 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000730 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000731
732 // Get the new insert position for the node we care about.
733 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000734 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000735 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000736 ASQualType *New = new (*this, 8) ASQualType(T.getTypePtr(), Canonical,
737 AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000738 ASQualTypes.InsertNode(New, InsertPos);
739 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000740 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000741}
742
Chris Lattner4b009652007-07-25 00:24:17 +0000743
744/// getComplexType - Return the uniqued reference to the type for a complex
745/// number with the specified element type.
746QualType ASTContext::getComplexType(QualType T) {
747 // Unique pointers, to guarantee there is only one pointer of a particular
748 // structure.
749 llvm::FoldingSetNodeID ID;
750 ComplexType::Profile(ID, T);
751
752 void *InsertPos = 0;
753 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
754 return QualType(CT, 0);
755
756 // If the pointee type isn't canonical, this won't be a canonical type either,
757 // so fill in the canonical type field.
758 QualType Canonical;
759 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000760 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000761
762 // Get the new insert position for the node we care about.
763 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000764 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000765 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000766 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000767 Types.push_back(New);
768 ComplexTypes.InsertNode(New, InsertPos);
769 return QualType(New, 0);
770}
771
772
773/// getPointerType - Return the uniqued reference to the type for a pointer to
774/// the specified type.
775QualType ASTContext::getPointerType(QualType T) {
776 // Unique pointers, to guarantee there is only one pointer of a particular
777 // structure.
778 llvm::FoldingSetNodeID ID;
779 PointerType::Profile(ID, T);
780
781 void *InsertPos = 0;
782 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
783 return QualType(PT, 0);
784
785 // If the pointee type isn't canonical, this won't be a canonical type either,
786 // so fill in the canonical type field.
787 QualType Canonical;
788 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000789 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000790
791 // Get the new insert position for the node we care about.
792 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000793 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000794 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000795 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000796 Types.push_back(New);
797 PointerTypes.InsertNode(New, InsertPos);
798 return QualType(New, 0);
799}
800
Steve Naroff7aa54752008-08-27 16:04:49 +0000801/// getBlockPointerType - Return the uniqued reference to the type for
802/// a pointer to the specified block.
803QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000804 assert(T->isFunctionType() && "block of function types only");
805 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000806 // structure.
807 llvm::FoldingSetNodeID ID;
808 BlockPointerType::Profile(ID, T);
809
810 void *InsertPos = 0;
811 if (BlockPointerType *PT =
812 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
813 return QualType(PT, 0);
814
Steve Narofffd5b19d2008-08-28 19:20:44 +0000815 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000816 // type either so fill in the canonical type field.
817 QualType Canonical;
818 if (!T->isCanonical()) {
819 Canonical = getBlockPointerType(getCanonicalType(T));
820
821 // Get the new insert position for the node we care about.
822 BlockPointerType *NewIP =
823 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000824 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000825 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000826 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000827 Types.push_back(New);
828 BlockPointerTypes.InsertNode(New, InsertPos);
829 return QualType(New, 0);
830}
831
Chris Lattner4b009652007-07-25 00:24:17 +0000832/// getReferenceType - Return the uniqued reference to the type for a reference
833/// to the specified type.
834QualType ASTContext::getReferenceType(QualType T) {
835 // Unique pointers, to guarantee there is only one pointer of a particular
836 // structure.
837 llvm::FoldingSetNodeID ID;
838 ReferenceType::Profile(ID, T);
839
840 void *InsertPos = 0;
841 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
842 return QualType(RT, 0);
843
844 // If the referencee type isn't canonical, this won't be a canonical type
845 // either, so fill in the canonical type field.
846 QualType Canonical;
847 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000848 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000849
850 // Get the new insert position for the node we care about.
851 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000852 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000853 }
854
Steve Naroff93fd2112009-01-27 22:08:43 +0000855 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000856 Types.push_back(New);
857 ReferenceTypes.InsertNode(New, InsertPos);
858 return QualType(New, 0);
859}
860
Sebastian Redl75555032009-01-24 21:16:55 +0000861/// getMemberPointerType - Return the uniqued reference to the type for a
862/// member pointer to the specified type, in the specified class.
863QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
864{
865 // Unique pointers, to guarantee there is only one pointer of a particular
866 // structure.
867 llvm::FoldingSetNodeID ID;
868 MemberPointerType::Profile(ID, T, Cls);
869
870 void *InsertPos = 0;
871 if (MemberPointerType *PT =
872 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
873 return QualType(PT, 0);
874
875 // If the pointee or class type isn't canonical, this won't be a canonical
876 // type either, so fill in the canonical type field.
877 QualType Canonical;
878 if (!T->isCanonical()) {
879 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
880
881 // Get the new insert position for the node we care about.
882 MemberPointerType *NewIP =
883 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
884 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
885 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000886 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000887 Types.push_back(New);
888 MemberPointerTypes.InsertNode(New, InsertPos);
889 return QualType(New, 0);
890}
891
Steve Naroff83c13012007-08-30 01:06:46 +0000892/// getConstantArrayType - Return the unique reference to the type for an
893/// array of the specified element type.
894QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000895 const llvm::APInt &ArySize,
896 ArrayType::ArraySizeModifier ASM,
897 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000898 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000899 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000900
901 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000902 if (ConstantArrayType *ATP =
903 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000904 return QualType(ATP, 0);
905
906 // If the element type isn't canonical, this won't be a canonical type either,
907 // so fill in the canonical type field.
908 QualType Canonical;
909 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000910 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000911 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000912 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000913 ConstantArrayType *NewIP =
914 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000915 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000916 }
917
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000918 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000919 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000920 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000921 Types.push_back(New);
922 return QualType(New, 0);
923}
924
Steve Naroffe2579e32007-08-30 18:14:25 +0000925/// getVariableArrayType - Returns a non-unique reference to the type for a
926/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000927QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
928 ArrayType::ArraySizeModifier ASM,
929 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000930 // Since we don't unique expressions, it isn't possible to unique VLA's
931 // that have an expression provided for their size.
932
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000933 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000934 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000935
936 VariableArrayTypes.push_back(New);
937 Types.push_back(New);
938 return QualType(New, 0);
939}
940
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000941/// getDependentSizedArrayType - Returns a non-unique reference to
942/// the type for a dependently-sized array of the specified element
943/// type. FIXME: We will need these to be uniqued, or at least
944/// comparable, at some point.
945QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
946 ArrayType::ArraySizeModifier ASM,
947 unsigned EltTypeQuals) {
948 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
949 "Size must be type- or value-dependent!");
950
951 // Since we don't unique expressions, it isn't possible to unique
952 // dependently-sized array types.
953
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000954 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000955 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
956 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000957
958 DependentSizedArrayTypes.push_back(New);
959 Types.push_back(New);
960 return QualType(New, 0);
961}
962
Eli Friedman8ff07782008-02-15 18:16:39 +0000963QualType ASTContext::getIncompleteArrayType(QualType EltTy,
964 ArrayType::ArraySizeModifier ASM,
965 unsigned EltTypeQuals) {
966 llvm::FoldingSetNodeID ID;
967 IncompleteArrayType::Profile(ID, EltTy);
968
969 void *InsertPos = 0;
970 if (IncompleteArrayType *ATP =
971 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
972 return QualType(ATP, 0);
973
974 // If the element type isn't canonical, this won't be a canonical type
975 // either, so fill in the canonical type field.
976 QualType Canonical;
977
978 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000979 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000980 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000981
982 // Get the new insert position for the node we care about.
983 IncompleteArrayType *NewIP =
984 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000985 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000986 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000987
Steve Naroff93fd2112009-01-27 22:08:43 +0000988 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000989 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000990
991 IncompleteArrayTypes.InsertNode(New, InsertPos);
992 Types.push_back(New);
993 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000994}
995
Chris Lattner4b009652007-07-25 00:24:17 +0000996/// getVectorType - Return the unique reference to a vector type of
997/// the specified element type and size. VectorType must be a built-in type.
998QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
999 BuiltinType *baseType;
1000
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001001 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001002 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1003
1004 // Check if we've already instantiated a vector of this type.
1005 llvm::FoldingSetNodeID ID;
1006 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1007 void *InsertPos = 0;
1008 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1009 return QualType(VTP, 0);
1010
1011 // If the element type isn't canonical, this won't be a canonical type either,
1012 // so fill in the canonical type field.
1013 QualType Canonical;
1014 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001015 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001016
1017 // Get the new insert position for the node we care about.
1018 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001019 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001020 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001021 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001022 VectorTypes.InsertNode(New, InsertPos);
1023 Types.push_back(New);
1024 return QualType(New, 0);
1025}
1026
Nate Begemanaf6ed502008-04-18 23:10:10 +00001027/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001028/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001029QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001030 BuiltinType *baseType;
1031
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001032 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001033 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001034
1035 // Check if we've already instantiated a vector of this type.
1036 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001037 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001038 void *InsertPos = 0;
1039 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1040 return QualType(VTP, 0);
1041
1042 // If the element type isn't canonical, this won't be a canonical type either,
1043 // so fill in the canonical type field.
1044 QualType Canonical;
1045 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001046 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001047
1048 // Get the new insert position for the node we care about.
1049 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001050 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001051 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001052 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001053 VectorTypes.InsertNode(New, InsertPos);
1054 Types.push_back(New);
1055 return QualType(New, 0);
1056}
1057
1058/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1059///
1060QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1061 // Unique functions, to guarantee there is only one function of a particular
1062 // structure.
1063 llvm::FoldingSetNodeID ID;
1064 FunctionTypeNoProto::Profile(ID, ResultTy);
1065
1066 void *InsertPos = 0;
1067 if (FunctionTypeNoProto *FT =
1068 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1069 return QualType(FT, 0);
1070
1071 QualType Canonical;
1072 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001073 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001074
1075 // Get the new insert position for the node we care about.
1076 FunctionTypeNoProto *NewIP =
1077 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001078 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001079 }
1080
Steve Naroff93fd2112009-01-27 22:08:43 +00001081 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001082 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001083 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001084 return QualType(New, 0);
1085}
1086
1087/// getFunctionType - Return a normal function type with a typed argument
1088/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001089QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001090 unsigned NumArgs, bool isVariadic,
1091 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001092 // Unique functions, to guarantee there is only one function of a particular
1093 // structure.
1094 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001095 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1096 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001097
1098 void *InsertPos = 0;
1099 if (FunctionTypeProto *FTP =
1100 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1101 return QualType(FTP, 0);
1102
1103 // Determine whether the type being created is already canonical or not.
1104 bool isCanonical = ResultTy->isCanonical();
1105 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1106 if (!ArgArray[i]->isCanonical())
1107 isCanonical = false;
1108
1109 // If this type isn't canonical, get the canonical version of it.
1110 QualType Canonical;
1111 if (!isCanonical) {
1112 llvm::SmallVector<QualType, 16> CanonicalArgs;
1113 CanonicalArgs.reserve(NumArgs);
1114 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001115 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001116
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001117 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001118 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001119 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001120
1121 // Get the new insert position for the node we care about.
1122 FunctionTypeProto *NewIP =
1123 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001124 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001125 }
1126
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001127 // FunctionTypeProto objects are allocated with extra bytes after them
1128 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001129 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001130 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1131 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001132 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001133 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001134 Types.push_back(FTP);
1135 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1136 return QualType(FTP, 0);
1137}
1138
Douglas Gregor1d661552008-04-13 21:07:44 +00001139/// getTypeDeclType - Return the unique reference to the type for the
1140/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001141QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001142 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001143 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1144
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001145 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001146 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001147 else if (isa<TemplateTypeParmDecl>(Decl)) {
1148 assert(false && "Template type parameter types are always available.");
1149 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001150 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001151
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001152 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001153 if (PrevDecl)
1154 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001155 else
1156 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001157 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001158 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001159 if (PrevDecl)
1160 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001161 else
1162 Decl->TypeForDecl = new (*this,8) RecordType(Record);
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;
Steve Naroff93fd2112009-01-27 22:08:43 +00001167 else
1168 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001169 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001170 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001171 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001172
Ted Kremenek46a837c2008-09-05 17:16:31 +00001173 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001174 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001175}
1176
Chris Lattner4b009652007-07-25 00:24:17 +00001177/// getTypedefType - Return the unique reference to the type for the
1178/// specified typename decl.
1179QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1180 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1181
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001182 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001183 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001184 Types.push_back(Decl->TypeForDecl);
1185 return QualType(Decl->TypeForDecl, 0);
1186}
1187
Ted Kremenek42730c52008-01-07 19:49:32 +00001188/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001189/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001190QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001191 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1192
Steve Naroff93fd2112009-01-27 22:08:43 +00001193 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001194 Types.push_back(Decl->TypeForDecl);
1195 return QualType(Decl->TypeForDecl, 0);
1196}
1197
Douglas Gregora4918772009-02-05 23:33:38 +00001198/// \brief Retrieve the template type parameter type for a template
1199/// parameter with the given depth, index, and (optionally) name.
1200QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1201 IdentifierInfo *Name) {
1202 llvm::FoldingSetNodeID ID;
1203 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1204 void *InsertPos = 0;
1205 TemplateTypeParmType *TypeParm
1206 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1207
1208 if (TypeParm)
1209 return QualType(TypeParm, 0);
1210
1211 if (Name)
1212 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1213 getTemplateTypeParmType(Depth, Index));
1214 else
1215 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1216
1217 Types.push_back(TypeParm);
1218 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1219
1220 return QualType(TypeParm, 0);
1221}
1222
Douglas Gregor8e458f42009-02-09 18:46:07 +00001223QualType
1224ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1225 unsigned NumArgs,
1226 uintptr_t *Args, bool *ArgIsType,
1227 QualType Canon) {
1228 llvm::FoldingSetNodeID ID;
1229 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1230 ArgIsType);
1231 void *InsertPos = 0;
1232 ClassTemplateSpecializationType *Spec
1233 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1234
1235 if (Spec)
1236 return QualType(Spec, 0);
1237
1238 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1239 (sizeof(uintptr_t) *
1240 (ClassTemplateSpecializationType::
1241 getNumPackedWords(NumArgs) +
1242 NumArgs)), 8);
1243 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1244 ArgIsType, Canon);
1245 Types.push_back(Spec);
1246 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1247
1248 return QualType(Spec, 0);
1249}
1250
Chris Lattnere1352302008-04-07 04:56:42 +00001251/// CmpProtocolNames - Comparison predicate for sorting protocols
1252/// alphabetically.
1253static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1254 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001255 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001256}
1257
1258static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1259 unsigned &NumProtocols) {
1260 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1261
1262 // Sort protocols, keyed by name.
1263 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1264
1265 // Remove duplicates.
1266 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1267 NumProtocols = ProtocolsEnd-Protocols;
1268}
1269
1270
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001271/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1272/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001273QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1274 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001275 // Sort the protocol list alphabetically to canonicalize it.
1276 SortAndUniqueProtocols(Protocols, NumProtocols);
1277
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001278 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001279 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001280
1281 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001282 if (ObjCQualifiedInterfaceType *QT =
1283 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001284 return QualType(QT, 0);
1285
1286 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001287 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001288 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001289
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001290 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001291 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001292 return QualType(QType, 0);
1293}
1294
Chris Lattnere1352302008-04-07 04:56:42 +00001295/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1296/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001297QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001298 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001299 // Sort the protocol list alphabetically to canonicalize it.
1300 SortAndUniqueProtocols(Protocols, NumProtocols);
1301
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001302 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001303 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001304
1305 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001307 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001308 return QualType(QT, 0);
1309
1310 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001311 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001312 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001313 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001315 return QualType(QType, 0);
1316}
1317
Steve Naroff0604dd92007-08-01 18:02:17 +00001318/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1319/// TypeOfExpr AST's (since expression's are never shared). For example,
1320/// multiple declarations that refer to "typeof(x)" all contain different
1321/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1322/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001323QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001324 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001325 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001326 Types.push_back(toe);
1327 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001328}
1329
Steve Naroff0604dd92007-08-01 18:02:17 +00001330/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1331/// TypeOfType AST's. The only motivation to unique these nodes would be
1332/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1333/// an issue. This doesn't effect the type checker, since it operates
1334/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001335QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001336 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001337 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001338 Types.push_back(tot);
1339 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001340}
1341
Chris Lattner4b009652007-07-25 00:24:17 +00001342/// getTagDeclType - Return the unique reference to the type for the
1343/// specified TagDecl (struct/union/class/enum) decl.
1344QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001345 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001346 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001347}
1348
1349/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1350/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1351/// needs to agree with the definition in <stddef.h>.
1352QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001353 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001354}
1355
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001356/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001357/// width of characters in wide strings, The value is target dependent and
1358/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001359QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001360 if (LangOpts.CPlusPlus)
1361 return WCharTy;
1362
Douglas Gregorc6507e42008-11-03 14:12:49 +00001363 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1364 // wide-character type?
1365 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001366}
1367
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001368/// getSignedWCharType - Return the type of "signed wchar_t".
1369/// Used when in C++, as a GCC extension.
1370QualType ASTContext::getSignedWCharType() const {
1371 // FIXME: derive from "Target" ?
1372 return WCharTy;
1373}
1374
1375/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1376/// Used when in C++, as a GCC extension.
1377QualType ASTContext::getUnsignedWCharType() const {
1378 // FIXME: derive from "Target" ?
1379 return UnsignedIntTy;
1380}
1381
Chris Lattner4b009652007-07-25 00:24:17 +00001382/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1383/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1384QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001385 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001386}
1387
Chris Lattner19eb97e2008-04-02 05:18:44 +00001388//===----------------------------------------------------------------------===//
1389// Type Operators
1390//===----------------------------------------------------------------------===//
1391
Chris Lattner3dae6f42008-04-06 22:41:35 +00001392/// getCanonicalType - Return the canonical (structural) type corresponding to
1393/// the specified potentially non-canonical type. The non-canonical version
1394/// of a type may have many "decorated" versions of types. Decorators can
1395/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1396/// to be free of any of these, allowing two canonical types to be compared
1397/// for exact equality with a simple pointer comparison.
1398QualType ASTContext::getCanonicalType(QualType T) {
1399 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001400
1401 // If the result has type qualifiers, make sure to canonicalize them as well.
1402 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1403 if (TypeQuals == 0) return CanType;
1404
1405 // If the type qualifiers are on an array type, get the canonical type of the
1406 // array with the qualifiers applied to the element type.
1407 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1408 if (!AT)
1409 return CanType.getQualifiedType(TypeQuals);
1410
1411 // Get the canonical version of the element with the extra qualifiers on it.
1412 // This can recursively sink qualifiers through multiple levels of arrays.
1413 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1414 NewEltTy = getCanonicalType(NewEltTy);
1415
1416 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1417 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1418 CAT->getIndexTypeQualifier());
1419 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1420 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1421 IAT->getIndexTypeQualifier());
1422
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001423 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1424 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1425 DSAT->getSizeModifier(),
1426 DSAT->getIndexTypeQualifier());
1427
Chris Lattnera1923f62008-08-04 07:31:14 +00001428 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1429 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1430 VAT->getSizeModifier(),
1431 VAT->getIndexTypeQualifier());
1432}
1433
1434
1435const ArrayType *ASTContext::getAsArrayType(QualType T) {
1436 // Handle the non-qualified case efficiently.
1437 if (T.getCVRQualifiers() == 0) {
1438 // Handle the common positive case fast.
1439 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1440 return AT;
1441 }
1442
1443 // Handle the common negative case fast, ignoring CVR qualifiers.
1444 QualType CType = T->getCanonicalTypeInternal();
1445
1446 // Make sure to look through type qualifiers (like ASQuals) for the negative
1447 // test.
1448 if (!isa<ArrayType>(CType) &&
1449 !isa<ArrayType>(CType.getUnqualifiedType()))
1450 return 0;
1451
1452 // Apply any CVR qualifiers from the array type to the element type. This
1453 // implements C99 6.7.3p8: "If the specification of an array type includes
1454 // any type qualifiers, the element type is so qualified, not the array type."
1455
1456 // If we get here, we either have type qualifiers on the type, or we have
1457 // sugar such as a typedef in the way. If we have type qualifiers on the type
1458 // we must propagate them down into the elemeng type.
1459 unsigned CVRQuals = T.getCVRQualifiers();
1460 unsigned AddrSpace = 0;
1461 Type *Ty = T.getTypePtr();
1462
1463 // Rip through ASQualType's and typedefs to get to a concrete type.
1464 while (1) {
1465 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1466 AddrSpace = ASQT->getAddressSpace();
1467 Ty = ASQT->getBaseType();
1468 } else {
1469 T = Ty->getDesugaredType();
1470 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1471 break;
1472 CVRQuals |= T.getCVRQualifiers();
1473 Ty = T.getTypePtr();
1474 }
1475 }
1476
1477 // If we have a simple case, just return now.
1478 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1479 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1480 return ATy;
1481
1482 // Otherwise, we have an array and we have qualifiers on it. Push the
1483 // qualifiers into the array element type and return a new array type.
1484 // Get the canonical version of the element with the extra qualifiers on it.
1485 // This can recursively sink qualifiers through multiple levels of arrays.
1486 QualType NewEltTy = ATy->getElementType();
1487 if (AddrSpace)
1488 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1489 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1490
1491 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1492 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1493 CAT->getSizeModifier(),
1494 CAT->getIndexTypeQualifier()));
1495 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1496 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1497 IAT->getSizeModifier(),
1498 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001499
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001500 if (const DependentSizedArrayType *DSAT
1501 = dyn_cast<DependentSizedArrayType>(ATy))
1502 return cast<ArrayType>(
1503 getDependentSizedArrayType(NewEltTy,
1504 DSAT->getSizeExpr(),
1505 DSAT->getSizeModifier(),
1506 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001507
Chris Lattnera1923f62008-08-04 07:31:14 +00001508 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1509 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1510 VAT->getSizeModifier(),
1511 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001512}
1513
1514
Chris Lattner19eb97e2008-04-02 05:18:44 +00001515/// getArrayDecayedType - Return the properly qualified result of decaying the
1516/// specified array type to a pointer. This operation is non-trivial when
1517/// handling typedefs etc. The canonical type of "T" must be an array type,
1518/// this returns a pointer to a properly qualified element of the array.
1519///
1520/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1521QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001522 // Get the element type with 'getAsArrayType' so that we don't lose any
1523 // typedefs in the element type of the array. This also handles propagation
1524 // of type qualifiers from the array type into the element type if present
1525 // (C99 6.7.3p8).
1526 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1527 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001528
Chris Lattnera1923f62008-08-04 07:31:14 +00001529 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001530
1531 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001532 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001533}
1534
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001535QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001536 QualType ElemTy = VAT->getElementType();
1537
1538 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1539 return getBaseElementType(VAT);
1540
1541 return ElemTy;
1542}
1543
Chris Lattner4b009652007-07-25 00:24:17 +00001544/// getFloatingRank - Return a relative rank for floating point types.
1545/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001546static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001547 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001548 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001549
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001550 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001551 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001552 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001553 case BuiltinType::Float: return FloatRank;
1554 case BuiltinType::Double: return DoubleRank;
1555 case BuiltinType::LongDouble: return LongDoubleRank;
1556 }
1557}
1558
Steve Narofffa0c4532007-08-27 01:41:48 +00001559/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1560/// point or a complex type (based on typeDomain/typeSize).
1561/// 'typeDomain' is a real floating point or complex type.
1562/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001563QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1564 QualType Domain) const {
1565 FloatingRank EltRank = getFloatingRank(Size);
1566 if (Domain->isComplexType()) {
1567 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001568 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001569 case FloatRank: return FloatComplexTy;
1570 case DoubleRank: return DoubleComplexTy;
1571 case LongDoubleRank: return LongDoubleComplexTy;
1572 }
Chris Lattner4b009652007-07-25 00:24:17 +00001573 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001574
1575 assert(Domain->isRealFloatingType() && "Unknown domain!");
1576 switch (EltRank) {
1577 default: assert(0 && "getFloatingRank(): illegal value for rank");
1578 case FloatRank: return FloatTy;
1579 case DoubleRank: return DoubleTy;
1580 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001581 }
Chris Lattner4b009652007-07-25 00:24:17 +00001582}
1583
Chris Lattner51285d82008-04-06 23:55:33 +00001584/// getFloatingTypeOrder - Compare the rank of the two specified floating
1585/// point types, ignoring the domain of the type (i.e. 'double' ==
1586/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1587/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001588int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1589 FloatingRank LHSR = getFloatingRank(LHS);
1590 FloatingRank RHSR = getFloatingRank(RHS);
1591
1592 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001593 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001594 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001595 return 1;
1596 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001597}
1598
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001599/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1600/// routine will assert if passed a built-in type that isn't an integer or enum,
1601/// or if it is not canonicalized.
1602static unsigned getIntegerRank(Type *T) {
1603 assert(T->isCanonical() && "T should be canonicalized");
1604 if (isa<EnumType>(T))
1605 return 4;
1606
1607 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001608 default: assert(0 && "getIntegerRank(): not a built-in integer");
1609 case BuiltinType::Bool:
1610 return 1;
1611 case BuiltinType::Char_S:
1612 case BuiltinType::Char_U:
1613 case BuiltinType::SChar:
1614 case BuiltinType::UChar:
1615 return 2;
1616 case BuiltinType::Short:
1617 case BuiltinType::UShort:
1618 return 3;
1619 case BuiltinType::Int:
1620 case BuiltinType::UInt:
1621 return 4;
1622 case BuiltinType::Long:
1623 case BuiltinType::ULong:
1624 return 5;
1625 case BuiltinType::LongLong:
1626 case BuiltinType::ULongLong:
1627 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001628 }
1629}
1630
Chris Lattner51285d82008-04-06 23:55:33 +00001631/// getIntegerTypeOrder - Returns the highest ranked integer type:
1632/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1633/// LHS < RHS, return -1.
1634int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001635 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1636 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001637 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001638
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001639 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1640 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001641
Chris Lattner51285d82008-04-06 23:55:33 +00001642 unsigned LHSRank = getIntegerRank(LHSC);
1643 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001644
Chris Lattner51285d82008-04-06 23:55:33 +00001645 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1646 if (LHSRank == RHSRank) return 0;
1647 return LHSRank > RHSRank ? 1 : -1;
1648 }
Chris Lattner4b009652007-07-25 00:24:17 +00001649
Chris Lattner51285d82008-04-06 23:55:33 +00001650 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1651 if (LHSUnsigned) {
1652 // If the unsigned [LHS] type is larger, return it.
1653 if (LHSRank >= RHSRank)
1654 return 1;
1655
1656 // If the signed type can represent all values of the unsigned type, it
1657 // wins. Because we are dealing with 2's complement and types that are
1658 // powers of two larger than each other, this is always safe.
1659 return -1;
1660 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001661
Chris Lattner51285d82008-04-06 23:55:33 +00001662 // If the unsigned [RHS] type is larger, return it.
1663 if (RHSRank >= LHSRank)
1664 return -1;
1665
1666 // If the signed type can represent all values of the unsigned type, it
1667 // wins. Because we are dealing with 2's complement and types that are
1668 // powers of two larger than each other, this is always safe.
1669 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001670}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001671
1672// getCFConstantStringType - Return the type used for constant CFStrings.
1673QualType ASTContext::getCFConstantStringType() {
1674 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001675 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001676 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001677 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001678 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001679
1680 // const int *isa;
1681 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001682 // int flags;
1683 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001684 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001685 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001686 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001687 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001688
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001689 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001690 for (unsigned i = 0; i < 4; ++i) {
1691 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1692 SourceLocation(), 0,
1693 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001694 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001695 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001696 }
1697
1698 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001699 }
1700
1701 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001702}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001703
Anders Carlssonf58cac72008-08-30 19:34:46 +00001704QualType ASTContext::getObjCFastEnumerationStateType()
1705{
1706 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001707 ObjCFastEnumerationStateTypeDecl =
1708 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1709 &Idents.get("__objcFastEnumerationState"));
1710
Anders Carlssonf58cac72008-08-30 19:34:46 +00001711 QualType FieldTypes[] = {
1712 UnsignedLongTy,
1713 getPointerType(ObjCIdType),
1714 getPointerType(UnsignedLongTy),
1715 getConstantArrayType(UnsignedLongTy,
1716 llvm::APInt(32, 5), ArrayType::Normal, 0)
1717 };
1718
Douglas Gregor8acb7272008-12-11 16:49:14 +00001719 for (size_t i = 0; i < 4; ++i) {
1720 FieldDecl *Field = FieldDecl::Create(*this,
1721 ObjCFastEnumerationStateTypeDecl,
1722 SourceLocation(), 0,
1723 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001724 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001725 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001726 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001727
Douglas Gregor8acb7272008-12-11 16:49:14 +00001728 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001729 }
1730
1731 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1732}
1733
Anders Carlssone3f02572007-10-29 06:33:42 +00001734// This returns true if a type has been typedefed to BOOL:
1735// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001736static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001737 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001738 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1739 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001740
1741 return false;
1742}
1743
Ted Kremenek42730c52008-01-07 19:49:32 +00001744/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001745/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001746int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001747 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001748
1749 // Make all integer and enum types at least as large as an int
1750 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001751 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001752 // Treat arrays as pointers, since that's how they're passed in.
1753 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001754 sz = getTypeSize(VoidPtrTy);
1755 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001756}
1757
Ted Kremenek42730c52008-01-07 19:49:32 +00001758/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001759/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001760void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001761 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001762 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001763 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001764 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001765 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001766 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001767 // Compute size of all parameters.
1768 // Start with computing size of a pointer in number of bytes.
1769 // FIXME: There might(should) be a better way of doing this computation!
1770 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001771 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001772 // The first two arguments (self and _cmd) are pointers; account for
1773 // their size.
1774 int ParmOffset = 2 * PtrSize;
1775 int NumOfParams = Decl->getNumParams();
1776 for (int i = 0; i < NumOfParams; i++) {
1777 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001778 int sz = getObjCEncodingTypeSize (PType);
1779 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001780 ParmOffset += sz;
1781 }
1782 S += llvm::utostr(ParmOffset);
1783 S += "@0:";
1784 S += llvm::utostr(PtrSize);
1785
1786 // Argument types.
1787 ParmOffset = 2 * PtrSize;
1788 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001789 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1790 QualType PType = PVDecl->getOriginalType();
1791 if (const ArrayType *AT =
1792 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1793 // Use array's original type only if it has known number of
1794 // elements.
1795 if (!dyn_cast<ConstantArrayType>(AT))
1796 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001797 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001798 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001799 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001800 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001801 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001802 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001803 }
1804}
1805
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001806/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001807/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001808/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1809/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001810/// Property attributes are stored as a comma-delimited C string. The simple
1811/// attributes readonly and bycopy are encoded as single characters. The
1812/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1813/// encoded as single characters, followed by an identifier. Property types
1814/// are also encoded as a parametrized attribute. The characters used to encode
1815/// these attributes are defined by the following enumeration:
1816/// @code
1817/// enum PropertyAttributes {
1818/// kPropertyReadOnly = 'R', // property is read-only.
1819/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1820/// kPropertyByref = '&', // property is a reference to the value last assigned
1821/// kPropertyDynamic = 'D', // property is dynamic
1822/// kPropertyGetter = 'G', // followed by getter selector name
1823/// kPropertySetter = 'S', // followed by setter selector name
1824/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1825/// kPropertyType = 't' // followed by old-style type encoding.
1826/// kPropertyWeak = 'W' // 'weak' property
1827/// kPropertyStrong = 'P' // property GC'able
1828/// kPropertyNonAtomic = 'N' // property non-atomic
1829/// };
1830/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001831void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1832 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001833 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001834 // Collect information from the property implementation decl(s).
1835 bool Dynamic = false;
1836 ObjCPropertyImplDecl *SynthesizePID = 0;
1837
1838 // FIXME: Duplicated code due to poor abstraction.
1839 if (Container) {
1840 if (const ObjCCategoryImplDecl *CID =
1841 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1842 for (ObjCCategoryImplDecl::propimpl_iterator
1843 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1844 ObjCPropertyImplDecl *PID = *i;
1845 if (PID->getPropertyDecl() == PD) {
1846 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1847 Dynamic = true;
1848 } else {
1849 SynthesizePID = PID;
1850 }
1851 }
1852 }
1853 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001854 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001855 for (ObjCCategoryImplDecl::propimpl_iterator
1856 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1857 ObjCPropertyImplDecl *PID = *i;
1858 if (PID->getPropertyDecl() == PD) {
1859 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1860 Dynamic = true;
1861 } else {
1862 SynthesizePID = PID;
1863 }
1864 }
1865 }
1866 }
1867 }
1868
1869 // FIXME: This is not very efficient.
1870 S = "T";
1871
1872 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001873 // GCC has some special rules regarding encoding of properties which
1874 // closely resembles encoding of ivars.
1875 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1876 true /* outermost type */,
1877 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001878
1879 if (PD->isReadOnly()) {
1880 S += ",R";
1881 } else {
1882 switch (PD->getSetterKind()) {
1883 case ObjCPropertyDecl::Assign: break;
1884 case ObjCPropertyDecl::Copy: S += ",C"; break;
1885 case ObjCPropertyDecl::Retain: S += ",&"; break;
1886 }
1887 }
1888
1889 // It really isn't clear at all what this means, since properties
1890 // are "dynamic by default".
1891 if (Dynamic)
1892 S += ",D";
1893
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001894 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1895 S += ",N";
1896
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001897 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1898 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001899 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001900 }
1901
1902 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1903 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001904 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001905 }
1906
1907 if (SynthesizePID) {
1908 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1909 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001910 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001911 }
1912
1913 // FIXME: OBJCGC: weak & strong
1914}
1915
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001916/// getLegacyIntegralTypeEncoding -
1917/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00001918/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001919/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1920///
1921void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1922 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1923 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00001924 if (BT->getKind() == BuiltinType::ULong &&
1925 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001926 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00001927 else
1928 if (BT->getKind() == BuiltinType::Long &&
1929 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001930 PointeeTy = IntTy;
1931 }
1932 }
1933}
1934
Fariborz Jahanian248db262008-01-22 22:44:46 +00001935void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001936 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001937 // We follow the behavior of gcc, expanding structures which are
1938 // directly pointed to, and expanding embedded structures. Note that
1939 // these rules are sufficient to prevent recursive encoding of the
1940 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001941 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1942 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001943}
1944
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001945static void EncodeBitField(const ASTContext *Context, std::string& S,
1946 FieldDecl *FD) {
1947 const Expr *E = FD->getBitWidth();
1948 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1949 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1950 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1951 S += 'b';
1952 S += llvm::utostr(N);
1953}
1954
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001955void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1956 bool ExpandPointedToStructures,
1957 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001958 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001959 bool OutermostType,
1960 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001961 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001962 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001963 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001964 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001965 else {
1966 char encoding;
1967 switch (BT->getKind()) {
1968 default: assert(0 && "Unhandled builtin type kind");
1969 case BuiltinType::Void: encoding = 'v'; break;
1970 case BuiltinType::Bool: encoding = 'B'; break;
1971 case BuiltinType::Char_U:
1972 case BuiltinType::UChar: encoding = 'C'; break;
1973 case BuiltinType::UShort: encoding = 'S'; break;
1974 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00001975 case BuiltinType::ULong:
1976 encoding =
1977 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
1978 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001979 case BuiltinType::ULongLong: encoding = 'Q'; break;
1980 case BuiltinType::Char_S:
1981 case BuiltinType::SChar: encoding = 'c'; break;
1982 case BuiltinType::Short: encoding = 's'; break;
1983 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00001984 case BuiltinType::Long:
1985 encoding =
1986 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
1987 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001988 case BuiltinType::LongLong: encoding = 'q'; break;
1989 case BuiltinType::Float: encoding = 'f'; break;
1990 case BuiltinType::Double: encoding = 'd'; break;
1991 case BuiltinType::LongDouble: encoding = 'd'; break;
1992 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001993
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001994 S += encoding;
1995 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001996 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001997 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001998 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1999 ExpandPointedToStructures,
2000 ExpandStructures, FD);
2001 if (FD || EncodingProperty) {
2002 // Note that we do extended encoding of protocol qualifer list
2003 // Only when doing ivar or property encoding.
2004 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2005 S += '"';
2006 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2007 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2008 S += '<';
2009 S += Proto->getNameAsString();
2010 S += '>';
2011 }
2012 S += '"';
2013 }
2014 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002015 }
2016 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002017 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002018 bool isReadOnly = false;
2019 // For historical/compatibility reasons, the read-only qualifier of the
2020 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2021 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2022 // Also, do not emit the 'r' for anything but the outermost type!
2023 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2024 if (OutermostType && T.isConstQualified()) {
2025 isReadOnly = true;
2026 S += 'r';
2027 }
2028 }
2029 else if (OutermostType) {
2030 QualType P = PointeeTy;
2031 while (P->getAsPointerType())
2032 P = P->getAsPointerType()->getPointeeType();
2033 if (P.isConstQualified()) {
2034 isReadOnly = true;
2035 S += 'r';
2036 }
2037 }
2038 if (isReadOnly) {
2039 // Another legacy compatibility encoding. Some ObjC qualifier and type
2040 // combinations need to be rearranged.
2041 // Rewrite "in const" from "nr" to "rn"
2042 const char * s = S.c_str();
2043 int len = S.length();
2044 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2045 std::string replace = "rn";
2046 S.replace(S.end()-2, S.end(), replace);
2047 }
2048 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002049 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002050 S += '@';
2051 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002052 }
2053 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002054 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2055 // Another historical/compatibility reason.
2056 // We encode the underlying type which comes out as
2057 // {...};
2058 S += '^';
2059 getObjCEncodingForTypeImpl(PointeeTy, S,
2060 false, ExpandPointedToStructures,
2061 NULL);
2062 return;
2063 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002064 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002065 if (FD || EncodingProperty) {
2066 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2067 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002068 S += '"';
2069 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002070 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2071 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2072 S += '<';
2073 S += Proto->getNameAsString();
2074 S += '>';
2075 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002076 S += '"';
2077 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002078 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002079 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002080 S += '#';
2081 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002082 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002083 S += ':';
2084 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002085 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002086
2087 if (PointeeTy->isCharType()) {
2088 // char pointer types should be encoded as '*' unless it is a
2089 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002090 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002091 S += '*';
2092 return;
2093 }
2094 }
2095
2096 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002097 getLegacyIntegralTypeEncoding(PointeeTy);
2098
2099 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002100 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002101 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002102 } else if (const ArrayType *AT =
2103 // Ignore type qualifiers etc.
2104 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002105 S += '[';
2106
2107 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2108 S += llvm::utostr(CAT->getSize().getZExtValue());
2109 else
2110 assert(0 && "Unhandled array type!");
2111
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002112 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002113 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002114 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002115 } else if (T->getAsFunctionType()) {
2116 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002117 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002118 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002119 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002120 // Anonymous structures print as '?'
2121 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2122 S += II->getName();
2123 } else {
2124 S += '?';
2125 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002126 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002127 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002128 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2129 FieldEnd = RDecl->field_end();
2130 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002131 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002132 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002133 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002134 S += '"';
2135 }
2136
2137 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002138 if (Field->isBitField()) {
2139 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2140 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002141 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002142 QualType qt = Field->getType();
2143 getLegacyIntegralTypeEncoding(qt);
2144 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002145 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002146 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002147 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002148 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002149 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002150 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002151 if (FD && FD->isBitField())
2152 EncodeBitField(this, S, FD);
2153 else
2154 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002155 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002156 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002157 } else if (T->isObjCInterfaceType()) {
2158 // @encode(class_name)
2159 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2160 S += '{';
2161 const IdentifierInfo *II = OI->getIdentifier();
2162 S += II->getName();
2163 S += '=';
2164 std::vector<FieldDecl*> RecFields;
2165 CollectObjCIvars(OI, RecFields);
2166 for (unsigned int i = 0; i != RecFields.size(); i++) {
2167 if (RecFields[i]->isBitField())
2168 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2169 RecFields[i]);
2170 else
2171 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2172 FD);
2173 }
2174 S += '}';
2175 }
2176 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002177 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002178}
2179
Ted Kremenek42730c52008-01-07 19:49:32 +00002180void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002181 std::string& S) const {
2182 if (QT & Decl::OBJC_TQ_In)
2183 S += 'n';
2184 if (QT & Decl::OBJC_TQ_Inout)
2185 S += 'N';
2186 if (QT & Decl::OBJC_TQ_Out)
2187 S += 'o';
2188 if (QT & Decl::OBJC_TQ_Bycopy)
2189 S += 'O';
2190 if (QT & Decl::OBJC_TQ_Byref)
2191 S += 'R';
2192 if (QT & Decl::OBJC_TQ_Oneway)
2193 S += 'V';
2194}
2195
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002196void ASTContext::setBuiltinVaListType(QualType T)
2197{
2198 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2199
2200 BuiltinVaListType = T;
2201}
2202
Ted Kremenek42730c52008-01-07 19:49:32 +00002203void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002204{
Ted Kremenek42730c52008-01-07 19:49:32 +00002205 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002206
2207 // typedef struct objc_object *id;
2208 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002209 // User error - caller will issue diagnostics.
2210 if (!ptr)
2211 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002212 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002213 // User error - caller will issue diagnostics.
2214 if (!rec)
2215 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002216 IdStructType = rec;
2217}
2218
Ted Kremenek42730c52008-01-07 19:49:32 +00002219void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002220{
Ted Kremenek42730c52008-01-07 19:49:32 +00002221 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002222
2223 // typedef struct objc_selector *SEL;
2224 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002225 if (!ptr)
2226 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002227 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002228 if (!rec)
2229 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002230 SelStructType = rec;
2231}
2232
Ted Kremenek42730c52008-01-07 19:49:32 +00002233void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002234{
Ted Kremenek42730c52008-01-07 19:49:32 +00002235 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002236}
2237
Ted Kremenek42730c52008-01-07 19:49:32 +00002238void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002239{
Ted Kremenek42730c52008-01-07 19:49:32 +00002240 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002241
2242 // typedef struct objc_class *Class;
2243 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2244 assert(ptr && "'Class' incorrectly typed");
2245 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2246 assert(rec && "'Class' incorrectly typed");
2247 ClassStructType = rec;
2248}
2249
Ted Kremenek42730c52008-01-07 19:49:32 +00002250void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2251 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002252 "'NSConstantString' type already set!");
2253
Ted Kremenek42730c52008-01-07 19:49:32 +00002254 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002255}
2256
Douglas Gregorc6507e42008-11-03 14:12:49 +00002257/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002258/// TargetInfo, produce the corresponding type. The unsigned @p Type
2259/// is actually a value of type @c TargetInfo::IntType.
2260QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002261 switch (Type) {
2262 case TargetInfo::NoInt: return QualType();
2263 case TargetInfo::SignedShort: return ShortTy;
2264 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2265 case TargetInfo::SignedInt: return IntTy;
2266 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2267 case TargetInfo::SignedLong: return LongTy;
2268 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2269 case TargetInfo::SignedLongLong: return LongLongTy;
2270 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2271 }
2272
2273 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002274 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002275}
Ted Kremenek118930e2008-07-24 23:58:27 +00002276
2277//===----------------------------------------------------------------------===//
2278// Type Predicates.
2279//===----------------------------------------------------------------------===//
2280
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002281/// isObjCNSObjectType - Return true if this is an NSObject object using
2282/// NSObject attribute on a c-style pointer type.
2283/// FIXME - Make it work directly on types.
2284///
2285bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2286 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2287 if (TypedefDecl *TD = TDT->getDecl())
2288 if (TD->getAttr<ObjCNSObjectAttr>())
2289 return true;
2290 }
2291 return false;
2292}
2293
Ted Kremenek118930e2008-07-24 23:58:27 +00002294/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2295/// to an object type. This includes "id" and "Class" (two 'special' pointers
2296/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2297/// ID type).
2298bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2299 if (Ty->isObjCQualifiedIdType())
2300 return true;
2301
Steve Naroffd9e00802008-10-21 18:24:04 +00002302 // Blocks are objects.
2303 if (Ty->isBlockPointerType())
2304 return true;
2305
2306 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002307 if (!Ty->isPointerType())
2308 return false;
2309
2310 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2311 // pointer types. This looks for the typedef specifically, not for the
2312 // underlying type.
2313 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2314 return true;
2315
2316 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002317 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2318 return true;
2319
2320 // If is has NSObject attribute, OK as well.
2321 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002322}
2323
Chris Lattner6ff358b2008-04-07 06:51:04 +00002324//===----------------------------------------------------------------------===//
2325// Type Compatibility Testing
2326//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002327
Steve Naroff3454b6c2008-09-04 15:10:53 +00002328/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002329/// block types. Types must be strictly compatible here. For example,
2330/// C unfortunately doesn't produce an error for the following:
2331///
2332/// int (*emptyArgFunc)();
2333/// int (*intArgList)(int) = emptyArgFunc;
2334///
2335/// For blocks, we will produce an error for the following (similar to C++):
2336///
2337/// int (^emptyArgBlock)();
2338/// int (^intArgBlock)(int) = emptyArgBlock;
2339///
2340/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2341///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002342bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002343 const FunctionType *lbase = lhs->getAsFunctionType();
2344 const FunctionType *rbase = rhs->getAsFunctionType();
2345 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2346 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2347 if (lproto && rproto)
2348 return !mergeTypes(lhs, rhs).isNull();
2349 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002350}
2351
Chris Lattner6ff358b2008-04-07 06:51:04 +00002352/// areCompatVectorTypes - Return true if the two specified vector types are
2353/// compatible.
2354static bool areCompatVectorTypes(const VectorType *LHS,
2355 const VectorType *RHS) {
2356 assert(LHS->isCanonical() && RHS->isCanonical());
2357 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002358 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002359}
2360
Eli Friedman0d9549b2008-08-22 00:56:42 +00002361/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002362/// compatible for assignment from RHS to LHS. This handles validation of any
2363/// protocol qualifiers on the LHS or RHS.
2364///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002365bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2366 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002367 // Verify that the base decls are compatible: the RHS must be a subclass of
2368 // the LHS.
2369 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2370 return false;
2371
2372 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2373 // protocol qualified at all, then we are good.
2374 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2375 return true;
2376
2377 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2378 // isn't a superset.
2379 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2380 return true; // FIXME: should return false!
2381
2382 // Finally, we must have two protocol-qualified interfaces.
2383 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2384 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2385 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2386 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2387 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2388 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2389
2390 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2391 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2392 // LHS in a single parallel scan until we run out of elements in LHS.
2393 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2394 ObjCProtocolDecl *LHSProto = *LHSPI;
2395
2396 while (RHSPI != RHSPE) {
2397 ObjCProtocolDecl *RHSProto = *RHSPI++;
2398 // If the RHS has a protocol that the LHS doesn't, ignore it.
2399 if (RHSProto != LHSProto)
2400 continue;
2401
2402 // Otherwise, the RHS does have this element.
2403 ++LHSPI;
2404 if (LHSPI == LHSPE)
2405 return true; // All protocols in LHS exist in RHS.
2406
2407 LHSProto = *LHSPI;
2408 }
2409
2410 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2411 return false;
2412}
2413
Steve Naroff85f0dc52007-10-15 20:41:53 +00002414/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2415/// both shall have the identically qualified version of a compatible type.
2416/// C99 6.2.7p1: Two types have compatible types if their types are the
2417/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002418bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2419 return !mergeTypes(LHS, RHS).isNull();
2420}
2421
2422QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2423 const FunctionType *lbase = lhs->getAsFunctionType();
2424 const FunctionType *rbase = rhs->getAsFunctionType();
2425 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2426 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2427 bool allLTypes = true;
2428 bool allRTypes = true;
2429
2430 // Check return type
2431 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2432 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002433 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2434 allLTypes = false;
2435 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2436 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002437
2438 if (lproto && rproto) { // two C99 style function prototypes
2439 unsigned lproto_nargs = lproto->getNumArgs();
2440 unsigned rproto_nargs = rproto->getNumArgs();
2441
2442 // Compatible functions must have the same number of arguments
2443 if (lproto_nargs != rproto_nargs)
2444 return QualType();
2445
2446 // Variadic and non-variadic functions aren't compatible
2447 if (lproto->isVariadic() != rproto->isVariadic())
2448 return QualType();
2449
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002450 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2451 return QualType();
2452
Eli Friedman0d9549b2008-08-22 00:56:42 +00002453 // Check argument compatibility
2454 llvm::SmallVector<QualType, 10> types;
2455 for (unsigned i = 0; i < lproto_nargs; i++) {
2456 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2457 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2458 QualType argtype = mergeTypes(largtype, rargtype);
2459 if (argtype.isNull()) return QualType();
2460 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002461 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2462 allLTypes = false;
2463 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2464 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002465 }
2466 if (allLTypes) return lhs;
2467 if (allRTypes) return rhs;
2468 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002469 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002470 }
2471
2472 if (lproto) allRTypes = false;
2473 if (rproto) allLTypes = false;
2474
2475 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2476 if (proto) {
2477 if (proto->isVariadic()) return QualType();
2478 // Check that the types are compatible with the types that
2479 // would result from default argument promotions (C99 6.7.5.3p15).
2480 // The only types actually affected are promotable integer
2481 // types and floats, which would be passed as a different
2482 // type depending on whether the prototype is visible.
2483 unsigned proto_nargs = proto->getNumArgs();
2484 for (unsigned i = 0; i < proto_nargs; ++i) {
2485 QualType argTy = proto->getArgType(i);
2486 if (argTy->isPromotableIntegerType() ||
2487 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2488 return QualType();
2489 }
2490
2491 if (allLTypes) return lhs;
2492 if (allRTypes) return rhs;
2493 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002494 proto->getNumArgs(), lproto->isVariadic(),
2495 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002496 }
2497
2498 if (allLTypes) return lhs;
2499 if (allRTypes) return rhs;
2500 return getFunctionTypeNoProto(retType);
2501}
2502
2503QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002504 // C++ [expr]: If an expression initially has the type "reference to T", the
2505 // type is adjusted to "T" prior to any further analysis, the expression
2506 // designates the object or function denoted by the reference, and the
2507 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002508 // FIXME: C++ shouldn't be going through here! The rules are different
2509 // enough that they should be handled separately.
2510 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002511 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002512 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002513 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002514
Eli Friedman0d9549b2008-08-22 00:56:42 +00002515 QualType LHSCan = getCanonicalType(LHS),
2516 RHSCan = getCanonicalType(RHS);
2517
2518 // If two types are identical, they are compatible.
2519 if (LHSCan == RHSCan)
2520 return LHS;
2521
2522 // If the qualifiers are different, the types aren't compatible
2523 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2524 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2525 return QualType();
2526
2527 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2528 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2529
Chris Lattnerc38d4522008-01-14 05:45:46 +00002530 // We want to consider the two function types to be the same for these
2531 // comparisons, just force one to the other.
2532 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2533 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002534
2535 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002536 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2537 LHSClass = Type::ConstantArray;
2538 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2539 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002540
Nate Begemanaf6ed502008-04-18 23:10:10 +00002541 // Canonicalize ExtVector -> Vector.
2542 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2543 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002544
Chris Lattner7cdcb252008-04-07 06:38:24 +00002545 // Consider qualified interfaces and interfaces the same.
2546 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2547 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002548
Chris Lattnerb5709e22008-04-07 05:43:21 +00002549 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002550 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002551 // ID is compatible with all qualified id types.
2552 if (LHS->isObjCQualifiedIdType()) {
2553 if (const PointerType *PT = RHS->getAsPointerType()) {
2554 QualType pType = PT->getPointeeType();
2555 if (isObjCIdType(pType))
2556 return LHS;
2557 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2558 // Unfortunately, this API is part of Sema (which we don't have access
2559 // to. Need to refactor. The following check is insufficient, since we
2560 // need to make sure the class implements the protocol.
2561 if (pType->isObjCInterfaceType())
2562 return LHS;
2563 }
2564 }
2565 if (RHS->isObjCQualifiedIdType()) {
2566 if (const PointerType *PT = LHS->getAsPointerType()) {
2567 QualType pType = PT->getPointeeType();
2568 if (isObjCIdType(pType))
2569 return RHS;
2570 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2571 // Unfortunately, this API is part of Sema (which we don't have access
2572 // to. Need to refactor. The following check is insufficient, since we
2573 // need to make sure the class implements the protocol.
2574 if (pType->isObjCInterfaceType())
2575 return RHS;
2576 }
2577 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002578 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2579 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002580 if (const EnumType* ETy = LHS->getAsEnumType()) {
2581 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2582 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002583 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002584 if (const EnumType* ETy = RHS->getAsEnumType()) {
2585 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2586 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002587 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002588
Eli Friedman0d9549b2008-08-22 00:56:42 +00002589 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002590 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002591
Steve Naroffc88babe2008-01-09 22:43:08 +00002592 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002593 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002594 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002595 {
2596 // Merge two pointer types, while trying to preserve typedef info
2597 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2598 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2599 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2600 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002601 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2602 return LHS;
2603 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2604 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002605 return getPointerType(ResultType);
2606 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002607 case Type::BlockPointer:
2608 {
2609 // Merge two block pointer types, while trying to preserve typedef info
2610 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2611 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2612 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2613 if (ResultType.isNull()) return QualType();
2614 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2615 return LHS;
2616 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2617 return RHS;
2618 return getBlockPointerType(ResultType);
2619 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002620 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002621 {
2622 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2623 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2624 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2625 return QualType();
2626
2627 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2628 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2629 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2630 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002631 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2632 return LHS;
2633 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2634 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002635 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2636 ArrayType::ArraySizeModifier(), 0);
2637 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2638 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002639 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2640 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002641 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2642 return LHS;
2643 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2644 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002645 if (LVAT) {
2646 // FIXME: This isn't correct! But tricky to implement because
2647 // the array's size has to be the size of LHS, but the type
2648 // has to be different.
2649 return LHS;
2650 }
2651 if (RVAT) {
2652 // FIXME: This isn't correct! But tricky to implement because
2653 // the array's size has to be the size of RHS, but the type
2654 // has to be different.
2655 return RHS;
2656 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002657 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2658 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002659 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002660 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002661 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002662 return mergeFunctionTypes(LHS, RHS);
2663 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002664 // FIXME: Why are these compatible?
2665 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2666 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2667 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002668 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002669 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002670 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002671 case Type::Complex:
2672 // Distinct complex types are incompatible.
2673 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002674 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002675 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2676 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002677 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002678 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002679 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2680 // for checking assignment/comparison safety
2681 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002682 case Type::ObjCQualifiedId:
2683 // Distinct qualified id's are not compatible.
2684 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002685 default:
2686 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002687 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002688 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002689}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002690
Chris Lattner1d78a862008-04-07 07:01:58 +00002691//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002692// Integer Predicates
2693//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002694
Eli Friedman0832dbc2008-06-28 06:23:08 +00002695unsigned ASTContext::getIntWidth(QualType T) {
2696 if (T == BoolTy)
2697 return 1;
2698 // At the moment, only bool has padding bits
2699 return (unsigned)getTypeSize(T);
2700}
2701
2702QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2703 assert(T->isSignedIntegerType() && "Unexpected type");
2704 if (const EnumType* ETy = T->getAsEnumType())
2705 T = ETy->getDecl()->getIntegerType();
2706 const BuiltinType* BTy = T->getAsBuiltinType();
2707 assert (BTy && "Unexpected signed integer type");
2708 switch (BTy->getKind()) {
2709 case BuiltinType::Char_S:
2710 case BuiltinType::SChar:
2711 return UnsignedCharTy;
2712 case BuiltinType::Short:
2713 return UnsignedShortTy;
2714 case BuiltinType::Int:
2715 return UnsignedIntTy;
2716 case BuiltinType::Long:
2717 return UnsignedLongTy;
2718 case BuiltinType::LongLong:
2719 return UnsignedLongLongTy;
2720 default:
2721 assert(0 && "Unexpected signed integer type");
2722 return QualType();
2723 }
2724}
2725
2726
2727//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002728// Serialization Support
2729//===----------------------------------------------------------------------===//
2730
Ted Kremenek738e6c02007-10-31 17:10:13 +00002731/// Emit - Serialize an ASTContext object to Bitcode.
2732void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002733 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002734 S.EmitRef(SourceMgr);
2735 S.EmitRef(Target);
2736 S.EmitRef(Idents);
2737 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002738
Ted Kremenek68228a92007-10-31 22:44:07 +00002739 // Emit the size of the type vector so that we can reserve that size
2740 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002741 S.EmitInt(Types.size());
2742
Ted Kremenek034a78c2007-11-13 22:02:55 +00002743 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2744 I!=E;++I)
2745 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002746
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002747 S.EmitOwnedPtr(TUDecl);
2748
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002749 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002750}
2751
Ted Kremenekacba3612007-11-13 00:25:37 +00002752ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002753
2754 // Read the language options.
2755 LangOptions LOpts;
2756 LOpts.Read(D);
2757
Ted Kremenek68228a92007-10-31 22:44:07 +00002758 SourceManager &SM = D.ReadRef<SourceManager>();
2759 TargetInfo &t = D.ReadRef<TargetInfo>();
2760 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2761 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002762
Ted Kremenek68228a92007-10-31 22:44:07 +00002763 unsigned size_reserve = D.ReadInt();
2764
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002765 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2766 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002767
Ted Kremenek034a78c2007-11-13 22:02:55 +00002768 for (unsigned i = 0; i < size_reserve; ++i)
2769 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002770
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002771 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2772
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002773 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002774
2775 return A;
2776}