blob: 6b49b85c243c5bdd99600bae149d7883eabfe0d7 [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.
471 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
472 if (BT->getKind() == BuiltinType::Double)
Fariborz Jahanianbaeeb752009-01-27 18:55:00 +0000473 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000474
475 return ABIAlign;
476}
477
478
Devang Patelbfe323c2008-06-04 21:22:16 +0000479/// LayoutField - Field layout.
480void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000481 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000482 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000483 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000484 uint64_t FieldOffset = IsUnion ? 0 : Size;
485 uint64_t FieldSize;
486 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000487
488 // FIXME: Should this override struct packing? Probably we want to
489 // take the minimum?
490 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
491 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000492
493 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
494 // TODO: Need to check this algorithm on other targets!
495 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000496 FieldSize =
497 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000498
499 std::pair<uint64_t, unsigned> FieldInfo =
500 Context.getTypeInfo(FD->getType());
501 uint64_t TypeSize = FieldInfo.first;
502
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000503 // Determine the alignment of this bitfield. The packing
504 // attributes define a maximum and the alignment attribute defines
505 // a minimum.
506 // FIXME: What is the right behavior when the specified alignment
507 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000508 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000509 if (FieldPacking)
510 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000511 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
512 FieldAlign = std::max(FieldAlign, AA->getAlignment());
513
514 // Check if we need to add padding to give the field the correct
515 // alignment.
516 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
517 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
518
519 // Padding members don't affect overall alignment
520 if (!FD->getIdentifier())
521 FieldAlign = 1;
522 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000523 if (FD->getType()->isIncompleteArrayType()) {
524 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000525 // query getTypeInfo about these, so we figure it out here.
526 // Flexible array members don't have any size, but they
527 // have to be aligned appropriately for their element type.
528 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000529 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000530 FieldAlign = Context.getTypeAlign(ATy->getElementType());
531 } else {
532 std::pair<uint64_t, unsigned> FieldInfo =
533 Context.getTypeInfo(FD->getType());
534 FieldSize = FieldInfo.first;
535 FieldAlign = FieldInfo.second;
536 }
537
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000538 // Determine the alignment of this bitfield. The packing
539 // attributes define a maximum and the alignment attribute defines
540 // a minimum. Additionally, the packing alignment must be at least
541 // a byte for non-bitfields.
542 //
543 // FIXME: What is the right behavior when the specified alignment
544 // is smaller than the specified packing?
545 if (FieldPacking)
546 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000547 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
548 FieldAlign = std::max(FieldAlign, AA->getAlignment());
549
550 // Round up the current record size to the field's alignment boundary.
551 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
552 }
553
554 // Place this field at the current location.
555 FieldOffsets[FieldNo] = FieldOffset;
556
557 // Reserve space for this field.
558 if (IsUnion) {
559 Size = std::max(Size, FieldSize);
560 } else {
561 Size = FieldOffset + FieldSize;
562 }
563
564 // Remember max struct/class alignment.
565 Alignment = std::max(Alignment, FieldAlign);
566}
567
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000568static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
569 std::vector<FieldDecl*> &Fields) {
570 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
571 if (SuperClass)
572 CollectObjCIvars(SuperClass, Fields);
573 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
574 E = OI->ivar_end(); I != E; ++I) {
575 ObjCIvarDecl *IVDecl = (*I);
576 if (!IVDecl->isInvalidDecl())
577 Fields.push_back(cast<FieldDecl>(IVDecl));
578 }
579}
580
581/// addRecordToClass - produces record info. for the class for its
582/// ivars and all those inherited.
583///
584const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
585{
586 const RecordDecl *&RD = ASTRecordForInterface[D];
587 if (RD)
588 return RD;
589 std::vector<FieldDecl*> RecFields;
590 CollectObjCIvars(D, RecFields);
591 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
592 D->getLocation(),
593 D->getIdentifier());
594 /// FIXME! Can do collection of ivars and adding to the record while
595 /// doing it.
596 for (unsigned int i = 0; i != RecFields.size(); i++) {
597 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
598 RecFields[i]->getLocation(),
599 RecFields[i]->getIdentifier(),
600 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000601 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000602 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000603 }
604 NewRD->completeDefinition(*this);
605 RD = NewRD;
606 return RD;
607}
Devang Patel4b6bf702008-06-04 21:54:36 +0000608
Fariborz Jahanianea944842008-12-18 17:29:46 +0000609/// setFieldDecl - maps a field for the given Ivar reference node.
610//
611void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
612 const ObjCIvarDecl *Ivar,
613 const ObjCIvarRefExpr *MRef) {
614 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
615 lookupFieldDeclForIvar(*this, Ivar);
616 ASTFieldForIvarRef[MRef] = FD;
617}
618
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000619/// getASTObjcInterfaceLayout - Get or compute information about the layout of
620/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000621/// position information.
622const ASTRecordLayout &
623ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
624 // Look up this layout, if already laid out, return what we have.
625 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
626 if (Entry) return *Entry;
627
628 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
629 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000630 ASTRecordLayout *NewEntry = NULL;
631 unsigned FieldCount = D->ivar_size();
632 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
633 FieldCount++;
634 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
635 unsigned Alignment = SL.getAlignment();
636 uint64_t Size = SL.getSize();
637 NewEntry = new ASTRecordLayout(Size, Alignment);
638 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000639 // Super class is at the beginning of the layout.
640 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000641 } else {
642 NewEntry = new ASTRecordLayout();
643 NewEntry->InitializeLayout(FieldCount);
644 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000645 Entry = NewEntry;
646
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000647 unsigned StructPacking = 0;
648 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
649 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000650
651 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
652 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
653 AA->getAlignment()));
654
655 // Layout each ivar sequentially.
656 unsigned i = 0;
657 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
658 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
659 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000660 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000661 }
662
663 // Finally, round the size of the total struct up to the alignment of the
664 // struct itself.
665 NewEntry->FinalizeLayout();
666 return *NewEntry;
667}
668
Devang Patel7a78e432007-11-01 19:11:01 +0000669/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000670/// specified record (struct/union/class), which indicates its size and field
671/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000672const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000673 D = D->getDefinition(*this);
674 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000675
Chris Lattner4b009652007-07-25 00:24:17 +0000676 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000677 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000678 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000679
Devang Patel7a78e432007-11-01 19:11:01 +0000680 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
681 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
682 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000683 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000684
Douglas Gregor39677622008-12-11 20:41:00 +0000685 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000686 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000687 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000688
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000689 unsigned StructPacking = 0;
690 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
691 StructPacking = PA->getAlignment();
692
Eli Friedman5949a022008-05-30 09:31:38 +0000693 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000694 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
695 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000696
Eli Friedman5949a022008-05-30 09:31:38 +0000697 // Layout each field, for now, just sequentially, respecting alignment. In
698 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000699 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000700 for (RecordDecl::field_iterator Field = D->field_begin(),
701 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000702 Field != FieldEnd; (void)++Field, ++FieldIdx)
703 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000704
705 // Finally, round the size of the total struct up to the alignment of the
706 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000707 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000708 return *NewEntry;
709}
710
Chris Lattner4b009652007-07-25 00:24:17 +0000711//===----------------------------------------------------------------------===//
712// Type creation/memoization methods
713//===----------------------------------------------------------------------===//
714
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000715QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000716 QualType CanT = getCanonicalType(T);
717 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000718 return T;
719
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000720 // Type's cannot have multiple ExtQuals, therefore we know we only have to deal
Chris Lattner35fef522008-02-20 20:55:12 +0000721 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000722 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000723 "Type is already address space qualified");
724
725 // Check if we've already instantiated an address space qual'd type of this
726 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000727 llvm::FoldingSetNodeID ID;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000728 ExtQualType::Profile(ID, T.getTypePtr(), AddressSpace, T.getObjCGCAttr());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000729 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000730 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
731 return QualType(EXTQy, 0);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000732
733 // If the base type isn't canonical, this won't be a canonical type either,
734 // so fill in the canonical type field.
735 QualType Canonical;
736 if (!T->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000737 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000738
739 // Get the new insert position for the node we care about.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000740 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000741 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000742 }
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000743 ExtQualType *New = new (*this, 8) ExtQualType(T.getTypePtr(), Canonical,
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000744 AddressSpace,
745 QualType::GCNone);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000746 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000747 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000748 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000749}
750
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000751QualType ASTContext::getObjCGCQualType(QualType T,
752 QualType::GCAttrTypes attr) {
753 QualType CanT = getCanonicalType(T);
754 if (CanT.getObjCGCAttr() == attr)
755 return T;
756
757 // Type's cannot have multiple ExtQuals, therefore we know we only have to deal
758 // with CVR qualifiers from here on out.
759 assert(CanT.getObjCGCAttr() == QualType::GCNone &&
760 "Type is already gc qualified");
761
762 // Check if we've already instantiated an gc qual'd type of this type.
763 llvm::FoldingSetNodeID ID;
764 ExtQualType::Profile(ID, T.getTypePtr(), T.getAddressSpace(), attr);
765 void *InsertPos = 0;
766 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
767 return QualType(EXTQy, 0);
768
769 // If the base type isn't canonical, this won't be a canonical type either,
770 // so fill in the canonical type field.
771 QualType Canonical;
772 if (!T->isCanonical()) {
773 Canonical = getObjCGCQualType(CanT, attr);
774
775 // Get the new insert position for the node we care about.
776 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
777 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
778 }
779 ExtQualType *New = new (*this, 8) ExtQualType(T.getTypePtr(), Canonical,
780 0, attr);
781 ExtQualTypes.InsertNode(New, InsertPos);
782 Types.push_back(New);
783 return QualType(New, T.getCVRQualifiers());
784}
Chris Lattner4b009652007-07-25 00:24:17 +0000785
786/// getComplexType - Return the uniqued reference to the type for a complex
787/// number with the specified element type.
788QualType ASTContext::getComplexType(QualType T) {
789 // Unique pointers, to guarantee there is only one pointer of a particular
790 // structure.
791 llvm::FoldingSetNodeID ID;
792 ComplexType::Profile(ID, T);
793
794 void *InsertPos = 0;
795 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
796 return QualType(CT, 0);
797
798 // If the pointee type isn't canonical, this won't be a canonical type either,
799 // so fill in the canonical type field.
800 QualType Canonical;
801 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000802 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000803
804 // Get the new insert position for the node we care about.
805 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000806 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000807 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000808 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000809 Types.push_back(New);
810 ComplexTypes.InsertNode(New, InsertPos);
811 return QualType(New, 0);
812}
813
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000814QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
815 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
816 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
817 FixedWidthIntType *&Entry = Map[Width];
818 if (!Entry)
819 Entry = new FixedWidthIntType(Width, Signed);
820 return QualType(Entry, 0);
821}
Chris Lattner4b009652007-07-25 00:24:17 +0000822
823/// getPointerType - Return the uniqued reference to the type for a pointer to
824/// the specified type.
825QualType ASTContext::getPointerType(QualType T) {
826 // Unique pointers, to guarantee there is only one pointer of a particular
827 // structure.
828 llvm::FoldingSetNodeID ID;
829 PointerType::Profile(ID, T);
830
831 void *InsertPos = 0;
832 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
833 return QualType(PT, 0);
834
835 // If the pointee type isn't canonical, this won't be a canonical type either,
836 // so fill in the canonical type field.
837 QualType Canonical;
838 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000839 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000840
841 // Get the new insert position for the node we care about.
842 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000843 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000844 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000845 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000846 Types.push_back(New);
847 PointerTypes.InsertNode(New, InsertPos);
848 return QualType(New, 0);
849}
850
Steve Naroff7aa54752008-08-27 16:04:49 +0000851/// getBlockPointerType - Return the uniqued reference to the type for
852/// a pointer to the specified block.
853QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000854 assert(T->isFunctionType() && "block of function types only");
855 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000856 // structure.
857 llvm::FoldingSetNodeID ID;
858 BlockPointerType::Profile(ID, T);
859
860 void *InsertPos = 0;
861 if (BlockPointerType *PT =
862 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
863 return QualType(PT, 0);
864
Steve Narofffd5b19d2008-08-28 19:20:44 +0000865 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000866 // type either so fill in the canonical type field.
867 QualType Canonical;
868 if (!T->isCanonical()) {
869 Canonical = getBlockPointerType(getCanonicalType(T));
870
871 // Get the new insert position for the node we care about.
872 BlockPointerType *NewIP =
873 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000874 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000875 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000876 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000877 Types.push_back(New);
878 BlockPointerTypes.InsertNode(New, InsertPos);
879 return QualType(New, 0);
880}
881
Chris Lattner4b009652007-07-25 00:24:17 +0000882/// getReferenceType - Return the uniqued reference to the type for a reference
883/// to the specified type.
884QualType ASTContext::getReferenceType(QualType T) {
885 // Unique pointers, to guarantee there is only one pointer of a particular
886 // structure.
887 llvm::FoldingSetNodeID ID;
888 ReferenceType::Profile(ID, T);
889
890 void *InsertPos = 0;
891 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
892 return QualType(RT, 0);
893
894 // If the referencee type isn't canonical, this won't be a canonical type
895 // either, so fill in the canonical type field.
896 QualType Canonical;
897 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000898 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000899
900 // Get the new insert position for the node we care about.
901 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000902 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000903 }
904
Steve Naroff93fd2112009-01-27 22:08:43 +0000905 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000906 Types.push_back(New);
907 ReferenceTypes.InsertNode(New, InsertPos);
908 return QualType(New, 0);
909}
910
Sebastian Redl75555032009-01-24 21:16:55 +0000911/// getMemberPointerType - Return the uniqued reference to the type for a
912/// member pointer to the specified type, in the specified class.
913QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
914{
915 // Unique pointers, to guarantee there is only one pointer of a particular
916 // structure.
917 llvm::FoldingSetNodeID ID;
918 MemberPointerType::Profile(ID, T, Cls);
919
920 void *InsertPos = 0;
921 if (MemberPointerType *PT =
922 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
923 return QualType(PT, 0);
924
925 // If the pointee or class type isn't canonical, this won't be a canonical
926 // type either, so fill in the canonical type field.
927 QualType Canonical;
928 if (!T->isCanonical()) {
929 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
930
931 // Get the new insert position for the node we care about.
932 MemberPointerType *NewIP =
933 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
934 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
935 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000936 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000937 Types.push_back(New);
938 MemberPointerTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Steve Naroff83c13012007-08-30 01:06:46 +0000942/// getConstantArrayType - Return the unique reference to the type for an
943/// array of the specified element type.
944QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000945 const llvm::APInt &ArySize,
946 ArrayType::ArraySizeModifier ASM,
947 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000948 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000949 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000950
951 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000952 if (ConstantArrayType *ATP =
953 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000954 return QualType(ATP, 0);
955
956 // If the element type isn't canonical, this won't be a canonical type either,
957 // so fill in the canonical type field.
958 QualType Canonical;
959 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000960 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000961 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000963 ConstantArrayType *NewIP =
964 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000965 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000966 }
967
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000968 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000969 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000970 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000971 Types.push_back(New);
972 return QualType(New, 0);
973}
974
Steve Naroffe2579e32007-08-30 18:14:25 +0000975/// getVariableArrayType - Returns a non-unique reference to the type for a
976/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000977QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
978 ArrayType::ArraySizeModifier ASM,
979 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000980 // Since we don't unique expressions, it isn't possible to unique VLA's
981 // that have an expression provided for their size.
982
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000983 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000984 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000985
986 VariableArrayTypes.push_back(New);
987 Types.push_back(New);
988 return QualType(New, 0);
989}
990
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000991/// getDependentSizedArrayType - Returns a non-unique reference to
992/// the type for a dependently-sized array of the specified element
993/// type. FIXME: We will need these to be uniqued, or at least
994/// comparable, at some point.
995QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
996 ArrayType::ArraySizeModifier ASM,
997 unsigned EltTypeQuals) {
998 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
999 "Size must be type- or value-dependent!");
1000
1001 // Since we don't unique expressions, it isn't possible to unique
1002 // dependently-sized array types.
1003
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001004 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001005 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1006 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001007
1008 DependentSizedArrayTypes.push_back(New);
1009 Types.push_back(New);
1010 return QualType(New, 0);
1011}
1012
Eli Friedman8ff07782008-02-15 18:16:39 +00001013QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1014 ArrayType::ArraySizeModifier ASM,
1015 unsigned EltTypeQuals) {
1016 llvm::FoldingSetNodeID ID;
1017 IncompleteArrayType::Profile(ID, EltTy);
1018
1019 void *InsertPos = 0;
1020 if (IncompleteArrayType *ATP =
1021 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1022 return QualType(ATP, 0);
1023
1024 // If the element type isn't canonical, this won't be a canonical type
1025 // either, so fill in the canonical type field.
1026 QualType Canonical;
1027
1028 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001029 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001030 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001031
1032 // Get the new insert position for the node we care about.
1033 IncompleteArrayType *NewIP =
1034 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001036 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001037
Steve Naroff93fd2112009-01-27 22:08:43 +00001038 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001039 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001040
1041 IncompleteArrayTypes.InsertNode(New, InsertPos);
1042 Types.push_back(New);
1043 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001044}
1045
Chris Lattner4b009652007-07-25 00:24:17 +00001046/// getVectorType - Return the unique reference to a vector type of
1047/// the specified element type and size. VectorType must be a built-in type.
1048QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1049 BuiltinType *baseType;
1050
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001051 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001052 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1053
1054 // Check if we've already instantiated a vector of this type.
1055 llvm::FoldingSetNodeID ID;
1056 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1057 void *InsertPos = 0;
1058 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1059 return QualType(VTP, 0);
1060
1061 // If the element type isn't canonical, this won't be a canonical type either,
1062 // so fill in the canonical type field.
1063 QualType Canonical;
1064 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001065 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001066
1067 // Get the new insert position for the node we care about.
1068 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001069 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001070 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001071 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001072 VectorTypes.InsertNode(New, InsertPos);
1073 Types.push_back(New);
1074 return QualType(New, 0);
1075}
1076
Nate Begemanaf6ed502008-04-18 23:10:10 +00001077/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001078/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001079QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001080 BuiltinType *baseType;
1081
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001082 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001083 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001084
1085 // Check if we've already instantiated a vector of this type.
1086 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001087 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001088 void *InsertPos = 0;
1089 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1090 return QualType(VTP, 0);
1091
1092 // If the element type isn't canonical, this won't be a canonical type either,
1093 // so fill in the canonical type field.
1094 QualType Canonical;
1095 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001096 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001097
1098 // Get the new insert position for the node we care about.
1099 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001100 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001101 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001102 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001103 VectorTypes.InsertNode(New, InsertPos);
1104 Types.push_back(New);
1105 return QualType(New, 0);
1106}
1107
1108/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1109///
1110QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1111 // Unique functions, to guarantee there is only one function of a particular
1112 // structure.
1113 llvm::FoldingSetNodeID ID;
1114 FunctionTypeNoProto::Profile(ID, ResultTy);
1115
1116 void *InsertPos = 0;
1117 if (FunctionTypeNoProto *FT =
1118 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1119 return QualType(FT, 0);
1120
1121 QualType Canonical;
1122 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001123 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001124
1125 // Get the new insert position for the node we care about.
1126 FunctionTypeNoProto *NewIP =
1127 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001128 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001129 }
1130
Steve Naroff93fd2112009-01-27 22:08:43 +00001131 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001132 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001133 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001134 return QualType(New, 0);
1135}
1136
1137/// getFunctionType - Return a normal function type with a typed argument
1138/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001139QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001140 unsigned NumArgs, bool isVariadic,
1141 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001142 // Unique functions, to guarantee there is only one function of a particular
1143 // structure.
1144 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001145 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1146 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001147
1148 void *InsertPos = 0;
1149 if (FunctionTypeProto *FTP =
1150 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1151 return QualType(FTP, 0);
1152
1153 // Determine whether the type being created is already canonical or not.
1154 bool isCanonical = ResultTy->isCanonical();
1155 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1156 if (!ArgArray[i]->isCanonical())
1157 isCanonical = false;
1158
1159 // If this type isn't canonical, get the canonical version of it.
1160 QualType Canonical;
1161 if (!isCanonical) {
1162 llvm::SmallVector<QualType, 16> CanonicalArgs;
1163 CanonicalArgs.reserve(NumArgs);
1164 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001165 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001166
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001167 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001168 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001169 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001170
1171 // Get the new insert position for the node we care about.
1172 FunctionTypeProto *NewIP =
1173 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001174 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001175 }
1176
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001177 // FunctionTypeProto objects are allocated with extra bytes after them
1178 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001179 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001180 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1181 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001182 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001183 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001184 Types.push_back(FTP);
1185 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1186 return QualType(FTP, 0);
1187}
1188
Douglas Gregor1d661552008-04-13 21:07:44 +00001189/// getTypeDeclType - Return the unique reference to the type for the
1190/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001191QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001192 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001193 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1194
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001195 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001196 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001197 else if (isa<TemplateTypeParmDecl>(Decl)) {
1198 assert(false && "Template type parameter types are always available.");
1199 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001200 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001201
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001202 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001203 if (PrevDecl)
1204 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001205 else
1206 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001207 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001208 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001209 if (PrevDecl)
1210 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001211 else
1212 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001213 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001214 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1215 if (PrevDecl)
1216 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001217 else
1218 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001219 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001220 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001221 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001222
Ted Kremenek46a837c2008-09-05 17:16:31 +00001223 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001224 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001225}
1226
Chris Lattner4b009652007-07-25 00:24:17 +00001227/// getTypedefType - Return the unique reference to the type for the
1228/// specified typename decl.
1229QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1230 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1231
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001232 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001233 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001234 Types.push_back(Decl->TypeForDecl);
1235 return QualType(Decl->TypeForDecl, 0);
1236}
1237
Ted Kremenek42730c52008-01-07 19:49:32 +00001238/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001239/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001240QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001241 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1242
Steve Naroff93fd2112009-01-27 22:08:43 +00001243 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001244 Types.push_back(Decl->TypeForDecl);
1245 return QualType(Decl->TypeForDecl, 0);
1246}
1247
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001248/// buildObjCInterfaceType - Returns a new type for the interface
1249/// declaration, regardless. It also removes any previously built
1250/// record declaration so caller can rebuild it.
1251QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1252 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1253 if (RD)
1254 RD = 0;
1255 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1256 Types.push_back(Decl->TypeForDecl);
1257 return QualType(Decl->TypeForDecl, 0);
1258}
1259
Douglas Gregora4918772009-02-05 23:33:38 +00001260/// \brief Retrieve the template type parameter type for a template
1261/// parameter with the given depth, index, and (optionally) name.
1262QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1263 IdentifierInfo *Name) {
1264 llvm::FoldingSetNodeID ID;
1265 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1266 void *InsertPos = 0;
1267 TemplateTypeParmType *TypeParm
1268 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1269
1270 if (TypeParm)
1271 return QualType(TypeParm, 0);
1272
1273 if (Name)
1274 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1275 getTemplateTypeParmType(Depth, Index));
1276 else
1277 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1278
1279 Types.push_back(TypeParm);
1280 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1281
1282 return QualType(TypeParm, 0);
1283}
1284
Douglas Gregor8e458f42009-02-09 18:46:07 +00001285QualType
1286ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1287 unsigned NumArgs,
1288 uintptr_t *Args, bool *ArgIsType,
1289 QualType Canon) {
1290 llvm::FoldingSetNodeID ID;
1291 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1292 ArgIsType);
1293 void *InsertPos = 0;
1294 ClassTemplateSpecializationType *Spec
1295 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1296
1297 if (Spec)
1298 return QualType(Spec, 0);
1299
1300 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1301 (sizeof(uintptr_t) *
1302 (ClassTemplateSpecializationType::
1303 getNumPackedWords(NumArgs) +
1304 NumArgs)), 8);
1305 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1306 ArgIsType, Canon);
1307 Types.push_back(Spec);
1308 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1309
1310 return QualType(Spec, 0);
1311}
1312
Chris Lattnere1352302008-04-07 04:56:42 +00001313/// CmpProtocolNames - Comparison predicate for sorting protocols
1314/// alphabetically.
1315static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1316 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001317 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001318}
1319
1320static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1321 unsigned &NumProtocols) {
1322 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1323
1324 // Sort protocols, keyed by name.
1325 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1326
1327 // Remove duplicates.
1328 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1329 NumProtocols = ProtocolsEnd-Protocols;
1330}
1331
1332
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001333/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1334/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001335QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1336 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001337 // Sort the protocol list alphabetically to canonicalize it.
1338 SortAndUniqueProtocols(Protocols, NumProtocols);
1339
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001340 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001341 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001342
1343 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001344 if (ObjCQualifiedInterfaceType *QT =
1345 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001346 return QualType(QT, 0);
1347
1348 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001349 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001350 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001351
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001352 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001353 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001354 return QualType(QType, 0);
1355}
1356
Chris Lattnere1352302008-04-07 04:56:42 +00001357/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1358/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001359QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001360 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001361 // Sort the protocol list alphabetically to canonicalize it.
1362 SortAndUniqueProtocols(Protocols, NumProtocols);
1363
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001364 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001365 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001366
1367 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001368 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001369 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001370 return QualType(QT, 0);
1371
1372 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001373 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001374 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001375 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001376 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001377 return QualType(QType, 0);
1378}
1379
Steve Naroff0604dd92007-08-01 18:02:17 +00001380/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1381/// TypeOfExpr AST's (since expression's are never shared). For example,
1382/// multiple declarations that refer to "typeof(x)" all contain different
1383/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1384/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001385QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001386 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001387 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001388 Types.push_back(toe);
1389 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001390}
1391
Steve Naroff0604dd92007-08-01 18:02:17 +00001392/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1393/// TypeOfType AST's. The only motivation to unique these nodes would be
1394/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1395/// an issue. This doesn't effect the type checker, since it operates
1396/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001397QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001398 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001399 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001400 Types.push_back(tot);
1401 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001402}
1403
Chris Lattner4b009652007-07-25 00:24:17 +00001404/// getTagDeclType - Return the unique reference to the type for the
1405/// specified TagDecl (struct/union/class/enum) decl.
1406QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001407 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001408 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001409}
1410
1411/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1412/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1413/// needs to agree with the definition in <stddef.h>.
1414QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001415 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001416}
1417
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001418/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001419/// width of characters in wide strings, The value is target dependent and
1420/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001421QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001422 if (LangOpts.CPlusPlus)
1423 return WCharTy;
1424
Douglas Gregorc6507e42008-11-03 14:12:49 +00001425 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1426 // wide-character type?
1427 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001428}
1429
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001430/// getSignedWCharType - Return the type of "signed wchar_t".
1431/// Used when in C++, as a GCC extension.
1432QualType ASTContext::getSignedWCharType() const {
1433 // FIXME: derive from "Target" ?
1434 return WCharTy;
1435}
1436
1437/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1438/// Used when in C++, as a GCC extension.
1439QualType ASTContext::getUnsignedWCharType() const {
1440 // FIXME: derive from "Target" ?
1441 return UnsignedIntTy;
1442}
1443
Chris Lattner4b009652007-07-25 00:24:17 +00001444/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1445/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1446QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001447 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001448}
1449
Chris Lattner19eb97e2008-04-02 05:18:44 +00001450//===----------------------------------------------------------------------===//
1451// Type Operators
1452//===----------------------------------------------------------------------===//
1453
Chris Lattner3dae6f42008-04-06 22:41:35 +00001454/// getCanonicalType - Return the canonical (structural) type corresponding to
1455/// the specified potentially non-canonical type. The non-canonical version
1456/// of a type may have many "decorated" versions of types. Decorators can
1457/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1458/// to be free of any of these, allowing two canonical types to be compared
1459/// for exact equality with a simple pointer comparison.
1460QualType ASTContext::getCanonicalType(QualType T) {
1461 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001462
1463 // If the result has type qualifiers, make sure to canonicalize them as well.
1464 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1465 if (TypeQuals == 0) return CanType;
1466
1467 // If the type qualifiers are on an array type, get the canonical type of the
1468 // array with the qualifiers applied to the element type.
1469 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1470 if (!AT)
1471 return CanType.getQualifiedType(TypeQuals);
1472
1473 // Get the canonical version of the element with the extra qualifiers on it.
1474 // This can recursively sink qualifiers through multiple levels of arrays.
1475 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1476 NewEltTy = getCanonicalType(NewEltTy);
1477
1478 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1479 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1480 CAT->getIndexTypeQualifier());
1481 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1482 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1483 IAT->getIndexTypeQualifier());
1484
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001485 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1486 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1487 DSAT->getSizeModifier(),
1488 DSAT->getIndexTypeQualifier());
1489
Chris Lattnera1923f62008-08-04 07:31:14 +00001490 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1491 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1492 VAT->getSizeModifier(),
1493 VAT->getIndexTypeQualifier());
1494}
1495
1496
1497const ArrayType *ASTContext::getAsArrayType(QualType T) {
1498 // Handle the non-qualified case efficiently.
1499 if (T.getCVRQualifiers() == 0) {
1500 // Handle the common positive case fast.
1501 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1502 return AT;
1503 }
1504
1505 // Handle the common negative case fast, ignoring CVR qualifiers.
1506 QualType CType = T->getCanonicalTypeInternal();
1507
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001508 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001509 // test.
1510 if (!isa<ArrayType>(CType) &&
1511 !isa<ArrayType>(CType.getUnqualifiedType()))
1512 return 0;
1513
1514 // Apply any CVR qualifiers from the array type to the element type. This
1515 // implements C99 6.7.3p8: "If the specification of an array type includes
1516 // any type qualifiers, the element type is so qualified, not the array type."
1517
1518 // If we get here, we either have type qualifiers on the type, or we have
1519 // sugar such as a typedef in the way. If we have type qualifiers on the type
1520 // we must propagate them down into the elemeng type.
1521 unsigned CVRQuals = T.getCVRQualifiers();
1522 unsigned AddrSpace = 0;
1523 Type *Ty = T.getTypePtr();
1524
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001525 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001526 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001527 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1528 AddrSpace = EXTQT->getAddressSpace();
1529 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001530 } else {
1531 T = Ty->getDesugaredType();
1532 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1533 break;
1534 CVRQuals |= T.getCVRQualifiers();
1535 Ty = T.getTypePtr();
1536 }
1537 }
1538
1539 // If we have a simple case, just return now.
1540 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1541 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1542 return ATy;
1543
1544 // Otherwise, we have an array and we have qualifiers on it. Push the
1545 // qualifiers into the array element type and return a new array type.
1546 // Get the canonical version of the element with the extra qualifiers on it.
1547 // This can recursively sink qualifiers through multiple levels of arrays.
1548 QualType NewEltTy = ATy->getElementType();
1549 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001550 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001551 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1552
1553 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1554 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1555 CAT->getSizeModifier(),
1556 CAT->getIndexTypeQualifier()));
1557 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1558 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1559 IAT->getSizeModifier(),
1560 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001561
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001562 if (const DependentSizedArrayType *DSAT
1563 = dyn_cast<DependentSizedArrayType>(ATy))
1564 return cast<ArrayType>(
1565 getDependentSizedArrayType(NewEltTy,
1566 DSAT->getSizeExpr(),
1567 DSAT->getSizeModifier(),
1568 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001569
Chris Lattnera1923f62008-08-04 07:31:14 +00001570 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1571 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1572 VAT->getSizeModifier(),
1573 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001574}
1575
1576
Chris Lattner19eb97e2008-04-02 05:18:44 +00001577/// getArrayDecayedType - Return the properly qualified result of decaying the
1578/// specified array type to a pointer. This operation is non-trivial when
1579/// handling typedefs etc. The canonical type of "T" must be an array type,
1580/// this returns a pointer to a properly qualified element of the array.
1581///
1582/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1583QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001584 // Get the element type with 'getAsArrayType' so that we don't lose any
1585 // typedefs in the element type of the array. This also handles propagation
1586 // of type qualifiers from the array type into the element type if present
1587 // (C99 6.7.3p8).
1588 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1589 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001590
Chris Lattnera1923f62008-08-04 07:31:14 +00001591 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001592
1593 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001594 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001595}
1596
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001597QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001598 QualType ElemTy = VAT->getElementType();
1599
1600 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1601 return getBaseElementType(VAT);
1602
1603 return ElemTy;
1604}
1605
Chris Lattner4b009652007-07-25 00:24:17 +00001606/// getFloatingRank - Return a relative rank for floating point types.
1607/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001608static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001609 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001610 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001611
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001612 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001613 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001614 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001615 case BuiltinType::Float: return FloatRank;
1616 case BuiltinType::Double: return DoubleRank;
1617 case BuiltinType::LongDouble: return LongDoubleRank;
1618 }
1619}
1620
Steve Narofffa0c4532007-08-27 01:41:48 +00001621/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1622/// point or a complex type (based on typeDomain/typeSize).
1623/// 'typeDomain' is a real floating point or complex type.
1624/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001625QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1626 QualType Domain) const {
1627 FloatingRank EltRank = getFloatingRank(Size);
1628 if (Domain->isComplexType()) {
1629 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001630 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001631 case FloatRank: return FloatComplexTy;
1632 case DoubleRank: return DoubleComplexTy;
1633 case LongDoubleRank: return LongDoubleComplexTy;
1634 }
Chris Lattner4b009652007-07-25 00:24:17 +00001635 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001636
1637 assert(Domain->isRealFloatingType() && "Unknown domain!");
1638 switch (EltRank) {
1639 default: assert(0 && "getFloatingRank(): illegal value for rank");
1640 case FloatRank: return FloatTy;
1641 case DoubleRank: return DoubleTy;
1642 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001643 }
Chris Lattner4b009652007-07-25 00:24:17 +00001644}
1645
Chris Lattner51285d82008-04-06 23:55:33 +00001646/// getFloatingTypeOrder - Compare the rank of the two specified floating
1647/// point types, ignoring the domain of the type (i.e. 'double' ==
1648/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1649/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001650int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1651 FloatingRank LHSR = getFloatingRank(LHS);
1652 FloatingRank RHSR = getFloatingRank(RHS);
1653
1654 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001655 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001656 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001657 return 1;
1658 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001659}
1660
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001661/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1662/// routine will assert if passed a built-in type that isn't an integer or enum,
1663/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001664unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001665 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001666 if (EnumType* ET = dyn_cast<EnumType>(T))
1667 T = ET->getDecl()->getIntegerType().getTypePtr();
1668
1669 // There are two things which impact the integer rank: the width, and
1670 // the ordering of builtins. The builtin ordering is encoded in the
1671 // bottom three bits; the width is encoded in the bits above that.
1672 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1673 return FWIT->getWidth() << 3;
1674 }
1675
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001676 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001677 default: assert(0 && "getIntegerRank(): not a built-in integer");
1678 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001679 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001680 case BuiltinType::Char_S:
1681 case BuiltinType::Char_U:
1682 case BuiltinType::SChar:
1683 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001684 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001685 case BuiltinType::Short:
1686 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001687 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001688 case BuiltinType::Int:
1689 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001690 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001691 case BuiltinType::Long:
1692 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001693 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001694 case BuiltinType::LongLong:
1695 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001696 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001697 }
1698}
1699
Chris Lattner51285d82008-04-06 23:55:33 +00001700/// getIntegerTypeOrder - Returns the highest ranked integer type:
1701/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1702/// LHS < RHS, return -1.
1703int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001704 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1705 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001706 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001707
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001708 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1709 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001710
Chris Lattner51285d82008-04-06 23:55:33 +00001711 unsigned LHSRank = getIntegerRank(LHSC);
1712 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001713
Chris Lattner51285d82008-04-06 23:55:33 +00001714 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1715 if (LHSRank == RHSRank) return 0;
1716 return LHSRank > RHSRank ? 1 : -1;
1717 }
Chris Lattner4b009652007-07-25 00:24:17 +00001718
Chris Lattner51285d82008-04-06 23:55:33 +00001719 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1720 if (LHSUnsigned) {
1721 // If the unsigned [LHS] type is larger, return it.
1722 if (LHSRank >= RHSRank)
1723 return 1;
1724
1725 // If the signed type can represent all values of the unsigned type, it
1726 // wins. Because we are dealing with 2's complement and types that are
1727 // powers of two larger than each other, this is always safe.
1728 return -1;
1729 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001730
Chris Lattner51285d82008-04-06 23:55:33 +00001731 // If the unsigned [RHS] type is larger, return it.
1732 if (RHSRank >= LHSRank)
1733 return -1;
1734
1735 // If the signed type can represent all values of the unsigned type, it
1736 // wins. Because we are dealing with 2's complement and types that are
1737 // powers of two larger than each other, this is always safe.
1738 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001739}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001740
1741// getCFConstantStringType - Return the type used for constant CFStrings.
1742QualType ASTContext::getCFConstantStringType() {
1743 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001744 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001745 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001746 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001747 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001748
1749 // const int *isa;
1750 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001751 // int flags;
1752 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001753 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001754 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001755 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001756 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001757
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001758 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001759 for (unsigned i = 0; i < 4; ++i) {
1760 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1761 SourceLocation(), 0,
1762 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001763 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001764 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001765 }
1766
1767 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001768 }
1769
1770 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001771}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001772
Anders Carlssonf58cac72008-08-30 19:34:46 +00001773QualType ASTContext::getObjCFastEnumerationStateType()
1774{
1775 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001776 ObjCFastEnumerationStateTypeDecl =
1777 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1778 &Idents.get("__objcFastEnumerationState"));
1779
Anders Carlssonf58cac72008-08-30 19:34:46 +00001780 QualType FieldTypes[] = {
1781 UnsignedLongTy,
1782 getPointerType(ObjCIdType),
1783 getPointerType(UnsignedLongTy),
1784 getConstantArrayType(UnsignedLongTy,
1785 llvm::APInt(32, 5), ArrayType::Normal, 0)
1786 };
1787
Douglas Gregor8acb7272008-12-11 16:49:14 +00001788 for (size_t i = 0; i < 4; ++i) {
1789 FieldDecl *Field = FieldDecl::Create(*this,
1790 ObjCFastEnumerationStateTypeDecl,
1791 SourceLocation(), 0,
1792 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001793 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001794 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001795 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001796
Douglas Gregor8acb7272008-12-11 16:49:14 +00001797 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001798 }
1799
1800 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1801}
1802
Anders Carlssone3f02572007-10-29 06:33:42 +00001803// This returns true if a type has been typedefed to BOOL:
1804// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001805static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001806 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001807 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1808 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001809
1810 return false;
1811}
1812
Ted Kremenek42730c52008-01-07 19:49:32 +00001813/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001814/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001815int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001816 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001817
1818 // Make all integer and enum types at least as large as an int
1819 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001820 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001821 // Treat arrays as pointers, since that's how they're passed in.
1822 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001823 sz = getTypeSize(VoidPtrTy);
1824 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001825}
1826
Ted Kremenek42730c52008-01-07 19:49:32 +00001827/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001828/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001829void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001830 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001831 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001832 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001833 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001834 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001835 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001836 // Compute size of all parameters.
1837 // Start with computing size of a pointer in number of bytes.
1838 // FIXME: There might(should) be a better way of doing this computation!
1839 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001840 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001841 // The first two arguments (self and _cmd) are pointers; account for
1842 // their size.
1843 int ParmOffset = 2 * PtrSize;
1844 int NumOfParams = Decl->getNumParams();
1845 for (int i = 0; i < NumOfParams; i++) {
1846 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001847 int sz = getObjCEncodingTypeSize (PType);
1848 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001849 ParmOffset += sz;
1850 }
1851 S += llvm::utostr(ParmOffset);
1852 S += "@0:";
1853 S += llvm::utostr(PtrSize);
1854
1855 // Argument types.
1856 ParmOffset = 2 * PtrSize;
1857 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001858 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1859 QualType PType = PVDecl->getOriginalType();
1860 if (const ArrayType *AT =
1861 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1862 // Use array's original type only if it has known number of
1863 // elements.
1864 if (!dyn_cast<ConstantArrayType>(AT))
1865 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001866 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001867 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001868 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001869 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001870 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001871 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001872 }
1873}
1874
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001875/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001876/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001877/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1878/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001879/// Property attributes are stored as a comma-delimited C string. The simple
1880/// attributes readonly and bycopy are encoded as single characters. The
1881/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1882/// encoded as single characters, followed by an identifier. Property types
1883/// are also encoded as a parametrized attribute. The characters used to encode
1884/// these attributes are defined by the following enumeration:
1885/// @code
1886/// enum PropertyAttributes {
1887/// kPropertyReadOnly = 'R', // property is read-only.
1888/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1889/// kPropertyByref = '&', // property is a reference to the value last assigned
1890/// kPropertyDynamic = 'D', // property is dynamic
1891/// kPropertyGetter = 'G', // followed by getter selector name
1892/// kPropertySetter = 'S', // followed by setter selector name
1893/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1894/// kPropertyType = 't' // followed by old-style type encoding.
1895/// kPropertyWeak = 'W' // 'weak' property
1896/// kPropertyStrong = 'P' // property GC'able
1897/// kPropertyNonAtomic = 'N' // property non-atomic
1898/// };
1899/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001900void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1901 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001902 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001903 // Collect information from the property implementation decl(s).
1904 bool Dynamic = false;
1905 ObjCPropertyImplDecl *SynthesizePID = 0;
1906
1907 // FIXME: Duplicated code due to poor abstraction.
1908 if (Container) {
1909 if (const ObjCCategoryImplDecl *CID =
1910 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1911 for (ObjCCategoryImplDecl::propimpl_iterator
1912 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1913 ObjCPropertyImplDecl *PID = *i;
1914 if (PID->getPropertyDecl() == PD) {
1915 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1916 Dynamic = true;
1917 } else {
1918 SynthesizePID = PID;
1919 }
1920 }
1921 }
1922 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001923 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001924 for (ObjCCategoryImplDecl::propimpl_iterator
1925 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1926 ObjCPropertyImplDecl *PID = *i;
1927 if (PID->getPropertyDecl() == PD) {
1928 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1929 Dynamic = true;
1930 } else {
1931 SynthesizePID = PID;
1932 }
1933 }
1934 }
1935 }
1936 }
1937
1938 // FIXME: This is not very efficient.
1939 S = "T";
1940
1941 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001942 // GCC has some special rules regarding encoding of properties which
1943 // closely resembles encoding of ivars.
1944 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1945 true /* outermost type */,
1946 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001947
1948 if (PD->isReadOnly()) {
1949 S += ",R";
1950 } else {
1951 switch (PD->getSetterKind()) {
1952 case ObjCPropertyDecl::Assign: break;
1953 case ObjCPropertyDecl::Copy: S += ",C"; break;
1954 case ObjCPropertyDecl::Retain: S += ",&"; break;
1955 }
1956 }
1957
1958 // It really isn't clear at all what this means, since properties
1959 // are "dynamic by default".
1960 if (Dynamic)
1961 S += ",D";
1962
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001963 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1964 S += ",N";
1965
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001966 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1967 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001968 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001969 }
1970
1971 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1972 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001973 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001974 }
1975
1976 if (SynthesizePID) {
1977 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1978 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001979 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001980 }
1981
1982 // FIXME: OBJCGC: weak & strong
1983}
1984
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001985/// getLegacyIntegralTypeEncoding -
1986/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00001987/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001988/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1989///
1990void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1991 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1992 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00001993 if (BT->getKind() == BuiltinType::ULong &&
1994 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001995 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00001996 else
1997 if (BT->getKind() == BuiltinType::Long &&
1998 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001999 PointeeTy = IntTy;
2000 }
2001 }
2002}
2003
Fariborz Jahanian248db262008-01-22 22:44:46 +00002004void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002005 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002006 // We follow the behavior of gcc, expanding structures which are
2007 // directly pointed to, and expanding embedded structures. Note that
2008 // these rules are sufficient to prevent recursive encoding of the
2009 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002010 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2011 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002012}
2013
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002014static void EncodeBitField(const ASTContext *Context, std::string& S,
2015 FieldDecl *FD) {
2016 const Expr *E = FD->getBitWidth();
2017 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2018 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2019 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2020 S += 'b';
2021 S += llvm::utostr(N);
2022}
2023
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002024void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2025 bool ExpandPointedToStructures,
2026 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002027 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002028 bool OutermostType,
2029 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002030 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002031 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002032 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002033 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002034 else {
2035 char encoding;
2036 switch (BT->getKind()) {
2037 default: assert(0 && "Unhandled builtin type kind");
2038 case BuiltinType::Void: encoding = 'v'; break;
2039 case BuiltinType::Bool: encoding = 'B'; break;
2040 case BuiltinType::Char_U:
2041 case BuiltinType::UChar: encoding = 'C'; break;
2042 case BuiltinType::UShort: encoding = 'S'; break;
2043 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002044 case BuiltinType::ULong:
2045 encoding =
2046 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2047 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002048 case BuiltinType::ULongLong: encoding = 'Q'; break;
2049 case BuiltinType::Char_S:
2050 case BuiltinType::SChar: encoding = 'c'; break;
2051 case BuiltinType::Short: encoding = 's'; break;
2052 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002053 case BuiltinType::Long:
2054 encoding =
2055 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2056 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002057 case BuiltinType::LongLong: encoding = 'q'; break;
2058 case BuiltinType::Float: encoding = 'f'; break;
2059 case BuiltinType::Double: encoding = 'd'; break;
2060 case BuiltinType::LongDouble: encoding = 'd'; break;
2061 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002062
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002063 S += encoding;
2064 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002065 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002066 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002067 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2068 ExpandPointedToStructures,
2069 ExpandStructures, FD);
2070 if (FD || EncodingProperty) {
2071 // Note that we do extended encoding of protocol qualifer list
2072 // Only when doing ivar or property encoding.
2073 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2074 S += '"';
2075 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2076 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2077 S += '<';
2078 S += Proto->getNameAsString();
2079 S += '>';
2080 }
2081 S += '"';
2082 }
2083 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002084 }
2085 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002086 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002087 bool isReadOnly = false;
2088 // For historical/compatibility reasons, the read-only qualifier of the
2089 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2090 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2091 // Also, do not emit the 'r' for anything but the outermost type!
2092 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2093 if (OutermostType && T.isConstQualified()) {
2094 isReadOnly = true;
2095 S += 'r';
2096 }
2097 }
2098 else if (OutermostType) {
2099 QualType P = PointeeTy;
2100 while (P->getAsPointerType())
2101 P = P->getAsPointerType()->getPointeeType();
2102 if (P.isConstQualified()) {
2103 isReadOnly = true;
2104 S += 'r';
2105 }
2106 }
2107 if (isReadOnly) {
2108 // Another legacy compatibility encoding. Some ObjC qualifier and type
2109 // combinations need to be rearranged.
2110 // Rewrite "in const" from "nr" to "rn"
2111 const char * s = S.c_str();
2112 int len = S.length();
2113 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2114 std::string replace = "rn";
2115 S.replace(S.end()-2, S.end(), replace);
2116 }
2117 }
Steve Naroff17c03822009-02-12 17:52:19 +00002118 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002119 S += '@';
2120 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002121 }
2122 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002123 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002124 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002125 // Another historical/compatibility reason.
2126 // We encode the underlying type which comes out as
2127 // {...};
2128 S += '^';
2129 getObjCEncodingForTypeImpl(PointeeTy, S,
2130 false, ExpandPointedToStructures,
2131 NULL);
2132 return;
2133 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002134 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002135 if (FD || EncodingProperty) {
2136 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2137 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002138 S += '"';
2139 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002140 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2141 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2142 S += '<';
2143 S += Proto->getNameAsString();
2144 S += '>';
2145 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002146 S += '"';
2147 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002148 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002149 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002150 S += '#';
2151 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002152 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002153 S += ':';
2154 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002155 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002156
2157 if (PointeeTy->isCharType()) {
2158 // char pointer types should be encoded as '*' unless it is a
2159 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002160 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002161 S += '*';
2162 return;
2163 }
2164 }
2165
2166 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002167 getLegacyIntegralTypeEncoding(PointeeTy);
2168
2169 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002170 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002171 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002172 } else if (const ArrayType *AT =
2173 // Ignore type qualifiers etc.
2174 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002175 S += '[';
2176
2177 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2178 S += llvm::utostr(CAT->getSize().getZExtValue());
2179 else
2180 assert(0 && "Unhandled array type!");
2181
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002182 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002183 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002184 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002185 } else if (T->getAsFunctionType()) {
2186 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002187 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002188 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002189 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002190 // Anonymous structures print as '?'
2191 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2192 S += II->getName();
2193 } else {
2194 S += '?';
2195 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002196 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002197 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002198 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2199 FieldEnd = RDecl->field_end();
2200 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002201 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002202 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002203 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002204 S += '"';
2205 }
2206
2207 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002208 if (Field->isBitField()) {
2209 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2210 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002211 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002212 QualType qt = Field->getType();
2213 getLegacyIntegralTypeEncoding(qt);
2214 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002215 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002216 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002217 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002218 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002219 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002220 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002221 if (FD && FD->isBitField())
2222 EncodeBitField(this, S, FD);
2223 else
2224 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002225 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002226 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002227 } else if (T->isObjCInterfaceType()) {
2228 // @encode(class_name)
2229 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2230 S += '{';
2231 const IdentifierInfo *II = OI->getIdentifier();
2232 S += II->getName();
2233 S += '=';
2234 std::vector<FieldDecl*> RecFields;
2235 CollectObjCIvars(OI, RecFields);
2236 for (unsigned int i = 0; i != RecFields.size(); i++) {
2237 if (RecFields[i]->isBitField())
2238 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2239 RecFields[i]);
2240 else
2241 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2242 FD);
2243 }
2244 S += '}';
2245 }
2246 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002247 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002248}
2249
Ted Kremenek42730c52008-01-07 19:49:32 +00002250void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002251 std::string& S) const {
2252 if (QT & Decl::OBJC_TQ_In)
2253 S += 'n';
2254 if (QT & Decl::OBJC_TQ_Inout)
2255 S += 'N';
2256 if (QT & Decl::OBJC_TQ_Out)
2257 S += 'o';
2258 if (QT & Decl::OBJC_TQ_Bycopy)
2259 S += 'O';
2260 if (QT & Decl::OBJC_TQ_Byref)
2261 S += 'R';
2262 if (QT & Decl::OBJC_TQ_Oneway)
2263 S += 'V';
2264}
2265
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002266void ASTContext::setBuiltinVaListType(QualType T)
2267{
2268 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2269
2270 BuiltinVaListType = T;
2271}
2272
Ted Kremenek42730c52008-01-07 19:49:32 +00002273void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002274{
Ted Kremenek42730c52008-01-07 19:49:32 +00002275 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002276
2277 // typedef struct objc_object *id;
2278 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002279 // User error - caller will issue diagnostics.
2280 if (!ptr)
2281 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002282 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002283 // User error - caller will issue diagnostics.
2284 if (!rec)
2285 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002286 IdStructType = rec;
2287}
2288
Ted Kremenek42730c52008-01-07 19:49:32 +00002289void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002290{
Ted Kremenek42730c52008-01-07 19:49:32 +00002291 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002292
2293 // typedef struct objc_selector *SEL;
2294 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002295 if (!ptr)
2296 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002297 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002298 if (!rec)
2299 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002300 SelStructType = rec;
2301}
2302
Ted Kremenek42730c52008-01-07 19:49:32 +00002303void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002304{
Ted Kremenek42730c52008-01-07 19:49:32 +00002305 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002306}
2307
Ted Kremenek42730c52008-01-07 19:49:32 +00002308void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002309{
Ted Kremenek42730c52008-01-07 19:49:32 +00002310 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002311
2312 // typedef struct objc_class *Class;
2313 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2314 assert(ptr && "'Class' incorrectly typed");
2315 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2316 assert(rec && "'Class' incorrectly typed");
2317 ClassStructType = rec;
2318}
2319
Ted Kremenek42730c52008-01-07 19:49:32 +00002320void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2321 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002322 "'NSConstantString' type already set!");
2323
Ted Kremenek42730c52008-01-07 19:49:32 +00002324 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002325}
2326
Douglas Gregorc6507e42008-11-03 14:12:49 +00002327/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002328/// TargetInfo, produce the corresponding type. The unsigned @p Type
2329/// is actually a value of type @c TargetInfo::IntType.
2330QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002331 switch (Type) {
2332 case TargetInfo::NoInt: return QualType();
2333 case TargetInfo::SignedShort: return ShortTy;
2334 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2335 case TargetInfo::SignedInt: return IntTy;
2336 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2337 case TargetInfo::SignedLong: return LongTy;
2338 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2339 case TargetInfo::SignedLongLong: return LongLongTy;
2340 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2341 }
2342
2343 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002344 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002345}
Ted Kremenek118930e2008-07-24 23:58:27 +00002346
2347//===----------------------------------------------------------------------===//
2348// Type Predicates.
2349//===----------------------------------------------------------------------===//
2350
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002351/// isObjCNSObjectType - Return true if this is an NSObject object using
2352/// NSObject attribute on a c-style pointer type.
2353/// FIXME - Make it work directly on types.
2354///
2355bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2356 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2357 if (TypedefDecl *TD = TDT->getDecl())
2358 if (TD->getAttr<ObjCNSObjectAttr>())
2359 return true;
2360 }
2361 return false;
2362}
2363
Ted Kremenek118930e2008-07-24 23:58:27 +00002364/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2365/// to an object type. This includes "id" and "Class" (two 'special' pointers
2366/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2367/// ID type).
2368bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2369 if (Ty->isObjCQualifiedIdType())
2370 return true;
2371
Steve Naroffd9e00802008-10-21 18:24:04 +00002372 // Blocks are objects.
2373 if (Ty->isBlockPointerType())
2374 return true;
2375
2376 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002377 if (!Ty->isPointerType())
2378 return false;
2379
2380 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2381 // pointer types. This looks for the typedef specifically, not for the
2382 // underlying type.
2383 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2384 return true;
2385
2386 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002387 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2388 return true;
2389
2390 // If is has NSObject attribute, OK as well.
2391 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002392}
2393
Chris Lattner6ff358b2008-04-07 06:51:04 +00002394//===----------------------------------------------------------------------===//
2395// Type Compatibility Testing
2396//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002397
Steve Naroff3454b6c2008-09-04 15:10:53 +00002398/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002399/// block types. Types must be strictly compatible here. For example,
2400/// C unfortunately doesn't produce an error for the following:
2401///
2402/// int (*emptyArgFunc)();
2403/// int (*intArgList)(int) = emptyArgFunc;
2404///
2405/// For blocks, we will produce an error for the following (similar to C++):
2406///
2407/// int (^emptyArgBlock)();
2408/// int (^intArgBlock)(int) = emptyArgBlock;
2409///
2410/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2411///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002412bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002413 const FunctionType *lbase = lhs->getAsFunctionType();
2414 const FunctionType *rbase = rhs->getAsFunctionType();
2415 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2416 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2417 if (lproto && rproto)
2418 return !mergeTypes(lhs, rhs).isNull();
2419 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002420}
2421
Chris Lattner6ff358b2008-04-07 06:51:04 +00002422/// areCompatVectorTypes - Return true if the two specified vector types are
2423/// compatible.
2424static bool areCompatVectorTypes(const VectorType *LHS,
2425 const VectorType *RHS) {
2426 assert(LHS->isCanonical() && RHS->isCanonical());
2427 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002428 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002429}
2430
Eli Friedman0d9549b2008-08-22 00:56:42 +00002431/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002432/// compatible for assignment from RHS to LHS. This handles validation of any
2433/// protocol qualifiers on the LHS or RHS.
2434///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002435bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2436 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002437 // Verify that the base decls are compatible: the RHS must be a subclass of
2438 // the LHS.
2439 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2440 return false;
2441
2442 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2443 // protocol qualified at all, then we are good.
2444 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2445 return true;
2446
2447 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2448 // isn't a superset.
2449 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2450 return true; // FIXME: should return false!
2451
2452 // Finally, we must have two protocol-qualified interfaces.
2453 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2454 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2455 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2456 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2457 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2458 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2459
2460 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2461 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2462 // LHS in a single parallel scan until we run out of elements in LHS.
2463 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2464 ObjCProtocolDecl *LHSProto = *LHSPI;
2465
2466 while (RHSPI != RHSPE) {
2467 ObjCProtocolDecl *RHSProto = *RHSPI++;
2468 // If the RHS has a protocol that the LHS doesn't, ignore it.
2469 if (RHSProto != LHSProto)
2470 continue;
2471
2472 // Otherwise, the RHS does have this element.
2473 ++LHSPI;
2474 if (LHSPI == LHSPE)
2475 return true; // All protocols in LHS exist in RHS.
2476
2477 LHSProto = *LHSPI;
2478 }
2479
2480 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2481 return false;
2482}
2483
Steve Naroff17c03822009-02-12 17:52:19 +00002484bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2485 // get the "pointed to" types
2486 const PointerType *LHSPT = LHS->getAsPointerType();
2487 const PointerType *RHSPT = RHS->getAsPointerType();
2488
2489 if (!LHSPT || !RHSPT)
2490 return false;
2491
2492 QualType lhptee = LHSPT->getPointeeType();
2493 QualType rhptee = RHSPT->getPointeeType();
2494 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2495 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2496 // ID acts sort of like void* for ObjC interfaces
2497 if (LHSIface && isObjCIdStructType(rhptee))
2498 return true;
2499 if (RHSIface && isObjCIdStructType(lhptee))
2500 return true;
2501 if (!LHSIface || !RHSIface)
2502 return false;
2503 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2504 canAssignObjCInterfaces(RHSIface, LHSIface);
2505}
2506
Steve Naroff85f0dc52007-10-15 20:41:53 +00002507/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2508/// both shall have the identically qualified version of a compatible type.
2509/// C99 6.2.7p1: Two types have compatible types if their types are the
2510/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002511bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2512 return !mergeTypes(LHS, RHS).isNull();
2513}
2514
2515QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2516 const FunctionType *lbase = lhs->getAsFunctionType();
2517 const FunctionType *rbase = rhs->getAsFunctionType();
2518 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2519 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2520 bool allLTypes = true;
2521 bool allRTypes = true;
2522
2523 // Check return type
2524 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2525 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002526 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2527 allLTypes = false;
2528 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2529 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002530
2531 if (lproto && rproto) { // two C99 style function prototypes
2532 unsigned lproto_nargs = lproto->getNumArgs();
2533 unsigned rproto_nargs = rproto->getNumArgs();
2534
2535 // Compatible functions must have the same number of arguments
2536 if (lproto_nargs != rproto_nargs)
2537 return QualType();
2538
2539 // Variadic and non-variadic functions aren't compatible
2540 if (lproto->isVariadic() != rproto->isVariadic())
2541 return QualType();
2542
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002543 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2544 return QualType();
2545
Eli Friedman0d9549b2008-08-22 00:56:42 +00002546 // Check argument compatibility
2547 llvm::SmallVector<QualType, 10> types;
2548 for (unsigned i = 0; i < lproto_nargs; i++) {
2549 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2550 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2551 QualType argtype = mergeTypes(largtype, rargtype);
2552 if (argtype.isNull()) return QualType();
2553 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002554 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2555 allLTypes = false;
2556 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2557 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002558 }
2559 if (allLTypes) return lhs;
2560 if (allRTypes) return rhs;
2561 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002562 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002563 }
2564
2565 if (lproto) allRTypes = false;
2566 if (rproto) allLTypes = false;
2567
2568 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2569 if (proto) {
2570 if (proto->isVariadic()) return QualType();
2571 // Check that the types are compatible with the types that
2572 // would result from default argument promotions (C99 6.7.5.3p15).
2573 // The only types actually affected are promotable integer
2574 // types and floats, which would be passed as a different
2575 // type depending on whether the prototype is visible.
2576 unsigned proto_nargs = proto->getNumArgs();
2577 for (unsigned i = 0; i < proto_nargs; ++i) {
2578 QualType argTy = proto->getArgType(i);
2579 if (argTy->isPromotableIntegerType() ||
2580 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2581 return QualType();
2582 }
2583
2584 if (allLTypes) return lhs;
2585 if (allRTypes) return rhs;
2586 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002587 proto->getNumArgs(), lproto->isVariadic(),
2588 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002589 }
2590
2591 if (allLTypes) return lhs;
2592 if (allRTypes) return rhs;
2593 return getFunctionTypeNoProto(retType);
2594}
2595
2596QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002597 // C++ [expr]: If an expression initially has the type "reference to T", the
2598 // type is adjusted to "T" prior to any further analysis, the expression
2599 // designates the object or function denoted by the reference, and the
2600 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002601 // FIXME: C++ shouldn't be going through here! The rules are different
2602 // enough that they should be handled separately.
2603 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002604 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002605 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002606 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002607
Eli Friedman0d9549b2008-08-22 00:56:42 +00002608 QualType LHSCan = getCanonicalType(LHS),
2609 RHSCan = getCanonicalType(RHS);
2610
2611 // If two types are identical, they are compatible.
2612 if (LHSCan == RHSCan)
2613 return LHS;
2614
2615 // If the qualifiers are different, the types aren't compatible
2616 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2617 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2618 return QualType();
2619
2620 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2621 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2622
Chris Lattnerc38d4522008-01-14 05:45:46 +00002623 // We want to consider the two function types to be the same for these
2624 // comparisons, just force one to the other.
2625 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2626 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002627
2628 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002629 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2630 LHSClass = Type::ConstantArray;
2631 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2632 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002633
Nate Begemanaf6ed502008-04-18 23:10:10 +00002634 // Canonicalize ExtVector -> Vector.
2635 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2636 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002637
Chris Lattner7cdcb252008-04-07 06:38:24 +00002638 // Consider qualified interfaces and interfaces the same.
2639 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2640 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002641
Chris Lattnerb5709e22008-04-07 05:43:21 +00002642 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002643 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002644 // ID is compatible with all qualified id types.
2645 if (LHS->isObjCQualifiedIdType()) {
2646 if (const PointerType *PT = RHS->getAsPointerType()) {
2647 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002648 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002649 return LHS;
2650 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2651 // Unfortunately, this API is part of Sema (which we don't have access
2652 // to. Need to refactor. The following check is insufficient, since we
2653 // need to make sure the class implements the protocol.
2654 if (pType->isObjCInterfaceType())
2655 return LHS;
2656 }
2657 }
2658 if (RHS->isObjCQualifiedIdType()) {
2659 if (const PointerType *PT = LHS->getAsPointerType()) {
2660 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002661 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002662 return RHS;
2663 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2664 // Unfortunately, this API is part of Sema (which we don't have access
2665 // to. Need to refactor. The following check is insufficient, since we
2666 // need to make sure the class implements the protocol.
2667 if (pType->isObjCInterfaceType())
2668 return RHS;
2669 }
2670 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002671 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2672 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002673 if (const EnumType* ETy = LHS->getAsEnumType()) {
2674 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2675 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002676 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002677 if (const EnumType* ETy = RHS->getAsEnumType()) {
2678 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2679 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002680 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002681
Eli Friedman0d9549b2008-08-22 00:56:42 +00002682 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002683 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002684
Steve Naroffc88babe2008-01-09 22:43:08 +00002685 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002686 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002687 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002688 {
2689 // Merge two pointer types, while trying to preserve typedef info
2690 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2691 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2692 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2693 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002694 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2695 return LHS;
2696 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2697 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002698 return getPointerType(ResultType);
2699 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002700 case Type::BlockPointer:
2701 {
2702 // Merge two block pointer types, while trying to preserve typedef info
2703 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2704 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2705 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2706 if (ResultType.isNull()) return QualType();
2707 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2708 return LHS;
2709 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2710 return RHS;
2711 return getBlockPointerType(ResultType);
2712 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002713 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002714 {
2715 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2716 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2717 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2718 return QualType();
2719
2720 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2721 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2722 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2723 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002724 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2725 return LHS;
2726 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2727 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002728 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2729 ArrayType::ArraySizeModifier(), 0);
2730 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2731 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002732 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2733 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002734 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2735 return LHS;
2736 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2737 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002738 if (LVAT) {
2739 // FIXME: This isn't correct! But tricky to implement because
2740 // the array's size has to be the size of LHS, but the type
2741 // has to be different.
2742 return LHS;
2743 }
2744 if (RVAT) {
2745 // FIXME: This isn't correct! But tricky to implement because
2746 // the array's size has to be the size of RHS, but the type
2747 // has to be different.
2748 return RHS;
2749 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002750 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2751 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002752 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002753 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002754 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002755 return mergeFunctionTypes(LHS, RHS);
2756 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002757 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002758 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2759 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002760 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002761 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002762 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002763 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002764 case Type::Complex:
2765 // Distinct complex types are incompatible.
2766 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002767 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002768 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2769 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002770 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002771 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002772 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2773 // for checking assignment/comparison safety
2774 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002775 case Type::ObjCQualifiedId:
2776 // Distinct qualified id's are not compatible.
2777 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002778 default:
2779 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002780 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002781 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002782}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002783
Chris Lattner1d78a862008-04-07 07:01:58 +00002784//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002785// Integer Predicates
2786//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002787
Eli Friedman0832dbc2008-06-28 06:23:08 +00002788unsigned ASTContext::getIntWidth(QualType T) {
2789 if (T == BoolTy)
2790 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002791 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2792 return FWIT->getWidth();
2793 }
2794 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002795 return (unsigned)getTypeSize(T);
2796}
2797
2798QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2799 assert(T->isSignedIntegerType() && "Unexpected type");
2800 if (const EnumType* ETy = T->getAsEnumType())
2801 T = ETy->getDecl()->getIntegerType();
2802 const BuiltinType* BTy = T->getAsBuiltinType();
2803 assert (BTy && "Unexpected signed integer type");
2804 switch (BTy->getKind()) {
2805 case BuiltinType::Char_S:
2806 case BuiltinType::SChar:
2807 return UnsignedCharTy;
2808 case BuiltinType::Short:
2809 return UnsignedShortTy;
2810 case BuiltinType::Int:
2811 return UnsignedIntTy;
2812 case BuiltinType::Long:
2813 return UnsignedLongTy;
2814 case BuiltinType::LongLong:
2815 return UnsignedLongLongTy;
2816 default:
2817 assert(0 && "Unexpected signed integer type");
2818 return QualType();
2819 }
2820}
2821
2822
2823//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002824// Serialization Support
2825//===----------------------------------------------------------------------===//
2826
Ted Kremenek738e6c02007-10-31 17:10:13 +00002827/// Emit - Serialize an ASTContext object to Bitcode.
2828void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002829 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002830 S.EmitRef(SourceMgr);
2831 S.EmitRef(Target);
2832 S.EmitRef(Idents);
2833 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002834
Ted Kremenek68228a92007-10-31 22:44:07 +00002835 // Emit the size of the type vector so that we can reserve that size
2836 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002837 S.EmitInt(Types.size());
2838
Ted Kremenek034a78c2007-11-13 22:02:55 +00002839 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2840 I!=E;++I)
2841 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002842
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002843 S.EmitOwnedPtr(TUDecl);
2844
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002845 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002846}
2847
Ted Kremenekacba3612007-11-13 00:25:37 +00002848ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002849
2850 // Read the language options.
2851 LangOptions LOpts;
2852 LOpts.Read(D);
2853
Ted Kremenek68228a92007-10-31 22:44:07 +00002854 SourceManager &SM = D.ReadRef<SourceManager>();
2855 TargetInfo &t = D.ReadRef<TargetInfo>();
2856 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2857 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002858
Ted Kremenek68228a92007-10-31 22:44:07 +00002859 unsigned size_reserve = D.ReadInt();
2860
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002861 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2862 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002863
Ted Kremenek034a78c2007-11-13 22:02:55 +00002864 for (unsigned i = 0; i < size_reserve; ++i)
2865 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002866
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002867 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2868
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002869 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002870
2871 return A;
2872}