blob: 181ea90691177bcad60215c2f7055d29b4e291fe [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 Naroff262a5dd2009-02-21 20:17:11 +00001397/// getObjCQualifiedClassType - Return an ObjCQualifiedIdType for the 'Class'
1398/// decl and the conforming protocol list.
1399QualType ASTContext::getObjCQualifiedClassType(ObjCProtocolDecl **Protocols,
1400 unsigned NumProtocols) {
1401 // Sort the protocol list alphabetically to canonicalize it.
1402 SortAndUniqueProtocols(Protocols, NumProtocols);
1403
1404 llvm::FoldingSetNodeID ID;
1405 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
1406
1407 void *InsertPos = 0;
1408 if (ObjCQualifiedClassType *QT =
1409 ObjCQualifiedClassTypes.FindNodeOrInsertPos(ID, InsertPos))
1410 return QualType(QT, 0);
1411
1412 // No Match;
1413 ObjCQualifiedClassType *QType =
1414 new (*this,8) ObjCQualifiedClassType(Protocols, NumProtocols);
1415 Types.push_back(QType);
1416 ObjCQualifiedClassTypes.InsertNode(QType, InsertPos);
1417 return QualType(QType, 0);
1418}
1419
Steve Naroff0604dd92007-08-01 18:02:17 +00001420/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1421/// TypeOfExpr AST's (since expression's are never shared). For example,
1422/// multiple declarations that refer to "typeof(x)" all contain different
1423/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1424/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001425QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001426 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001427 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001428 Types.push_back(toe);
1429 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001430}
1431
Steve Naroff0604dd92007-08-01 18:02:17 +00001432/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1433/// TypeOfType AST's. The only motivation to unique these nodes would be
1434/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1435/// an issue. This doesn't effect the type checker, since it operates
1436/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001437QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001438 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001439 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001440 Types.push_back(tot);
1441 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001442}
1443
Chris Lattner4b009652007-07-25 00:24:17 +00001444/// getTagDeclType - Return the unique reference to the type for the
1445/// specified TagDecl (struct/union/class/enum) decl.
1446QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001447 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001448 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001449}
1450
1451/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1452/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1453/// needs to agree with the definition in <stddef.h>.
1454QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001455 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001456}
1457
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001458/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001459/// width of characters in wide strings, The value is target dependent and
1460/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001461QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001462 if (LangOpts.CPlusPlus)
1463 return WCharTy;
1464
Douglas Gregorc6507e42008-11-03 14:12:49 +00001465 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1466 // wide-character type?
1467 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001468}
1469
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001470/// getSignedWCharType - Return the type of "signed wchar_t".
1471/// Used when in C++, as a GCC extension.
1472QualType ASTContext::getSignedWCharType() const {
1473 // FIXME: derive from "Target" ?
1474 return WCharTy;
1475}
1476
1477/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1478/// Used when in C++, as a GCC extension.
1479QualType ASTContext::getUnsignedWCharType() const {
1480 // FIXME: derive from "Target" ?
1481 return UnsignedIntTy;
1482}
1483
Chris Lattner4b009652007-07-25 00:24:17 +00001484/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1485/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1486QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001487 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001488}
1489
Chris Lattner19eb97e2008-04-02 05:18:44 +00001490//===----------------------------------------------------------------------===//
1491// Type Operators
1492//===----------------------------------------------------------------------===//
1493
Chris Lattner3dae6f42008-04-06 22:41:35 +00001494/// getCanonicalType - Return the canonical (structural) type corresponding to
1495/// the specified potentially non-canonical type. The non-canonical version
1496/// of a type may have many "decorated" versions of types. Decorators can
1497/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1498/// to be free of any of these, allowing two canonical types to be compared
1499/// for exact equality with a simple pointer comparison.
1500QualType ASTContext::getCanonicalType(QualType T) {
1501 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001502
1503 // If the result has type qualifiers, make sure to canonicalize them as well.
1504 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1505 if (TypeQuals == 0) return CanType;
1506
1507 // If the type qualifiers are on an array type, get the canonical type of the
1508 // array with the qualifiers applied to the element type.
1509 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1510 if (!AT)
1511 return CanType.getQualifiedType(TypeQuals);
1512
1513 // Get the canonical version of the element with the extra qualifiers on it.
1514 // This can recursively sink qualifiers through multiple levels of arrays.
1515 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1516 NewEltTy = getCanonicalType(NewEltTy);
1517
1518 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1519 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1520 CAT->getIndexTypeQualifier());
1521 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1522 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1523 IAT->getIndexTypeQualifier());
1524
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001525 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1526 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1527 DSAT->getSizeModifier(),
1528 DSAT->getIndexTypeQualifier());
1529
Chris Lattnera1923f62008-08-04 07:31:14 +00001530 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1531 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1532 VAT->getSizeModifier(),
1533 VAT->getIndexTypeQualifier());
1534}
1535
1536
1537const ArrayType *ASTContext::getAsArrayType(QualType T) {
1538 // Handle the non-qualified case efficiently.
1539 if (T.getCVRQualifiers() == 0) {
1540 // Handle the common positive case fast.
1541 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1542 return AT;
1543 }
1544
1545 // Handle the common negative case fast, ignoring CVR qualifiers.
1546 QualType CType = T->getCanonicalTypeInternal();
1547
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001548 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001549 // test.
1550 if (!isa<ArrayType>(CType) &&
1551 !isa<ArrayType>(CType.getUnqualifiedType()))
1552 return 0;
1553
1554 // Apply any CVR qualifiers from the array type to the element type. This
1555 // implements C99 6.7.3p8: "If the specification of an array type includes
1556 // any type qualifiers, the element type is so qualified, not the array type."
1557
1558 // If we get here, we either have type qualifiers on the type, or we have
1559 // sugar such as a typedef in the way. If we have type qualifiers on the type
1560 // we must propagate them down into the elemeng type.
1561 unsigned CVRQuals = T.getCVRQualifiers();
1562 unsigned AddrSpace = 0;
1563 Type *Ty = T.getTypePtr();
1564
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001565 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001566 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001567 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1568 AddrSpace = EXTQT->getAddressSpace();
1569 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001570 } else {
1571 T = Ty->getDesugaredType();
1572 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1573 break;
1574 CVRQuals |= T.getCVRQualifiers();
1575 Ty = T.getTypePtr();
1576 }
1577 }
1578
1579 // If we have a simple case, just return now.
1580 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1581 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1582 return ATy;
1583
1584 // Otherwise, we have an array and we have qualifiers on it. Push the
1585 // qualifiers into the array element type and return a new array type.
1586 // Get the canonical version of the element with the extra qualifiers on it.
1587 // This can recursively sink qualifiers through multiple levels of arrays.
1588 QualType NewEltTy = ATy->getElementType();
1589 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001590 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001591 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1592
1593 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1594 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1595 CAT->getSizeModifier(),
1596 CAT->getIndexTypeQualifier()));
1597 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1598 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1599 IAT->getSizeModifier(),
1600 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001601
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001602 if (const DependentSizedArrayType *DSAT
1603 = dyn_cast<DependentSizedArrayType>(ATy))
1604 return cast<ArrayType>(
1605 getDependentSizedArrayType(NewEltTy,
1606 DSAT->getSizeExpr(),
1607 DSAT->getSizeModifier(),
1608 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001609
Chris Lattnera1923f62008-08-04 07:31:14 +00001610 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1611 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1612 VAT->getSizeModifier(),
1613 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001614}
1615
1616
Chris Lattner19eb97e2008-04-02 05:18:44 +00001617/// getArrayDecayedType - Return the properly qualified result of decaying the
1618/// specified array type to a pointer. This operation is non-trivial when
1619/// handling typedefs etc. The canonical type of "T" must be an array type,
1620/// this returns a pointer to a properly qualified element of the array.
1621///
1622/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1623QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001624 // Get the element type with 'getAsArrayType' so that we don't lose any
1625 // typedefs in the element type of the array. This also handles propagation
1626 // of type qualifiers from the array type into the element type if present
1627 // (C99 6.7.3p8).
1628 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1629 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001630
Chris Lattnera1923f62008-08-04 07:31:14 +00001631 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001632
1633 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001634 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001635}
1636
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001637QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001638 QualType ElemTy = VAT->getElementType();
1639
1640 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1641 return getBaseElementType(VAT);
1642
1643 return ElemTy;
1644}
1645
Chris Lattner4b009652007-07-25 00:24:17 +00001646/// getFloatingRank - Return a relative rank for floating point types.
1647/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001648static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001649 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001650 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001651
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001652 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001653 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001654 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001655 case BuiltinType::Float: return FloatRank;
1656 case BuiltinType::Double: return DoubleRank;
1657 case BuiltinType::LongDouble: return LongDoubleRank;
1658 }
1659}
1660
Steve Narofffa0c4532007-08-27 01:41:48 +00001661/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1662/// point or a complex type (based on typeDomain/typeSize).
1663/// 'typeDomain' is a real floating point or complex type.
1664/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001665QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1666 QualType Domain) const {
1667 FloatingRank EltRank = getFloatingRank(Size);
1668 if (Domain->isComplexType()) {
1669 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001670 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001671 case FloatRank: return FloatComplexTy;
1672 case DoubleRank: return DoubleComplexTy;
1673 case LongDoubleRank: return LongDoubleComplexTy;
1674 }
Chris Lattner4b009652007-07-25 00:24:17 +00001675 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001676
1677 assert(Domain->isRealFloatingType() && "Unknown domain!");
1678 switch (EltRank) {
1679 default: assert(0 && "getFloatingRank(): illegal value for rank");
1680 case FloatRank: return FloatTy;
1681 case DoubleRank: return DoubleTy;
1682 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001683 }
Chris Lattner4b009652007-07-25 00:24:17 +00001684}
1685
Chris Lattner51285d82008-04-06 23:55:33 +00001686/// getFloatingTypeOrder - Compare the rank of the two specified floating
1687/// point types, ignoring the domain of the type (i.e. 'double' ==
1688/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1689/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001690int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1691 FloatingRank LHSR = getFloatingRank(LHS);
1692 FloatingRank RHSR = getFloatingRank(RHS);
1693
1694 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001695 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001696 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001697 return 1;
1698 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001699}
1700
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001701/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1702/// routine will assert if passed a built-in type that isn't an integer or enum,
1703/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001704unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001705 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001706 if (EnumType* ET = dyn_cast<EnumType>(T))
1707 T = ET->getDecl()->getIntegerType().getTypePtr();
1708
1709 // There are two things which impact the integer rank: the width, and
1710 // the ordering of builtins. The builtin ordering is encoded in the
1711 // bottom three bits; the width is encoded in the bits above that.
1712 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1713 return FWIT->getWidth() << 3;
1714 }
1715
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001716 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001717 default: assert(0 && "getIntegerRank(): not a built-in integer");
1718 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001719 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001720 case BuiltinType::Char_S:
1721 case BuiltinType::Char_U:
1722 case BuiltinType::SChar:
1723 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001724 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001725 case BuiltinType::Short:
1726 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001727 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001728 case BuiltinType::Int:
1729 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001730 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001731 case BuiltinType::Long:
1732 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001733 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001734 case BuiltinType::LongLong:
1735 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001736 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001737 }
1738}
1739
Chris Lattner51285d82008-04-06 23:55:33 +00001740/// getIntegerTypeOrder - Returns the highest ranked integer type:
1741/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1742/// LHS < RHS, return -1.
1743int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001744 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1745 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001746 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001747
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001748 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1749 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001750
Chris Lattner51285d82008-04-06 23:55:33 +00001751 unsigned LHSRank = getIntegerRank(LHSC);
1752 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001753
Chris Lattner51285d82008-04-06 23:55:33 +00001754 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1755 if (LHSRank == RHSRank) return 0;
1756 return LHSRank > RHSRank ? 1 : -1;
1757 }
Chris Lattner4b009652007-07-25 00:24:17 +00001758
Chris Lattner51285d82008-04-06 23:55:33 +00001759 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1760 if (LHSUnsigned) {
1761 // If the unsigned [LHS] type is larger, return it.
1762 if (LHSRank >= RHSRank)
1763 return 1;
1764
1765 // If the signed type can represent all values of the unsigned type, it
1766 // wins. Because we are dealing with 2's complement and types that are
1767 // powers of two larger than each other, this is always safe.
1768 return -1;
1769 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001770
Chris Lattner51285d82008-04-06 23:55:33 +00001771 // If the unsigned [RHS] type is larger, return it.
1772 if (RHSRank >= LHSRank)
1773 return -1;
1774
1775 // If the signed type can represent all values of the unsigned type, it
1776 // wins. Because we are dealing with 2's complement and types that are
1777 // powers of two larger than each other, this is always safe.
1778 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001779}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001780
1781// getCFConstantStringType - Return the type used for constant CFStrings.
1782QualType ASTContext::getCFConstantStringType() {
1783 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001784 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001785 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001786 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001787 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001788
1789 // const int *isa;
1790 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001791 // int flags;
1792 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001793 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001794 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001795 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001796 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001797
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001798 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001799 for (unsigned i = 0; i < 4; ++i) {
1800 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1801 SourceLocation(), 0,
1802 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001803 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001804 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001805 }
1806
1807 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001808 }
1809
1810 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001811}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001812
Anders Carlssonf58cac72008-08-30 19:34:46 +00001813QualType ASTContext::getObjCFastEnumerationStateType()
1814{
1815 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001816 ObjCFastEnumerationStateTypeDecl =
1817 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1818 &Idents.get("__objcFastEnumerationState"));
1819
Anders Carlssonf58cac72008-08-30 19:34:46 +00001820 QualType FieldTypes[] = {
1821 UnsignedLongTy,
1822 getPointerType(ObjCIdType),
1823 getPointerType(UnsignedLongTy),
1824 getConstantArrayType(UnsignedLongTy,
1825 llvm::APInt(32, 5), ArrayType::Normal, 0)
1826 };
1827
Douglas Gregor8acb7272008-12-11 16:49:14 +00001828 for (size_t i = 0; i < 4; ++i) {
1829 FieldDecl *Field = FieldDecl::Create(*this,
1830 ObjCFastEnumerationStateTypeDecl,
1831 SourceLocation(), 0,
1832 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001833 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001834 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001835 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001836
Douglas Gregor8acb7272008-12-11 16:49:14 +00001837 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001838 }
1839
1840 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1841}
1842
Anders Carlssone3f02572007-10-29 06:33:42 +00001843// This returns true if a type has been typedefed to BOOL:
1844// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001845static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001846 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001847 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1848 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001849
1850 return false;
1851}
1852
Ted Kremenek42730c52008-01-07 19:49:32 +00001853/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001854/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001855int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001856 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001857
1858 // Make all integer and enum types at least as large as an int
1859 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001860 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001861 // Treat arrays as pointers, since that's how they're passed in.
1862 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001863 sz = getTypeSize(VoidPtrTy);
1864 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001865}
1866
Ted Kremenek42730c52008-01-07 19:49:32 +00001867/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001868/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001869void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001870 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001871 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001872 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001873 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001874 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001875 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001876 // Compute size of all parameters.
1877 // Start with computing size of a pointer in number of bytes.
1878 // FIXME: There might(should) be a better way of doing this computation!
1879 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001880 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001881 // The first two arguments (self and _cmd) are pointers; account for
1882 // their size.
1883 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001884 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1885 E = Decl->param_end(); PI != E; ++PI) {
1886 QualType PType = (*PI)->getType();
1887 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001888 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001889 ParmOffset += sz;
1890 }
1891 S += llvm::utostr(ParmOffset);
1892 S += "@0:";
1893 S += llvm::utostr(PtrSize);
1894
1895 // Argument types.
1896 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001897 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1898 E = Decl->param_end(); PI != E; ++PI) {
1899 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001900 QualType PType = PVDecl->getOriginalType();
1901 if (const ArrayType *AT =
1902 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1903 // Use array's original type only if it has known number of
1904 // elements.
1905 if (!dyn_cast<ConstantArrayType>(AT))
1906 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001907 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001908 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001909 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001910 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001911 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001912 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001913 }
1914}
1915
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001916/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001917/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001918/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1919/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001920/// Property attributes are stored as a comma-delimited C string. The simple
1921/// attributes readonly and bycopy are encoded as single characters. The
1922/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1923/// encoded as single characters, followed by an identifier. Property types
1924/// are also encoded as a parametrized attribute. The characters used to encode
1925/// these attributes are defined by the following enumeration:
1926/// @code
1927/// enum PropertyAttributes {
1928/// kPropertyReadOnly = 'R', // property is read-only.
1929/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1930/// kPropertyByref = '&', // property is a reference to the value last assigned
1931/// kPropertyDynamic = 'D', // property is dynamic
1932/// kPropertyGetter = 'G', // followed by getter selector name
1933/// kPropertySetter = 'S', // followed by setter selector name
1934/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1935/// kPropertyType = 't' // followed by old-style type encoding.
1936/// kPropertyWeak = 'W' // 'weak' property
1937/// kPropertyStrong = 'P' // property GC'able
1938/// kPropertyNonAtomic = 'N' // property non-atomic
1939/// };
1940/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001941void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1942 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001943 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001944 // Collect information from the property implementation decl(s).
1945 bool Dynamic = false;
1946 ObjCPropertyImplDecl *SynthesizePID = 0;
1947
1948 // FIXME: Duplicated code due to poor abstraction.
1949 if (Container) {
1950 if (const ObjCCategoryImplDecl *CID =
1951 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1952 for (ObjCCategoryImplDecl::propimpl_iterator
1953 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1954 ObjCPropertyImplDecl *PID = *i;
1955 if (PID->getPropertyDecl() == PD) {
1956 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1957 Dynamic = true;
1958 } else {
1959 SynthesizePID = PID;
1960 }
1961 }
1962 }
1963 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001964 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001965 for (ObjCCategoryImplDecl::propimpl_iterator
1966 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1967 ObjCPropertyImplDecl *PID = *i;
1968 if (PID->getPropertyDecl() == PD) {
1969 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1970 Dynamic = true;
1971 } else {
1972 SynthesizePID = PID;
1973 }
1974 }
1975 }
1976 }
1977 }
1978
1979 // FIXME: This is not very efficient.
1980 S = "T";
1981
1982 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001983 // GCC has some special rules regarding encoding of properties which
1984 // closely resembles encoding of ivars.
1985 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1986 true /* outermost type */,
1987 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001988
1989 if (PD->isReadOnly()) {
1990 S += ",R";
1991 } else {
1992 switch (PD->getSetterKind()) {
1993 case ObjCPropertyDecl::Assign: break;
1994 case ObjCPropertyDecl::Copy: S += ",C"; break;
1995 case ObjCPropertyDecl::Retain: S += ",&"; break;
1996 }
1997 }
1998
1999 // It really isn't clear at all what this means, since properties
2000 // are "dynamic by default".
2001 if (Dynamic)
2002 S += ",D";
2003
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002004 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2005 S += ",N";
2006
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002007 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2008 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002009 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002010 }
2011
2012 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2013 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002014 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002015 }
2016
2017 if (SynthesizePID) {
2018 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2019 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002020 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002021 }
2022
2023 // FIXME: OBJCGC: weak & strong
2024}
2025
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002026/// getLegacyIntegralTypeEncoding -
2027/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002028/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002029/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2030///
2031void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2032 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2033 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002034 if (BT->getKind() == BuiltinType::ULong &&
2035 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002036 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002037 else
2038 if (BT->getKind() == BuiltinType::Long &&
2039 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002040 PointeeTy = IntTy;
2041 }
2042 }
2043}
2044
Fariborz Jahanian248db262008-01-22 22:44:46 +00002045void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002046 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002047 // We follow the behavior of gcc, expanding structures which are
2048 // directly pointed to, and expanding embedded structures. Note that
2049 // these rules are sufficient to prevent recursive encoding of the
2050 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002051 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2052 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002053}
2054
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002055static void EncodeBitField(const ASTContext *Context, std::string& S,
2056 FieldDecl *FD) {
2057 const Expr *E = FD->getBitWidth();
2058 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2059 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2060 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2061 S += 'b';
2062 S += llvm::utostr(N);
2063}
2064
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002065void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2066 bool ExpandPointedToStructures,
2067 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002068 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002069 bool OutermostType,
2070 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002071 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002072 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002073 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002074 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002075 else {
2076 char encoding;
2077 switch (BT->getKind()) {
2078 default: assert(0 && "Unhandled builtin type kind");
2079 case BuiltinType::Void: encoding = 'v'; break;
2080 case BuiltinType::Bool: encoding = 'B'; break;
2081 case BuiltinType::Char_U:
2082 case BuiltinType::UChar: encoding = 'C'; break;
2083 case BuiltinType::UShort: encoding = 'S'; break;
2084 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002085 case BuiltinType::ULong:
2086 encoding =
2087 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2088 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002089 case BuiltinType::ULongLong: encoding = 'Q'; break;
2090 case BuiltinType::Char_S:
2091 case BuiltinType::SChar: encoding = 'c'; break;
2092 case BuiltinType::Short: encoding = 's'; break;
2093 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002094 case BuiltinType::Long:
2095 encoding =
2096 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2097 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002098 case BuiltinType::LongLong: encoding = 'q'; break;
2099 case BuiltinType::Float: encoding = 'f'; break;
2100 case BuiltinType::Double: encoding = 'd'; break;
2101 case BuiltinType::LongDouble: encoding = 'd'; break;
2102 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002103
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002104 S += encoding;
2105 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002106 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002107 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002108 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2109 ExpandPointedToStructures,
2110 ExpandStructures, FD);
2111 if (FD || EncodingProperty) {
2112 // Note that we do extended encoding of protocol qualifer list
2113 // Only when doing ivar or property encoding.
2114 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2115 S += '"';
2116 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2117 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2118 S += '<';
2119 S += Proto->getNameAsString();
2120 S += '>';
2121 }
2122 S += '"';
2123 }
2124 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002125 }
2126 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002127 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002128 bool isReadOnly = false;
2129 // For historical/compatibility reasons, the read-only qualifier of the
2130 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2131 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2132 // Also, do not emit the 'r' for anything but the outermost type!
2133 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2134 if (OutermostType && T.isConstQualified()) {
2135 isReadOnly = true;
2136 S += 'r';
2137 }
2138 }
2139 else if (OutermostType) {
2140 QualType P = PointeeTy;
2141 while (P->getAsPointerType())
2142 P = P->getAsPointerType()->getPointeeType();
2143 if (P.isConstQualified()) {
2144 isReadOnly = true;
2145 S += 'r';
2146 }
2147 }
2148 if (isReadOnly) {
2149 // Another legacy compatibility encoding. Some ObjC qualifier and type
2150 // combinations need to be rearranged.
2151 // Rewrite "in const" from "nr" to "rn"
2152 const char * s = S.c_str();
2153 int len = S.length();
2154 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2155 std::string replace = "rn";
2156 S.replace(S.end()-2, S.end(), replace);
2157 }
2158 }
Steve Naroff17c03822009-02-12 17:52:19 +00002159 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002160 S += '@';
2161 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002162 }
2163 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002164 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002165 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002166 // Another historical/compatibility reason.
2167 // We encode the underlying type which comes out as
2168 // {...};
2169 S += '^';
2170 getObjCEncodingForTypeImpl(PointeeTy, S,
2171 false, ExpandPointedToStructures,
2172 NULL);
2173 return;
2174 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002175 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002176 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002177 const ObjCInterfaceType *OIT =
2178 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002179 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002180 S += '"';
2181 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002182 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2183 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2184 S += '<';
2185 S += Proto->getNameAsString();
2186 S += '>';
2187 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002188 S += '"';
2189 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002190 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002191 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002192 S += '#';
2193 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002194 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002195 S += ':';
2196 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002197 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002198
2199 if (PointeeTy->isCharType()) {
2200 // char pointer types should be encoded as '*' unless it is a
2201 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002202 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002203 S += '*';
2204 return;
2205 }
2206 }
2207
2208 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002209 getLegacyIntegralTypeEncoding(PointeeTy);
2210
2211 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002212 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002213 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002214 } else if (const ArrayType *AT =
2215 // Ignore type qualifiers etc.
2216 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002217 if (isa<IncompleteArrayType>(AT)) {
2218 // Incomplete arrays are encoded as a pointer to the array element.
2219 S += '^';
2220
2221 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2222 false, ExpandStructures, FD);
2223 } else {
2224 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002225
Anders Carlsson858c64d2009-02-22 01:38:57 +00002226 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2227 S += llvm::utostr(CAT->getSize().getZExtValue());
2228 else {
2229 //Variable length arrays are encoded as a regular array with 0 elements.
2230 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2231 S += '0';
2232 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002233
Anders Carlsson858c64d2009-02-22 01:38:57 +00002234 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2235 false, ExpandStructures, FD);
2236 S += ']';
2237 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002238 } else if (T->getAsFunctionType()) {
2239 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002240 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002241 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002242 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002243 // Anonymous structures print as '?'
2244 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2245 S += II->getName();
2246 } else {
2247 S += '?';
2248 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002249 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002250 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002251 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2252 FieldEnd = RDecl->field_end();
2253 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002254 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002255 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002256 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002257 S += '"';
2258 }
2259
2260 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002261 if (Field->isBitField()) {
2262 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2263 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002264 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002265 QualType qt = Field->getType();
2266 getLegacyIntegralTypeEncoding(qt);
2267 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002268 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002269 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002270 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002271 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002272 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002273 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002274 if (FD && FD->isBitField())
2275 EncodeBitField(this, S, FD);
2276 else
2277 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002278 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002279 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002280 } else if (T->isObjCInterfaceType()) {
2281 // @encode(class_name)
2282 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2283 S += '{';
2284 const IdentifierInfo *II = OI->getIdentifier();
2285 S += II->getName();
2286 S += '=';
2287 std::vector<FieldDecl*> RecFields;
2288 CollectObjCIvars(OI, RecFields);
2289 for (unsigned int i = 0; i != RecFields.size(); i++) {
2290 if (RecFields[i]->isBitField())
2291 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2292 RecFields[i]);
2293 else
2294 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2295 FD);
2296 }
2297 S += '}';
2298 }
2299 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002300 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002301}
2302
Ted Kremenek42730c52008-01-07 19:49:32 +00002303void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002304 std::string& S) const {
2305 if (QT & Decl::OBJC_TQ_In)
2306 S += 'n';
2307 if (QT & Decl::OBJC_TQ_Inout)
2308 S += 'N';
2309 if (QT & Decl::OBJC_TQ_Out)
2310 S += 'o';
2311 if (QT & Decl::OBJC_TQ_Bycopy)
2312 S += 'O';
2313 if (QT & Decl::OBJC_TQ_Byref)
2314 S += 'R';
2315 if (QT & Decl::OBJC_TQ_Oneway)
2316 S += 'V';
2317}
2318
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002319void ASTContext::setBuiltinVaListType(QualType T)
2320{
2321 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2322
2323 BuiltinVaListType = T;
2324}
2325
Ted Kremenek42730c52008-01-07 19:49:32 +00002326void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002327{
Ted Kremenek42730c52008-01-07 19:49:32 +00002328 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002329
2330 // typedef struct objc_object *id;
2331 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002332 // User error - caller will issue diagnostics.
2333 if (!ptr)
2334 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002335 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002336 // User error - caller will issue diagnostics.
2337 if (!rec)
2338 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002339 IdStructType = rec;
2340}
2341
Ted Kremenek42730c52008-01-07 19:49:32 +00002342void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002343{
Ted Kremenek42730c52008-01-07 19:49:32 +00002344 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002345
2346 // typedef struct objc_selector *SEL;
2347 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002348 if (!ptr)
2349 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002350 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002351 if (!rec)
2352 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002353 SelStructType = rec;
2354}
2355
Ted Kremenek42730c52008-01-07 19:49:32 +00002356void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002357{
Ted Kremenek42730c52008-01-07 19:49:32 +00002358 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002359}
2360
Ted Kremenek42730c52008-01-07 19:49:32 +00002361void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002362{
Ted Kremenek42730c52008-01-07 19:49:32 +00002363 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002364
2365 // typedef struct objc_class *Class;
2366 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2367 assert(ptr && "'Class' incorrectly typed");
2368 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2369 assert(rec && "'Class' incorrectly typed");
2370 ClassStructType = rec;
2371}
2372
Ted Kremenek42730c52008-01-07 19:49:32 +00002373void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2374 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002375 "'NSConstantString' type already set!");
2376
Ted Kremenek42730c52008-01-07 19:49:32 +00002377 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002378}
2379
Douglas Gregorc6507e42008-11-03 14:12:49 +00002380/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002381/// TargetInfo, produce the corresponding type. The unsigned @p Type
2382/// is actually a value of type @c TargetInfo::IntType.
2383QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002384 switch (Type) {
2385 case TargetInfo::NoInt: return QualType();
2386 case TargetInfo::SignedShort: return ShortTy;
2387 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2388 case TargetInfo::SignedInt: return IntTy;
2389 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2390 case TargetInfo::SignedLong: return LongTy;
2391 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2392 case TargetInfo::SignedLongLong: return LongLongTy;
2393 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2394 }
2395
2396 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002397 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002398}
Ted Kremenek118930e2008-07-24 23:58:27 +00002399
2400//===----------------------------------------------------------------------===//
2401// Type Predicates.
2402//===----------------------------------------------------------------------===//
2403
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002404/// isObjCNSObjectType - Return true if this is an NSObject object using
2405/// NSObject attribute on a c-style pointer type.
2406/// FIXME - Make it work directly on types.
2407///
2408bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2409 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2410 if (TypedefDecl *TD = TDT->getDecl())
2411 if (TD->getAttr<ObjCNSObjectAttr>())
2412 return true;
2413 }
2414 return false;
2415}
2416
Ted Kremenek118930e2008-07-24 23:58:27 +00002417/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2418/// to an object type. This includes "id" and "Class" (two 'special' pointers
2419/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2420/// ID type).
2421bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd305a862009-02-21 21:17:01 +00002422 if (Ty->isObjCQualifiedIdType() || Ty->isObjCQualifiedClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002423 return true;
2424
Steve Naroffd9e00802008-10-21 18:24:04 +00002425 // Blocks are objects.
2426 if (Ty->isBlockPointerType())
2427 return true;
2428
2429 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002430 if (!Ty->isPointerType())
2431 return false;
2432
2433 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2434 // pointer types. This looks for the typedef specifically, not for the
2435 // underlying type.
2436 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2437 return true;
2438
2439 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002440 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2441 return true;
2442
2443 // If is has NSObject attribute, OK as well.
2444 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002445}
2446
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002447/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2448/// garbage collection attribute.
2449///
2450QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002451 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002452 if (getLangOptions().ObjC1 &&
2453 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002454 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002455 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002456 // (or pointers to them) be treated as though they were declared
2457 // as __strong.
2458 if (GCAttrs == QualType::GCNone) {
2459 if (isObjCObjectPointerType(Ty))
2460 GCAttrs = QualType::Strong;
2461 else if (Ty->isPointerType())
2462 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2463 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002464 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002465 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002466}
2467
Chris Lattner6ff358b2008-04-07 06:51:04 +00002468//===----------------------------------------------------------------------===//
2469// Type Compatibility Testing
2470//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002471
Steve Naroff3454b6c2008-09-04 15:10:53 +00002472/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002473/// block types. Types must be strictly compatible here. For example,
2474/// C unfortunately doesn't produce an error for the following:
2475///
2476/// int (*emptyArgFunc)();
2477/// int (*intArgList)(int) = emptyArgFunc;
2478///
2479/// For blocks, we will produce an error for the following (similar to C++):
2480///
2481/// int (^emptyArgBlock)();
2482/// int (^intArgBlock)(int) = emptyArgBlock;
2483///
2484/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2485///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002486bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002487 const FunctionType *lbase = lhs->getAsFunctionType();
2488 const FunctionType *rbase = rhs->getAsFunctionType();
2489 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2490 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2491 if (lproto && rproto)
2492 return !mergeTypes(lhs, rhs).isNull();
2493 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002494}
2495
Chris Lattner6ff358b2008-04-07 06:51:04 +00002496/// areCompatVectorTypes - Return true if the two specified vector types are
2497/// compatible.
2498static bool areCompatVectorTypes(const VectorType *LHS,
2499 const VectorType *RHS) {
2500 assert(LHS->isCanonical() && RHS->isCanonical());
2501 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002502 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002503}
2504
Eli Friedman0d9549b2008-08-22 00:56:42 +00002505/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002506/// compatible for assignment from RHS to LHS. This handles validation of any
2507/// protocol qualifiers on the LHS or RHS.
2508///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002509bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2510 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002511 // Verify that the base decls are compatible: the RHS must be a subclass of
2512 // the LHS.
2513 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2514 return false;
2515
2516 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2517 // protocol qualified at all, then we are good.
2518 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2519 return true;
2520
2521 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2522 // isn't a superset.
2523 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2524 return true; // FIXME: should return false!
2525
2526 // Finally, we must have two protocol-qualified interfaces.
2527 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2528 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2529 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2530 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2531 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2532 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2533
2534 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2535 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2536 // LHS in a single parallel scan until we run out of elements in LHS.
2537 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2538 ObjCProtocolDecl *LHSProto = *LHSPI;
2539
2540 while (RHSPI != RHSPE) {
2541 ObjCProtocolDecl *RHSProto = *RHSPI++;
2542 // If the RHS has a protocol that the LHS doesn't, ignore it.
2543 if (RHSProto != LHSProto)
2544 continue;
2545
2546 // Otherwise, the RHS does have this element.
2547 ++LHSPI;
2548 if (LHSPI == LHSPE)
2549 return true; // All protocols in LHS exist in RHS.
2550
2551 LHSProto = *LHSPI;
2552 }
2553
2554 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2555 return false;
2556}
2557
Steve Naroff17c03822009-02-12 17:52:19 +00002558bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2559 // get the "pointed to" types
2560 const PointerType *LHSPT = LHS->getAsPointerType();
2561 const PointerType *RHSPT = RHS->getAsPointerType();
2562
2563 if (!LHSPT || !RHSPT)
2564 return false;
2565
2566 QualType lhptee = LHSPT->getPointeeType();
2567 QualType rhptee = RHSPT->getPointeeType();
2568 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2569 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2570 // ID acts sort of like void* for ObjC interfaces
2571 if (LHSIface && isObjCIdStructType(rhptee))
2572 return true;
2573 if (RHSIface && isObjCIdStructType(lhptee))
2574 return true;
2575 if (!LHSIface || !RHSIface)
2576 return false;
2577 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2578 canAssignObjCInterfaces(RHSIface, LHSIface);
2579}
2580
Steve Naroff85f0dc52007-10-15 20:41:53 +00002581/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2582/// both shall have the identically qualified version of a compatible type.
2583/// C99 6.2.7p1: Two types have compatible types if their types are the
2584/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002585bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2586 return !mergeTypes(LHS, RHS).isNull();
2587}
2588
2589QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2590 const FunctionType *lbase = lhs->getAsFunctionType();
2591 const FunctionType *rbase = rhs->getAsFunctionType();
2592 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2593 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2594 bool allLTypes = true;
2595 bool allRTypes = true;
2596
2597 // Check return type
2598 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2599 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002600 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2601 allLTypes = false;
2602 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2603 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002604
2605 if (lproto && rproto) { // two C99 style function prototypes
2606 unsigned lproto_nargs = lproto->getNumArgs();
2607 unsigned rproto_nargs = rproto->getNumArgs();
2608
2609 // Compatible functions must have the same number of arguments
2610 if (lproto_nargs != rproto_nargs)
2611 return QualType();
2612
2613 // Variadic and non-variadic functions aren't compatible
2614 if (lproto->isVariadic() != rproto->isVariadic())
2615 return QualType();
2616
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002617 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2618 return QualType();
2619
Eli Friedman0d9549b2008-08-22 00:56:42 +00002620 // Check argument compatibility
2621 llvm::SmallVector<QualType, 10> types;
2622 for (unsigned i = 0; i < lproto_nargs; i++) {
2623 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2624 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2625 QualType argtype = mergeTypes(largtype, rargtype);
2626 if (argtype.isNull()) return QualType();
2627 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002628 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2629 allLTypes = false;
2630 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2631 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002632 }
2633 if (allLTypes) return lhs;
2634 if (allRTypes) return rhs;
2635 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002636 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002637 }
2638
2639 if (lproto) allRTypes = false;
2640 if (rproto) allLTypes = false;
2641
2642 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2643 if (proto) {
2644 if (proto->isVariadic()) return QualType();
2645 // Check that the types are compatible with the types that
2646 // would result from default argument promotions (C99 6.7.5.3p15).
2647 // The only types actually affected are promotable integer
2648 // types and floats, which would be passed as a different
2649 // type depending on whether the prototype is visible.
2650 unsigned proto_nargs = proto->getNumArgs();
2651 for (unsigned i = 0; i < proto_nargs; ++i) {
2652 QualType argTy = proto->getArgType(i);
2653 if (argTy->isPromotableIntegerType() ||
2654 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2655 return QualType();
2656 }
2657
2658 if (allLTypes) return lhs;
2659 if (allRTypes) return rhs;
2660 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002661 proto->getNumArgs(), lproto->isVariadic(),
2662 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002663 }
2664
2665 if (allLTypes) return lhs;
2666 if (allRTypes) return rhs;
2667 return getFunctionTypeNoProto(retType);
2668}
2669
2670QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002671 // C++ [expr]: If an expression initially has the type "reference to T", the
2672 // type is adjusted to "T" prior to any further analysis, the expression
2673 // designates the object or function denoted by the reference, and the
2674 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002675 // FIXME: C++ shouldn't be going through here! The rules are different
2676 // enough that they should be handled separately.
2677 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002678 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002679 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002680 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002681
Eli Friedman0d9549b2008-08-22 00:56:42 +00002682 QualType LHSCan = getCanonicalType(LHS),
2683 RHSCan = getCanonicalType(RHS);
2684
2685 // If two types are identical, they are compatible.
2686 if (LHSCan == RHSCan)
2687 return LHS;
2688
2689 // If the qualifiers are different, the types aren't compatible
2690 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2691 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2692 return QualType();
2693
2694 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2695 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2696
Chris Lattnerc38d4522008-01-14 05:45:46 +00002697 // We want to consider the two function types to be the same for these
2698 // comparisons, just force one to the other.
2699 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2700 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002701
2702 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002703 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2704 LHSClass = Type::ConstantArray;
2705 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2706 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002707
Nate Begemanaf6ed502008-04-18 23:10:10 +00002708 // Canonicalize ExtVector -> Vector.
2709 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2710 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002711
Chris Lattner7cdcb252008-04-07 06:38:24 +00002712 // Consider qualified interfaces and interfaces the same.
2713 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2714 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002715
Chris Lattnerb5709e22008-04-07 05:43:21 +00002716 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002717 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002718 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2719 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2720
2721 // ID acts sort of like void* for ObjC interfaces
2722 if (LHSIface && isObjCIdStructType(RHS))
2723 return LHS;
2724 if (RHSIface && isObjCIdStructType(LHS))
2725 return RHS;
2726
Steve Naroff28ceff72008-12-10 22:14:21 +00002727 // ID is compatible with all qualified id types.
2728 if (LHS->isObjCQualifiedIdType()) {
2729 if (const PointerType *PT = RHS->getAsPointerType()) {
2730 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002731 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002732 return LHS;
2733 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2734 // Unfortunately, this API is part of Sema (which we don't have access
2735 // to. Need to refactor. The following check is insufficient, since we
2736 // need to make sure the class implements the protocol.
2737 if (pType->isObjCInterfaceType())
2738 return LHS;
2739 }
2740 }
2741 if (RHS->isObjCQualifiedIdType()) {
2742 if (const PointerType *PT = LHS->getAsPointerType()) {
2743 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002744 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002745 return RHS;
2746 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2747 // Unfortunately, this API is part of Sema (which we don't have access
2748 // to. Need to refactor. The following check is insufficient, since we
2749 // need to make sure the class implements the protocol.
2750 if (pType->isObjCInterfaceType())
2751 return RHS;
2752 }
2753 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002754 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2755 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002756 if (const EnumType* ETy = LHS->getAsEnumType()) {
2757 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2758 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002759 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002760 if (const EnumType* ETy = RHS->getAsEnumType()) {
2761 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2762 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002763 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002764
Eli Friedman0d9549b2008-08-22 00:56:42 +00002765 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002766 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002767
Steve Naroffc88babe2008-01-09 22:43:08 +00002768 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002769 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002770 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002771 {
2772 // Merge two pointer types, while trying to preserve typedef info
2773 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2774 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2775 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2776 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002777 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2778 return LHS;
2779 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2780 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002781 return getPointerType(ResultType);
2782 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002783 case Type::BlockPointer:
2784 {
2785 // Merge two block pointer types, while trying to preserve typedef info
2786 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2787 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2788 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2789 if (ResultType.isNull()) return QualType();
2790 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2791 return LHS;
2792 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2793 return RHS;
2794 return getBlockPointerType(ResultType);
2795 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002796 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002797 {
2798 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2799 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2800 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2801 return QualType();
2802
2803 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2804 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2805 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2806 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002807 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2808 return LHS;
2809 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2810 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002811 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2812 ArrayType::ArraySizeModifier(), 0);
2813 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2814 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002815 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2816 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002817 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2818 return LHS;
2819 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2820 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002821 if (LVAT) {
2822 // FIXME: This isn't correct! But tricky to implement because
2823 // the array's size has to be the size of LHS, but the type
2824 // has to be different.
2825 return LHS;
2826 }
2827 if (RVAT) {
2828 // FIXME: This isn't correct! But tricky to implement because
2829 // the array's size has to be the size of RHS, but the type
2830 // has to be different.
2831 return RHS;
2832 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002833 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2834 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002835 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002836 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002837 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002838 return mergeFunctionTypes(LHS, RHS);
2839 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002840 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002841 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2842 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002843 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002844 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002845 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002847 case Type::Complex:
2848 // Distinct complex types are incompatible.
2849 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002850 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002851 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2852 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002853 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002854 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002855 // Check if the interfaces are assignment compatible.
2856 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2857 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2858 if (LHSIface && RHSIface &&
2859 canAssignObjCInterfaces(LHSIface, RHSIface))
2860 return LHS;
2861
Eli Friedman0d9549b2008-08-22 00:56:42 +00002862 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002863 }
Steve Naroff28ceff72008-12-10 22:14:21 +00002864 case Type::ObjCQualifiedId:
2865 // Distinct qualified id's are not compatible.
2866 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002867 default:
2868 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002869 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002870 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002871}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002872
Chris Lattner1d78a862008-04-07 07:01:58 +00002873//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002874// Integer Predicates
2875//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002876
Eli Friedman0832dbc2008-06-28 06:23:08 +00002877unsigned ASTContext::getIntWidth(QualType T) {
2878 if (T == BoolTy)
2879 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002880 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2881 return FWIT->getWidth();
2882 }
2883 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002884 return (unsigned)getTypeSize(T);
2885}
2886
2887QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2888 assert(T->isSignedIntegerType() && "Unexpected type");
2889 if (const EnumType* ETy = T->getAsEnumType())
2890 T = ETy->getDecl()->getIntegerType();
2891 const BuiltinType* BTy = T->getAsBuiltinType();
2892 assert (BTy && "Unexpected signed integer type");
2893 switch (BTy->getKind()) {
2894 case BuiltinType::Char_S:
2895 case BuiltinType::SChar:
2896 return UnsignedCharTy;
2897 case BuiltinType::Short:
2898 return UnsignedShortTy;
2899 case BuiltinType::Int:
2900 return UnsignedIntTy;
2901 case BuiltinType::Long:
2902 return UnsignedLongTy;
2903 case BuiltinType::LongLong:
2904 return UnsignedLongLongTy;
2905 default:
2906 assert(0 && "Unexpected signed integer type");
2907 return QualType();
2908 }
2909}
2910
2911
2912//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002913// Serialization Support
2914//===----------------------------------------------------------------------===//
2915
Ted Kremenek738e6c02007-10-31 17:10:13 +00002916/// Emit - Serialize an ASTContext object to Bitcode.
2917void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002918 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002919 S.EmitRef(SourceMgr);
2920 S.EmitRef(Target);
2921 S.EmitRef(Idents);
2922 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002923
Ted Kremenek68228a92007-10-31 22:44:07 +00002924 // Emit the size of the type vector so that we can reserve that size
2925 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002926 S.EmitInt(Types.size());
2927
Ted Kremenek034a78c2007-11-13 22:02:55 +00002928 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2929 I!=E;++I)
2930 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002931
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002932 S.EmitOwnedPtr(TUDecl);
2933
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002934 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002935}
2936
Ted Kremenekacba3612007-11-13 00:25:37 +00002937ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002938
2939 // Read the language options.
2940 LangOptions LOpts;
2941 LOpts.Read(D);
2942
Ted Kremenek68228a92007-10-31 22:44:07 +00002943 SourceManager &SM = D.ReadRef<SourceManager>();
2944 TargetInfo &t = D.ReadRef<TargetInfo>();
2945 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2946 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002947
Ted Kremenek68228a92007-10-31 22:44:07 +00002948 unsigned size_reserve = D.ReadInt();
2949
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002950 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2951 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002952
Ted Kremenek034a78c2007-11-13 22:02:55 +00002953 for (unsigned i = 0; i < size_reserve; ++i)
2954 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002955
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002956 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2957
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002958 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002959
2960 return A;
2961}