blob: 5c02100ec4d87add965328d489bb1f1eeb24f801 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor23d23262009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
110 else if (isa<ArrayType>(T))
111 ++NumArray;
112 else if (isa<VectorType>(T))
113 ++NumVector;
114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000139 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
151 fprintf(stderr, " %d array types\n", NumArray);
152 fprintf(stderr, " %d vector types\n", NumVector);
153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregord2baafd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000233
Steve Naroff9d12c902007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
249
Chris Lattner2a674dc2008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000266unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000267 // 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
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000278 return getTypeAlign(T) / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000279 }
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.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000471 if (T->isSpecificBuiltinType(BuiltinType::Double))
472 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000473
474 return ABIAlign;
475}
476
477
Devang Patelbfe323c2008-06-04 21:22:16 +0000478/// LayoutField - Field layout.
479void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000480 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000481 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000482 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000483 uint64_t FieldOffset = IsUnion ? 0 : Size;
484 uint64_t FieldSize;
485 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000486
487 // FIXME: Should this override struct packing? Probably we want to
488 // take the minimum?
489 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
490 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000491
492 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
493 // TODO: Need to check this algorithm on other targets!
494 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000495 FieldSize =
496 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000497
498 std::pair<uint64_t, unsigned> FieldInfo =
499 Context.getTypeInfo(FD->getType());
500 uint64_t TypeSize = FieldInfo.first;
501
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000502 // Determine the alignment of this bitfield. The packing
503 // attributes define a maximum and the alignment attribute defines
504 // a minimum.
505 // FIXME: What is the right behavior when the specified alignment
506 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000507 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000508 if (FieldPacking)
509 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000510 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
511 FieldAlign = std::max(FieldAlign, AA->getAlignment());
512
513 // Check if we need to add padding to give the field the correct
514 // alignment.
515 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
516 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
517
518 // Padding members don't affect overall alignment
519 if (!FD->getIdentifier())
520 FieldAlign = 1;
521 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000522 if (FD->getType()->isIncompleteArrayType()) {
523 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000524 // query getTypeInfo about these, so we figure it out here.
525 // Flexible array members don't have any size, but they
526 // have to be aligned appropriately for their element type.
527 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000528 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000529 FieldAlign = Context.getTypeAlign(ATy->getElementType());
530 } else {
531 std::pair<uint64_t, unsigned> FieldInfo =
532 Context.getTypeInfo(FD->getType());
533 FieldSize = FieldInfo.first;
534 FieldAlign = FieldInfo.second;
535 }
536
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000537 // Determine the alignment of this bitfield. The packing
538 // attributes define a maximum and the alignment attribute defines
539 // a minimum. Additionally, the packing alignment must be at least
540 // a byte for non-bitfields.
541 //
542 // FIXME: What is the right behavior when the specified alignment
543 // is smaller than the specified packing?
544 if (FieldPacking)
545 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000546 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
547 FieldAlign = std::max(FieldAlign, AA->getAlignment());
548
549 // Round up the current record size to the field's alignment boundary.
550 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
551 }
552
553 // Place this field at the current location.
554 FieldOffsets[FieldNo] = FieldOffset;
555
556 // Reserve space for this field.
557 if (IsUnion) {
558 Size = std::max(Size, FieldSize);
559 } else {
560 Size = FieldOffset + FieldSize;
561 }
562
563 // Remember max struct/class alignment.
564 Alignment = std::max(Alignment, FieldAlign);
565}
566
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000567static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
568 std::vector<FieldDecl*> &Fields) {
569 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
570 if (SuperClass)
571 CollectObjCIvars(SuperClass, Fields);
572 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
573 E = OI->ivar_end(); I != E; ++I) {
574 ObjCIvarDecl *IVDecl = (*I);
575 if (!IVDecl->isInvalidDecl())
576 Fields.push_back(cast<FieldDecl>(IVDecl));
577 }
578}
579
580/// addRecordToClass - produces record info. for the class for its
581/// ivars and all those inherited.
582///
583const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
584{
585 const RecordDecl *&RD = ASTRecordForInterface[D];
586 if (RD)
587 return RD;
588 std::vector<FieldDecl*> RecFields;
589 CollectObjCIvars(D, RecFields);
590 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
591 D->getLocation(),
592 D->getIdentifier());
593 /// FIXME! Can do collection of ivars and adding to the record while
594 /// doing it.
595 for (unsigned int i = 0; i != RecFields.size(); i++) {
596 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
597 RecFields[i]->getLocation(),
598 RecFields[i]->getIdentifier(),
599 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000600 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000601 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000602 }
603 NewRD->completeDefinition(*this);
604 RD = NewRD;
605 return RD;
606}
Devang Patel4b6bf702008-06-04 21:54:36 +0000607
Fariborz Jahanianea944842008-12-18 17:29:46 +0000608/// setFieldDecl - maps a field for the given Ivar reference node.
609//
610void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
611 const ObjCIvarDecl *Ivar,
612 const ObjCIvarRefExpr *MRef) {
613 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
614 lookupFieldDeclForIvar(*this, Ivar);
615 ASTFieldForIvarRef[MRef] = FD;
616}
617
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000618/// getASTObjcInterfaceLayout - Get or compute information about the layout of
619/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000620/// position information.
621const ASTRecordLayout &
622ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
623 // Look up this layout, if already laid out, return what we have.
624 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
625 if (Entry) return *Entry;
626
627 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
628 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000629 ASTRecordLayout *NewEntry = NULL;
630 unsigned FieldCount = D->ivar_size();
631 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
632 FieldCount++;
633 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
634 unsigned Alignment = SL.getAlignment();
635 uint64_t Size = SL.getSize();
636 NewEntry = new ASTRecordLayout(Size, Alignment);
637 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000638 // Super class is at the beginning of the layout.
639 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000640 } else {
641 NewEntry = new ASTRecordLayout();
642 NewEntry->InitializeLayout(FieldCount);
643 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000644 Entry = NewEntry;
645
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000646 unsigned StructPacking = 0;
647 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
648 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000649
650 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
651 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
652 AA->getAlignment()));
653
654 // Layout each ivar sequentially.
655 unsigned i = 0;
656 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
657 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
658 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000659 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000660 }
661
662 // Finally, round the size of the total struct up to the alignment of the
663 // struct itself.
664 NewEntry->FinalizeLayout();
665 return *NewEntry;
666}
667
Devang Patel7a78e432007-11-01 19:11:01 +0000668/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000669/// specified record (struct/union/class), which indicates its size and field
670/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000671const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000672 D = D->getDefinition(*this);
673 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000674
Chris Lattner4b009652007-07-25 00:24:17 +0000675 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000676 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000677 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000678
Devang Patel7a78e432007-11-01 19:11:01 +0000679 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
680 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
681 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000682 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000683
Douglas Gregor39677622008-12-11 20:41:00 +0000684 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000685 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000686 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000687
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000688 unsigned StructPacking = 0;
689 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
690 StructPacking = PA->getAlignment();
691
Eli Friedman5949a022008-05-30 09:31:38 +0000692 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000693 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
694 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000695
Eli Friedman5949a022008-05-30 09:31:38 +0000696 // Layout each field, for now, just sequentially, respecting alignment. In
697 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000698 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000699 for (RecordDecl::field_iterator Field = D->field_begin(),
700 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000701 Field != FieldEnd; (void)++Field, ++FieldIdx)
702 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000703
704 // Finally, round the size of the total struct up to the alignment of the
705 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000706 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000707 return *NewEntry;
708}
709
Chris Lattner4b009652007-07-25 00:24:17 +0000710//===----------------------------------------------------------------------===//
711// Type creation/memoization methods
712//===----------------------------------------------------------------------===//
713
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000714QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000715 QualType CanT = getCanonicalType(T);
716 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000717 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000718
719 // If we are composing extended qualifiers together, merge together into one
720 // ExtQualType node.
721 unsigned CVRQuals = T.getCVRQualifiers();
722 QualType::GCAttrTypes GCAttr = QualType::GCNone;
723 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000724
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000725 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
726 // If this type already has an address space specified, it cannot get
727 // another one.
728 assert(EQT->getAddressSpace() == 0 &&
729 "Type cannot be in multiple addr spaces!");
730 GCAttr = EQT->getObjCGCAttr();
731 TypeNode = EQT->getBaseType();
732 }
Chris Lattner35fef522008-02-20 20:55:12 +0000733
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000734 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000735 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000736 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000737 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000738 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000739 return QualType(EXTQy, CVRQuals);
740
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000741 // If the base type isn't canonical, this won't be a canonical type either,
742 // so fill in the canonical type field.
743 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000744 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000745 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000746
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000747 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000748 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000749 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000750 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000751 ExtQualType *New =
752 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000753 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000754 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000755 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000756}
757
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000758QualType ASTContext::getObjCGCQualType(QualType T,
759 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000760 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000761 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000762 return T;
763
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000764 // If we are composing extended qualifiers together, merge together into one
765 // ExtQualType node.
766 unsigned CVRQuals = T.getCVRQualifiers();
767 Type *TypeNode = T.getTypePtr();
768 unsigned AddressSpace = 0;
769
770 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
771 // If this type already has an address space specified, it cannot get
772 // another one.
773 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
774 "Type cannot be in multiple addr spaces!");
775 AddressSpace = EQT->getAddressSpace();
776 TypeNode = EQT->getBaseType();
777 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000778
779 // Check if we've already instantiated an gc qual'd type of this type.
780 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000781 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000782 void *InsertPos = 0;
783 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000784 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000785
786 // If the base type isn't canonical, this won't be a canonical type either,
787 // so fill in the canonical type field.
788 QualType Canonical;
789 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000790 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000791
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000792 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000793 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
794 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
795 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000796 ExtQualType *New =
797 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000798 ExtQualTypes.InsertNode(New, InsertPos);
799 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000800 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000801}
Chris Lattner4b009652007-07-25 00:24:17 +0000802
803/// getComplexType - Return the uniqued reference to the type for a complex
804/// number with the specified element type.
805QualType ASTContext::getComplexType(QualType T) {
806 // Unique pointers, to guarantee there is only one pointer of a particular
807 // structure.
808 llvm::FoldingSetNodeID ID;
809 ComplexType::Profile(ID, T);
810
811 void *InsertPos = 0;
812 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
813 return QualType(CT, 0);
814
815 // If the pointee type isn't canonical, this won't be a canonical type either,
816 // so fill in the canonical type field.
817 QualType Canonical;
818 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000819 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000820
821 // Get the new insert position for the node we care about.
822 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000823 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000824 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000825 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000826 Types.push_back(New);
827 ComplexTypes.InsertNode(New, InsertPos);
828 return QualType(New, 0);
829}
830
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000831QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
832 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
833 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
834 FixedWidthIntType *&Entry = Map[Width];
835 if (!Entry)
836 Entry = new FixedWidthIntType(Width, Signed);
837 return QualType(Entry, 0);
838}
Chris Lattner4b009652007-07-25 00:24:17 +0000839
840/// getPointerType - Return the uniqued reference to the type for a pointer to
841/// the specified type.
842QualType ASTContext::getPointerType(QualType T) {
843 // Unique pointers, to guarantee there is only one pointer of a particular
844 // structure.
845 llvm::FoldingSetNodeID ID;
846 PointerType::Profile(ID, T);
847
848 void *InsertPos = 0;
849 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
850 return QualType(PT, 0);
851
852 // If the pointee type isn't canonical, this won't be a canonical type either,
853 // so fill in the canonical type field.
854 QualType Canonical;
855 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000856 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000857
858 // Get the new insert position for the node we care about.
859 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000860 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000861 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000862 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000863 Types.push_back(New);
864 PointerTypes.InsertNode(New, InsertPos);
865 return QualType(New, 0);
866}
867
Steve Naroff7aa54752008-08-27 16:04:49 +0000868/// getBlockPointerType - Return the uniqued reference to the type for
869/// a pointer to the specified block.
870QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000871 assert(T->isFunctionType() && "block of function types only");
872 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000873 // structure.
874 llvm::FoldingSetNodeID ID;
875 BlockPointerType::Profile(ID, T);
876
877 void *InsertPos = 0;
878 if (BlockPointerType *PT =
879 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
880 return QualType(PT, 0);
881
Steve Narofffd5b19d2008-08-28 19:20:44 +0000882 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000883 // type either so fill in the canonical type field.
884 QualType Canonical;
885 if (!T->isCanonical()) {
886 Canonical = getBlockPointerType(getCanonicalType(T));
887
888 // Get the new insert position for the node we care about.
889 BlockPointerType *NewIP =
890 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000891 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000892 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000893 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000894 Types.push_back(New);
895 BlockPointerTypes.InsertNode(New, InsertPos);
896 return QualType(New, 0);
897}
898
Chris Lattner4b009652007-07-25 00:24:17 +0000899/// getReferenceType - Return the uniqued reference to the type for a reference
900/// to the specified type.
901QualType ASTContext::getReferenceType(QualType T) {
902 // Unique pointers, to guarantee there is only one pointer of a particular
903 // structure.
904 llvm::FoldingSetNodeID ID;
905 ReferenceType::Profile(ID, T);
906
907 void *InsertPos = 0;
908 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
909 return QualType(RT, 0);
910
911 // If the referencee type isn't canonical, this won't be a canonical type
912 // either, so fill in the canonical type field.
913 QualType Canonical;
914 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000915 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000916
917 // Get the new insert position for the node we care about.
918 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000919 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000920 }
921
Steve Naroff93fd2112009-01-27 22:08:43 +0000922 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000923 Types.push_back(New);
924 ReferenceTypes.InsertNode(New, InsertPos);
925 return QualType(New, 0);
926}
927
Sebastian Redl75555032009-01-24 21:16:55 +0000928/// getMemberPointerType - Return the uniqued reference to the type for a
929/// member pointer to the specified type, in the specified class.
930QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
931{
932 // Unique pointers, to guarantee there is only one pointer of a particular
933 // structure.
934 llvm::FoldingSetNodeID ID;
935 MemberPointerType::Profile(ID, T, Cls);
936
937 void *InsertPos = 0;
938 if (MemberPointerType *PT =
939 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
940 return QualType(PT, 0);
941
942 // If the pointee or class type isn't canonical, this won't be a canonical
943 // type either, so fill in the canonical type field.
944 QualType Canonical;
945 if (!T->isCanonical()) {
946 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
947
948 // Get the new insert position for the node we care about.
949 MemberPointerType *NewIP =
950 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
951 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
952 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000953 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000954 Types.push_back(New);
955 MemberPointerTypes.InsertNode(New, InsertPos);
956 return QualType(New, 0);
957}
958
Steve Naroff83c13012007-08-30 01:06:46 +0000959/// getConstantArrayType - Return the unique reference to the type for an
960/// array of the specified element type.
961QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000962 const llvm::APInt &ArySize,
963 ArrayType::ArraySizeModifier ASM,
964 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000965 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +0000966 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000967
968 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000969 if (ConstantArrayType *ATP =
970 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000971 return QualType(ATP, 0);
972
973 // If the element type isn't canonical, this won't be a canonical type either,
974 // so fill in the canonical type field.
975 QualType Canonical;
976 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000977 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000978 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000979 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000980 ConstantArrayType *NewIP =
981 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000982 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000983 }
984
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000985 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000986 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000987 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000988 Types.push_back(New);
989 return QualType(New, 0);
990}
991
Steve Naroffe2579e32007-08-30 18:14:25 +0000992/// getVariableArrayType - Returns a non-unique reference to the type for a
993/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000994QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
995 ArrayType::ArraySizeModifier ASM,
996 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000997 // Since we don't unique expressions, it isn't possible to unique VLA's
998 // that have an expression provided for their size.
999
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001000 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001001 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001002
1003 VariableArrayTypes.push_back(New);
1004 Types.push_back(New);
1005 return QualType(New, 0);
1006}
1007
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001008/// getDependentSizedArrayType - Returns a non-unique reference to
1009/// the type for a dependently-sized array of the specified element
1010/// type. FIXME: We will need these to be uniqued, or at least
1011/// comparable, at some point.
1012QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1013 ArrayType::ArraySizeModifier ASM,
1014 unsigned EltTypeQuals) {
1015 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1016 "Size must be type- or value-dependent!");
1017
1018 // Since we don't unique expressions, it isn't possible to unique
1019 // dependently-sized array types.
1020
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001021 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001022 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1023 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001024
1025 DependentSizedArrayTypes.push_back(New);
1026 Types.push_back(New);
1027 return QualType(New, 0);
1028}
1029
Eli Friedman8ff07782008-02-15 18:16:39 +00001030QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1031 ArrayType::ArraySizeModifier ASM,
1032 unsigned EltTypeQuals) {
1033 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001034 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001035
1036 void *InsertPos = 0;
1037 if (IncompleteArrayType *ATP =
1038 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1039 return QualType(ATP, 0);
1040
1041 // If the element type isn't canonical, this won't be a canonical type
1042 // either, so fill in the canonical type field.
1043 QualType Canonical;
1044
1045 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001046 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001047 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001048
1049 // Get the new insert position for the node we care about.
1050 IncompleteArrayType *NewIP =
1051 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001052 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001053 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001054
Steve Naroff93fd2112009-01-27 22:08:43 +00001055 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001056 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001057
1058 IncompleteArrayTypes.InsertNode(New, InsertPos);
1059 Types.push_back(New);
1060 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001061}
1062
Chris Lattner4b009652007-07-25 00:24:17 +00001063/// getVectorType - Return the unique reference to a vector type of
1064/// the specified element type and size. VectorType must be a built-in type.
1065QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1066 BuiltinType *baseType;
1067
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001068 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001069 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1070
1071 // Check if we've already instantiated a vector of this type.
1072 llvm::FoldingSetNodeID ID;
1073 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1074 void *InsertPos = 0;
1075 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1076 return QualType(VTP, 0);
1077
1078 // If the element type isn't canonical, this won't be a canonical type either,
1079 // so fill in the canonical type field.
1080 QualType Canonical;
1081 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001082 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001083
1084 // Get the new insert position for the node we care about.
1085 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001086 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001087 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001088 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001089 VectorTypes.InsertNode(New, InsertPos);
1090 Types.push_back(New);
1091 return QualType(New, 0);
1092}
1093
Nate Begemanaf6ed502008-04-18 23:10:10 +00001094/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001095/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001096QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001097 BuiltinType *baseType;
1098
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001099 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001100 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001101
1102 // Check if we've already instantiated a vector of this type.
1103 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001104 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001105 void *InsertPos = 0;
1106 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1107 return QualType(VTP, 0);
1108
1109 // If the element type isn't canonical, this won't be a canonical type either,
1110 // so fill in the canonical type field.
1111 QualType Canonical;
1112 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001113 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001114
1115 // Get the new insert position for the node we care about.
1116 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001117 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001118 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001119 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001120 VectorTypes.InsertNode(New, InsertPos);
1121 Types.push_back(New);
1122 return QualType(New, 0);
1123}
1124
1125/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1126///
1127QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1128 // Unique functions, to guarantee there is only one function of a particular
1129 // structure.
1130 llvm::FoldingSetNodeID ID;
1131 FunctionTypeNoProto::Profile(ID, ResultTy);
1132
1133 void *InsertPos = 0;
1134 if (FunctionTypeNoProto *FT =
1135 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1136 return QualType(FT, 0);
1137
1138 QualType Canonical;
1139 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001140 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001141
1142 // Get the new insert position for the node we care about.
1143 FunctionTypeNoProto *NewIP =
1144 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001145 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001146 }
1147
Steve Naroff93fd2112009-01-27 22:08:43 +00001148 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001149 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001150 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001151 return QualType(New, 0);
1152}
1153
1154/// getFunctionType - Return a normal function type with a typed argument
1155/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001156QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001157 unsigned NumArgs, bool isVariadic,
1158 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001159 // Unique functions, to guarantee there is only one function of a particular
1160 // structure.
1161 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001162 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1163 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001164
1165 void *InsertPos = 0;
1166 if (FunctionTypeProto *FTP =
1167 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1168 return QualType(FTP, 0);
1169
1170 // Determine whether the type being created is already canonical or not.
1171 bool isCanonical = ResultTy->isCanonical();
1172 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1173 if (!ArgArray[i]->isCanonical())
1174 isCanonical = false;
1175
1176 // If this type isn't canonical, get the canonical version of it.
1177 QualType Canonical;
1178 if (!isCanonical) {
1179 llvm::SmallVector<QualType, 16> CanonicalArgs;
1180 CanonicalArgs.reserve(NumArgs);
1181 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001182 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001183
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001184 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001185 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001186 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001187
1188 // Get the new insert position for the node we care about.
1189 FunctionTypeProto *NewIP =
1190 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001191 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001192 }
1193
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001194 // FunctionTypeProto objects are allocated with extra bytes after them
1195 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001196 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001197 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1198 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001199 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001200 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001201 Types.push_back(FTP);
1202 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1203 return QualType(FTP, 0);
1204}
1205
Douglas Gregor1d661552008-04-13 21:07:44 +00001206/// getTypeDeclType - Return the unique reference to the type for the
1207/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001208QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001209 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001210 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1211
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001212 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001213 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001214 else if (isa<TemplateTypeParmDecl>(Decl)) {
1215 assert(false && "Template type parameter types are always available.");
1216 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001217 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001218
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001219 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001220 if (PrevDecl)
1221 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001222 else
1223 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001224 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001225 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001226 if (PrevDecl)
1227 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001228 else
1229 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001230 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001231 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1232 if (PrevDecl)
1233 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001234 else
1235 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001236 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001237 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001238 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001239
Ted Kremenek46a837c2008-09-05 17:16:31 +00001240 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001241 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001242}
1243
Chris Lattner4b009652007-07-25 00:24:17 +00001244/// getTypedefType - Return the unique reference to the type for the
1245/// specified typename decl.
1246QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1247 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1248
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001249 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001250 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001251 Types.push_back(Decl->TypeForDecl);
1252 return QualType(Decl->TypeForDecl, 0);
1253}
1254
Ted Kremenek42730c52008-01-07 19:49:32 +00001255/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001256/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001257QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001258 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1259
Steve Naroff93fd2112009-01-27 22:08:43 +00001260 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001261 Types.push_back(Decl->TypeForDecl);
1262 return QualType(Decl->TypeForDecl, 0);
1263}
1264
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001265/// buildObjCInterfaceType - Returns a new type for the interface
1266/// declaration, regardless. It also removes any previously built
1267/// record declaration so caller can rebuild it.
1268QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1269 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1270 if (RD)
1271 RD = 0;
1272 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1273 Types.push_back(Decl->TypeForDecl);
1274 return QualType(Decl->TypeForDecl, 0);
1275}
1276
Douglas Gregora4918772009-02-05 23:33:38 +00001277/// \brief Retrieve the template type parameter type for a template
1278/// parameter with the given depth, index, and (optionally) name.
1279QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1280 IdentifierInfo *Name) {
1281 llvm::FoldingSetNodeID ID;
1282 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1283 void *InsertPos = 0;
1284 TemplateTypeParmType *TypeParm
1285 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1286
1287 if (TypeParm)
1288 return QualType(TypeParm, 0);
1289
1290 if (Name)
1291 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1292 getTemplateTypeParmType(Depth, Index));
1293 else
1294 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1295
1296 Types.push_back(TypeParm);
1297 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1298
1299 return QualType(TypeParm, 0);
1300}
1301
Douglas Gregor8e458f42009-02-09 18:46:07 +00001302QualType
1303ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1304 unsigned NumArgs,
1305 uintptr_t *Args, bool *ArgIsType,
1306 QualType Canon) {
1307 llvm::FoldingSetNodeID ID;
1308 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1309 ArgIsType);
1310 void *InsertPos = 0;
1311 ClassTemplateSpecializationType *Spec
1312 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1313
1314 if (Spec)
1315 return QualType(Spec, 0);
1316
1317 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1318 (sizeof(uintptr_t) *
1319 (ClassTemplateSpecializationType::
1320 getNumPackedWords(NumArgs) +
1321 NumArgs)), 8);
1322 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1323 ArgIsType, Canon);
1324 Types.push_back(Spec);
1325 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1326
1327 return QualType(Spec, 0);
1328}
1329
Chris Lattnere1352302008-04-07 04:56:42 +00001330/// CmpProtocolNames - Comparison predicate for sorting protocols
1331/// alphabetically.
1332static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1333 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001334 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001335}
1336
1337static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1338 unsigned &NumProtocols) {
1339 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1340
1341 // Sort protocols, keyed by name.
1342 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1343
1344 // Remove duplicates.
1345 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1346 NumProtocols = ProtocolsEnd-Protocols;
1347}
1348
1349
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001350/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1351/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001352QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1353 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001354 // Sort the protocol list alphabetically to canonicalize it.
1355 SortAndUniqueProtocols(Protocols, NumProtocols);
1356
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001357 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001358 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001359
1360 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001361 if (ObjCQualifiedInterfaceType *QT =
1362 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001363 return QualType(QT, 0);
1364
1365 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001366 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001367 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001368
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001369 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001370 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001371 return QualType(QType, 0);
1372}
1373
Chris Lattnere1352302008-04-07 04:56:42 +00001374/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1375/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001376QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001377 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001378 // Sort the protocol list alphabetically to canonicalize it.
1379 SortAndUniqueProtocols(Protocols, NumProtocols);
1380
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001381 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001382 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001383
1384 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001385 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001386 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001387 return QualType(QT, 0);
1388
1389 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001390 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001391 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001392 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001393 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001394 return QualType(QType, 0);
1395}
1396
Steve Naroff0604dd92007-08-01 18:02:17 +00001397/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1398/// TypeOfExpr AST's (since expression's are never shared). For example,
1399/// multiple declarations that refer to "typeof(x)" all contain different
1400/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1401/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001402QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001403 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001404 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001405 Types.push_back(toe);
1406 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001407}
1408
Steve Naroff0604dd92007-08-01 18:02:17 +00001409/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1410/// TypeOfType AST's. The only motivation to unique these nodes would be
1411/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1412/// an issue. This doesn't effect the type checker, since it operates
1413/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001414QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001415 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001416 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001417 Types.push_back(tot);
1418 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001419}
1420
Chris Lattner4b009652007-07-25 00:24:17 +00001421/// getTagDeclType - Return the unique reference to the type for the
1422/// specified TagDecl (struct/union/class/enum) decl.
1423QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001424 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001425 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001426}
1427
1428/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1429/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1430/// needs to agree with the definition in <stddef.h>.
1431QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001432 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001433}
1434
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001435/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001436/// width of characters in wide strings, The value is target dependent and
1437/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001438QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001439 if (LangOpts.CPlusPlus)
1440 return WCharTy;
1441
Douglas Gregorc6507e42008-11-03 14:12:49 +00001442 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1443 // wide-character type?
1444 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001445}
1446
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001447/// getSignedWCharType - Return the type of "signed wchar_t".
1448/// Used when in C++, as a GCC extension.
1449QualType ASTContext::getSignedWCharType() const {
1450 // FIXME: derive from "Target" ?
1451 return WCharTy;
1452}
1453
1454/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1455/// Used when in C++, as a GCC extension.
1456QualType ASTContext::getUnsignedWCharType() const {
1457 // FIXME: derive from "Target" ?
1458 return UnsignedIntTy;
1459}
1460
Chris Lattner4b009652007-07-25 00:24:17 +00001461/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1462/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1463QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001464 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001465}
1466
Chris Lattner19eb97e2008-04-02 05:18:44 +00001467//===----------------------------------------------------------------------===//
1468// Type Operators
1469//===----------------------------------------------------------------------===//
1470
Chris Lattner3dae6f42008-04-06 22:41:35 +00001471/// getCanonicalType - Return the canonical (structural) type corresponding to
1472/// the specified potentially non-canonical type. The non-canonical version
1473/// of a type may have many "decorated" versions of types. Decorators can
1474/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1475/// to be free of any of these, allowing two canonical types to be compared
1476/// for exact equality with a simple pointer comparison.
1477QualType ASTContext::getCanonicalType(QualType T) {
1478 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001479
1480 // If the result has type qualifiers, make sure to canonicalize them as well.
1481 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1482 if (TypeQuals == 0) return CanType;
1483
1484 // If the type qualifiers are on an array type, get the canonical type of the
1485 // array with the qualifiers applied to the element type.
1486 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1487 if (!AT)
1488 return CanType.getQualifiedType(TypeQuals);
1489
1490 // Get the canonical version of the element with the extra qualifiers on it.
1491 // This can recursively sink qualifiers through multiple levels of arrays.
1492 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1493 NewEltTy = getCanonicalType(NewEltTy);
1494
1495 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1496 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1497 CAT->getIndexTypeQualifier());
1498 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1499 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1500 IAT->getIndexTypeQualifier());
1501
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001502 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1503 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1504 DSAT->getSizeModifier(),
1505 DSAT->getIndexTypeQualifier());
1506
Chris Lattnera1923f62008-08-04 07:31:14 +00001507 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1508 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1509 VAT->getSizeModifier(),
1510 VAT->getIndexTypeQualifier());
1511}
1512
1513
1514const ArrayType *ASTContext::getAsArrayType(QualType T) {
1515 // Handle the non-qualified case efficiently.
1516 if (T.getCVRQualifiers() == 0) {
1517 // Handle the common positive case fast.
1518 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1519 return AT;
1520 }
1521
1522 // Handle the common negative case fast, ignoring CVR qualifiers.
1523 QualType CType = T->getCanonicalTypeInternal();
1524
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001525 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001526 // test.
1527 if (!isa<ArrayType>(CType) &&
1528 !isa<ArrayType>(CType.getUnqualifiedType()))
1529 return 0;
1530
1531 // Apply any CVR qualifiers from the array type to the element type. This
1532 // implements C99 6.7.3p8: "If the specification of an array type includes
1533 // any type qualifiers, the element type is so qualified, not the array type."
1534
1535 // If we get here, we either have type qualifiers on the type, or we have
1536 // sugar such as a typedef in the way. If we have type qualifiers on the type
1537 // we must propagate them down into the elemeng type.
1538 unsigned CVRQuals = T.getCVRQualifiers();
1539 unsigned AddrSpace = 0;
1540 Type *Ty = T.getTypePtr();
1541
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001542 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001543 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001544 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1545 AddrSpace = EXTQT->getAddressSpace();
1546 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001547 } else {
1548 T = Ty->getDesugaredType();
1549 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1550 break;
1551 CVRQuals |= T.getCVRQualifiers();
1552 Ty = T.getTypePtr();
1553 }
1554 }
1555
1556 // If we have a simple case, just return now.
1557 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1558 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1559 return ATy;
1560
1561 // Otherwise, we have an array and we have qualifiers on it. Push the
1562 // qualifiers into the array element type and return a new array type.
1563 // Get the canonical version of the element with the extra qualifiers on it.
1564 // This can recursively sink qualifiers through multiple levels of arrays.
1565 QualType NewEltTy = ATy->getElementType();
1566 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001567 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001568 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1569
1570 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1571 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1572 CAT->getSizeModifier(),
1573 CAT->getIndexTypeQualifier()));
1574 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1575 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1576 IAT->getSizeModifier(),
1577 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001578
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001579 if (const DependentSizedArrayType *DSAT
1580 = dyn_cast<DependentSizedArrayType>(ATy))
1581 return cast<ArrayType>(
1582 getDependentSizedArrayType(NewEltTy,
1583 DSAT->getSizeExpr(),
1584 DSAT->getSizeModifier(),
1585 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001586
Chris Lattnera1923f62008-08-04 07:31:14 +00001587 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1588 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1589 VAT->getSizeModifier(),
1590 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001591}
1592
1593
Chris Lattner19eb97e2008-04-02 05:18:44 +00001594/// getArrayDecayedType - Return the properly qualified result of decaying the
1595/// specified array type to a pointer. This operation is non-trivial when
1596/// handling typedefs etc. The canonical type of "T" must be an array type,
1597/// this returns a pointer to a properly qualified element of the array.
1598///
1599/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1600QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001601 // Get the element type with 'getAsArrayType' so that we don't lose any
1602 // typedefs in the element type of the array. This also handles propagation
1603 // of type qualifiers from the array type into the element type if present
1604 // (C99 6.7.3p8).
1605 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1606 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001607
Chris Lattnera1923f62008-08-04 07:31:14 +00001608 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001609
1610 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001611 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001612}
1613
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001614QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001615 QualType ElemTy = VAT->getElementType();
1616
1617 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1618 return getBaseElementType(VAT);
1619
1620 return ElemTy;
1621}
1622
Chris Lattner4b009652007-07-25 00:24:17 +00001623/// getFloatingRank - Return a relative rank for floating point types.
1624/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001625static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001626 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001627 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001628
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001629 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001630 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001631 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001632 case BuiltinType::Float: return FloatRank;
1633 case BuiltinType::Double: return DoubleRank;
1634 case BuiltinType::LongDouble: return LongDoubleRank;
1635 }
1636}
1637
Steve Narofffa0c4532007-08-27 01:41:48 +00001638/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1639/// point or a complex type (based on typeDomain/typeSize).
1640/// 'typeDomain' is a real floating point or complex type.
1641/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001642QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1643 QualType Domain) const {
1644 FloatingRank EltRank = getFloatingRank(Size);
1645 if (Domain->isComplexType()) {
1646 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001647 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001648 case FloatRank: return FloatComplexTy;
1649 case DoubleRank: return DoubleComplexTy;
1650 case LongDoubleRank: return LongDoubleComplexTy;
1651 }
Chris Lattner4b009652007-07-25 00:24:17 +00001652 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001653
1654 assert(Domain->isRealFloatingType() && "Unknown domain!");
1655 switch (EltRank) {
1656 default: assert(0 && "getFloatingRank(): illegal value for rank");
1657 case FloatRank: return FloatTy;
1658 case DoubleRank: return DoubleTy;
1659 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001660 }
Chris Lattner4b009652007-07-25 00:24:17 +00001661}
1662
Chris Lattner51285d82008-04-06 23:55:33 +00001663/// getFloatingTypeOrder - Compare the rank of the two specified floating
1664/// point types, ignoring the domain of the type (i.e. 'double' ==
1665/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1666/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001667int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1668 FloatingRank LHSR = getFloatingRank(LHS);
1669 FloatingRank RHSR = getFloatingRank(RHS);
1670
1671 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001672 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001673 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001674 return 1;
1675 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001676}
1677
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001678/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1679/// routine will assert if passed a built-in type that isn't an integer or enum,
1680/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001681unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001682 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001683 if (EnumType* ET = dyn_cast<EnumType>(T))
1684 T = ET->getDecl()->getIntegerType().getTypePtr();
1685
1686 // There are two things which impact the integer rank: the width, and
1687 // the ordering of builtins. The builtin ordering is encoded in the
1688 // bottom three bits; the width is encoded in the bits above that.
1689 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1690 return FWIT->getWidth() << 3;
1691 }
1692
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001693 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001694 default: assert(0 && "getIntegerRank(): not a built-in integer");
1695 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001696 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001697 case BuiltinType::Char_S:
1698 case BuiltinType::Char_U:
1699 case BuiltinType::SChar:
1700 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001701 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001702 case BuiltinType::Short:
1703 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001704 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001705 case BuiltinType::Int:
1706 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001707 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001708 case BuiltinType::Long:
1709 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001710 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001711 case BuiltinType::LongLong:
1712 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001713 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001714 }
1715}
1716
Chris Lattner51285d82008-04-06 23:55:33 +00001717/// getIntegerTypeOrder - Returns the highest ranked integer type:
1718/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1719/// LHS < RHS, return -1.
1720int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001721 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1722 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001723 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001724
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001725 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1726 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001727
Chris Lattner51285d82008-04-06 23:55:33 +00001728 unsigned LHSRank = getIntegerRank(LHSC);
1729 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001730
Chris Lattner51285d82008-04-06 23:55:33 +00001731 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1732 if (LHSRank == RHSRank) return 0;
1733 return LHSRank > RHSRank ? 1 : -1;
1734 }
Chris Lattner4b009652007-07-25 00:24:17 +00001735
Chris Lattner51285d82008-04-06 23:55:33 +00001736 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1737 if (LHSUnsigned) {
1738 // If the unsigned [LHS] type is larger, return it.
1739 if (LHSRank >= RHSRank)
1740 return 1;
1741
1742 // If the signed type can represent all values of the unsigned type, it
1743 // wins. Because we are dealing with 2's complement and types that are
1744 // powers of two larger than each other, this is always safe.
1745 return -1;
1746 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001747
Chris Lattner51285d82008-04-06 23:55:33 +00001748 // If the unsigned [RHS] type is larger, return it.
1749 if (RHSRank >= LHSRank)
1750 return -1;
1751
1752 // If the signed type can represent all values of the unsigned type, it
1753 // wins. Because we are dealing with 2's complement and types that are
1754 // powers of two larger than each other, this is always safe.
1755 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001756}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001757
1758// getCFConstantStringType - Return the type used for constant CFStrings.
1759QualType ASTContext::getCFConstantStringType() {
1760 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001761 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001762 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001763 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001764 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001765
1766 // const int *isa;
1767 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001768 // int flags;
1769 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001770 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001771 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001772 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001773 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001774
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001775 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001776 for (unsigned i = 0; i < 4; ++i) {
1777 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1778 SourceLocation(), 0,
1779 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001780 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001781 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001782 }
1783
1784 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001785 }
1786
1787 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001788}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001789
Anders Carlssonf58cac72008-08-30 19:34:46 +00001790QualType ASTContext::getObjCFastEnumerationStateType()
1791{
1792 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001793 ObjCFastEnumerationStateTypeDecl =
1794 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1795 &Idents.get("__objcFastEnumerationState"));
1796
Anders Carlssonf58cac72008-08-30 19:34:46 +00001797 QualType FieldTypes[] = {
1798 UnsignedLongTy,
1799 getPointerType(ObjCIdType),
1800 getPointerType(UnsignedLongTy),
1801 getConstantArrayType(UnsignedLongTy,
1802 llvm::APInt(32, 5), ArrayType::Normal, 0)
1803 };
1804
Douglas Gregor8acb7272008-12-11 16:49:14 +00001805 for (size_t i = 0; i < 4; ++i) {
1806 FieldDecl *Field = FieldDecl::Create(*this,
1807 ObjCFastEnumerationStateTypeDecl,
1808 SourceLocation(), 0,
1809 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001810 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001811 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001812 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001813
Douglas Gregor8acb7272008-12-11 16:49:14 +00001814 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001815 }
1816
1817 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1818}
1819
Anders Carlssone3f02572007-10-29 06:33:42 +00001820// This returns true if a type has been typedefed to BOOL:
1821// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001822static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001823 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001824 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1825 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001826
1827 return false;
1828}
1829
Ted Kremenek42730c52008-01-07 19:49:32 +00001830/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001831/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001832int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001833 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001834
1835 // Make all integer and enum types at least as large as an int
1836 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001837 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001838 // Treat arrays as pointers, since that's how they're passed in.
1839 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001840 sz = getTypeSize(VoidPtrTy);
1841 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001842}
1843
Ted Kremenek42730c52008-01-07 19:49:32 +00001844/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001845/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001846void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001847 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001848 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001849 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001850 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001851 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001852 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001853 // Compute size of all parameters.
1854 // Start with computing size of a pointer in number of bytes.
1855 // FIXME: There might(should) be a better way of doing this computation!
1856 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001857 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001858 // The first two arguments (self and _cmd) are pointers; account for
1859 // their size.
1860 int ParmOffset = 2 * PtrSize;
1861 int NumOfParams = Decl->getNumParams();
1862 for (int i = 0; i < NumOfParams; i++) {
1863 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001864 int sz = getObjCEncodingTypeSize (PType);
1865 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001866 ParmOffset += sz;
1867 }
1868 S += llvm::utostr(ParmOffset);
1869 S += "@0:";
1870 S += llvm::utostr(PtrSize);
1871
1872 // Argument types.
1873 ParmOffset = 2 * PtrSize;
1874 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001875 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1876 QualType PType = PVDecl->getOriginalType();
1877 if (const ArrayType *AT =
1878 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1879 // Use array's original type only if it has known number of
1880 // elements.
1881 if (!dyn_cast<ConstantArrayType>(AT))
1882 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001883 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001884 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001885 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001886 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001887 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001888 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001889 }
1890}
1891
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001892/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001893/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001894/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1895/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001896/// Property attributes are stored as a comma-delimited C string. The simple
1897/// attributes readonly and bycopy are encoded as single characters. The
1898/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1899/// encoded as single characters, followed by an identifier. Property types
1900/// are also encoded as a parametrized attribute. The characters used to encode
1901/// these attributes are defined by the following enumeration:
1902/// @code
1903/// enum PropertyAttributes {
1904/// kPropertyReadOnly = 'R', // property is read-only.
1905/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1906/// kPropertyByref = '&', // property is a reference to the value last assigned
1907/// kPropertyDynamic = 'D', // property is dynamic
1908/// kPropertyGetter = 'G', // followed by getter selector name
1909/// kPropertySetter = 'S', // followed by setter selector name
1910/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1911/// kPropertyType = 't' // followed by old-style type encoding.
1912/// kPropertyWeak = 'W' // 'weak' property
1913/// kPropertyStrong = 'P' // property GC'able
1914/// kPropertyNonAtomic = 'N' // property non-atomic
1915/// };
1916/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001917void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1918 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001919 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001920 // Collect information from the property implementation decl(s).
1921 bool Dynamic = false;
1922 ObjCPropertyImplDecl *SynthesizePID = 0;
1923
1924 // FIXME: Duplicated code due to poor abstraction.
1925 if (Container) {
1926 if (const ObjCCategoryImplDecl *CID =
1927 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1928 for (ObjCCategoryImplDecl::propimpl_iterator
1929 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1930 ObjCPropertyImplDecl *PID = *i;
1931 if (PID->getPropertyDecl() == PD) {
1932 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1933 Dynamic = true;
1934 } else {
1935 SynthesizePID = PID;
1936 }
1937 }
1938 }
1939 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001940 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001941 for (ObjCCategoryImplDecl::propimpl_iterator
1942 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1943 ObjCPropertyImplDecl *PID = *i;
1944 if (PID->getPropertyDecl() == PD) {
1945 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1946 Dynamic = true;
1947 } else {
1948 SynthesizePID = PID;
1949 }
1950 }
1951 }
1952 }
1953 }
1954
1955 // FIXME: This is not very efficient.
1956 S = "T";
1957
1958 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001959 // GCC has some special rules regarding encoding of properties which
1960 // closely resembles encoding of ivars.
1961 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1962 true /* outermost type */,
1963 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001964
1965 if (PD->isReadOnly()) {
1966 S += ",R";
1967 } else {
1968 switch (PD->getSetterKind()) {
1969 case ObjCPropertyDecl::Assign: break;
1970 case ObjCPropertyDecl::Copy: S += ",C"; break;
1971 case ObjCPropertyDecl::Retain: S += ",&"; break;
1972 }
1973 }
1974
1975 // It really isn't clear at all what this means, since properties
1976 // are "dynamic by default".
1977 if (Dynamic)
1978 S += ",D";
1979
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001980 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1981 S += ",N";
1982
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001983 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1984 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001985 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001986 }
1987
1988 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1989 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001990 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001991 }
1992
1993 if (SynthesizePID) {
1994 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1995 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001996 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001997 }
1998
1999 // FIXME: OBJCGC: weak & strong
2000}
2001
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002002/// getLegacyIntegralTypeEncoding -
2003/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002004/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002005/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2006///
2007void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2008 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2009 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002010 if (BT->getKind() == BuiltinType::ULong &&
2011 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002012 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002013 else
2014 if (BT->getKind() == BuiltinType::Long &&
2015 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002016 PointeeTy = IntTy;
2017 }
2018 }
2019}
2020
Fariborz Jahanian248db262008-01-22 22:44:46 +00002021void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002022 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002023 // We follow the behavior of gcc, expanding structures which are
2024 // directly pointed to, and expanding embedded structures. Note that
2025 // these rules are sufficient to prevent recursive encoding of the
2026 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002027 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2028 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002029}
2030
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002031static void EncodeBitField(const ASTContext *Context, std::string& S,
2032 FieldDecl *FD) {
2033 const Expr *E = FD->getBitWidth();
2034 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2035 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2036 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2037 S += 'b';
2038 S += llvm::utostr(N);
2039}
2040
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002041void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2042 bool ExpandPointedToStructures,
2043 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002044 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002045 bool OutermostType,
2046 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002047 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002048 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002049 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002050 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002051 else {
2052 char encoding;
2053 switch (BT->getKind()) {
2054 default: assert(0 && "Unhandled builtin type kind");
2055 case BuiltinType::Void: encoding = 'v'; break;
2056 case BuiltinType::Bool: encoding = 'B'; break;
2057 case BuiltinType::Char_U:
2058 case BuiltinType::UChar: encoding = 'C'; break;
2059 case BuiltinType::UShort: encoding = 'S'; break;
2060 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002061 case BuiltinType::ULong:
2062 encoding =
2063 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2064 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002065 case BuiltinType::ULongLong: encoding = 'Q'; break;
2066 case BuiltinType::Char_S:
2067 case BuiltinType::SChar: encoding = 'c'; break;
2068 case BuiltinType::Short: encoding = 's'; break;
2069 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002070 case BuiltinType::Long:
2071 encoding =
2072 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2073 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002074 case BuiltinType::LongLong: encoding = 'q'; break;
2075 case BuiltinType::Float: encoding = 'f'; break;
2076 case BuiltinType::Double: encoding = 'd'; break;
2077 case BuiltinType::LongDouble: encoding = 'd'; break;
2078 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002079
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002080 S += encoding;
2081 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002082 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002083 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002084 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2085 ExpandPointedToStructures,
2086 ExpandStructures, FD);
2087 if (FD || EncodingProperty) {
2088 // Note that we do extended encoding of protocol qualifer list
2089 // Only when doing ivar or property encoding.
2090 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2091 S += '"';
2092 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2093 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2094 S += '<';
2095 S += Proto->getNameAsString();
2096 S += '>';
2097 }
2098 S += '"';
2099 }
2100 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002101 }
2102 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002103 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002104 bool isReadOnly = false;
2105 // For historical/compatibility reasons, the read-only qualifier of the
2106 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2107 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2108 // Also, do not emit the 'r' for anything but the outermost type!
2109 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2110 if (OutermostType && T.isConstQualified()) {
2111 isReadOnly = true;
2112 S += 'r';
2113 }
2114 }
2115 else if (OutermostType) {
2116 QualType P = PointeeTy;
2117 while (P->getAsPointerType())
2118 P = P->getAsPointerType()->getPointeeType();
2119 if (P.isConstQualified()) {
2120 isReadOnly = true;
2121 S += 'r';
2122 }
2123 }
2124 if (isReadOnly) {
2125 // Another legacy compatibility encoding. Some ObjC qualifier and type
2126 // combinations need to be rearranged.
2127 // Rewrite "in const" from "nr" to "rn"
2128 const char * s = S.c_str();
2129 int len = S.length();
2130 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2131 std::string replace = "rn";
2132 S.replace(S.end()-2, S.end(), replace);
2133 }
2134 }
Steve Naroff17c03822009-02-12 17:52:19 +00002135 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002136 S += '@';
2137 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002138 }
2139 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002140 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002141 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002142 // Another historical/compatibility reason.
2143 // We encode the underlying type which comes out as
2144 // {...};
2145 S += '^';
2146 getObjCEncodingForTypeImpl(PointeeTy, S,
2147 false, ExpandPointedToStructures,
2148 NULL);
2149 return;
2150 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002151 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002152 if (FD || EncodingProperty) {
2153 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2154 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002155 S += '"';
2156 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002157 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2158 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2159 S += '<';
2160 S += Proto->getNameAsString();
2161 S += '>';
2162 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002163 S += '"';
2164 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002165 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002166 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002167 S += '#';
2168 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002169 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002170 S += ':';
2171 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002172 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002173
2174 if (PointeeTy->isCharType()) {
2175 // char pointer types should be encoded as '*' unless it is a
2176 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002177 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002178 S += '*';
2179 return;
2180 }
2181 }
2182
2183 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002184 getLegacyIntegralTypeEncoding(PointeeTy);
2185
2186 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002187 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002188 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002189 } else if (const ArrayType *AT =
2190 // Ignore type qualifiers etc.
2191 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002192 S += '[';
2193
2194 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2195 S += llvm::utostr(CAT->getSize().getZExtValue());
2196 else
2197 assert(0 && "Unhandled array type!");
2198
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002199 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002200 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002201 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002202 } else if (T->getAsFunctionType()) {
2203 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002204 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002205 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002206 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002207 // Anonymous structures print as '?'
2208 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2209 S += II->getName();
2210 } else {
2211 S += '?';
2212 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002213 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002214 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002215 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2216 FieldEnd = RDecl->field_end();
2217 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002218 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002219 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002220 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002221 S += '"';
2222 }
2223
2224 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002225 if (Field->isBitField()) {
2226 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2227 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002228 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002229 QualType qt = Field->getType();
2230 getLegacyIntegralTypeEncoding(qt);
2231 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002232 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002233 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002234 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002235 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002236 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002237 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002238 if (FD && FD->isBitField())
2239 EncodeBitField(this, S, FD);
2240 else
2241 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002242 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002243 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002244 } else if (T->isObjCInterfaceType()) {
2245 // @encode(class_name)
2246 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2247 S += '{';
2248 const IdentifierInfo *II = OI->getIdentifier();
2249 S += II->getName();
2250 S += '=';
2251 std::vector<FieldDecl*> RecFields;
2252 CollectObjCIvars(OI, RecFields);
2253 for (unsigned int i = 0; i != RecFields.size(); i++) {
2254 if (RecFields[i]->isBitField())
2255 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2256 RecFields[i]);
2257 else
2258 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2259 FD);
2260 }
2261 S += '}';
2262 }
2263 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002264 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002265}
2266
Ted Kremenek42730c52008-01-07 19:49:32 +00002267void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002268 std::string& S) const {
2269 if (QT & Decl::OBJC_TQ_In)
2270 S += 'n';
2271 if (QT & Decl::OBJC_TQ_Inout)
2272 S += 'N';
2273 if (QT & Decl::OBJC_TQ_Out)
2274 S += 'o';
2275 if (QT & Decl::OBJC_TQ_Bycopy)
2276 S += 'O';
2277 if (QT & Decl::OBJC_TQ_Byref)
2278 S += 'R';
2279 if (QT & Decl::OBJC_TQ_Oneway)
2280 S += 'V';
2281}
2282
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002283void ASTContext::setBuiltinVaListType(QualType T)
2284{
2285 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2286
2287 BuiltinVaListType = T;
2288}
2289
Ted Kremenek42730c52008-01-07 19:49:32 +00002290void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002291{
Ted Kremenek42730c52008-01-07 19:49:32 +00002292 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002293
2294 // typedef struct objc_object *id;
2295 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002296 // User error - caller will issue diagnostics.
2297 if (!ptr)
2298 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002299 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002300 // User error - caller will issue diagnostics.
2301 if (!rec)
2302 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002303 IdStructType = rec;
2304}
2305
Ted Kremenek42730c52008-01-07 19:49:32 +00002306void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002307{
Ted Kremenek42730c52008-01-07 19:49:32 +00002308 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002309
2310 // typedef struct objc_selector *SEL;
2311 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002312 if (!ptr)
2313 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002314 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002315 if (!rec)
2316 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002317 SelStructType = rec;
2318}
2319
Ted Kremenek42730c52008-01-07 19:49:32 +00002320void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002321{
Ted Kremenek42730c52008-01-07 19:49:32 +00002322 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002323}
2324
Ted Kremenek42730c52008-01-07 19:49:32 +00002325void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002326{
Ted Kremenek42730c52008-01-07 19:49:32 +00002327 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002328
2329 // typedef struct objc_class *Class;
2330 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2331 assert(ptr && "'Class' incorrectly typed");
2332 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2333 assert(rec && "'Class' incorrectly typed");
2334 ClassStructType = rec;
2335}
2336
Ted Kremenek42730c52008-01-07 19:49:32 +00002337void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2338 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002339 "'NSConstantString' type already set!");
2340
Ted Kremenek42730c52008-01-07 19:49:32 +00002341 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002342}
2343
Douglas Gregorc6507e42008-11-03 14:12:49 +00002344/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002345/// TargetInfo, produce the corresponding type. The unsigned @p Type
2346/// is actually a value of type @c TargetInfo::IntType.
2347QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002348 switch (Type) {
2349 case TargetInfo::NoInt: return QualType();
2350 case TargetInfo::SignedShort: return ShortTy;
2351 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2352 case TargetInfo::SignedInt: return IntTy;
2353 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2354 case TargetInfo::SignedLong: return LongTy;
2355 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2356 case TargetInfo::SignedLongLong: return LongLongTy;
2357 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2358 }
2359
2360 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002361 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002362}
Ted Kremenek118930e2008-07-24 23:58:27 +00002363
2364//===----------------------------------------------------------------------===//
2365// Type Predicates.
2366//===----------------------------------------------------------------------===//
2367
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002368/// isObjCNSObjectType - Return true if this is an NSObject object using
2369/// NSObject attribute on a c-style pointer type.
2370/// FIXME - Make it work directly on types.
2371///
2372bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2373 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2374 if (TypedefDecl *TD = TDT->getDecl())
2375 if (TD->getAttr<ObjCNSObjectAttr>())
2376 return true;
2377 }
2378 return false;
2379}
2380
Ted Kremenek118930e2008-07-24 23:58:27 +00002381/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2382/// to an object type. This includes "id" and "Class" (two 'special' pointers
2383/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2384/// ID type).
2385bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2386 if (Ty->isObjCQualifiedIdType())
2387 return true;
2388
Steve Naroffd9e00802008-10-21 18:24:04 +00002389 // Blocks are objects.
2390 if (Ty->isBlockPointerType())
2391 return true;
2392
2393 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002394 if (!Ty->isPointerType())
2395 return false;
2396
2397 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2398 // pointer types. This looks for the typedef specifically, not for the
2399 // underlying type.
2400 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2401 return true;
2402
2403 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002404 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2405 return true;
2406
2407 // If is has NSObject attribute, OK as well.
2408 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002409}
2410
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002411/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2412/// garbage collection attribute.
2413///
2414QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002415 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002416 if (getLangOptions().ObjC1 &&
2417 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002418 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002419 // Default behavious under objective-c's gc is for objective-c pointers
2420 // be treated as though they were declared as __strong.
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002421 if (GCAttrs == QualType::GCNone && isObjCObjectPointerType(Ty))
2422 GCAttrs = QualType::Strong;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002423 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002424 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002425}
2426
Chris Lattner6ff358b2008-04-07 06:51:04 +00002427//===----------------------------------------------------------------------===//
2428// Type Compatibility Testing
2429//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002430
Steve Naroff3454b6c2008-09-04 15:10:53 +00002431/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002432/// block types. Types must be strictly compatible here. For example,
2433/// C unfortunately doesn't produce an error for the following:
2434///
2435/// int (*emptyArgFunc)();
2436/// int (*intArgList)(int) = emptyArgFunc;
2437///
2438/// For blocks, we will produce an error for the following (similar to C++):
2439///
2440/// int (^emptyArgBlock)();
2441/// int (^intArgBlock)(int) = emptyArgBlock;
2442///
2443/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2444///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002445bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002446 const FunctionType *lbase = lhs->getAsFunctionType();
2447 const FunctionType *rbase = rhs->getAsFunctionType();
2448 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2449 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2450 if (lproto && rproto)
2451 return !mergeTypes(lhs, rhs).isNull();
2452 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002453}
2454
Chris Lattner6ff358b2008-04-07 06:51:04 +00002455/// areCompatVectorTypes - Return true if the two specified vector types are
2456/// compatible.
2457static bool areCompatVectorTypes(const VectorType *LHS,
2458 const VectorType *RHS) {
2459 assert(LHS->isCanonical() && RHS->isCanonical());
2460 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002461 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002462}
2463
Eli Friedman0d9549b2008-08-22 00:56:42 +00002464/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002465/// compatible for assignment from RHS to LHS. This handles validation of any
2466/// protocol qualifiers on the LHS or RHS.
2467///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002468bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2469 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002470 // Verify that the base decls are compatible: the RHS must be a subclass of
2471 // the LHS.
2472 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2473 return false;
2474
2475 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2476 // protocol qualified at all, then we are good.
2477 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2478 return true;
2479
2480 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2481 // isn't a superset.
2482 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2483 return true; // FIXME: should return false!
2484
2485 // Finally, we must have two protocol-qualified interfaces.
2486 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2487 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2488 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2489 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2490 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2491 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2492
2493 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2494 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2495 // LHS in a single parallel scan until we run out of elements in LHS.
2496 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2497 ObjCProtocolDecl *LHSProto = *LHSPI;
2498
2499 while (RHSPI != RHSPE) {
2500 ObjCProtocolDecl *RHSProto = *RHSPI++;
2501 // If the RHS has a protocol that the LHS doesn't, ignore it.
2502 if (RHSProto != LHSProto)
2503 continue;
2504
2505 // Otherwise, the RHS does have this element.
2506 ++LHSPI;
2507 if (LHSPI == LHSPE)
2508 return true; // All protocols in LHS exist in RHS.
2509
2510 LHSProto = *LHSPI;
2511 }
2512
2513 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2514 return false;
2515}
2516
Steve Naroff17c03822009-02-12 17:52:19 +00002517bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2518 // get the "pointed to" types
2519 const PointerType *LHSPT = LHS->getAsPointerType();
2520 const PointerType *RHSPT = RHS->getAsPointerType();
2521
2522 if (!LHSPT || !RHSPT)
2523 return false;
2524
2525 QualType lhptee = LHSPT->getPointeeType();
2526 QualType rhptee = RHSPT->getPointeeType();
2527 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2528 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2529 // ID acts sort of like void* for ObjC interfaces
2530 if (LHSIface && isObjCIdStructType(rhptee))
2531 return true;
2532 if (RHSIface && isObjCIdStructType(lhptee))
2533 return true;
2534 if (!LHSIface || !RHSIface)
2535 return false;
2536 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2537 canAssignObjCInterfaces(RHSIface, LHSIface);
2538}
2539
Steve Naroff85f0dc52007-10-15 20:41:53 +00002540/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2541/// both shall have the identically qualified version of a compatible type.
2542/// C99 6.2.7p1: Two types have compatible types if their types are the
2543/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002544bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2545 return !mergeTypes(LHS, RHS).isNull();
2546}
2547
2548QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2549 const FunctionType *lbase = lhs->getAsFunctionType();
2550 const FunctionType *rbase = rhs->getAsFunctionType();
2551 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2552 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2553 bool allLTypes = true;
2554 bool allRTypes = true;
2555
2556 // Check return type
2557 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2558 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002559 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2560 allLTypes = false;
2561 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2562 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002563
2564 if (lproto && rproto) { // two C99 style function prototypes
2565 unsigned lproto_nargs = lproto->getNumArgs();
2566 unsigned rproto_nargs = rproto->getNumArgs();
2567
2568 // Compatible functions must have the same number of arguments
2569 if (lproto_nargs != rproto_nargs)
2570 return QualType();
2571
2572 // Variadic and non-variadic functions aren't compatible
2573 if (lproto->isVariadic() != rproto->isVariadic())
2574 return QualType();
2575
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002576 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2577 return QualType();
2578
Eli Friedman0d9549b2008-08-22 00:56:42 +00002579 // Check argument compatibility
2580 llvm::SmallVector<QualType, 10> types;
2581 for (unsigned i = 0; i < lproto_nargs; i++) {
2582 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2583 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2584 QualType argtype = mergeTypes(largtype, rargtype);
2585 if (argtype.isNull()) return QualType();
2586 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002587 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2588 allLTypes = false;
2589 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2590 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002591 }
2592 if (allLTypes) return lhs;
2593 if (allRTypes) return rhs;
2594 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002595 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002596 }
2597
2598 if (lproto) allRTypes = false;
2599 if (rproto) allLTypes = false;
2600
2601 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2602 if (proto) {
2603 if (proto->isVariadic()) return QualType();
2604 // Check that the types are compatible with the types that
2605 // would result from default argument promotions (C99 6.7.5.3p15).
2606 // The only types actually affected are promotable integer
2607 // types and floats, which would be passed as a different
2608 // type depending on whether the prototype is visible.
2609 unsigned proto_nargs = proto->getNumArgs();
2610 for (unsigned i = 0; i < proto_nargs; ++i) {
2611 QualType argTy = proto->getArgType(i);
2612 if (argTy->isPromotableIntegerType() ||
2613 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2614 return QualType();
2615 }
2616
2617 if (allLTypes) return lhs;
2618 if (allRTypes) return rhs;
2619 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002620 proto->getNumArgs(), lproto->isVariadic(),
2621 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002622 }
2623
2624 if (allLTypes) return lhs;
2625 if (allRTypes) return rhs;
2626 return getFunctionTypeNoProto(retType);
2627}
2628
2629QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002630 // C++ [expr]: If an expression initially has the type "reference to T", the
2631 // type is adjusted to "T" prior to any further analysis, the expression
2632 // designates the object or function denoted by the reference, and the
2633 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002634 // FIXME: C++ shouldn't be going through here! The rules are different
2635 // enough that they should be handled separately.
2636 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002637 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002638 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002639 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002640
Eli Friedman0d9549b2008-08-22 00:56:42 +00002641 QualType LHSCan = getCanonicalType(LHS),
2642 RHSCan = getCanonicalType(RHS);
2643
2644 // If two types are identical, they are compatible.
2645 if (LHSCan == RHSCan)
2646 return LHS;
2647
2648 // If the qualifiers are different, the types aren't compatible
2649 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2650 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2651 return QualType();
2652
2653 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2654 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2655
Chris Lattnerc38d4522008-01-14 05:45:46 +00002656 // We want to consider the two function types to be the same for these
2657 // comparisons, just force one to the other.
2658 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2659 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002660
2661 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002662 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2663 LHSClass = Type::ConstantArray;
2664 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2665 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002666
Nate Begemanaf6ed502008-04-18 23:10:10 +00002667 // Canonicalize ExtVector -> Vector.
2668 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2669 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002670
Chris Lattner7cdcb252008-04-07 06:38:24 +00002671 // Consider qualified interfaces and interfaces the same.
2672 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2673 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002674
Chris Lattnerb5709e22008-04-07 05:43:21 +00002675 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002676 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002677 // ID is compatible with all qualified id types.
2678 if (LHS->isObjCQualifiedIdType()) {
2679 if (const PointerType *PT = RHS->getAsPointerType()) {
2680 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002681 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002682 return LHS;
2683 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2684 // Unfortunately, this API is part of Sema (which we don't have access
2685 // to. Need to refactor. The following check is insufficient, since we
2686 // need to make sure the class implements the protocol.
2687 if (pType->isObjCInterfaceType())
2688 return LHS;
2689 }
2690 }
2691 if (RHS->isObjCQualifiedIdType()) {
2692 if (const PointerType *PT = LHS->getAsPointerType()) {
2693 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002694 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002695 return RHS;
2696 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2697 // Unfortunately, this API is part of Sema (which we don't have access
2698 // to. Need to refactor. The following check is insufficient, since we
2699 // need to make sure the class implements the protocol.
2700 if (pType->isObjCInterfaceType())
2701 return RHS;
2702 }
2703 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002704 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2705 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002706 if (const EnumType* ETy = LHS->getAsEnumType()) {
2707 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2708 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002709 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002710 if (const EnumType* ETy = RHS->getAsEnumType()) {
2711 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2712 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002713 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002714
Eli Friedman0d9549b2008-08-22 00:56:42 +00002715 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002716 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002717
Steve Naroffc88babe2008-01-09 22:43:08 +00002718 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002719 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002720 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002721 {
2722 // Merge two pointer types, while trying to preserve typedef info
2723 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2724 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2725 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2726 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002727 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2728 return LHS;
2729 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2730 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002731 return getPointerType(ResultType);
2732 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002733 case Type::BlockPointer:
2734 {
2735 // Merge two block pointer types, while trying to preserve typedef info
2736 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2737 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2738 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2739 if (ResultType.isNull()) return QualType();
2740 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2741 return LHS;
2742 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2743 return RHS;
2744 return getBlockPointerType(ResultType);
2745 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002746 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002747 {
2748 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2749 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2750 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2751 return QualType();
2752
2753 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2754 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2755 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2756 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002757 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2758 return LHS;
2759 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2760 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002761 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2762 ArrayType::ArraySizeModifier(), 0);
2763 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2764 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002765 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2766 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002767 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2768 return LHS;
2769 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2770 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002771 if (LVAT) {
2772 // FIXME: This isn't correct! But tricky to implement because
2773 // the array's size has to be the size of LHS, but the type
2774 // has to be different.
2775 return LHS;
2776 }
2777 if (RVAT) {
2778 // FIXME: This isn't correct! But tricky to implement because
2779 // the array's size has to be the size of RHS, but the type
2780 // has to be different.
2781 return RHS;
2782 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002783 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2784 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002785 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002786 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002787 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002788 return mergeFunctionTypes(LHS, RHS);
2789 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002790 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002791 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2792 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002793 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002794 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002795 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002796 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002797 case Type::Complex:
2798 // Distinct complex types are incompatible.
2799 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002800 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002801 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2802 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002803 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002804 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002805 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2806 // for checking assignment/comparison safety
2807 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002808 case Type::ObjCQualifiedId:
2809 // Distinct qualified id's are not compatible.
2810 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002811 default:
2812 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002813 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002814 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002815}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002816
Chris Lattner1d78a862008-04-07 07:01:58 +00002817//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002818// Integer Predicates
2819//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002820
Eli Friedman0832dbc2008-06-28 06:23:08 +00002821unsigned ASTContext::getIntWidth(QualType T) {
2822 if (T == BoolTy)
2823 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002824 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2825 return FWIT->getWidth();
2826 }
2827 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002828 return (unsigned)getTypeSize(T);
2829}
2830
2831QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2832 assert(T->isSignedIntegerType() && "Unexpected type");
2833 if (const EnumType* ETy = T->getAsEnumType())
2834 T = ETy->getDecl()->getIntegerType();
2835 const BuiltinType* BTy = T->getAsBuiltinType();
2836 assert (BTy && "Unexpected signed integer type");
2837 switch (BTy->getKind()) {
2838 case BuiltinType::Char_S:
2839 case BuiltinType::SChar:
2840 return UnsignedCharTy;
2841 case BuiltinType::Short:
2842 return UnsignedShortTy;
2843 case BuiltinType::Int:
2844 return UnsignedIntTy;
2845 case BuiltinType::Long:
2846 return UnsignedLongTy;
2847 case BuiltinType::LongLong:
2848 return UnsignedLongLongTy;
2849 default:
2850 assert(0 && "Unexpected signed integer type");
2851 return QualType();
2852 }
2853}
2854
2855
2856//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002857// Serialization Support
2858//===----------------------------------------------------------------------===//
2859
Ted Kremenek738e6c02007-10-31 17:10:13 +00002860/// Emit - Serialize an ASTContext object to Bitcode.
2861void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002862 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002863 S.EmitRef(SourceMgr);
2864 S.EmitRef(Target);
2865 S.EmitRef(Idents);
2866 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002867
Ted Kremenek68228a92007-10-31 22:44:07 +00002868 // Emit the size of the type vector so that we can reserve that size
2869 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002870 S.EmitInt(Types.size());
2871
Ted Kremenek034a78c2007-11-13 22:02:55 +00002872 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2873 I!=E;++I)
2874 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002875
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002876 S.EmitOwnedPtr(TUDecl);
2877
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002878 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002879}
2880
Ted Kremenekacba3612007-11-13 00:25:37 +00002881ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002882
2883 // Read the language options.
2884 LangOptions LOpts;
2885 LOpts.Read(D);
2886
Ted Kremenek68228a92007-10-31 22:44:07 +00002887 SourceManager &SM = D.ReadRef<SourceManager>();
2888 TargetInfo &t = D.ReadRef<TargetInfo>();
2889 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2890 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002891
Ted Kremenek68228a92007-10-31 22:44:07 +00002892 unsigned size_reserve = D.ReadInt();
2893
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002894 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2895 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002896
Ted Kremenek034a78c2007-11-13 22:02:55 +00002897 for (unsigned i = 0; i < size_reserve; ++i)
2898 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002899
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002900 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2901
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002902 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002903
2904 return A;
2905}