blob: dec73182b81ea7d700cba5d8a96b354d1b072f41 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor23d23262009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
110 else if (isa<ArrayType>(T))
111 ++NumArray;
112 else if (isa<VectorType>(T))
113 ++NumVector;
114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000139 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
151 fprintf(stderr, " %d array types\n", NumArray);
152 fprintf(stderr, " %d vector types\n", NumVector);
153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregord2baafd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000233
Steve Naroff9d12c902007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
249
Chris Lattner2a674dc2008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
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;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000376 case Type::FixedWidthInt:
377 // FIXME: This isn't precisely correct; the width/alignment should depend
378 // on the available types for the target
379 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000380 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000381 Align = Width;
382 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000383 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000384 // FIXME: Pointers into different addr spaces could have different sizes and
385 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000386 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000388 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000389 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000390 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000391 case Type::BlockPointer: {
392 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
393 Width = Target.getPointerWidth(AS);
394 Align = Target.getPointerAlign(AS);
395 break;
396 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000397 case Type::Pointer: {
398 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000399 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000400 Align = Target.getPointerAlign(AS);
401 break;
402 }
Chris Lattner4b009652007-07-25 00:24:17 +0000403 case Type::Reference:
404 // "When applied to a reference or a reference type, the result is the size
405 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 // FIXME: This is wrong for struct layout: a reference in a struct has
407 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000408 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000409 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000410 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000411 // the GCC ABI, where pointers to data are one pointer large, pointers to
412 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000413 // other compilers too, we need to delegate this completely to TargetInfo
414 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000415 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
416 unsigned AS = Pointee.getAddressSpace();
417 Width = Target.getPointerWidth(AS);
418 if (Pointee->isFunctionType())
419 Width *= 2;
420 Align = Target.getPointerAlign(AS);
421 // GCC aligns at single pointer width.
422 }
Chris Lattner4b009652007-07-25 00:24:17 +0000423 case Type::Complex: {
424 // Complex types have the same alignment as their elements, but twice the
425 // size.
426 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000427 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000428 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000429 Align = EltInfo.second;
430 break;
431 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000432 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000433 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000434 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
435 Width = Layout.getSize();
436 Align = Layout.getAlignment();
437 break;
438 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000439 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000440 const TagType *TT = cast<TagType>(T);
441
442 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000443 Width = 1;
444 Align = 1;
445 break;
446 }
447
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000448 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000449 return getTypeInfo(ET->getDecl()->getIntegerType());
450
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000451 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000452 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
453 Width = Layout.getSize();
454 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000455 break;
456 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000457 }
Chris Lattner4b009652007-07-25 00:24:17 +0000458
459 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000460 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000461}
462
Chris Lattner83165b52009-01-27 18:08:34 +0000463/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
464/// type for the current target in bits. This can be different than the ABI
465/// alignment in cases where it is beneficial for performance to overalign
466/// a data type.
467unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
468 unsigned ABIAlign = getTypeAlign(T);
469
470 // Doubles should be naturally aligned if possible.
471 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
472 if (BT->getKind() == BuiltinType::Double)
Fariborz Jahanianbaeeb752009-01-27 18:55:00 +0000473 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000474
475 return ABIAlign;
476}
477
478
Devang Patelbfe323c2008-06-04 21:22:16 +0000479/// LayoutField - Field layout.
480void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000481 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000482 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000483 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000484 uint64_t FieldOffset = IsUnion ? 0 : Size;
485 uint64_t FieldSize;
486 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000487
488 // FIXME: Should this override struct packing? Probably we want to
489 // take the minimum?
490 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
491 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000492
493 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
494 // TODO: Need to check this algorithm on other targets!
495 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000496 FieldSize =
497 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000498
499 std::pair<uint64_t, unsigned> FieldInfo =
500 Context.getTypeInfo(FD->getType());
501 uint64_t TypeSize = FieldInfo.first;
502
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000503 // Determine the alignment of this bitfield. The packing
504 // attributes define a maximum and the alignment attribute defines
505 // a minimum.
506 // FIXME: What is the right behavior when the specified alignment
507 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000508 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000509 if (FieldPacking)
510 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000511 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
512 FieldAlign = std::max(FieldAlign, AA->getAlignment());
513
514 // Check if we need to add padding to give the field the correct
515 // alignment.
516 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
517 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
518
519 // Padding members don't affect overall alignment
520 if (!FD->getIdentifier())
521 FieldAlign = 1;
522 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000523 if (FD->getType()->isIncompleteArrayType()) {
524 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000525 // query getTypeInfo about these, so we figure it out here.
526 // Flexible array members don't have any size, but they
527 // have to be aligned appropriately for their element type.
528 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000529 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000530 FieldAlign = Context.getTypeAlign(ATy->getElementType());
531 } else {
532 std::pair<uint64_t, unsigned> FieldInfo =
533 Context.getTypeInfo(FD->getType());
534 FieldSize = FieldInfo.first;
535 FieldAlign = FieldInfo.second;
536 }
537
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000538 // Determine the alignment of this bitfield. The packing
539 // attributes define a maximum and the alignment attribute defines
540 // a minimum. Additionally, the packing alignment must be at least
541 // a byte for non-bitfields.
542 //
543 // FIXME: What is the right behavior when the specified alignment
544 // is smaller than the specified packing?
545 if (FieldPacking)
546 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000547 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
548 FieldAlign = std::max(FieldAlign, AA->getAlignment());
549
550 // Round up the current record size to the field's alignment boundary.
551 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
552 }
553
554 // Place this field at the current location.
555 FieldOffsets[FieldNo] = FieldOffset;
556
557 // Reserve space for this field.
558 if (IsUnion) {
559 Size = std::max(Size, FieldSize);
560 } else {
561 Size = FieldOffset + FieldSize;
562 }
563
564 // Remember max struct/class alignment.
565 Alignment = std::max(Alignment, FieldAlign);
566}
567
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000568static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
569 std::vector<FieldDecl*> &Fields) {
570 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
571 if (SuperClass)
572 CollectObjCIvars(SuperClass, Fields);
573 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
574 E = OI->ivar_end(); I != E; ++I) {
575 ObjCIvarDecl *IVDecl = (*I);
576 if (!IVDecl->isInvalidDecl())
577 Fields.push_back(cast<FieldDecl>(IVDecl));
578 }
579}
580
581/// addRecordToClass - produces record info. for the class for its
582/// ivars and all those inherited.
583///
584const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
585{
586 const RecordDecl *&RD = ASTRecordForInterface[D];
587 if (RD)
588 return RD;
589 std::vector<FieldDecl*> RecFields;
590 CollectObjCIvars(D, RecFields);
591 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
592 D->getLocation(),
593 D->getIdentifier());
594 /// FIXME! Can do collection of ivars and adding to the record while
595 /// doing it.
596 for (unsigned int i = 0; i != RecFields.size(); i++) {
597 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
598 RecFields[i]->getLocation(),
599 RecFields[i]->getIdentifier(),
600 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000601 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000602 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000603 }
604 NewRD->completeDefinition(*this);
605 RD = NewRD;
606 return RD;
607}
Devang Patel4b6bf702008-06-04 21:54:36 +0000608
Fariborz Jahanianea944842008-12-18 17:29:46 +0000609/// setFieldDecl - maps a field for the given Ivar reference node.
610//
611void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
612 const ObjCIvarDecl *Ivar,
613 const ObjCIvarRefExpr *MRef) {
614 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
615 lookupFieldDeclForIvar(*this, Ivar);
616 ASTFieldForIvarRef[MRef] = FD;
617}
618
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000619/// getASTObjcInterfaceLayout - Get or compute information about the layout of
620/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000621/// position information.
622const ASTRecordLayout &
623ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
624 // Look up this layout, if already laid out, return what we have.
625 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
626 if (Entry) return *Entry;
627
628 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
629 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000630 ASTRecordLayout *NewEntry = NULL;
631 unsigned FieldCount = D->ivar_size();
632 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
633 FieldCount++;
634 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
635 unsigned Alignment = SL.getAlignment();
636 uint64_t Size = SL.getSize();
637 NewEntry = new ASTRecordLayout(Size, Alignment);
638 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000639 // Super class is at the beginning of the layout.
640 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000641 } else {
642 NewEntry = new ASTRecordLayout();
643 NewEntry->InitializeLayout(FieldCount);
644 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000645 Entry = NewEntry;
646
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000647 unsigned StructPacking = 0;
648 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
649 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000650
651 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
652 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
653 AA->getAlignment()));
654
655 // Layout each ivar sequentially.
656 unsigned i = 0;
657 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
658 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
659 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000660 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000661 }
662
663 // Finally, round the size of the total struct up to the alignment of the
664 // struct itself.
665 NewEntry->FinalizeLayout();
666 return *NewEntry;
667}
668
Devang Patel7a78e432007-11-01 19:11:01 +0000669/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000670/// specified record (struct/union/class), which indicates its size and field
671/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000672const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000673 D = D->getDefinition(*this);
674 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000675
Chris Lattner4b009652007-07-25 00:24:17 +0000676 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000677 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000678 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000679
Devang Patel7a78e432007-11-01 19:11:01 +0000680 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
681 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
682 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000683 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000684
Douglas Gregor39677622008-12-11 20:41:00 +0000685 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000686 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000687 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000688
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000689 unsigned StructPacking = 0;
690 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
691 StructPacking = PA->getAlignment();
692
Eli Friedman5949a022008-05-30 09:31:38 +0000693 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000694 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
695 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000696
Eli Friedman5949a022008-05-30 09:31:38 +0000697 // Layout each field, for now, just sequentially, respecting alignment. In
698 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000699 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000700 for (RecordDecl::field_iterator Field = D->field_begin(),
701 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000702 Field != FieldEnd; (void)++Field, ++FieldIdx)
703 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000704
705 // Finally, round the size of the total struct up to the alignment of the
706 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000707 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000708 return *NewEntry;
709}
710
Chris Lattner4b009652007-07-25 00:24:17 +0000711//===----------------------------------------------------------------------===//
712// Type creation/memoization methods
713//===----------------------------------------------------------------------===//
714
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000715QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000716 QualType CanT = getCanonicalType(T);
717 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000718 return T;
719
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000720 // Type's cannot have multiple ExtQuals, therefore we know we only have to deal
Chris Lattner35fef522008-02-20 20:55:12 +0000721 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000722 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000723 "Type is already address space qualified");
724
725 // Check if we've already instantiated an address space qual'd type of this
726 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000727 llvm::FoldingSetNodeID ID;
Fariborz Jahaniandae66ed2009-02-17 20:16:45 +0000728 ExtQualType::Profile(ID, T.getTypePtr(), AddressSpace, 0);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000729 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000730 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
731 return QualType(EXTQy, 0);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000732
733 // If the base type isn't canonical, this won't be a canonical type either,
734 // so fill in the canonical type field.
735 QualType Canonical;
736 if (!T->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000737 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000738
739 // Get the new insert position for the node we care about.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000740 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000741 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000742 }
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000743 ExtQualType *New = new (*this, 8) ExtQualType(T.getTypePtr(), Canonical,
Fariborz Jahaniandae66ed2009-02-17 20:16:45 +0000744 AddressSpace, 0,
745 ExtQualType::ASQUAL);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000746 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000747 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000748 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000749}
750
Chris Lattner4b009652007-07-25 00:24:17 +0000751
752/// getComplexType - Return the uniqued reference to the type for a complex
753/// number with the specified element type.
754QualType ASTContext::getComplexType(QualType T) {
755 // Unique pointers, to guarantee there is only one pointer of a particular
756 // structure.
757 llvm::FoldingSetNodeID ID;
758 ComplexType::Profile(ID, T);
759
760 void *InsertPos = 0;
761 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
762 return QualType(CT, 0);
763
764 // If the pointee type isn't canonical, this won't be a canonical type either,
765 // so fill in the canonical type field.
766 QualType Canonical;
767 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000768 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000769
770 // Get the new insert position for the node we care about.
771 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000772 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000773 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000774 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000775 Types.push_back(New);
776 ComplexTypes.InsertNode(New, InsertPos);
777 return QualType(New, 0);
778}
779
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000780QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
781 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
782 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
783 FixedWidthIntType *&Entry = Map[Width];
784 if (!Entry)
785 Entry = new FixedWidthIntType(Width, Signed);
786 return QualType(Entry, 0);
787}
Chris Lattner4b009652007-07-25 00:24:17 +0000788
789/// getPointerType - Return the uniqued reference to the type for a pointer to
790/// the specified type.
791QualType ASTContext::getPointerType(QualType T) {
792 // Unique pointers, to guarantee there is only one pointer of a particular
793 // structure.
794 llvm::FoldingSetNodeID ID;
795 PointerType::Profile(ID, T);
796
797 void *InsertPos = 0;
798 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
799 return QualType(PT, 0);
800
801 // If the pointee type isn't canonical, this won't be a canonical type either,
802 // so fill in the canonical type field.
803 QualType Canonical;
804 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000805 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000806
807 // Get the new insert position for the node we care about.
808 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000809 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000810 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000811 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000812 Types.push_back(New);
813 PointerTypes.InsertNode(New, InsertPos);
814 return QualType(New, 0);
815}
816
Steve Naroff7aa54752008-08-27 16:04:49 +0000817/// getBlockPointerType - Return the uniqued reference to the type for
818/// a pointer to the specified block.
819QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000820 assert(T->isFunctionType() && "block of function types only");
821 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000822 // structure.
823 llvm::FoldingSetNodeID ID;
824 BlockPointerType::Profile(ID, T);
825
826 void *InsertPos = 0;
827 if (BlockPointerType *PT =
828 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
829 return QualType(PT, 0);
830
Steve Narofffd5b19d2008-08-28 19:20:44 +0000831 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000832 // type either so fill in the canonical type field.
833 QualType Canonical;
834 if (!T->isCanonical()) {
835 Canonical = getBlockPointerType(getCanonicalType(T));
836
837 // Get the new insert position for the node we care about.
838 BlockPointerType *NewIP =
839 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000840 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000841 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000842 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000843 Types.push_back(New);
844 BlockPointerTypes.InsertNode(New, InsertPos);
845 return QualType(New, 0);
846}
847
Chris Lattner4b009652007-07-25 00:24:17 +0000848/// getReferenceType - Return the uniqued reference to the type for a reference
849/// to the specified type.
850QualType ASTContext::getReferenceType(QualType T) {
851 // Unique pointers, to guarantee there is only one pointer of a particular
852 // structure.
853 llvm::FoldingSetNodeID ID;
854 ReferenceType::Profile(ID, T);
855
856 void *InsertPos = 0;
857 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
858 return QualType(RT, 0);
859
860 // If the referencee type isn't canonical, this won't be a canonical type
861 // either, so fill in the canonical type field.
862 QualType Canonical;
863 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000864 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000865
866 // Get the new insert position for the node we care about.
867 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000868 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000869 }
870
Steve Naroff93fd2112009-01-27 22:08:43 +0000871 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000872 Types.push_back(New);
873 ReferenceTypes.InsertNode(New, InsertPos);
874 return QualType(New, 0);
875}
876
Sebastian Redl75555032009-01-24 21:16:55 +0000877/// getMemberPointerType - Return the uniqued reference to the type for a
878/// member pointer to the specified type, in the specified class.
879QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
880{
881 // Unique pointers, to guarantee there is only one pointer of a particular
882 // structure.
883 llvm::FoldingSetNodeID ID;
884 MemberPointerType::Profile(ID, T, Cls);
885
886 void *InsertPos = 0;
887 if (MemberPointerType *PT =
888 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
889 return QualType(PT, 0);
890
891 // If the pointee or class type isn't canonical, this won't be a canonical
892 // type either, so fill in the canonical type field.
893 QualType Canonical;
894 if (!T->isCanonical()) {
895 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
896
897 // Get the new insert position for the node we care about.
898 MemberPointerType *NewIP =
899 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
900 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
901 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000902 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000903 Types.push_back(New);
904 MemberPointerTypes.InsertNode(New, InsertPos);
905 return QualType(New, 0);
906}
907
Steve Naroff83c13012007-08-30 01:06:46 +0000908/// getConstantArrayType - Return the unique reference to the type for an
909/// array of the specified element type.
910QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000911 const llvm::APInt &ArySize,
912 ArrayType::ArraySizeModifier ASM,
913 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000914 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000915 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000916
917 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000918 if (ConstantArrayType *ATP =
919 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000920 return QualType(ATP, 0);
921
922 // If the element type isn't canonical, this won't be a canonical type either,
923 // so fill in the canonical type field.
924 QualType Canonical;
925 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000926 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000927 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000928 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000929 ConstantArrayType *NewIP =
930 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000931 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000932 }
933
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000934 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000935 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000936 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000937 Types.push_back(New);
938 return QualType(New, 0);
939}
940
Steve Naroffe2579e32007-08-30 18:14:25 +0000941/// getVariableArrayType - Returns a non-unique reference to the type for a
942/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000943QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
944 ArrayType::ArraySizeModifier ASM,
945 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000946 // Since we don't unique expressions, it isn't possible to unique VLA's
947 // that have an expression provided for their size.
948
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000949 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000950 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000951
952 VariableArrayTypes.push_back(New);
953 Types.push_back(New);
954 return QualType(New, 0);
955}
956
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000957/// getDependentSizedArrayType - Returns a non-unique reference to
958/// the type for a dependently-sized array of the specified element
959/// type. FIXME: We will need these to be uniqued, or at least
960/// comparable, at some point.
961QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
962 ArrayType::ArraySizeModifier ASM,
963 unsigned EltTypeQuals) {
964 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
965 "Size must be type- or value-dependent!");
966
967 // Since we don't unique expressions, it isn't possible to unique
968 // dependently-sized array types.
969
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000970 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000971 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
972 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000973
974 DependentSizedArrayTypes.push_back(New);
975 Types.push_back(New);
976 return QualType(New, 0);
977}
978
Eli Friedman8ff07782008-02-15 18:16:39 +0000979QualType ASTContext::getIncompleteArrayType(QualType EltTy,
980 ArrayType::ArraySizeModifier ASM,
981 unsigned EltTypeQuals) {
982 llvm::FoldingSetNodeID ID;
983 IncompleteArrayType::Profile(ID, EltTy);
984
985 void *InsertPos = 0;
986 if (IncompleteArrayType *ATP =
987 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
988 return QualType(ATP, 0);
989
990 // If the element type isn't canonical, this won't be a canonical type
991 // either, so fill in the canonical type field.
992 QualType Canonical;
993
994 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000995 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000996 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000997
998 // Get the new insert position for the node we care about.
999 IncompleteArrayType *NewIP =
1000 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001001 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001002 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001003
Steve Naroff93fd2112009-01-27 22:08:43 +00001004 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001005 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001006
1007 IncompleteArrayTypes.InsertNode(New, InsertPos);
1008 Types.push_back(New);
1009 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001010}
1011
Chris Lattner4b009652007-07-25 00:24:17 +00001012/// getVectorType - Return the unique reference to a vector type of
1013/// the specified element type and size. VectorType must be a built-in type.
1014QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1015 BuiltinType *baseType;
1016
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001017 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001018 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1019
1020 // Check if we've already instantiated a vector of this type.
1021 llvm::FoldingSetNodeID ID;
1022 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1023 void *InsertPos = 0;
1024 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1025 return QualType(VTP, 0);
1026
1027 // If the element type isn't canonical, this won't be a canonical type either,
1028 // so fill in the canonical type field.
1029 QualType Canonical;
1030 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001031 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001032
1033 // Get the new insert position for the node we care about.
1034 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001036 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001037 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001038 VectorTypes.InsertNode(New, InsertPos);
1039 Types.push_back(New);
1040 return QualType(New, 0);
1041}
1042
Nate Begemanaf6ed502008-04-18 23:10:10 +00001043/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001044/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001045QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001046 BuiltinType *baseType;
1047
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001048 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001049 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001050
1051 // Check if we've already instantiated a vector of this type.
1052 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001053 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001054 void *InsertPos = 0;
1055 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1056 return QualType(VTP, 0);
1057
1058 // If the element type isn't canonical, this won't be a canonical type either,
1059 // so fill in the canonical type field.
1060 QualType Canonical;
1061 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001062 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001063
1064 // Get the new insert position for the node we care about.
1065 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001066 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001067 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001068 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001069 VectorTypes.InsertNode(New, InsertPos);
1070 Types.push_back(New);
1071 return QualType(New, 0);
1072}
1073
1074/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1075///
1076QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1077 // Unique functions, to guarantee there is only one function of a particular
1078 // structure.
1079 llvm::FoldingSetNodeID ID;
1080 FunctionTypeNoProto::Profile(ID, ResultTy);
1081
1082 void *InsertPos = 0;
1083 if (FunctionTypeNoProto *FT =
1084 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1085 return QualType(FT, 0);
1086
1087 QualType Canonical;
1088 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001089 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001090
1091 // Get the new insert position for the node we care about.
1092 FunctionTypeNoProto *NewIP =
1093 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001094 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001095 }
1096
Steve Naroff93fd2112009-01-27 22:08:43 +00001097 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001098 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001099 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001100 return QualType(New, 0);
1101}
1102
1103/// getFunctionType - Return a normal function type with a typed argument
1104/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001105QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001106 unsigned NumArgs, bool isVariadic,
1107 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001108 // Unique functions, to guarantee there is only one function of a particular
1109 // structure.
1110 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001111 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1112 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 void *InsertPos = 0;
1115 if (FunctionTypeProto *FTP =
1116 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1117 return QualType(FTP, 0);
1118
1119 // Determine whether the type being created is already canonical or not.
1120 bool isCanonical = ResultTy->isCanonical();
1121 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1122 if (!ArgArray[i]->isCanonical())
1123 isCanonical = false;
1124
1125 // If this type isn't canonical, get the canonical version of it.
1126 QualType Canonical;
1127 if (!isCanonical) {
1128 llvm::SmallVector<QualType, 16> CanonicalArgs;
1129 CanonicalArgs.reserve(NumArgs);
1130 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001131 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001132
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001133 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001134 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001135 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001136
1137 // Get the new insert position for the node we care about.
1138 FunctionTypeProto *NewIP =
1139 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001140 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001141 }
1142
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001143 // FunctionTypeProto objects are allocated with extra bytes after them
1144 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001145 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001146 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1147 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001148 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001149 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001150 Types.push_back(FTP);
1151 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1152 return QualType(FTP, 0);
1153}
1154
Douglas Gregor1d661552008-04-13 21:07:44 +00001155/// getTypeDeclType - Return the unique reference to the type for the
1156/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001157QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001158 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001159 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1160
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001161 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001162 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001163 else if (isa<TemplateTypeParmDecl>(Decl)) {
1164 assert(false && "Template type parameter types are always available.");
1165 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001166 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001167
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001168 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001169 if (PrevDecl)
1170 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001171 else
1172 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001173 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001174 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001175 if (PrevDecl)
1176 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001177 else
1178 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001179 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001180 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1181 if (PrevDecl)
1182 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001183 else
1184 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001185 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001186 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001187 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001188
Ted Kremenek46a837c2008-09-05 17:16:31 +00001189 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001190 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001191}
1192
Chris Lattner4b009652007-07-25 00:24:17 +00001193/// getTypedefType - Return the unique reference to the type for the
1194/// specified typename decl.
1195QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1196 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1197
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001198 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001199 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001200 Types.push_back(Decl->TypeForDecl);
1201 return QualType(Decl->TypeForDecl, 0);
1202}
1203
Ted Kremenek42730c52008-01-07 19:49:32 +00001204/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001205/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001206QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001207 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1208
Steve Naroff93fd2112009-01-27 22:08:43 +00001209 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001210 Types.push_back(Decl->TypeForDecl);
1211 return QualType(Decl->TypeForDecl, 0);
1212}
1213
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001214/// buildObjCInterfaceType - Returns a new type for the interface
1215/// declaration, regardless. It also removes any previously built
1216/// record declaration so caller can rebuild it.
1217QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1218 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1219 if (RD)
1220 RD = 0;
1221 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1222 Types.push_back(Decl->TypeForDecl);
1223 return QualType(Decl->TypeForDecl, 0);
1224}
1225
Douglas Gregora4918772009-02-05 23:33:38 +00001226/// \brief Retrieve the template type parameter type for a template
1227/// parameter with the given depth, index, and (optionally) name.
1228QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1229 IdentifierInfo *Name) {
1230 llvm::FoldingSetNodeID ID;
1231 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1232 void *InsertPos = 0;
1233 TemplateTypeParmType *TypeParm
1234 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1235
1236 if (TypeParm)
1237 return QualType(TypeParm, 0);
1238
1239 if (Name)
1240 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1241 getTemplateTypeParmType(Depth, Index));
1242 else
1243 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1244
1245 Types.push_back(TypeParm);
1246 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1247
1248 return QualType(TypeParm, 0);
1249}
1250
Douglas Gregor8e458f42009-02-09 18:46:07 +00001251QualType
1252ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1253 unsigned NumArgs,
1254 uintptr_t *Args, bool *ArgIsType,
1255 QualType Canon) {
1256 llvm::FoldingSetNodeID ID;
1257 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1258 ArgIsType);
1259 void *InsertPos = 0;
1260 ClassTemplateSpecializationType *Spec
1261 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1262
1263 if (Spec)
1264 return QualType(Spec, 0);
1265
1266 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1267 (sizeof(uintptr_t) *
1268 (ClassTemplateSpecializationType::
1269 getNumPackedWords(NumArgs) +
1270 NumArgs)), 8);
1271 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1272 ArgIsType, Canon);
1273 Types.push_back(Spec);
1274 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1275
1276 return QualType(Spec, 0);
1277}
1278
Chris Lattnere1352302008-04-07 04:56:42 +00001279/// CmpProtocolNames - Comparison predicate for sorting protocols
1280/// alphabetically.
1281static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1282 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001283 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001284}
1285
1286static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1287 unsigned &NumProtocols) {
1288 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1289
1290 // Sort protocols, keyed by name.
1291 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1292
1293 // Remove duplicates.
1294 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1295 NumProtocols = ProtocolsEnd-Protocols;
1296}
1297
1298
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001299/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1300/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001301QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1302 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001303 // Sort the protocol list alphabetically to canonicalize it.
1304 SortAndUniqueProtocols(Protocols, NumProtocols);
1305
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001306 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001307 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001308
1309 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001310 if (ObjCQualifiedInterfaceType *QT =
1311 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001312 return QualType(QT, 0);
1313
1314 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001316 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001317
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001318 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001319 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001320 return QualType(QType, 0);
1321}
1322
Chris Lattnere1352302008-04-07 04:56:42 +00001323/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1324/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001325QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001326 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001327 // Sort the protocol list alphabetically to canonicalize it.
1328 SortAndUniqueProtocols(Protocols, NumProtocols);
1329
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001330 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001331 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001332
1333 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001334 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001335 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001336 return QualType(QT, 0);
1337
1338 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001339 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001340 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001341 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001342 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001343 return QualType(QType, 0);
1344}
1345
Steve Naroff0604dd92007-08-01 18:02:17 +00001346/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1347/// TypeOfExpr AST's (since expression's are never shared). For example,
1348/// multiple declarations that refer to "typeof(x)" all contain different
1349/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1350/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001351QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001352 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001353 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001354 Types.push_back(toe);
1355 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001356}
1357
Steve Naroff0604dd92007-08-01 18:02:17 +00001358/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1359/// TypeOfType AST's. The only motivation to unique these nodes would be
1360/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1361/// an issue. This doesn't effect the type checker, since it operates
1362/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001363QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001364 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001365 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001366 Types.push_back(tot);
1367 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001368}
1369
Chris Lattner4b009652007-07-25 00:24:17 +00001370/// getTagDeclType - Return the unique reference to the type for the
1371/// specified TagDecl (struct/union/class/enum) decl.
1372QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001373 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001374 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001375}
1376
1377/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1378/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1379/// needs to agree with the definition in <stddef.h>.
1380QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001381 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001382}
1383
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001384/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001385/// width of characters in wide strings, The value is target dependent and
1386/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001387QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001388 if (LangOpts.CPlusPlus)
1389 return WCharTy;
1390
Douglas Gregorc6507e42008-11-03 14:12:49 +00001391 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1392 // wide-character type?
1393 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001394}
1395
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001396/// getSignedWCharType - Return the type of "signed wchar_t".
1397/// Used when in C++, as a GCC extension.
1398QualType ASTContext::getSignedWCharType() const {
1399 // FIXME: derive from "Target" ?
1400 return WCharTy;
1401}
1402
1403/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1404/// Used when in C++, as a GCC extension.
1405QualType ASTContext::getUnsignedWCharType() const {
1406 // FIXME: derive from "Target" ?
1407 return UnsignedIntTy;
1408}
1409
Chris Lattner4b009652007-07-25 00:24:17 +00001410/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1411/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1412QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001413 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001414}
1415
Chris Lattner19eb97e2008-04-02 05:18:44 +00001416//===----------------------------------------------------------------------===//
1417// Type Operators
1418//===----------------------------------------------------------------------===//
1419
Chris Lattner3dae6f42008-04-06 22:41:35 +00001420/// getCanonicalType - Return the canonical (structural) type corresponding to
1421/// the specified potentially non-canonical type. The non-canonical version
1422/// of a type may have many "decorated" versions of types. Decorators can
1423/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1424/// to be free of any of these, allowing two canonical types to be compared
1425/// for exact equality with a simple pointer comparison.
1426QualType ASTContext::getCanonicalType(QualType T) {
1427 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001428
1429 // If the result has type qualifiers, make sure to canonicalize them as well.
1430 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1431 if (TypeQuals == 0) return CanType;
1432
1433 // If the type qualifiers are on an array type, get the canonical type of the
1434 // array with the qualifiers applied to the element type.
1435 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1436 if (!AT)
1437 return CanType.getQualifiedType(TypeQuals);
1438
1439 // Get the canonical version of the element with the extra qualifiers on it.
1440 // This can recursively sink qualifiers through multiple levels of arrays.
1441 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1442 NewEltTy = getCanonicalType(NewEltTy);
1443
1444 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1445 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1446 CAT->getIndexTypeQualifier());
1447 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1448 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1449 IAT->getIndexTypeQualifier());
1450
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001451 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1452 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1453 DSAT->getSizeModifier(),
1454 DSAT->getIndexTypeQualifier());
1455
Chris Lattnera1923f62008-08-04 07:31:14 +00001456 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1457 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1458 VAT->getSizeModifier(),
1459 VAT->getIndexTypeQualifier());
1460}
1461
1462
1463const ArrayType *ASTContext::getAsArrayType(QualType T) {
1464 // Handle the non-qualified case efficiently.
1465 if (T.getCVRQualifiers() == 0) {
1466 // Handle the common positive case fast.
1467 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1468 return AT;
1469 }
1470
1471 // Handle the common negative case fast, ignoring CVR qualifiers.
1472 QualType CType = T->getCanonicalTypeInternal();
1473
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001474 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001475 // test.
1476 if (!isa<ArrayType>(CType) &&
1477 !isa<ArrayType>(CType.getUnqualifiedType()))
1478 return 0;
1479
1480 // Apply any CVR qualifiers from the array type to the element type. This
1481 // implements C99 6.7.3p8: "If the specification of an array type includes
1482 // any type qualifiers, the element type is so qualified, not the array type."
1483
1484 // If we get here, we either have type qualifiers on the type, or we have
1485 // sugar such as a typedef in the way. If we have type qualifiers on the type
1486 // we must propagate them down into the elemeng type.
1487 unsigned CVRQuals = T.getCVRQualifiers();
1488 unsigned AddrSpace = 0;
1489 Type *Ty = T.getTypePtr();
1490
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001491 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001492 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001493 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1494 AddrSpace = EXTQT->getAddressSpace();
1495 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001496 } else {
1497 T = Ty->getDesugaredType();
1498 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1499 break;
1500 CVRQuals |= T.getCVRQualifiers();
1501 Ty = T.getTypePtr();
1502 }
1503 }
1504
1505 // If we have a simple case, just return now.
1506 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1507 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1508 return ATy;
1509
1510 // Otherwise, we have an array and we have qualifiers on it. Push the
1511 // qualifiers into the array element type and return a new array type.
1512 // Get the canonical version of the element with the extra qualifiers on it.
1513 // This can recursively sink qualifiers through multiple levels of arrays.
1514 QualType NewEltTy = ATy->getElementType();
1515 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001516 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001517 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1518
1519 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1520 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1521 CAT->getSizeModifier(),
1522 CAT->getIndexTypeQualifier()));
1523 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1524 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1525 IAT->getSizeModifier(),
1526 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001527
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001528 if (const DependentSizedArrayType *DSAT
1529 = dyn_cast<DependentSizedArrayType>(ATy))
1530 return cast<ArrayType>(
1531 getDependentSizedArrayType(NewEltTy,
1532 DSAT->getSizeExpr(),
1533 DSAT->getSizeModifier(),
1534 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001535
Chris Lattnera1923f62008-08-04 07:31:14 +00001536 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1537 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1538 VAT->getSizeModifier(),
1539 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001540}
1541
1542
Chris Lattner19eb97e2008-04-02 05:18:44 +00001543/// getArrayDecayedType - Return the properly qualified result of decaying the
1544/// specified array type to a pointer. This operation is non-trivial when
1545/// handling typedefs etc. The canonical type of "T" must be an array type,
1546/// this returns a pointer to a properly qualified element of the array.
1547///
1548/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1549QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001550 // Get the element type with 'getAsArrayType' so that we don't lose any
1551 // typedefs in the element type of the array. This also handles propagation
1552 // of type qualifiers from the array type into the element type if present
1553 // (C99 6.7.3p8).
1554 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1555 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001556
Chris Lattnera1923f62008-08-04 07:31:14 +00001557 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001558
1559 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001560 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001561}
1562
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001563QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001564 QualType ElemTy = VAT->getElementType();
1565
1566 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1567 return getBaseElementType(VAT);
1568
1569 return ElemTy;
1570}
1571
Chris Lattner4b009652007-07-25 00:24:17 +00001572/// getFloatingRank - Return a relative rank for floating point types.
1573/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001574static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001575 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001576 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001577
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001578 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001579 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001580 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001581 case BuiltinType::Float: return FloatRank;
1582 case BuiltinType::Double: return DoubleRank;
1583 case BuiltinType::LongDouble: return LongDoubleRank;
1584 }
1585}
1586
Steve Narofffa0c4532007-08-27 01:41:48 +00001587/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1588/// point or a complex type (based on typeDomain/typeSize).
1589/// 'typeDomain' is a real floating point or complex type.
1590/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001591QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1592 QualType Domain) const {
1593 FloatingRank EltRank = getFloatingRank(Size);
1594 if (Domain->isComplexType()) {
1595 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001596 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001597 case FloatRank: return FloatComplexTy;
1598 case DoubleRank: return DoubleComplexTy;
1599 case LongDoubleRank: return LongDoubleComplexTy;
1600 }
Chris Lattner4b009652007-07-25 00:24:17 +00001601 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001602
1603 assert(Domain->isRealFloatingType() && "Unknown domain!");
1604 switch (EltRank) {
1605 default: assert(0 && "getFloatingRank(): illegal value for rank");
1606 case FloatRank: return FloatTy;
1607 case DoubleRank: return DoubleTy;
1608 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001609 }
Chris Lattner4b009652007-07-25 00:24:17 +00001610}
1611
Chris Lattner51285d82008-04-06 23:55:33 +00001612/// getFloatingTypeOrder - Compare the rank of the two specified floating
1613/// point types, ignoring the domain of the type (i.e. 'double' ==
1614/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1615/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001616int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1617 FloatingRank LHSR = getFloatingRank(LHS);
1618 FloatingRank RHSR = getFloatingRank(RHS);
1619
1620 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001621 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001622 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001623 return 1;
1624 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001625}
1626
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001627/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1628/// routine will assert if passed a built-in type that isn't an integer or enum,
1629/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001630unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001631 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001632 if (EnumType* ET = dyn_cast<EnumType>(T))
1633 T = ET->getDecl()->getIntegerType().getTypePtr();
1634
1635 // There are two things which impact the integer rank: the width, and
1636 // the ordering of builtins. The builtin ordering is encoded in the
1637 // bottom three bits; the width is encoded in the bits above that.
1638 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1639 return FWIT->getWidth() << 3;
1640 }
1641
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001642 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001643 default: assert(0 && "getIntegerRank(): not a built-in integer");
1644 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001645 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001646 case BuiltinType::Char_S:
1647 case BuiltinType::Char_U:
1648 case BuiltinType::SChar:
1649 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001650 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001651 case BuiltinType::Short:
1652 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001653 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001654 case BuiltinType::Int:
1655 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001656 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001657 case BuiltinType::Long:
1658 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001659 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001660 case BuiltinType::LongLong:
1661 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001662 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001663 }
1664}
1665
Chris Lattner51285d82008-04-06 23:55:33 +00001666/// getIntegerTypeOrder - Returns the highest ranked integer type:
1667/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1668/// LHS < RHS, return -1.
1669int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001670 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1671 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001672 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001673
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001674 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1675 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001676
Chris Lattner51285d82008-04-06 23:55:33 +00001677 unsigned LHSRank = getIntegerRank(LHSC);
1678 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001679
Chris Lattner51285d82008-04-06 23:55:33 +00001680 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1681 if (LHSRank == RHSRank) return 0;
1682 return LHSRank > RHSRank ? 1 : -1;
1683 }
Chris Lattner4b009652007-07-25 00:24:17 +00001684
Chris Lattner51285d82008-04-06 23:55:33 +00001685 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1686 if (LHSUnsigned) {
1687 // If the unsigned [LHS] type is larger, return it.
1688 if (LHSRank >= RHSRank)
1689 return 1;
1690
1691 // If the signed type can represent all values of the unsigned type, it
1692 // wins. Because we are dealing with 2's complement and types that are
1693 // powers of two larger than each other, this is always safe.
1694 return -1;
1695 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001696
Chris Lattner51285d82008-04-06 23:55:33 +00001697 // If the unsigned [RHS] type is larger, return it.
1698 if (RHSRank >= LHSRank)
1699 return -1;
1700
1701 // If the signed type can represent all values of the unsigned type, it
1702 // wins. Because we are dealing with 2's complement and types that are
1703 // powers of two larger than each other, this is always safe.
1704 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001705}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001706
1707// getCFConstantStringType - Return the type used for constant CFStrings.
1708QualType ASTContext::getCFConstantStringType() {
1709 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001710 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001711 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001712 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001713 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001714
1715 // const int *isa;
1716 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001717 // int flags;
1718 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001719 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001720 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001721 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001722 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001723
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001724 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001725 for (unsigned i = 0; i < 4; ++i) {
1726 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1727 SourceLocation(), 0,
1728 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001729 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001730 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001731 }
1732
1733 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001734 }
1735
1736 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001737}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001738
Anders Carlssonf58cac72008-08-30 19:34:46 +00001739QualType ASTContext::getObjCFastEnumerationStateType()
1740{
1741 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001742 ObjCFastEnumerationStateTypeDecl =
1743 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1744 &Idents.get("__objcFastEnumerationState"));
1745
Anders Carlssonf58cac72008-08-30 19:34:46 +00001746 QualType FieldTypes[] = {
1747 UnsignedLongTy,
1748 getPointerType(ObjCIdType),
1749 getPointerType(UnsignedLongTy),
1750 getConstantArrayType(UnsignedLongTy,
1751 llvm::APInt(32, 5), ArrayType::Normal, 0)
1752 };
1753
Douglas Gregor8acb7272008-12-11 16:49:14 +00001754 for (size_t i = 0; i < 4; ++i) {
1755 FieldDecl *Field = FieldDecl::Create(*this,
1756 ObjCFastEnumerationStateTypeDecl,
1757 SourceLocation(), 0,
1758 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001759 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001760 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001761 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001762
Douglas Gregor8acb7272008-12-11 16:49:14 +00001763 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001764 }
1765
1766 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1767}
1768
Anders Carlssone3f02572007-10-29 06:33:42 +00001769// This returns true if a type has been typedefed to BOOL:
1770// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001771static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001772 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001773 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1774 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001775
1776 return false;
1777}
1778
Ted Kremenek42730c52008-01-07 19:49:32 +00001779/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001780/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001781int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001782 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001783
1784 // Make all integer and enum types at least as large as an int
1785 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001786 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001787 // Treat arrays as pointers, since that's how they're passed in.
1788 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001789 sz = getTypeSize(VoidPtrTy);
1790 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001791}
1792
Ted Kremenek42730c52008-01-07 19:49:32 +00001793/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001794/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001795void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001796 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001797 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001798 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001799 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001800 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001801 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001802 // Compute size of all parameters.
1803 // Start with computing size of a pointer in number of bytes.
1804 // FIXME: There might(should) be a better way of doing this computation!
1805 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001806 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001807 // The first two arguments (self and _cmd) are pointers; account for
1808 // their size.
1809 int ParmOffset = 2 * PtrSize;
1810 int NumOfParams = Decl->getNumParams();
1811 for (int i = 0; i < NumOfParams; i++) {
1812 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001813 int sz = getObjCEncodingTypeSize (PType);
1814 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001815 ParmOffset += sz;
1816 }
1817 S += llvm::utostr(ParmOffset);
1818 S += "@0:";
1819 S += llvm::utostr(PtrSize);
1820
1821 // Argument types.
1822 ParmOffset = 2 * PtrSize;
1823 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001824 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1825 QualType PType = PVDecl->getOriginalType();
1826 if (const ArrayType *AT =
1827 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1828 // Use array's original type only if it has known number of
1829 // elements.
1830 if (!dyn_cast<ConstantArrayType>(AT))
1831 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001832 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001833 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001834 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001835 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001836 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001837 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001838 }
1839}
1840
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001841/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001842/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001843/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1844/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001845/// Property attributes are stored as a comma-delimited C string. The simple
1846/// attributes readonly and bycopy are encoded as single characters. The
1847/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1848/// encoded as single characters, followed by an identifier. Property types
1849/// are also encoded as a parametrized attribute. The characters used to encode
1850/// these attributes are defined by the following enumeration:
1851/// @code
1852/// enum PropertyAttributes {
1853/// kPropertyReadOnly = 'R', // property is read-only.
1854/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1855/// kPropertyByref = '&', // property is a reference to the value last assigned
1856/// kPropertyDynamic = 'D', // property is dynamic
1857/// kPropertyGetter = 'G', // followed by getter selector name
1858/// kPropertySetter = 'S', // followed by setter selector name
1859/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1860/// kPropertyType = 't' // followed by old-style type encoding.
1861/// kPropertyWeak = 'W' // 'weak' property
1862/// kPropertyStrong = 'P' // property GC'able
1863/// kPropertyNonAtomic = 'N' // property non-atomic
1864/// };
1865/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001866void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1867 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001868 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001869 // Collect information from the property implementation decl(s).
1870 bool Dynamic = false;
1871 ObjCPropertyImplDecl *SynthesizePID = 0;
1872
1873 // FIXME: Duplicated code due to poor abstraction.
1874 if (Container) {
1875 if (const ObjCCategoryImplDecl *CID =
1876 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1877 for (ObjCCategoryImplDecl::propimpl_iterator
1878 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1879 ObjCPropertyImplDecl *PID = *i;
1880 if (PID->getPropertyDecl() == PD) {
1881 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1882 Dynamic = true;
1883 } else {
1884 SynthesizePID = PID;
1885 }
1886 }
1887 }
1888 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001889 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001890 for (ObjCCategoryImplDecl::propimpl_iterator
1891 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1892 ObjCPropertyImplDecl *PID = *i;
1893 if (PID->getPropertyDecl() == PD) {
1894 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1895 Dynamic = true;
1896 } else {
1897 SynthesizePID = PID;
1898 }
1899 }
1900 }
1901 }
1902 }
1903
1904 // FIXME: This is not very efficient.
1905 S = "T";
1906
1907 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001908 // GCC has some special rules regarding encoding of properties which
1909 // closely resembles encoding of ivars.
1910 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1911 true /* outermost type */,
1912 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001913
1914 if (PD->isReadOnly()) {
1915 S += ",R";
1916 } else {
1917 switch (PD->getSetterKind()) {
1918 case ObjCPropertyDecl::Assign: break;
1919 case ObjCPropertyDecl::Copy: S += ",C"; break;
1920 case ObjCPropertyDecl::Retain: S += ",&"; break;
1921 }
1922 }
1923
1924 // It really isn't clear at all what this means, since properties
1925 // are "dynamic by default".
1926 if (Dynamic)
1927 S += ",D";
1928
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001929 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1930 S += ",N";
1931
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001932 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1933 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001934 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001935 }
1936
1937 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1938 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001939 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001940 }
1941
1942 if (SynthesizePID) {
1943 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1944 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001945 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001946 }
1947
1948 // FIXME: OBJCGC: weak & strong
1949}
1950
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001951/// getLegacyIntegralTypeEncoding -
1952/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00001953/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001954/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1955///
1956void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1957 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1958 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00001959 if (BT->getKind() == BuiltinType::ULong &&
1960 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001961 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00001962 else
1963 if (BT->getKind() == BuiltinType::Long &&
1964 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001965 PointeeTy = IntTy;
1966 }
1967 }
1968}
1969
Fariborz Jahanian248db262008-01-22 22:44:46 +00001970void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001971 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001972 // We follow the behavior of gcc, expanding structures which are
1973 // directly pointed to, and expanding embedded structures. Note that
1974 // these rules are sufficient to prevent recursive encoding of the
1975 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001976 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1977 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001978}
1979
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001980static void EncodeBitField(const ASTContext *Context, std::string& S,
1981 FieldDecl *FD) {
1982 const Expr *E = FD->getBitWidth();
1983 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1984 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1985 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1986 S += 'b';
1987 S += llvm::utostr(N);
1988}
1989
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001990void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1991 bool ExpandPointedToStructures,
1992 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001993 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001994 bool OutermostType,
1995 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001996 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001997 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001998 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001999 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002000 else {
2001 char encoding;
2002 switch (BT->getKind()) {
2003 default: assert(0 && "Unhandled builtin type kind");
2004 case BuiltinType::Void: encoding = 'v'; break;
2005 case BuiltinType::Bool: encoding = 'B'; break;
2006 case BuiltinType::Char_U:
2007 case BuiltinType::UChar: encoding = 'C'; break;
2008 case BuiltinType::UShort: encoding = 'S'; break;
2009 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002010 case BuiltinType::ULong:
2011 encoding =
2012 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2013 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002014 case BuiltinType::ULongLong: encoding = 'Q'; break;
2015 case BuiltinType::Char_S:
2016 case BuiltinType::SChar: encoding = 'c'; break;
2017 case BuiltinType::Short: encoding = 's'; break;
2018 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002019 case BuiltinType::Long:
2020 encoding =
2021 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2022 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002023 case BuiltinType::LongLong: encoding = 'q'; break;
2024 case BuiltinType::Float: encoding = 'f'; break;
2025 case BuiltinType::Double: encoding = 'd'; break;
2026 case BuiltinType::LongDouble: encoding = 'd'; break;
2027 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002028
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002029 S += encoding;
2030 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002031 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002032 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002033 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2034 ExpandPointedToStructures,
2035 ExpandStructures, FD);
2036 if (FD || EncodingProperty) {
2037 // Note that we do extended encoding of protocol qualifer list
2038 // Only when doing ivar or property encoding.
2039 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2040 S += '"';
2041 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2042 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2043 S += '<';
2044 S += Proto->getNameAsString();
2045 S += '>';
2046 }
2047 S += '"';
2048 }
2049 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002050 }
2051 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002052 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002053 bool isReadOnly = false;
2054 // For historical/compatibility reasons, the read-only qualifier of the
2055 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2056 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2057 // Also, do not emit the 'r' for anything but the outermost type!
2058 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2059 if (OutermostType && T.isConstQualified()) {
2060 isReadOnly = true;
2061 S += 'r';
2062 }
2063 }
2064 else if (OutermostType) {
2065 QualType P = PointeeTy;
2066 while (P->getAsPointerType())
2067 P = P->getAsPointerType()->getPointeeType();
2068 if (P.isConstQualified()) {
2069 isReadOnly = true;
2070 S += 'r';
2071 }
2072 }
2073 if (isReadOnly) {
2074 // Another legacy compatibility encoding. Some ObjC qualifier and type
2075 // combinations need to be rearranged.
2076 // Rewrite "in const" from "nr" to "rn"
2077 const char * s = S.c_str();
2078 int len = S.length();
2079 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2080 std::string replace = "rn";
2081 S.replace(S.end()-2, S.end(), replace);
2082 }
2083 }
Steve Naroff17c03822009-02-12 17:52:19 +00002084 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002085 S += '@';
2086 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002087 }
2088 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002089 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002090 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002091 // Another historical/compatibility reason.
2092 // We encode the underlying type which comes out as
2093 // {...};
2094 S += '^';
2095 getObjCEncodingForTypeImpl(PointeeTy, S,
2096 false, ExpandPointedToStructures,
2097 NULL);
2098 return;
2099 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002100 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002101 if (FD || EncodingProperty) {
2102 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2103 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002104 S += '"';
2105 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002106 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2107 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2108 S += '<';
2109 S += Proto->getNameAsString();
2110 S += '>';
2111 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002112 S += '"';
2113 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002114 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002115 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002116 S += '#';
2117 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002118 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002119 S += ':';
2120 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002121 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002122
2123 if (PointeeTy->isCharType()) {
2124 // char pointer types should be encoded as '*' unless it is a
2125 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002126 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002127 S += '*';
2128 return;
2129 }
2130 }
2131
2132 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002133 getLegacyIntegralTypeEncoding(PointeeTy);
2134
2135 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002136 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002137 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002138 } else if (const ArrayType *AT =
2139 // Ignore type qualifiers etc.
2140 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002141 S += '[';
2142
2143 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2144 S += llvm::utostr(CAT->getSize().getZExtValue());
2145 else
2146 assert(0 && "Unhandled array type!");
2147
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002148 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002149 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002150 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002151 } else if (T->getAsFunctionType()) {
2152 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002153 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002154 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002155 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002156 // Anonymous structures print as '?'
2157 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2158 S += II->getName();
2159 } else {
2160 S += '?';
2161 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002162 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002163 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002164 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2165 FieldEnd = RDecl->field_end();
2166 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002167 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002168 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002169 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002170 S += '"';
2171 }
2172
2173 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002174 if (Field->isBitField()) {
2175 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2176 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002177 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002178 QualType qt = Field->getType();
2179 getLegacyIntegralTypeEncoding(qt);
2180 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002181 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002182 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002183 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002184 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002185 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002186 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002187 if (FD && FD->isBitField())
2188 EncodeBitField(this, S, FD);
2189 else
2190 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002191 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002192 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002193 } else if (T->isObjCInterfaceType()) {
2194 // @encode(class_name)
2195 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2196 S += '{';
2197 const IdentifierInfo *II = OI->getIdentifier();
2198 S += II->getName();
2199 S += '=';
2200 std::vector<FieldDecl*> RecFields;
2201 CollectObjCIvars(OI, RecFields);
2202 for (unsigned int i = 0; i != RecFields.size(); i++) {
2203 if (RecFields[i]->isBitField())
2204 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2205 RecFields[i]);
2206 else
2207 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2208 FD);
2209 }
2210 S += '}';
2211 }
2212 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002213 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002214}
2215
Ted Kremenek42730c52008-01-07 19:49:32 +00002216void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002217 std::string& S) const {
2218 if (QT & Decl::OBJC_TQ_In)
2219 S += 'n';
2220 if (QT & Decl::OBJC_TQ_Inout)
2221 S += 'N';
2222 if (QT & Decl::OBJC_TQ_Out)
2223 S += 'o';
2224 if (QT & Decl::OBJC_TQ_Bycopy)
2225 S += 'O';
2226 if (QT & Decl::OBJC_TQ_Byref)
2227 S += 'R';
2228 if (QT & Decl::OBJC_TQ_Oneway)
2229 S += 'V';
2230}
2231
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002232void ASTContext::setBuiltinVaListType(QualType T)
2233{
2234 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2235
2236 BuiltinVaListType = T;
2237}
2238
Ted Kremenek42730c52008-01-07 19:49:32 +00002239void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002240{
Ted Kremenek42730c52008-01-07 19:49:32 +00002241 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002242
2243 // typedef struct objc_object *id;
2244 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002245 // User error - caller will issue diagnostics.
2246 if (!ptr)
2247 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002248 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002249 // User error - caller will issue diagnostics.
2250 if (!rec)
2251 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002252 IdStructType = rec;
2253}
2254
Ted Kremenek42730c52008-01-07 19:49:32 +00002255void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002256{
Ted Kremenek42730c52008-01-07 19:49:32 +00002257 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002258
2259 // typedef struct objc_selector *SEL;
2260 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002261 if (!ptr)
2262 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002263 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002264 if (!rec)
2265 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002266 SelStructType = rec;
2267}
2268
Ted Kremenek42730c52008-01-07 19:49:32 +00002269void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002270{
Ted Kremenek42730c52008-01-07 19:49:32 +00002271 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002272}
2273
Ted Kremenek42730c52008-01-07 19:49:32 +00002274void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002275{
Ted Kremenek42730c52008-01-07 19:49:32 +00002276 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002277
2278 // typedef struct objc_class *Class;
2279 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2280 assert(ptr && "'Class' incorrectly typed");
2281 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2282 assert(rec && "'Class' incorrectly typed");
2283 ClassStructType = rec;
2284}
2285
Ted Kremenek42730c52008-01-07 19:49:32 +00002286void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2287 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002288 "'NSConstantString' type already set!");
2289
Ted Kremenek42730c52008-01-07 19:49:32 +00002290 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002291}
2292
Douglas Gregorc6507e42008-11-03 14:12:49 +00002293/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002294/// TargetInfo, produce the corresponding type. The unsigned @p Type
2295/// is actually a value of type @c TargetInfo::IntType.
2296QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002297 switch (Type) {
2298 case TargetInfo::NoInt: return QualType();
2299 case TargetInfo::SignedShort: return ShortTy;
2300 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2301 case TargetInfo::SignedInt: return IntTy;
2302 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2303 case TargetInfo::SignedLong: return LongTy;
2304 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2305 case TargetInfo::SignedLongLong: return LongLongTy;
2306 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2307 }
2308
2309 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002310 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002311}
Ted Kremenek118930e2008-07-24 23:58:27 +00002312
2313//===----------------------------------------------------------------------===//
2314// Type Predicates.
2315//===----------------------------------------------------------------------===//
2316
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002317/// isObjCNSObjectType - Return true if this is an NSObject object using
2318/// NSObject attribute on a c-style pointer type.
2319/// FIXME - Make it work directly on types.
2320///
2321bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2322 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2323 if (TypedefDecl *TD = TDT->getDecl())
2324 if (TD->getAttr<ObjCNSObjectAttr>())
2325 return true;
2326 }
2327 return false;
2328}
2329
Ted Kremenek118930e2008-07-24 23:58:27 +00002330/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2331/// to an object type. This includes "id" and "Class" (two 'special' pointers
2332/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2333/// ID type).
2334bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2335 if (Ty->isObjCQualifiedIdType())
2336 return true;
2337
Steve Naroffd9e00802008-10-21 18:24:04 +00002338 // Blocks are objects.
2339 if (Ty->isBlockPointerType())
2340 return true;
2341
2342 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002343 if (!Ty->isPointerType())
2344 return false;
2345
2346 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2347 // pointer types. This looks for the typedef specifically, not for the
2348 // underlying type.
2349 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2350 return true;
2351
2352 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002353 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2354 return true;
2355
2356 // If is has NSObject attribute, OK as well.
2357 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002358}
2359
Chris Lattner6ff358b2008-04-07 06:51:04 +00002360//===----------------------------------------------------------------------===//
2361// Type Compatibility Testing
2362//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002363
Steve Naroff3454b6c2008-09-04 15:10:53 +00002364/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002365/// block types. Types must be strictly compatible here. For example,
2366/// C unfortunately doesn't produce an error for the following:
2367///
2368/// int (*emptyArgFunc)();
2369/// int (*intArgList)(int) = emptyArgFunc;
2370///
2371/// For blocks, we will produce an error for the following (similar to C++):
2372///
2373/// int (^emptyArgBlock)();
2374/// int (^intArgBlock)(int) = emptyArgBlock;
2375///
2376/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2377///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002378bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002379 const FunctionType *lbase = lhs->getAsFunctionType();
2380 const FunctionType *rbase = rhs->getAsFunctionType();
2381 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2382 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2383 if (lproto && rproto)
2384 return !mergeTypes(lhs, rhs).isNull();
2385 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002386}
2387
Chris Lattner6ff358b2008-04-07 06:51:04 +00002388/// areCompatVectorTypes - Return true if the two specified vector types are
2389/// compatible.
2390static bool areCompatVectorTypes(const VectorType *LHS,
2391 const VectorType *RHS) {
2392 assert(LHS->isCanonical() && RHS->isCanonical());
2393 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002394 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002395}
2396
Eli Friedman0d9549b2008-08-22 00:56:42 +00002397/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002398/// compatible for assignment from RHS to LHS. This handles validation of any
2399/// protocol qualifiers on the LHS or RHS.
2400///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002401bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2402 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002403 // Verify that the base decls are compatible: the RHS must be a subclass of
2404 // the LHS.
2405 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2406 return false;
2407
2408 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2409 // protocol qualified at all, then we are good.
2410 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2411 return true;
2412
2413 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2414 // isn't a superset.
2415 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2416 return true; // FIXME: should return false!
2417
2418 // Finally, we must have two protocol-qualified interfaces.
2419 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2420 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2421 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2422 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2423 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2424 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2425
2426 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2427 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2428 // LHS in a single parallel scan until we run out of elements in LHS.
2429 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2430 ObjCProtocolDecl *LHSProto = *LHSPI;
2431
2432 while (RHSPI != RHSPE) {
2433 ObjCProtocolDecl *RHSProto = *RHSPI++;
2434 // If the RHS has a protocol that the LHS doesn't, ignore it.
2435 if (RHSProto != LHSProto)
2436 continue;
2437
2438 // Otherwise, the RHS does have this element.
2439 ++LHSPI;
2440 if (LHSPI == LHSPE)
2441 return true; // All protocols in LHS exist in RHS.
2442
2443 LHSProto = *LHSPI;
2444 }
2445
2446 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2447 return false;
2448}
2449
Steve Naroff17c03822009-02-12 17:52:19 +00002450bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2451 // get the "pointed to" types
2452 const PointerType *LHSPT = LHS->getAsPointerType();
2453 const PointerType *RHSPT = RHS->getAsPointerType();
2454
2455 if (!LHSPT || !RHSPT)
2456 return false;
2457
2458 QualType lhptee = LHSPT->getPointeeType();
2459 QualType rhptee = RHSPT->getPointeeType();
2460 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2461 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2462 // ID acts sort of like void* for ObjC interfaces
2463 if (LHSIface && isObjCIdStructType(rhptee))
2464 return true;
2465 if (RHSIface && isObjCIdStructType(lhptee))
2466 return true;
2467 if (!LHSIface || !RHSIface)
2468 return false;
2469 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2470 canAssignObjCInterfaces(RHSIface, LHSIface);
2471}
2472
Steve Naroff85f0dc52007-10-15 20:41:53 +00002473/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2474/// both shall have the identically qualified version of a compatible type.
2475/// C99 6.2.7p1: Two types have compatible types if their types are the
2476/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002477bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2478 return !mergeTypes(LHS, RHS).isNull();
2479}
2480
2481QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2482 const FunctionType *lbase = lhs->getAsFunctionType();
2483 const FunctionType *rbase = rhs->getAsFunctionType();
2484 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2485 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2486 bool allLTypes = true;
2487 bool allRTypes = true;
2488
2489 // Check return type
2490 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2491 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002492 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2493 allLTypes = false;
2494 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2495 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002496
2497 if (lproto && rproto) { // two C99 style function prototypes
2498 unsigned lproto_nargs = lproto->getNumArgs();
2499 unsigned rproto_nargs = rproto->getNumArgs();
2500
2501 // Compatible functions must have the same number of arguments
2502 if (lproto_nargs != rproto_nargs)
2503 return QualType();
2504
2505 // Variadic and non-variadic functions aren't compatible
2506 if (lproto->isVariadic() != rproto->isVariadic())
2507 return QualType();
2508
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002509 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2510 return QualType();
2511
Eli Friedman0d9549b2008-08-22 00:56:42 +00002512 // Check argument compatibility
2513 llvm::SmallVector<QualType, 10> types;
2514 for (unsigned i = 0; i < lproto_nargs; i++) {
2515 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2516 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2517 QualType argtype = mergeTypes(largtype, rargtype);
2518 if (argtype.isNull()) return QualType();
2519 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002520 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2521 allLTypes = false;
2522 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2523 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002524 }
2525 if (allLTypes) return lhs;
2526 if (allRTypes) return rhs;
2527 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002528 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002529 }
2530
2531 if (lproto) allRTypes = false;
2532 if (rproto) allLTypes = false;
2533
2534 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2535 if (proto) {
2536 if (proto->isVariadic()) return QualType();
2537 // Check that the types are compatible with the types that
2538 // would result from default argument promotions (C99 6.7.5.3p15).
2539 // The only types actually affected are promotable integer
2540 // types and floats, which would be passed as a different
2541 // type depending on whether the prototype is visible.
2542 unsigned proto_nargs = proto->getNumArgs();
2543 for (unsigned i = 0; i < proto_nargs; ++i) {
2544 QualType argTy = proto->getArgType(i);
2545 if (argTy->isPromotableIntegerType() ||
2546 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2547 return QualType();
2548 }
2549
2550 if (allLTypes) return lhs;
2551 if (allRTypes) return rhs;
2552 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002553 proto->getNumArgs(), lproto->isVariadic(),
2554 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002555 }
2556
2557 if (allLTypes) return lhs;
2558 if (allRTypes) return rhs;
2559 return getFunctionTypeNoProto(retType);
2560}
2561
2562QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002563 // C++ [expr]: If an expression initially has the type "reference to T", the
2564 // type is adjusted to "T" prior to any further analysis, the expression
2565 // designates the object or function denoted by the reference, and the
2566 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002567 // FIXME: C++ shouldn't be going through here! The rules are different
2568 // enough that they should be handled separately.
2569 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002570 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002571 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002572 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002573
Eli Friedman0d9549b2008-08-22 00:56:42 +00002574 QualType LHSCan = getCanonicalType(LHS),
2575 RHSCan = getCanonicalType(RHS);
2576
2577 // If two types are identical, they are compatible.
2578 if (LHSCan == RHSCan)
2579 return LHS;
2580
2581 // If the qualifiers are different, the types aren't compatible
2582 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2583 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2584 return QualType();
2585
2586 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2587 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2588
Chris Lattnerc38d4522008-01-14 05:45:46 +00002589 // We want to consider the two function types to be the same for these
2590 // comparisons, just force one to the other.
2591 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2592 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002593
2594 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002595 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2596 LHSClass = Type::ConstantArray;
2597 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2598 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002599
Nate Begemanaf6ed502008-04-18 23:10:10 +00002600 // Canonicalize ExtVector -> Vector.
2601 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2602 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002603
Chris Lattner7cdcb252008-04-07 06:38:24 +00002604 // Consider qualified interfaces and interfaces the same.
2605 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2606 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002607
Chris Lattnerb5709e22008-04-07 05:43:21 +00002608 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002609 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002610 // ID is compatible with all qualified id types.
2611 if (LHS->isObjCQualifiedIdType()) {
2612 if (const PointerType *PT = RHS->getAsPointerType()) {
2613 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002614 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002615 return LHS;
2616 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2617 // Unfortunately, this API is part of Sema (which we don't have access
2618 // to. Need to refactor. The following check is insufficient, since we
2619 // need to make sure the class implements the protocol.
2620 if (pType->isObjCInterfaceType())
2621 return LHS;
2622 }
2623 }
2624 if (RHS->isObjCQualifiedIdType()) {
2625 if (const PointerType *PT = LHS->getAsPointerType()) {
2626 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002627 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002628 return RHS;
2629 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2630 // Unfortunately, this API is part of Sema (which we don't have access
2631 // to. Need to refactor. The following check is insufficient, since we
2632 // need to make sure the class implements the protocol.
2633 if (pType->isObjCInterfaceType())
2634 return RHS;
2635 }
2636 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002637 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2638 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002639 if (const EnumType* ETy = LHS->getAsEnumType()) {
2640 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2641 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002642 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002643 if (const EnumType* ETy = RHS->getAsEnumType()) {
2644 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2645 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002646 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002647
Eli Friedman0d9549b2008-08-22 00:56:42 +00002648 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002649 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002650
Steve Naroffc88babe2008-01-09 22:43:08 +00002651 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002652 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002653 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002654 {
2655 // Merge two pointer types, while trying to preserve typedef info
2656 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2657 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2658 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2659 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002660 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2661 return LHS;
2662 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2663 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002664 return getPointerType(ResultType);
2665 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002666 case Type::BlockPointer:
2667 {
2668 // Merge two block pointer types, while trying to preserve typedef info
2669 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2670 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2671 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2672 if (ResultType.isNull()) return QualType();
2673 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2674 return LHS;
2675 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2676 return RHS;
2677 return getBlockPointerType(ResultType);
2678 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002679 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002680 {
2681 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2682 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2683 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2684 return QualType();
2685
2686 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2687 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2688 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2689 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002690 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2691 return LHS;
2692 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2693 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002694 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2695 ArrayType::ArraySizeModifier(), 0);
2696 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2697 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002698 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2699 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002700 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2701 return LHS;
2702 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2703 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002704 if (LVAT) {
2705 // FIXME: This isn't correct! But tricky to implement because
2706 // the array's size has to be the size of LHS, but the type
2707 // has to be different.
2708 return LHS;
2709 }
2710 if (RVAT) {
2711 // FIXME: This isn't correct! But tricky to implement because
2712 // the array's size has to be the size of RHS, but the type
2713 // has to be different.
2714 return RHS;
2715 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002716 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2717 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002718 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002719 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002720 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002721 return mergeFunctionTypes(LHS, RHS);
2722 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002723 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002724 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2725 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002726 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002727 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002728 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002729 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002730 case Type::Complex:
2731 // Distinct complex types are incompatible.
2732 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002733 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002734 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2735 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002736 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002737 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002738 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2739 // for checking assignment/comparison safety
2740 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002741 case Type::ObjCQualifiedId:
2742 // Distinct qualified id's are not compatible.
2743 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002744 default:
2745 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002746 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002747 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002748}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002749
Chris Lattner1d78a862008-04-07 07:01:58 +00002750//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002751// Integer Predicates
2752//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002753
Eli Friedman0832dbc2008-06-28 06:23:08 +00002754unsigned ASTContext::getIntWidth(QualType T) {
2755 if (T == BoolTy)
2756 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002757 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2758 return FWIT->getWidth();
2759 }
2760 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002761 return (unsigned)getTypeSize(T);
2762}
2763
2764QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2765 assert(T->isSignedIntegerType() && "Unexpected type");
2766 if (const EnumType* ETy = T->getAsEnumType())
2767 T = ETy->getDecl()->getIntegerType();
2768 const BuiltinType* BTy = T->getAsBuiltinType();
2769 assert (BTy && "Unexpected signed integer type");
2770 switch (BTy->getKind()) {
2771 case BuiltinType::Char_S:
2772 case BuiltinType::SChar:
2773 return UnsignedCharTy;
2774 case BuiltinType::Short:
2775 return UnsignedShortTy;
2776 case BuiltinType::Int:
2777 return UnsignedIntTy;
2778 case BuiltinType::Long:
2779 return UnsignedLongTy;
2780 case BuiltinType::LongLong:
2781 return UnsignedLongLongTy;
2782 default:
2783 assert(0 && "Unexpected signed integer type");
2784 return QualType();
2785 }
2786}
2787
2788
2789//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002790// Serialization Support
2791//===----------------------------------------------------------------------===//
2792
Ted Kremenek738e6c02007-10-31 17:10:13 +00002793/// Emit - Serialize an ASTContext object to Bitcode.
2794void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002795 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002796 S.EmitRef(SourceMgr);
2797 S.EmitRef(Target);
2798 S.EmitRef(Idents);
2799 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002800
Ted Kremenek68228a92007-10-31 22:44:07 +00002801 // Emit the size of the type vector so that we can reserve that size
2802 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002803 S.EmitInt(Types.size());
2804
Ted Kremenek034a78c2007-11-13 22:02:55 +00002805 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2806 I!=E;++I)
2807 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002808
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002809 S.EmitOwnedPtr(TUDecl);
2810
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002811 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002812}
2813
Ted Kremenekacba3612007-11-13 00:25:37 +00002814ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002815
2816 // Read the language options.
2817 LangOptions LOpts;
2818 LOpts.Read(D);
2819
Ted Kremenek68228a92007-10-31 22:44:07 +00002820 SourceManager &SM = D.ReadRef<SourceManager>();
2821 TargetInfo &t = D.ReadRef<TargetInfo>();
2822 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2823 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002824
Ted Kremenek68228a92007-10-31 22:44:07 +00002825 unsigned size_reserve = D.ReadInt();
2826
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002827 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2828 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002829
Ted Kremenek034a78c2007-11-13 22:02:55 +00002830 for (unsigned i = 0; i < size_reserve; ++i)
2831 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002832
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002833 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2834
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002835 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002836
2837 return A;
2838}