blob: 9198bf71cda20198a08a724a5f55498c2d9f95aa [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor23d23262009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redl75555032009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
110 else if (isa<ArrayType>(T))
111 ++NumArray;
112 else if (isa<VectorType>(T))
113 ++NumVector;
114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000139 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
151 fprintf(stderr, " %d array types\n", NumArray);
152 fprintf(stderr, " %d vector types\n", NumVector);
153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Chris Lattner4b009652007-07-25 00:24:17 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregord2baafd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000233
Steve Naroff9d12c902007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremenek42730c52008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
249
Chris Lattner2a674dc2008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000266unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000267 // FIXME: If attribute(align) is specified on the decl, round up to it.
268
269 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
270 QualType T = VD->getType();
271 // Incomplete or function types default to 1.
272 if (T->isIncompleteType() || T->isFunctionType())
273 return 1;
274
275 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
276 T = cast<ArrayType>(T)->getElementType();
277
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000278 return getTypeAlign(T) / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000279 }
280
281 return 1;
282}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000283
Chris Lattner4b009652007-07-25 00:24:17 +0000284/// getTypeSize - Return the size of the specified type, in bits. This method
285/// does not work on incomplete types.
286std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000287ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000288 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000290 unsigned Align;
291 switch (T->getTypeClass()) {
292 case Type::TypeName: assert(0 && "Not a canonical type!");
293 case Type::FunctionNoProto:
294 case Type::FunctionProto:
295 default:
296 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000297 case Type::VariableArray:
298 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000299 case Type::DependentSizedArray:
300 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000301 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000302 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000303
Chris Lattner8cd0e932008-03-05 18:54:05 +0000304 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000305 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000306 Align = EltInfo.second;
307 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000308 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000309 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000310 case Type::Vector: {
311 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000312 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000313 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000314 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000315 // If the alignment is not a power of 2, round up to the next power of 2.
316 // This happens for non-power-of-2 length vectors.
317 // FIXME: this should probably be a target property.
318 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000319 break;
320 }
321
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000322 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000323 switch (cast<BuiltinType>(T)->getKind()) {
324 default: assert(0 && "Unknown builtin type!");
325 case BuiltinType::Void:
326 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000327 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000328 Width = Target.getBoolWidth();
329 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000330 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 case BuiltinType::Char_S:
332 case BuiltinType::Char_U:
333 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000334 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000335 Width = Target.getCharWidth();
336 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000337 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000338 case BuiltinType::WChar:
339 Width = Target.getWCharWidth();
340 Align = Target.getWCharAlign();
341 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000344 Width = Target.getShortWidth();
345 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000346 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000347 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000349 Width = Target.getIntWidth();
350 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000351 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000353 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000354 Width = Target.getLongWidth();
355 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000356 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000357 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = Target.getLongLongWidth();
360 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000361 break;
362 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000363 Width = Target.getFloatWidth();
364 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000365 break;
366 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000367 Width = Target.getDoubleWidth();
368 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000369 break;
370 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000371 Width = Target.getLongDoubleWidth();
372 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000374 }
375 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000376 case Type::FixedWidthInt:
377 // FIXME: This isn't precisely correct; the width/alignment should depend
378 // on the available types for the target
379 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000380 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000381 Align = Width;
382 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000383 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000384 // FIXME: Pointers into different addr spaces could have different sizes and
385 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000386 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000388 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000389 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000390 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000391 case Type::BlockPointer: {
392 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
393 Width = Target.getPointerWidth(AS);
394 Align = Target.getPointerAlign(AS);
395 break;
396 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000397 case Type::Pointer: {
398 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000399 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000400 Align = Target.getPointerAlign(AS);
401 break;
402 }
Chris Lattner4b009652007-07-25 00:24:17 +0000403 case Type::Reference:
404 // "When applied to a reference or a reference type, the result is the size
405 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 // FIXME: This is wrong for struct layout: a reference in a struct has
407 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000408 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000409 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000410 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000411 // the GCC ABI, where pointers to data are one pointer large, pointers to
412 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000413 // other compilers too, we need to delegate this completely to TargetInfo
414 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000415 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
416 unsigned AS = Pointee.getAddressSpace();
417 Width = Target.getPointerWidth(AS);
418 if (Pointee->isFunctionType())
419 Width *= 2;
420 Align = Target.getPointerAlign(AS);
421 // GCC aligns at single pointer width.
422 }
Chris Lattner4b009652007-07-25 00:24:17 +0000423 case Type::Complex: {
424 // Complex types have the same alignment as their elements, but twice the
425 // size.
426 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000427 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000428 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000429 Align = EltInfo.second;
430 break;
431 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000432 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000433 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000434 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
435 Width = Layout.getSize();
436 Align = Layout.getAlignment();
437 break;
438 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000439 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000440 const TagType *TT = cast<TagType>(T);
441
442 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000443 Width = 1;
444 Align = 1;
445 break;
446 }
447
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000448 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000449 return getTypeInfo(ET->getDecl()->getIntegerType());
450
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000451 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000452 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
453 Width = Layout.getSize();
454 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000455 break;
456 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000457 }
Chris Lattner4b009652007-07-25 00:24:17 +0000458
459 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000460 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000461}
462
Chris Lattner83165b52009-01-27 18:08:34 +0000463/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
464/// type for the current target in bits. This can be different than the ABI
465/// alignment in cases where it is beneficial for performance to overalign
466/// a data type.
467unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
468 unsigned ABIAlign = getTypeAlign(T);
469
470 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000471 if (T->isSpecificBuiltinType(BuiltinType::Double))
472 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000473
474 return ABIAlign;
475}
476
477
Devang Patelbfe323c2008-06-04 21:22:16 +0000478/// LayoutField - Field layout.
479void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000480 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000481 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000482 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000483 uint64_t FieldOffset = IsUnion ? 0 : Size;
484 uint64_t FieldSize;
485 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000486
487 // FIXME: Should this override struct packing? Probably we want to
488 // take the minimum?
489 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
490 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000491
492 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
493 // TODO: Need to check this algorithm on other targets!
494 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000495 FieldSize =
496 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000497
498 std::pair<uint64_t, unsigned> FieldInfo =
499 Context.getTypeInfo(FD->getType());
500 uint64_t TypeSize = FieldInfo.first;
501
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000502 // Determine the alignment of this bitfield. The packing
503 // attributes define a maximum and the alignment attribute defines
504 // a minimum.
505 // FIXME: What is the right behavior when the specified alignment
506 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000507 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000508 if (FieldPacking)
509 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000510 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
511 FieldAlign = std::max(FieldAlign, AA->getAlignment());
512
513 // Check if we need to add padding to give the field the correct
514 // alignment.
515 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
516 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
517
518 // Padding members don't affect overall alignment
519 if (!FD->getIdentifier())
520 FieldAlign = 1;
521 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000522 if (FD->getType()->isIncompleteArrayType()) {
523 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000524 // query getTypeInfo about these, so we figure it out here.
525 // Flexible array members don't have any size, but they
526 // have to be aligned appropriately for their element type.
527 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000528 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000529 FieldAlign = Context.getTypeAlign(ATy->getElementType());
530 } else {
531 std::pair<uint64_t, unsigned> FieldInfo =
532 Context.getTypeInfo(FD->getType());
533 FieldSize = FieldInfo.first;
534 FieldAlign = FieldInfo.second;
535 }
536
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000537 // Determine the alignment of this bitfield. The packing
538 // attributes define a maximum and the alignment attribute defines
539 // a minimum. Additionally, the packing alignment must be at least
540 // a byte for non-bitfields.
541 //
542 // FIXME: What is the right behavior when the specified alignment
543 // is smaller than the specified packing?
544 if (FieldPacking)
545 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000546 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
547 FieldAlign = std::max(FieldAlign, AA->getAlignment());
548
549 // Round up the current record size to the field's alignment boundary.
550 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
551 }
552
553 // Place this field at the current location.
554 FieldOffsets[FieldNo] = FieldOffset;
555
556 // Reserve space for this field.
557 if (IsUnion) {
558 Size = std::max(Size, FieldSize);
559 } else {
560 Size = FieldOffset + FieldSize;
561 }
562
563 // Remember max struct/class alignment.
564 Alignment = std::max(Alignment, FieldAlign);
565}
566
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000567static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
568 std::vector<FieldDecl*> &Fields) {
569 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
570 if (SuperClass)
571 CollectObjCIvars(SuperClass, Fields);
572 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
573 E = OI->ivar_end(); I != E; ++I) {
574 ObjCIvarDecl *IVDecl = (*I);
575 if (!IVDecl->isInvalidDecl())
576 Fields.push_back(cast<FieldDecl>(IVDecl));
577 }
578}
579
580/// addRecordToClass - produces record info. for the class for its
581/// ivars and all those inherited.
582///
583const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
584{
585 const RecordDecl *&RD = ASTRecordForInterface[D];
586 if (RD)
587 return RD;
588 std::vector<FieldDecl*> RecFields;
589 CollectObjCIvars(D, RecFields);
590 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
591 D->getLocation(),
592 D->getIdentifier());
593 /// FIXME! Can do collection of ivars and adding to the record while
594 /// doing it.
595 for (unsigned int i = 0; i != RecFields.size(); i++) {
596 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
597 RecFields[i]->getLocation(),
598 RecFields[i]->getIdentifier(),
599 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000600 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000601 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000602 }
603 NewRD->completeDefinition(*this);
604 RD = NewRD;
605 return RD;
606}
Devang Patel4b6bf702008-06-04 21:54:36 +0000607
Fariborz Jahanianea944842008-12-18 17:29:46 +0000608/// setFieldDecl - maps a field for the given Ivar reference node.
609//
610void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
611 const ObjCIvarDecl *Ivar,
612 const ObjCIvarRefExpr *MRef) {
613 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
614 lookupFieldDeclForIvar(*this, Ivar);
615 ASTFieldForIvarRef[MRef] = FD;
616}
617
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000618/// getASTObjcInterfaceLayout - Get or compute information about the layout of
619/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000620/// position information.
621const ASTRecordLayout &
622ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
623 // Look up this layout, if already laid out, return what we have.
624 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
625 if (Entry) return *Entry;
626
627 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
628 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000629 ASTRecordLayout *NewEntry = NULL;
630 unsigned FieldCount = D->ivar_size();
631 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
632 FieldCount++;
633 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
634 unsigned Alignment = SL.getAlignment();
635 uint64_t Size = SL.getSize();
636 NewEntry = new ASTRecordLayout(Size, Alignment);
637 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000638 // Super class is at the beginning of the layout.
639 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000640 } else {
641 NewEntry = new ASTRecordLayout();
642 NewEntry->InitializeLayout(FieldCount);
643 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000644 Entry = NewEntry;
645
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000646 unsigned StructPacking = 0;
647 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
648 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000649
650 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
651 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
652 AA->getAlignment()));
653
654 // Layout each ivar sequentially.
655 unsigned i = 0;
656 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
657 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
658 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000659 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000660 }
661
662 // Finally, round the size of the total struct up to the alignment of the
663 // struct itself.
664 NewEntry->FinalizeLayout();
665 return *NewEntry;
666}
667
Devang Patel7a78e432007-11-01 19:11:01 +0000668/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000669/// specified record (struct/union/class), which indicates its size and field
670/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000671const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000672 D = D->getDefinition(*this);
673 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000674
Chris Lattner4b009652007-07-25 00:24:17 +0000675 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000676 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000677 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000678
Devang Patel7a78e432007-11-01 19:11:01 +0000679 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
680 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
681 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000682 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000683
Douglas Gregor39677622008-12-11 20:41:00 +0000684 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000685 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000686 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000687
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000688 unsigned StructPacking = 0;
689 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
690 StructPacking = PA->getAlignment();
691
Eli Friedman5949a022008-05-30 09:31:38 +0000692 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000693 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
694 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000695
Eli Friedman5949a022008-05-30 09:31:38 +0000696 // Layout each field, for now, just sequentially, respecting alignment. In
697 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000698 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000699 for (RecordDecl::field_iterator Field = D->field_begin(),
700 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000701 Field != FieldEnd; (void)++Field, ++FieldIdx)
702 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000703
704 // Finally, round the size of the total struct up to the alignment of the
705 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000706 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000707 return *NewEntry;
708}
709
Chris Lattner4b009652007-07-25 00:24:17 +0000710//===----------------------------------------------------------------------===//
711// Type creation/memoization methods
712//===----------------------------------------------------------------------===//
713
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000714QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000715 QualType CanT = getCanonicalType(T);
716 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000717 return T;
718
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000719 // Type's cannot have multiple ExtQuals, therefore we know we only have to deal
Chris Lattner35fef522008-02-20 20:55:12 +0000720 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000721 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000722 "Type is already address space qualified");
723
724 // Check if we've already instantiated an address space qual'd type of this
725 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000726 llvm::FoldingSetNodeID ID;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000727 ExtQualType::Profile(ID, T.getTypePtr(), AddressSpace, T.getObjCGCAttr());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000728 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000729 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
730 return QualType(EXTQy, 0);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000731
732 // If the base type isn't canonical, this won't be a canonical type either,
733 // so fill in the canonical type field.
734 QualType Canonical;
735 if (!T->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000736 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000737
738 // Get the new insert position for the node we care about.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000739 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000740 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000741 }
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000742 ExtQualType *New = new (*this, 8) ExtQualType(T.getTypePtr(), Canonical,
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000743 AddressSpace,
744 QualType::GCNone);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000745 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000746 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000747 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000748}
749
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000750QualType ASTContext::getObjCGCQualType(QualType T,
751 QualType::GCAttrTypes attr) {
752 QualType CanT = getCanonicalType(T);
753 if (CanT.getObjCGCAttr() == attr)
754 return T;
755
756 // Type's cannot have multiple ExtQuals, therefore we know we only have to deal
757 // with CVR qualifiers from here on out.
758 assert(CanT.getObjCGCAttr() == QualType::GCNone &&
759 "Type is already gc qualified");
760
761 // Check if we've already instantiated an gc qual'd type of this type.
762 llvm::FoldingSetNodeID ID;
763 ExtQualType::Profile(ID, T.getTypePtr(), T.getAddressSpace(), attr);
764 void *InsertPos = 0;
765 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
766 return QualType(EXTQy, 0);
767
768 // If the base type isn't canonical, this won't be a canonical type either,
769 // so fill in the canonical type field.
770 QualType Canonical;
771 if (!T->isCanonical()) {
772 Canonical = getObjCGCQualType(CanT, attr);
773
774 // Get the new insert position for the node we care about.
775 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
776 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
777 }
778 ExtQualType *New = new (*this, 8) ExtQualType(T.getTypePtr(), Canonical,
779 0, attr);
780 ExtQualTypes.InsertNode(New, InsertPos);
781 Types.push_back(New);
782 return QualType(New, T.getCVRQualifiers());
783}
Chris Lattner4b009652007-07-25 00:24:17 +0000784
785/// getComplexType - Return the uniqued reference to the type for a complex
786/// number with the specified element type.
787QualType ASTContext::getComplexType(QualType T) {
788 // Unique pointers, to guarantee there is only one pointer of a particular
789 // structure.
790 llvm::FoldingSetNodeID ID;
791 ComplexType::Profile(ID, T);
792
793 void *InsertPos = 0;
794 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
795 return QualType(CT, 0);
796
797 // If the pointee type isn't canonical, this won't be a canonical type either,
798 // so fill in the canonical type field.
799 QualType Canonical;
800 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000801 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000802
803 // Get the new insert position for the node we care about.
804 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000805 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000806 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000807 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000808 Types.push_back(New);
809 ComplexTypes.InsertNode(New, InsertPos);
810 return QualType(New, 0);
811}
812
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000813QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
814 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
815 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
816 FixedWidthIntType *&Entry = Map[Width];
817 if (!Entry)
818 Entry = new FixedWidthIntType(Width, Signed);
819 return QualType(Entry, 0);
820}
Chris Lattner4b009652007-07-25 00:24:17 +0000821
822/// getPointerType - Return the uniqued reference to the type for a pointer to
823/// the specified type.
824QualType ASTContext::getPointerType(QualType T) {
825 // Unique pointers, to guarantee there is only one pointer of a particular
826 // structure.
827 llvm::FoldingSetNodeID ID;
828 PointerType::Profile(ID, T);
829
830 void *InsertPos = 0;
831 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
832 return QualType(PT, 0);
833
834 // If the pointee type isn't canonical, this won't be a canonical type either,
835 // so fill in the canonical type field.
836 QualType Canonical;
837 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000838 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000839
840 // Get the new insert position for the node we care about.
841 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000842 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000843 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000844 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000845 Types.push_back(New);
846 PointerTypes.InsertNode(New, InsertPos);
847 return QualType(New, 0);
848}
849
Steve Naroff7aa54752008-08-27 16:04:49 +0000850/// getBlockPointerType - Return the uniqued reference to the type for
851/// a pointer to the specified block.
852QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000853 assert(T->isFunctionType() && "block of function types only");
854 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000855 // structure.
856 llvm::FoldingSetNodeID ID;
857 BlockPointerType::Profile(ID, T);
858
859 void *InsertPos = 0;
860 if (BlockPointerType *PT =
861 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
862 return QualType(PT, 0);
863
Steve Narofffd5b19d2008-08-28 19:20:44 +0000864 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000865 // type either so fill in the canonical type field.
866 QualType Canonical;
867 if (!T->isCanonical()) {
868 Canonical = getBlockPointerType(getCanonicalType(T));
869
870 // Get the new insert position for the node we care about.
871 BlockPointerType *NewIP =
872 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000873 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000874 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000875 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000876 Types.push_back(New);
877 BlockPointerTypes.InsertNode(New, InsertPos);
878 return QualType(New, 0);
879}
880
Chris Lattner4b009652007-07-25 00:24:17 +0000881/// getReferenceType - Return the uniqued reference to the type for a reference
882/// to the specified type.
883QualType ASTContext::getReferenceType(QualType T) {
884 // Unique pointers, to guarantee there is only one pointer of a particular
885 // structure.
886 llvm::FoldingSetNodeID ID;
887 ReferenceType::Profile(ID, T);
888
889 void *InsertPos = 0;
890 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
891 return QualType(RT, 0);
892
893 // If the referencee type isn't canonical, this won't be a canonical type
894 // either, so fill in the canonical type field.
895 QualType Canonical;
896 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000897 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000898
899 // Get the new insert position for the node we care about.
900 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000901 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000902 }
903
Steve Naroff93fd2112009-01-27 22:08:43 +0000904 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000905 Types.push_back(New);
906 ReferenceTypes.InsertNode(New, InsertPos);
907 return QualType(New, 0);
908}
909
Sebastian Redl75555032009-01-24 21:16:55 +0000910/// getMemberPointerType - Return the uniqued reference to the type for a
911/// member pointer to the specified type, in the specified class.
912QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
913{
914 // Unique pointers, to guarantee there is only one pointer of a particular
915 // structure.
916 llvm::FoldingSetNodeID ID;
917 MemberPointerType::Profile(ID, T, Cls);
918
919 void *InsertPos = 0;
920 if (MemberPointerType *PT =
921 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
922 return QualType(PT, 0);
923
924 // If the pointee or class type isn't canonical, this won't be a canonical
925 // type either, so fill in the canonical type field.
926 QualType Canonical;
927 if (!T->isCanonical()) {
928 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
929
930 // Get the new insert position for the node we care about.
931 MemberPointerType *NewIP =
932 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
933 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
934 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000935 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +0000936 Types.push_back(New);
937 MemberPointerTypes.InsertNode(New, InsertPos);
938 return QualType(New, 0);
939}
940
Steve Naroff83c13012007-08-30 01:06:46 +0000941/// getConstantArrayType - Return the unique reference to the type for an
942/// array of the specified element type.
943QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000944 const llvm::APInt &ArySize,
945 ArrayType::ArraySizeModifier ASM,
946 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000947 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000948 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000949
950 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000951 if (ConstantArrayType *ATP =
952 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000953 return QualType(ATP, 0);
954
955 // If the element type isn't canonical, this won't be a canonical type either,
956 // so fill in the canonical type field.
957 QualType Canonical;
958 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000959 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000960 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000961 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000962 ConstantArrayType *NewIP =
963 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000964 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000965 }
966
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000967 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000968 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000969 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000970 Types.push_back(New);
971 return QualType(New, 0);
972}
973
Steve Naroffe2579e32007-08-30 18:14:25 +0000974/// getVariableArrayType - Returns a non-unique reference to the type for a
975/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000976QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
977 ArrayType::ArraySizeModifier ASM,
978 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000979 // Since we don't unique expressions, it isn't possible to unique VLA's
980 // that have an expression provided for their size.
981
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000982 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +0000983 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000984
985 VariableArrayTypes.push_back(New);
986 Types.push_back(New);
987 return QualType(New, 0);
988}
989
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000990/// getDependentSizedArrayType - Returns a non-unique reference to
991/// the type for a dependently-sized array of the specified element
992/// type. FIXME: We will need these to be uniqued, or at least
993/// comparable, at some point.
994QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
995 ArrayType::ArraySizeModifier ASM,
996 unsigned EltTypeQuals) {
997 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
998 "Size must be type- or value-dependent!");
999
1000 // Since we don't unique expressions, it isn't possible to unique
1001 // dependently-sized array types.
1002
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001003 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001004 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1005 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001006
1007 DependentSizedArrayTypes.push_back(New);
1008 Types.push_back(New);
1009 return QualType(New, 0);
1010}
1011
Eli Friedman8ff07782008-02-15 18:16:39 +00001012QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1013 ArrayType::ArraySizeModifier ASM,
1014 unsigned EltTypeQuals) {
1015 llvm::FoldingSetNodeID ID;
1016 IncompleteArrayType::Profile(ID, EltTy);
1017
1018 void *InsertPos = 0;
1019 if (IncompleteArrayType *ATP =
1020 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1021 return QualType(ATP, 0);
1022
1023 // If the element type isn't canonical, this won't be a canonical type
1024 // either, so fill in the canonical type field.
1025 QualType Canonical;
1026
1027 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001028 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001029 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001030
1031 // Get the new insert position for the node we care about.
1032 IncompleteArrayType *NewIP =
1033 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001034 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001035 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001036
Steve Naroff93fd2112009-01-27 22:08:43 +00001037 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001038 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001039
1040 IncompleteArrayTypes.InsertNode(New, InsertPos);
1041 Types.push_back(New);
1042 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001043}
1044
Chris Lattner4b009652007-07-25 00:24:17 +00001045/// getVectorType - Return the unique reference to a vector type of
1046/// the specified element type and size. VectorType must be a built-in type.
1047QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1048 BuiltinType *baseType;
1049
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001050 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001051 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1052
1053 // Check if we've already instantiated a vector of this type.
1054 llvm::FoldingSetNodeID ID;
1055 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1056 void *InsertPos = 0;
1057 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1058 return QualType(VTP, 0);
1059
1060 // If the element type isn't canonical, this won't be a canonical type either,
1061 // so fill in the canonical type field.
1062 QualType Canonical;
1063 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001064 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001065
1066 // Get the new insert position for the node we care about.
1067 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001068 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001069 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001070 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001071 VectorTypes.InsertNode(New, InsertPos);
1072 Types.push_back(New);
1073 return QualType(New, 0);
1074}
1075
Nate Begemanaf6ed502008-04-18 23:10:10 +00001076/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001077/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001078QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001079 BuiltinType *baseType;
1080
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001081 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001082 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001083
1084 // Check if we've already instantiated a vector of this type.
1085 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001086 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001087 void *InsertPos = 0;
1088 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1089 return QualType(VTP, 0);
1090
1091 // If the element type isn't canonical, this won't be a canonical type either,
1092 // so fill in the canonical type field.
1093 QualType Canonical;
1094 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001095 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001096
1097 // Get the new insert position for the node we care about.
1098 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001099 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001100 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001101 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001102 VectorTypes.InsertNode(New, InsertPos);
1103 Types.push_back(New);
1104 return QualType(New, 0);
1105}
1106
1107/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1108///
1109QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1110 // Unique functions, to guarantee there is only one function of a particular
1111 // structure.
1112 llvm::FoldingSetNodeID ID;
1113 FunctionTypeNoProto::Profile(ID, ResultTy);
1114
1115 void *InsertPos = 0;
1116 if (FunctionTypeNoProto *FT =
1117 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1118 return QualType(FT, 0);
1119
1120 QualType Canonical;
1121 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001122 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001123
1124 // Get the new insert position for the node we care about.
1125 FunctionTypeNoProto *NewIP =
1126 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001127 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001128 }
1129
Steve Naroff93fd2112009-01-27 22:08:43 +00001130 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001131 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001132 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001133 return QualType(New, 0);
1134}
1135
1136/// getFunctionType - Return a normal function type with a typed argument
1137/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001138QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001139 unsigned NumArgs, bool isVariadic,
1140 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001141 // Unique functions, to guarantee there is only one function of a particular
1142 // structure.
1143 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001144 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1145 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001146
1147 void *InsertPos = 0;
1148 if (FunctionTypeProto *FTP =
1149 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1150 return QualType(FTP, 0);
1151
1152 // Determine whether the type being created is already canonical or not.
1153 bool isCanonical = ResultTy->isCanonical();
1154 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1155 if (!ArgArray[i]->isCanonical())
1156 isCanonical = false;
1157
1158 // If this type isn't canonical, get the canonical version of it.
1159 QualType Canonical;
1160 if (!isCanonical) {
1161 llvm::SmallVector<QualType, 16> CanonicalArgs;
1162 CanonicalArgs.reserve(NumArgs);
1163 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001164 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001165
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001166 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001167 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001168 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001169
1170 // Get the new insert position for the node we care about.
1171 FunctionTypeProto *NewIP =
1172 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001173 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001174 }
1175
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001176 // FunctionTypeProto objects are allocated with extra bytes after them
1177 // for a variable size array (for parameter types) at the end of them.
Chris Lattner4b009652007-07-25 00:24:17 +00001178 FunctionTypeProto *FTP =
Steve Naroff207b9ec2009-01-27 23:20:32 +00001179 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1180 NumArgs*sizeof(QualType), 8);
Chris Lattner4b009652007-07-25 00:24:17 +00001181 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001182 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001183 Types.push_back(FTP);
1184 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1185 return QualType(FTP, 0);
1186}
1187
Douglas Gregor1d661552008-04-13 21:07:44 +00001188/// getTypeDeclType - Return the unique reference to the type for the
1189/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001190QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001191 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001192 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1193
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001194 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001195 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001196 else if (isa<TemplateTypeParmDecl>(Decl)) {
1197 assert(false && "Template type parameter types are always available.");
1198 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001199 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001200
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001201 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001202 if (PrevDecl)
1203 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001204 else
1205 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001206 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001207 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001208 if (PrevDecl)
1209 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001210 else
1211 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001212 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001213 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1214 if (PrevDecl)
1215 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001216 else
1217 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001218 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001219 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001220 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001221
Ted Kremenek46a837c2008-09-05 17:16:31 +00001222 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001223 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001224}
1225
Chris Lattner4b009652007-07-25 00:24:17 +00001226/// getTypedefType - Return the unique reference to the type for the
1227/// specified typename decl.
1228QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1229 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1230
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001231 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001232 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001233 Types.push_back(Decl->TypeForDecl);
1234 return QualType(Decl->TypeForDecl, 0);
1235}
1236
Ted Kremenek42730c52008-01-07 19:49:32 +00001237/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001238/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001239QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001240 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1241
Steve Naroff93fd2112009-01-27 22:08:43 +00001242 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001243 Types.push_back(Decl->TypeForDecl);
1244 return QualType(Decl->TypeForDecl, 0);
1245}
1246
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001247/// buildObjCInterfaceType - Returns a new type for the interface
1248/// declaration, regardless. It also removes any previously built
1249/// record declaration so caller can rebuild it.
1250QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1251 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1252 if (RD)
1253 RD = 0;
1254 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1255 Types.push_back(Decl->TypeForDecl);
1256 return QualType(Decl->TypeForDecl, 0);
1257}
1258
Douglas Gregora4918772009-02-05 23:33:38 +00001259/// \brief Retrieve the template type parameter type for a template
1260/// parameter with the given depth, index, and (optionally) name.
1261QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1262 IdentifierInfo *Name) {
1263 llvm::FoldingSetNodeID ID;
1264 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1265 void *InsertPos = 0;
1266 TemplateTypeParmType *TypeParm
1267 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1268
1269 if (TypeParm)
1270 return QualType(TypeParm, 0);
1271
1272 if (Name)
1273 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1274 getTemplateTypeParmType(Depth, Index));
1275 else
1276 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1277
1278 Types.push_back(TypeParm);
1279 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1280
1281 return QualType(TypeParm, 0);
1282}
1283
Douglas Gregor8e458f42009-02-09 18:46:07 +00001284QualType
1285ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1286 unsigned NumArgs,
1287 uintptr_t *Args, bool *ArgIsType,
1288 QualType Canon) {
1289 llvm::FoldingSetNodeID ID;
1290 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1291 ArgIsType);
1292 void *InsertPos = 0;
1293 ClassTemplateSpecializationType *Spec
1294 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1295
1296 if (Spec)
1297 return QualType(Spec, 0);
1298
1299 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1300 (sizeof(uintptr_t) *
1301 (ClassTemplateSpecializationType::
1302 getNumPackedWords(NumArgs) +
1303 NumArgs)), 8);
1304 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1305 ArgIsType, Canon);
1306 Types.push_back(Spec);
1307 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1308
1309 return QualType(Spec, 0);
1310}
1311
Chris Lattnere1352302008-04-07 04:56:42 +00001312/// CmpProtocolNames - Comparison predicate for sorting protocols
1313/// alphabetically.
1314static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1315 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001316 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001317}
1318
1319static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1320 unsigned &NumProtocols) {
1321 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1322
1323 // Sort protocols, keyed by name.
1324 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1325
1326 // Remove duplicates.
1327 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1328 NumProtocols = ProtocolsEnd-Protocols;
1329}
1330
1331
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001332/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1333/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001334QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1335 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001336 // Sort the protocol list alphabetically to canonicalize it.
1337 SortAndUniqueProtocols(Protocols, NumProtocols);
1338
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001339 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001340 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001341
1342 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001343 if (ObjCQualifiedInterfaceType *QT =
1344 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001345 return QualType(QT, 0);
1346
1347 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001348 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001349 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001350
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001351 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001352 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001353 return QualType(QType, 0);
1354}
1355
Chris Lattnere1352302008-04-07 04:56:42 +00001356/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1357/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001358QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001359 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001360 // Sort the protocol list alphabetically to canonicalize it.
1361 SortAndUniqueProtocols(Protocols, NumProtocols);
1362
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001363 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001364 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001365
1366 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001367 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001368 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001369 return QualType(QT, 0);
1370
1371 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001372 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001373 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001374 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001375 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001376 return QualType(QType, 0);
1377}
1378
Steve Naroff0604dd92007-08-01 18:02:17 +00001379/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1380/// TypeOfExpr AST's (since expression's are never shared). For example,
1381/// multiple declarations that refer to "typeof(x)" all contain different
1382/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1383/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001384QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001385 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff93fd2112009-01-27 22:08:43 +00001386 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001387 Types.push_back(toe);
1388 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001389}
1390
Steve Naroff0604dd92007-08-01 18:02:17 +00001391/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1392/// TypeOfType AST's. The only motivation to unique these nodes would be
1393/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1394/// an issue. This doesn't effect the type checker, since it operates
1395/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001396QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001397 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001398 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001399 Types.push_back(tot);
1400 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001401}
1402
Chris Lattner4b009652007-07-25 00:24:17 +00001403/// getTagDeclType - Return the unique reference to the type for the
1404/// specified TagDecl (struct/union/class/enum) decl.
1405QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001406 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001407 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001408}
1409
1410/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1411/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1412/// needs to agree with the definition in <stddef.h>.
1413QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001414 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001415}
1416
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001417/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001418/// width of characters in wide strings, The value is target dependent and
1419/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001420QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001421 if (LangOpts.CPlusPlus)
1422 return WCharTy;
1423
Douglas Gregorc6507e42008-11-03 14:12:49 +00001424 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1425 // wide-character type?
1426 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001427}
1428
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001429/// getSignedWCharType - Return the type of "signed wchar_t".
1430/// Used when in C++, as a GCC extension.
1431QualType ASTContext::getSignedWCharType() const {
1432 // FIXME: derive from "Target" ?
1433 return WCharTy;
1434}
1435
1436/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1437/// Used when in C++, as a GCC extension.
1438QualType ASTContext::getUnsignedWCharType() const {
1439 // FIXME: derive from "Target" ?
1440 return UnsignedIntTy;
1441}
1442
Chris Lattner4b009652007-07-25 00:24:17 +00001443/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1444/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1445QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001446 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001447}
1448
Chris Lattner19eb97e2008-04-02 05:18:44 +00001449//===----------------------------------------------------------------------===//
1450// Type Operators
1451//===----------------------------------------------------------------------===//
1452
Chris Lattner3dae6f42008-04-06 22:41:35 +00001453/// getCanonicalType - Return the canonical (structural) type corresponding to
1454/// the specified potentially non-canonical type. The non-canonical version
1455/// of a type may have many "decorated" versions of types. Decorators can
1456/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1457/// to be free of any of these, allowing two canonical types to be compared
1458/// for exact equality with a simple pointer comparison.
1459QualType ASTContext::getCanonicalType(QualType T) {
1460 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001461
1462 // If the result has type qualifiers, make sure to canonicalize them as well.
1463 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1464 if (TypeQuals == 0) return CanType;
1465
1466 // If the type qualifiers are on an array type, get the canonical type of the
1467 // array with the qualifiers applied to the element type.
1468 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1469 if (!AT)
1470 return CanType.getQualifiedType(TypeQuals);
1471
1472 // Get the canonical version of the element with the extra qualifiers on it.
1473 // This can recursively sink qualifiers through multiple levels of arrays.
1474 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1475 NewEltTy = getCanonicalType(NewEltTy);
1476
1477 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1478 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1479 CAT->getIndexTypeQualifier());
1480 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1481 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1482 IAT->getIndexTypeQualifier());
1483
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001484 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1485 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1486 DSAT->getSizeModifier(),
1487 DSAT->getIndexTypeQualifier());
1488
Chris Lattnera1923f62008-08-04 07:31:14 +00001489 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1490 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1491 VAT->getSizeModifier(),
1492 VAT->getIndexTypeQualifier());
1493}
1494
1495
1496const ArrayType *ASTContext::getAsArrayType(QualType T) {
1497 // Handle the non-qualified case efficiently.
1498 if (T.getCVRQualifiers() == 0) {
1499 // Handle the common positive case fast.
1500 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1501 return AT;
1502 }
1503
1504 // Handle the common negative case fast, ignoring CVR qualifiers.
1505 QualType CType = T->getCanonicalTypeInternal();
1506
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001507 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001508 // test.
1509 if (!isa<ArrayType>(CType) &&
1510 !isa<ArrayType>(CType.getUnqualifiedType()))
1511 return 0;
1512
1513 // Apply any CVR qualifiers from the array type to the element type. This
1514 // implements C99 6.7.3p8: "If the specification of an array type includes
1515 // any type qualifiers, the element type is so qualified, not the array type."
1516
1517 // If we get here, we either have type qualifiers on the type, or we have
1518 // sugar such as a typedef in the way. If we have type qualifiers on the type
1519 // we must propagate them down into the elemeng type.
1520 unsigned CVRQuals = T.getCVRQualifiers();
1521 unsigned AddrSpace = 0;
1522 Type *Ty = T.getTypePtr();
1523
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001524 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001525 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001526 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1527 AddrSpace = EXTQT->getAddressSpace();
1528 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001529 } else {
1530 T = Ty->getDesugaredType();
1531 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1532 break;
1533 CVRQuals |= T.getCVRQualifiers();
1534 Ty = T.getTypePtr();
1535 }
1536 }
1537
1538 // If we have a simple case, just return now.
1539 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1540 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1541 return ATy;
1542
1543 // Otherwise, we have an array and we have qualifiers on it. Push the
1544 // qualifiers into the array element type and return a new array type.
1545 // Get the canonical version of the element with the extra qualifiers on it.
1546 // This can recursively sink qualifiers through multiple levels of arrays.
1547 QualType NewEltTy = ATy->getElementType();
1548 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001549 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001550 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1551
1552 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1553 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1554 CAT->getSizeModifier(),
1555 CAT->getIndexTypeQualifier()));
1556 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1557 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1558 IAT->getSizeModifier(),
1559 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001560
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001561 if (const DependentSizedArrayType *DSAT
1562 = dyn_cast<DependentSizedArrayType>(ATy))
1563 return cast<ArrayType>(
1564 getDependentSizedArrayType(NewEltTy,
1565 DSAT->getSizeExpr(),
1566 DSAT->getSizeModifier(),
1567 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001568
Chris Lattnera1923f62008-08-04 07:31:14 +00001569 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1570 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1571 VAT->getSizeModifier(),
1572 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001573}
1574
1575
Chris Lattner19eb97e2008-04-02 05:18:44 +00001576/// getArrayDecayedType - Return the properly qualified result of decaying the
1577/// specified array type to a pointer. This operation is non-trivial when
1578/// handling typedefs etc. The canonical type of "T" must be an array type,
1579/// this returns a pointer to a properly qualified element of the array.
1580///
1581/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1582QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001583 // Get the element type with 'getAsArrayType' so that we don't lose any
1584 // typedefs in the element type of the array. This also handles propagation
1585 // of type qualifiers from the array type into the element type if present
1586 // (C99 6.7.3p8).
1587 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1588 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001589
Chris Lattnera1923f62008-08-04 07:31:14 +00001590 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001591
1592 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001593 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001594}
1595
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001596QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001597 QualType ElemTy = VAT->getElementType();
1598
1599 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1600 return getBaseElementType(VAT);
1601
1602 return ElemTy;
1603}
1604
Chris Lattner4b009652007-07-25 00:24:17 +00001605/// getFloatingRank - Return a relative rank for floating point types.
1606/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001607static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001608 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001609 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001610
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001611 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001612 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001613 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001614 case BuiltinType::Float: return FloatRank;
1615 case BuiltinType::Double: return DoubleRank;
1616 case BuiltinType::LongDouble: return LongDoubleRank;
1617 }
1618}
1619
Steve Narofffa0c4532007-08-27 01:41:48 +00001620/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1621/// point or a complex type (based on typeDomain/typeSize).
1622/// 'typeDomain' is a real floating point or complex type.
1623/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001624QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1625 QualType Domain) const {
1626 FloatingRank EltRank = getFloatingRank(Size);
1627 if (Domain->isComplexType()) {
1628 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001629 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001630 case FloatRank: return FloatComplexTy;
1631 case DoubleRank: return DoubleComplexTy;
1632 case LongDoubleRank: return LongDoubleComplexTy;
1633 }
Chris Lattner4b009652007-07-25 00:24:17 +00001634 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001635
1636 assert(Domain->isRealFloatingType() && "Unknown domain!");
1637 switch (EltRank) {
1638 default: assert(0 && "getFloatingRank(): illegal value for rank");
1639 case FloatRank: return FloatTy;
1640 case DoubleRank: return DoubleTy;
1641 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001642 }
Chris Lattner4b009652007-07-25 00:24:17 +00001643}
1644
Chris Lattner51285d82008-04-06 23:55:33 +00001645/// getFloatingTypeOrder - Compare the rank of the two specified floating
1646/// point types, ignoring the domain of the type (i.e. 'double' ==
1647/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1648/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001649int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1650 FloatingRank LHSR = getFloatingRank(LHS);
1651 FloatingRank RHSR = getFloatingRank(RHS);
1652
1653 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001654 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001655 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001656 return 1;
1657 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001658}
1659
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001660/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1661/// routine will assert if passed a built-in type that isn't an integer or enum,
1662/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001663unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001664 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001665 if (EnumType* ET = dyn_cast<EnumType>(T))
1666 T = ET->getDecl()->getIntegerType().getTypePtr();
1667
1668 // There are two things which impact the integer rank: the width, and
1669 // the ordering of builtins. The builtin ordering is encoded in the
1670 // bottom three bits; the width is encoded in the bits above that.
1671 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1672 return FWIT->getWidth() << 3;
1673 }
1674
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001675 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001676 default: assert(0 && "getIntegerRank(): not a built-in integer");
1677 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001678 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001679 case BuiltinType::Char_S:
1680 case BuiltinType::Char_U:
1681 case BuiltinType::SChar:
1682 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001683 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001684 case BuiltinType::Short:
1685 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001686 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001687 case BuiltinType::Int:
1688 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001689 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001690 case BuiltinType::Long:
1691 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001692 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001693 case BuiltinType::LongLong:
1694 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001695 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001696 }
1697}
1698
Chris Lattner51285d82008-04-06 23:55:33 +00001699/// getIntegerTypeOrder - Returns the highest ranked integer type:
1700/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1701/// LHS < RHS, return -1.
1702int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001703 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1704 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001705 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001706
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001707 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1708 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001709
Chris Lattner51285d82008-04-06 23:55:33 +00001710 unsigned LHSRank = getIntegerRank(LHSC);
1711 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001712
Chris Lattner51285d82008-04-06 23:55:33 +00001713 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1714 if (LHSRank == RHSRank) return 0;
1715 return LHSRank > RHSRank ? 1 : -1;
1716 }
Chris Lattner4b009652007-07-25 00:24:17 +00001717
Chris Lattner51285d82008-04-06 23:55:33 +00001718 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1719 if (LHSUnsigned) {
1720 // If the unsigned [LHS] type is larger, return it.
1721 if (LHSRank >= RHSRank)
1722 return 1;
1723
1724 // If the signed type can represent all values of the unsigned type, it
1725 // wins. Because we are dealing with 2's complement and types that are
1726 // powers of two larger than each other, this is always safe.
1727 return -1;
1728 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001729
Chris Lattner51285d82008-04-06 23:55:33 +00001730 // If the unsigned [RHS] type is larger, return it.
1731 if (RHSRank >= LHSRank)
1732 return -1;
1733
1734 // If the signed type can represent all values of the unsigned type, it
1735 // wins. Because we are dealing with 2's complement and types that are
1736 // powers of two larger than each other, this is always safe.
1737 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001738}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001739
1740// getCFConstantStringType - Return the type used for constant CFStrings.
1741QualType ASTContext::getCFConstantStringType() {
1742 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001743 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001744 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001745 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001746 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001747
1748 // const int *isa;
1749 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001750 // int flags;
1751 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001752 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001753 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001754 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001755 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001756
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001757 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001758 for (unsigned i = 0; i < 4; ++i) {
1759 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1760 SourceLocation(), 0,
1761 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001762 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001763 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001764 }
1765
1766 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001767 }
1768
1769 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001770}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001771
Anders Carlssonf58cac72008-08-30 19:34:46 +00001772QualType ASTContext::getObjCFastEnumerationStateType()
1773{
1774 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001775 ObjCFastEnumerationStateTypeDecl =
1776 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1777 &Idents.get("__objcFastEnumerationState"));
1778
Anders Carlssonf58cac72008-08-30 19:34:46 +00001779 QualType FieldTypes[] = {
1780 UnsignedLongTy,
1781 getPointerType(ObjCIdType),
1782 getPointerType(UnsignedLongTy),
1783 getConstantArrayType(UnsignedLongTy,
1784 llvm::APInt(32, 5), ArrayType::Normal, 0)
1785 };
1786
Douglas Gregor8acb7272008-12-11 16:49:14 +00001787 for (size_t i = 0; i < 4; ++i) {
1788 FieldDecl *Field = FieldDecl::Create(*this,
1789 ObjCFastEnumerationStateTypeDecl,
1790 SourceLocation(), 0,
1791 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001792 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001793 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001794 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001795
Douglas Gregor8acb7272008-12-11 16:49:14 +00001796 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001797 }
1798
1799 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1800}
1801
Anders Carlssone3f02572007-10-29 06:33:42 +00001802// This returns true if a type has been typedefed to BOOL:
1803// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001804static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001805 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001806 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1807 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001808
1809 return false;
1810}
1811
Ted Kremenek42730c52008-01-07 19:49:32 +00001812/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001813/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001814int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001815 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001816
1817 // Make all integer and enum types at least as large as an int
1818 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001819 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001820 // Treat arrays as pointers, since that's how they're passed in.
1821 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001822 sz = getTypeSize(VoidPtrTy);
1823 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001824}
1825
Ted Kremenek42730c52008-01-07 19:49:32 +00001826/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001827/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001828void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001829 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001830 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001831 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001832 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001833 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001834 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001835 // Compute size of all parameters.
1836 // Start with computing size of a pointer in number of bytes.
1837 // FIXME: There might(should) be a better way of doing this computation!
1838 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001839 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001840 // The first two arguments (self and _cmd) are pointers; account for
1841 // their size.
1842 int ParmOffset = 2 * PtrSize;
1843 int NumOfParams = Decl->getNumParams();
1844 for (int i = 0; i < NumOfParams; i++) {
1845 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001846 int sz = getObjCEncodingTypeSize (PType);
1847 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001848 ParmOffset += sz;
1849 }
1850 S += llvm::utostr(ParmOffset);
1851 S += "@0:";
1852 S += llvm::utostr(PtrSize);
1853
1854 // Argument types.
1855 ParmOffset = 2 * PtrSize;
1856 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001857 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1858 QualType PType = PVDecl->getOriginalType();
1859 if (const ArrayType *AT =
1860 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1861 // Use array's original type only if it has known number of
1862 // elements.
1863 if (!dyn_cast<ConstantArrayType>(AT))
1864 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001865 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001866 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001867 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001868 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001869 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001870 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001871 }
1872}
1873
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001874/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001875/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001876/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1877/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001878/// Property attributes are stored as a comma-delimited C string. The simple
1879/// attributes readonly and bycopy are encoded as single characters. The
1880/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1881/// encoded as single characters, followed by an identifier. Property types
1882/// are also encoded as a parametrized attribute. The characters used to encode
1883/// these attributes are defined by the following enumeration:
1884/// @code
1885/// enum PropertyAttributes {
1886/// kPropertyReadOnly = 'R', // property is read-only.
1887/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1888/// kPropertyByref = '&', // property is a reference to the value last assigned
1889/// kPropertyDynamic = 'D', // property is dynamic
1890/// kPropertyGetter = 'G', // followed by getter selector name
1891/// kPropertySetter = 'S', // followed by setter selector name
1892/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1893/// kPropertyType = 't' // followed by old-style type encoding.
1894/// kPropertyWeak = 'W' // 'weak' property
1895/// kPropertyStrong = 'P' // property GC'able
1896/// kPropertyNonAtomic = 'N' // property non-atomic
1897/// };
1898/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001899void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1900 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001901 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001902 // Collect information from the property implementation decl(s).
1903 bool Dynamic = false;
1904 ObjCPropertyImplDecl *SynthesizePID = 0;
1905
1906 // FIXME: Duplicated code due to poor abstraction.
1907 if (Container) {
1908 if (const ObjCCategoryImplDecl *CID =
1909 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1910 for (ObjCCategoryImplDecl::propimpl_iterator
1911 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1912 ObjCPropertyImplDecl *PID = *i;
1913 if (PID->getPropertyDecl() == PD) {
1914 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1915 Dynamic = true;
1916 } else {
1917 SynthesizePID = PID;
1918 }
1919 }
1920 }
1921 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001922 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001923 for (ObjCCategoryImplDecl::propimpl_iterator
1924 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1925 ObjCPropertyImplDecl *PID = *i;
1926 if (PID->getPropertyDecl() == PD) {
1927 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1928 Dynamic = true;
1929 } else {
1930 SynthesizePID = PID;
1931 }
1932 }
1933 }
1934 }
1935 }
1936
1937 // FIXME: This is not very efficient.
1938 S = "T";
1939
1940 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001941 // GCC has some special rules regarding encoding of properties which
1942 // closely resembles encoding of ivars.
1943 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1944 true /* outermost type */,
1945 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001946
1947 if (PD->isReadOnly()) {
1948 S += ",R";
1949 } else {
1950 switch (PD->getSetterKind()) {
1951 case ObjCPropertyDecl::Assign: break;
1952 case ObjCPropertyDecl::Copy: S += ",C"; break;
1953 case ObjCPropertyDecl::Retain: S += ",&"; break;
1954 }
1955 }
1956
1957 // It really isn't clear at all what this means, since properties
1958 // are "dynamic by default".
1959 if (Dynamic)
1960 S += ",D";
1961
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001962 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1963 S += ",N";
1964
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001965 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1966 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001967 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001968 }
1969
1970 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1971 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001972 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001973 }
1974
1975 if (SynthesizePID) {
1976 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1977 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001978 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001979 }
1980
1981 // FIXME: OBJCGC: weak & strong
1982}
1983
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001984/// getLegacyIntegralTypeEncoding -
1985/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00001986/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001987/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1988///
1989void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1990 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1991 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00001992 if (BT->getKind() == BuiltinType::ULong &&
1993 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001994 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00001995 else
1996 if (BT->getKind() == BuiltinType::Long &&
1997 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001998 PointeeTy = IntTy;
1999 }
2000 }
2001}
2002
Fariborz Jahanian248db262008-01-22 22:44:46 +00002003void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002004 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002005 // We follow the behavior of gcc, expanding structures which are
2006 // directly pointed to, and expanding embedded structures. Note that
2007 // these rules are sufficient to prevent recursive encoding of the
2008 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002009 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2010 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002011}
2012
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002013static void EncodeBitField(const ASTContext *Context, std::string& S,
2014 FieldDecl *FD) {
2015 const Expr *E = FD->getBitWidth();
2016 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2017 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2018 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2019 S += 'b';
2020 S += llvm::utostr(N);
2021}
2022
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002023void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2024 bool ExpandPointedToStructures,
2025 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002026 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002027 bool OutermostType,
2028 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002029 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002030 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002031 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002032 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002033 else {
2034 char encoding;
2035 switch (BT->getKind()) {
2036 default: assert(0 && "Unhandled builtin type kind");
2037 case BuiltinType::Void: encoding = 'v'; break;
2038 case BuiltinType::Bool: encoding = 'B'; break;
2039 case BuiltinType::Char_U:
2040 case BuiltinType::UChar: encoding = 'C'; break;
2041 case BuiltinType::UShort: encoding = 'S'; break;
2042 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002043 case BuiltinType::ULong:
2044 encoding =
2045 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2046 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002047 case BuiltinType::ULongLong: encoding = 'Q'; break;
2048 case BuiltinType::Char_S:
2049 case BuiltinType::SChar: encoding = 'c'; break;
2050 case BuiltinType::Short: encoding = 's'; break;
2051 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002052 case BuiltinType::Long:
2053 encoding =
2054 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2055 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002056 case BuiltinType::LongLong: encoding = 'q'; break;
2057 case BuiltinType::Float: encoding = 'f'; break;
2058 case BuiltinType::Double: encoding = 'd'; break;
2059 case BuiltinType::LongDouble: encoding = 'd'; break;
2060 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002061
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002062 S += encoding;
2063 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002064 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002065 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002066 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2067 ExpandPointedToStructures,
2068 ExpandStructures, FD);
2069 if (FD || EncodingProperty) {
2070 // Note that we do extended encoding of protocol qualifer list
2071 // Only when doing ivar or property encoding.
2072 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2073 S += '"';
2074 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2075 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2076 S += '<';
2077 S += Proto->getNameAsString();
2078 S += '>';
2079 }
2080 S += '"';
2081 }
2082 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002083 }
2084 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002085 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002086 bool isReadOnly = false;
2087 // For historical/compatibility reasons, the read-only qualifier of the
2088 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2089 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2090 // Also, do not emit the 'r' for anything but the outermost type!
2091 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2092 if (OutermostType && T.isConstQualified()) {
2093 isReadOnly = true;
2094 S += 'r';
2095 }
2096 }
2097 else if (OutermostType) {
2098 QualType P = PointeeTy;
2099 while (P->getAsPointerType())
2100 P = P->getAsPointerType()->getPointeeType();
2101 if (P.isConstQualified()) {
2102 isReadOnly = true;
2103 S += 'r';
2104 }
2105 }
2106 if (isReadOnly) {
2107 // Another legacy compatibility encoding. Some ObjC qualifier and type
2108 // combinations need to be rearranged.
2109 // Rewrite "in const" from "nr" to "rn"
2110 const char * s = S.c_str();
2111 int len = S.length();
2112 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2113 std::string replace = "rn";
2114 S.replace(S.end()-2, S.end(), replace);
2115 }
2116 }
Steve Naroff17c03822009-02-12 17:52:19 +00002117 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002118 S += '@';
2119 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002120 }
2121 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002122 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002123 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002124 // Another historical/compatibility reason.
2125 // We encode the underlying type which comes out as
2126 // {...};
2127 S += '^';
2128 getObjCEncodingForTypeImpl(PointeeTy, S,
2129 false, ExpandPointedToStructures,
2130 NULL);
2131 return;
2132 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002133 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002134 if (FD || EncodingProperty) {
2135 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2136 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002137 S += '"';
2138 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002139 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2140 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2141 S += '<';
2142 S += Proto->getNameAsString();
2143 S += '>';
2144 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002145 S += '"';
2146 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002147 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002148 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002149 S += '#';
2150 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002151 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002152 S += ':';
2153 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002154 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002155
2156 if (PointeeTy->isCharType()) {
2157 // char pointer types should be encoded as '*' unless it is a
2158 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002159 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002160 S += '*';
2161 return;
2162 }
2163 }
2164
2165 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002166 getLegacyIntegralTypeEncoding(PointeeTy);
2167
2168 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002169 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002170 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002171 } else if (const ArrayType *AT =
2172 // Ignore type qualifiers etc.
2173 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002174 S += '[';
2175
2176 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2177 S += llvm::utostr(CAT->getSize().getZExtValue());
2178 else
2179 assert(0 && "Unhandled array type!");
2180
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002181 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002182 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002183 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002184 } else if (T->getAsFunctionType()) {
2185 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002186 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002187 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002188 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002189 // Anonymous structures print as '?'
2190 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2191 S += II->getName();
2192 } else {
2193 S += '?';
2194 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002195 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002196 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002197 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2198 FieldEnd = RDecl->field_end();
2199 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002200 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002201 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002202 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002203 S += '"';
2204 }
2205
2206 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002207 if (Field->isBitField()) {
2208 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2209 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002210 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002211 QualType qt = Field->getType();
2212 getLegacyIntegralTypeEncoding(qt);
2213 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002214 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002215 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002216 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002217 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002218 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002219 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002220 if (FD && FD->isBitField())
2221 EncodeBitField(this, S, FD);
2222 else
2223 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002224 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002225 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002226 } else if (T->isObjCInterfaceType()) {
2227 // @encode(class_name)
2228 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2229 S += '{';
2230 const IdentifierInfo *II = OI->getIdentifier();
2231 S += II->getName();
2232 S += '=';
2233 std::vector<FieldDecl*> RecFields;
2234 CollectObjCIvars(OI, RecFields);
2235 for (unsigned int i = 0; i != RecFields.size(); i++) {
2236 if (RecFields[i]->isBitField())
2237 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2238 RecFields[i]);
2239 else
2240 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2241 FD);
2242 }
2243 S += '}';
2244 }
2245 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002246 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002247}
2248
Ted Kremenek42730c52008-01-07 19:49:32 +00002249void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002250 std::string& S) const {
2251 if (QT & Decl::OBJC_TQ_In)
2252 S += 'n';
2253 if (QT & Decl::OBJC_TQ_Inout)
2254 S += 'N';
2255 if (QT & Decl::OBJC_TQ_Out)
2256 S += 'o';
2257 if (QT & Decl::OBJC_TQ_Bycopy)
2258 S += 'O';
2259 if (QT & Decl::OBJC_TQ_Byref)
2260 S += 'R';
2261 if (QT & Decl::OBJC_TQ_Oneway)
2262 S += 'V';
2263}
2264
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002265void ASTContext::setBuiltinVaListType(QualType T)
2266{
2267 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2268
2269 BuiltinVaListType = T;
2270}
2271
Ted Kremenek42730c52008-01-07 19:49:32 +00002272void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002273{
Ted Kremenek42730c52008-01-07 19:49:32 +00002274 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002275
2276 // typedef struct objc_object *id;
2277 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002278 // User error - caller will issue diagnostics.
2279 if (!ptr)
2280 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002281 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002282 // User error - caller will issue diagnostics.
2283 if (!rec)
2284 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002285 IdStructType = rec;
2286}
2287
Ted Kremenek42730c52008-01-07 19:49:32 +00002288void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002289{
Ted Kremenek42730c52008-01-07 19:49:32 +00002290 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002291
2292 // typedef struct objc_selector *SEL;
2293 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002294 if (!ptr)
2295 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002296 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002297 if (!rec)
2298 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002299 SelStructType = rec;
2300}
2301
Ted Kremenek42730c52008-01-07 19:49:32 +00002302void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002303{
Ted Kremenek42730c52008-01-07 19:49:32 +00002304 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002305}
2306
Ted Kremenek42730c52008-01-07 19:49:32 +00002307void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002308{
Ted Kremenek42730c52008-01-07 19:49:32 +00002309 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002310
2311 // typedef struct objc_class *Class;
2312 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2313 assert(ptr && "'Class' incorrectly typed");
2314 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2315 assert(rec && "'Class' incorrectly typed");
2316 ClassStructType = rec;
2317}
2318
Ted Kremenek42730c52008-01-07 19:49:32 +00002319void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2320 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002321 "'NSConstantString' type already set!");
2322
Ted Kremenek42730c52008-01-07 19:49:32 +00002323 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002324}
2325
Douglas Gregorc6507e42008-11-03 14:12:49 +00002326/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002327/// TargetInfo, produce the corresponding type. The unsigned @p Type
2328/// is actually a value of type @c TargetInfo::IntType.
2329QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002330 switch (Type) {
2331 case TargetInfo::NoInt: return QualType();
2332 case TargetInfo::SignedShort: return ShortTy;
2333 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2334 case TargetInfo::SignedInt: return IntTy;
2335 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2336 case TargetInfo::SignedLong: return LongTy;
2337 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2338 case TargetInfo::SignedLongLong: return LongLongTy;
2339 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2340 }
2341
2342 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002343 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002344}
Ted Kremenek118930e2008-07-24 23:58:27 +00002345
2346//===----------------------------------------------------------------------===//
2347// Type Predicates.
2348//===----------------------------------------------------------------------===//
2349
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002350/// isObjCNSObjectType - Return true if this is an NSObject object using
2351/// NSObject attribute on a c-style pointer type.
2352/// FIXME - Make it work directly on types.
2353///
2354bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2355 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2356 if (TypedefDecl *TD = TDT->getDecl())
2357 if (TD->getAttr<ObjCNSObjectAttr>())
2358 return true;
2359 }
2360 return false;
2361}
2362
Ted Kremenek118930e2008-07-24 23:58:27 +00002363/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2364/// to an object type. This includes "id" and "Class" (two 'special' pointers
2365/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2366/// ID type).
2367bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2368 if (Ty->isObjCQualifiedIdType())
2369 return true;
2370
Steve Naroffd9e00802008-10-21 18:24:04 +00002371 // Blocks are objects.
2372 if (Ty->isBlockPointerType())
2373 return true;
2374
2375 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002376 if (!Ty->isPointerType())
2377 return false;
2378
2379 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2380 // pointer types. This looks for the typedef specifically, not for the
2381 // underlying type.
2382 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2383 return true;
2384
2385 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002386 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2387 return true;
2388
2389 // If is has NSObject attribute, OK as well.
2390 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002391}
2392
Chris Lattner6ff358b2008-04-07 06:51:04 +00002393//===----------------------------------------------------------------------===//
2394// Type Compatibility Testing
2395//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002396
Steve Naroff3454b6c2008-09-04 15:10:53 +00002397/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002398/// block types. Types must be strictly compatible here. For example,
2399/// C unfortunately doesn't produce an error for the following:
2400///
2401/// int (*emptyArgFunc)();
2402/// int (*intArgList)(int) = emptyArgFunc;
2403///
2404/// For blocks, we will produce an error for the following (similar to C++):
2405///
2406/// int (^emptyArgBlock)();
2407/// int (^intArgBlock)(int) = emptyArgBlock;
2408///
2409/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2410///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002411bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002412 const FunctionType *lbase = lhs->getAsFunctionType();
2413 const FunctionType *rbase = rhs->getAsFunctionType();
2414 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2415 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2416 if (lproto && rproto)
2417 return !mergeTypes(lhs, rhs).isNull();
2418 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002419}
2420
Chris Lattner6ff358b2008-04-07 06:51:04 +00002421/// areCompatVectorTypes - Return true if the two specified vector types are
2422/// compatible.
2423static bool areCompatVectorTypes(const VectorType *LHS,
2424 const VectorType *RHS) {
2425 assert(LHS->isCanonical() && RHS->isCanonical());
2426 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002427 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002428}
2429
Eli Friedman0d9549b2008-08-22 00:56:42 +00002430/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002431/// compatible for assignment from RHS to LHS. This handles validation of any
2432/// protocol qualifiers on the LHS or RHS.
2433///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002434bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2435 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002436 // Verify that the base decls are compatible: the RHS must be a subclass of
2437 // the LHS.
2438 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2439 return false;
2440
2441 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2442 // protocol qualified at all, then we are good.
2443 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2444 return true;
2445
2446 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2447 // isn't a superset.
2448 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2449 return true; // FIXME: should return false!
2450
2451 // Finally, we must have two protocol-qualified interfaces.
2452 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2453 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2454 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2455 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2456 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2457 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2458
2459 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2460 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2461 // LHS in a single parallel scan until we run out of elements in LHS.
2462 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2463 ObjCProtocolDecl *LHSProto = *LHSPI;
2464
2465 while (RHSPI != RHSPE) {
2466 ObjCProtocolDecl *RHSProto = *RHSPI++;
2467 // If the RHS has a protocol that the LHS doesn't, ignore it.
2468 if (RHSProto != LHSProto)
2469 continue;
2470
2471 // Otherwise, the RHS does have this element.
2472 ++LHSPI;
2473 if (LHSPI == LHSPE)
2474 return true; // All protocols in LHS exist in RHS.
2475
2476 LHSProto = *LHSPI;
2477 }
2478
2479 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2480 return false;
2481}
2482
Steve Naroff17c03822009-02-12 17:52:19 +00002483bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2484 // get the "pointed to" types
2485 const PointerType *LHSPT = LHS->getAsPointerType();
2486 const PointerType *RHSPT = RHS->getAsPointerType();
2487
2488 if (!LHSPT || !RHSPT)
2489 return false;
2490
2491 QualType lhptee = LHSPT->getPointeeType();
2492 QualType rhptee = RHSPT->getPointeeType();
2493 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2494 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2495 // ID acts sort of like void* for ObjC interfaces
2496 if (LHSIface && isObjCIdStructType(rhptee))
2497 return true;
2498 if (RHSIface && isObjCIdStructType(lhptee))
2499 return true;
2500 if (!LHSIface || !RHSIface)
2501 return false;
2502 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2503 canAssignObjCInterfaces(RHSIface, LHSIface);
2504}
2505
Steve Naroff85f0dc52007-10-15 20:41:53 +00002506/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2507/// both shall have the identically qualified version of a compatible type.
2508/// C99 6.2.7p1: Two types have compatible types if their types are the
2509/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002510bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2511 return !mergeTypes(LHS, RHS).isNull();
2512}
2513
2514QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2515 const FunctionType *lbase = lhs->getAsFunctionType();
2516 const FunctionType *rbase = rhs->getAsFunctionType();
2517 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2518 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2519 bool allLTypes = true;
2520 bool allRTypes = true;
2521
2522 // Check return type
2523 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2524 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002525 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2526 allLTypes = false;
2527 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2528 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002529
2530 if (lproto && rproto) { // two C99 style function prototypes
2531 unsigned lproto_nargs = lproto->getNumArgs();
2532 unsigned rproto_nargs = rproto->getNumArgs();
2533
2534 // Compatible functions must have the same number of arguments
2535 if (lproto_nargs != rproto_nargs)
2536 return QualType();
2537
2538 // Variadic and non-variadic functions aren't compatible
2539 if (lproto->isVariadic() != rproto->isVariadic())
2540 return QualType();
2541
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002542 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2543 return QualType();
2544
Eli Friedman0d9549b2008-08-22 00:56:42 +00002545 // Check argument compatibility
2546 llvm::SmallVector<QualType, 10> types;
2547 for (unsigned i = 0; i < lproto_nargs; i++) {
2548 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2549 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2550 QualType argtype = mergeTypes(largtype, rargtype);
2551 if (argtype.isNull()) return QualType();
2552 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002553 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2554 allLTypes = false;
2555 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2556 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002557 }
2558 if (allLTypes) return lhs;
2559 if (allRTypes) return rhs;
2560 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002561 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002562 }
2563
2564 if (lproto) allRTypes = false;
2565 if (rproto) allLTypes = false;
2566
2567 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2568 if (proto) {
2569 if (proto->isVariadic()) return QualType();
2570 // Check that the types are compatible with the types that
2571 // would result from default argument promotions (C99 6.7.5.3p15).
2572 // The only types actually affected are promotable integer
2573 // types and floats, which would be passed as a different
2574 // type depending on whether the prototype is visible.
2575 unsigned proto_nargs = proto->getNumArgs();
2576 for (unsigned i = 0; i < proto_nargs; ++i) {
2577 QualType argTy = proto->getArgType(i);
2578 if (argTy->isPromotableIntegerType() ||
2579 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2580 return QualType();
2581 }
2582
2583 if (allLTypes) return lhs;
2584 if (allRTypes) return rhs;
2585 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002586 proto->getNumArgs(), lproto->isVariadic(),
2587 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002588 }
2589
2590 if (allLTypes) return lhs;
2591 if (allRTypes) return rhs;
2592 return getFunctionTypeNoProto(retType);
2593}
2594
2595QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002596 // C++ [expr]: If an expression initially has the type "reference to T", the
2597 // type is adjusted to "T" prior to any further analysis, the expression
2598 // designates the object or function denoted by the reference, and the
2599 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002600 // FIXME: C++ shouldn't be going through here! The rules are different
2601 // enough that they should be handled separately.
2602 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002603 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002604 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002605 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002606
Eli Friedman0d9549b2008-08-22 00:56:42 +00002607 QualType LHSCan = getCanonicalType(LHS),
2608 RHSCan = getCanonicalType(RHS);
2609
2610 // If two types are identical, they are compatible.
2611 if (LHSCan == RHSCan)
2612 return LHS;
2613
2614 // If the qualifiers are different, the types aren't compatible
2615 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2616 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2617 return QualType();
2618
2619 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2620 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2621
Chris Lattnerc38d4522008-01-14 05:45:46 +00002622 // We want to consider the two function types to be the same for these
2623 // comparisons, just force one to the other.
2624 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2625 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002626
2627 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002628 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2629 LHSClass = Type::ConstantArray;
2630 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2631 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002632
Nate Begemanaf6ed502008-04-18 23:10:10 +00002633 // Canonicalize ExtVector -> Vector.
2634 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2635 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002636
Chris Lattner7cdcb252008-04-07 06:38:24 +00002637 // Consider qualified interfaces and interfaces the same.
2638 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2639 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002640
Chris Lattnerb5709e22008-04-07 05:43:21 +00002641 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002642 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002643 // ID is compatible with all qualified id types.
2644 if (LHS->isObjCQualifiedIdType()) {
2645 if (const PointerType *PT = RHS->getAsPointerType()) {
2646 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002647 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002648 return LHS;
2649 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2650 // Unfortunately, this API is part of Sema (which we don't have access
2651 // to. Need to refactor. The following check is insufficient, since we
2652 // need to make sure the class implements the protocol.
2653 if (pType->isObjCInterfaceType())
2654 return LHS;
2655 }
2656 }
2657 if (RHS->isObjCQualifiedIdType()) {
2658 if (const PointerType *PT = LHS->getAsPointerType()) {
2659 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002660 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002661 return RHS;
2662 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2663 // Unfortunately, this API is part of Sema (which we don't have access
2664 // to. Need to refactor. The following check is insufficient, since we
2665 // need to make sure the class implements the protocol.
2666 if (pType->isObjCInterfaceType())
2667 return RHS;
2668 }
2669 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002670 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2671 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002672 if (const EnumType* ETy = LHS->getAsEnumType()) {
2673 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2674 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002675 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002676 if (const EnumType* ETy = RHS->getAsEnumType()) {
2677 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2678 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002679 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002680
Eli Friedman0d9549b2008-08-22 00:56:42 +00002681 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002682 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002683
Steve Naroffc88babe2008-01-09 22:43:08 +00002684 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002685 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002686 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002687 {
2688 // Merge two pointer types, while trying to preserve typedef info
2689 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2690 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2691 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2692 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002693 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2694 return LHS;
2695 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2696 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002697 return getPointerType(ResultType);
2698 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002699 case Type::BlockPointer:
2700 {
2701 // Merge two block pointer types, while trying to preserve typedef info
2702 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2703 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2704 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2705 if (ResultType.isNull()) return QualType();
2706 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2707 return LHS;
2708 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2709 return RHS;
2710 return getBlockPointerType(ResultType);
2711 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002712 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002713 {
2714 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2715 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2716 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2717 return QualType();
2718
2719 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2720 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2721 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2722 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002723 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2724 return LHS;
2725 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2726 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002727 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2728 ArrayType::ArraySizeModifier(), 0);
2729 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2730 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002731 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2732 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002733 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2734 return LHS;
2735 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2736 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002737 if (LVAT) {
2738 // FIXME: This isn't correct! But tricky to implement because
2739 // the array's size has to be the size of LHS, but the type
2740 // has to be different.
2741 return LHS;
2742 }
2743 if (RVAT) {
2744 // FIXME: This isn't correct! But tricky to implement because
2745 // the array's size has to be the size of RHS, but the type
2746 // has to be different.
2747 return RHS;
2748 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002749 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2750 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002751 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002752 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002753 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002754 return mergeFunctionTypes(LHS, RHS);
2755 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002756 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002757 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2758 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002759 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002760 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002761 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002762 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002763 case Type::Complex:
2764 // Distinct complex types are incompatible.
2765 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002766 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002767 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2768 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002769 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002770 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002771 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2772 // for checking assignment/comparison safety
2773 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002774 case Type::ObjCQualifiedId:
2775 // Distinct qualified id's are not compatible.
2776 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002777 default:
2778 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002779 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002780 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002781}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002782
Chris Lattner1d78a862008-04-07 07:01:58 +00002783//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002784// Integer Predicates
2785//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002786
Eli Friedman0832dbc2008-06-28 06:23:08 +00002787unsigned ASTContext::getIntWidth(QualType T) {
2788 if (T == BoolTy)
2789 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002790 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2791 return FWIT->getWidth();
2792 }
2793 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002794 return (unsigned)getTypeSize(T);
2795}
2796
2797QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2798 assert(T->isSignedIntegerType() && "Unexpected type");
2799 if (const EnumType* ETy = T->getAsEnumType())
2800 T = ETy->getDecl()->getIntegerType();
2801 const BuiltinType* BTy = T->getAsBuiltinType();
2802 assert (BTy && "Unexpected signed integer type");
2803 switch (BTy->getKind()) {
2804 case BuiltinType::Char_S:
2805 case BuiltinType::SChar:
2806 return UnsignedCharTy;
2807 case BuiltinType::Short:
2808 return UnsignedShortTy;
2809 case BuiltinType::Int:
2810 return UnsignedIntTy;
2811 case BuiltinType::Long:
2812 return UnsignedLongTy;
2813 case BuiltinType::LongLong:
2814 return UnsignedLongLongTy;
2815 default:
2816 assert(0 && "Unexpected signed integer type");
2817 return QualType();
2818 }
2819}
2820
2821
2822//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002823// Serialization Support
2824//===----------------------------------------------------------------------===//
2825
Ted Kremenek738e6c02007-10-31 17:10:13 +00002826/// Emit - Serialize an ASTContext object to Bitcode.
2827void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002828 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002829 S.EmitRef(SourceMgr);
2830 S.EmitRef(Target);
2831 S.EmitRef(Idents);
2832 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002833
Ted Kremenek68228a92007-10-31 22:44:07 +00002834 // Emit the size of the type vector so that we can reserve that size
2835 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002836 S.EmitInt(Types.size());
2837
Ted Kremenek034a78c2007-11-13 22:02:55 +00002838 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2839 I!=E;++I)
2840 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002841
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002842 S.EmitOwnedPtr(TUDecl);
2843
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002844 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002845}
2846
Ted Kremenekacba3612007-11-13 00:25:37 +00002847ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002848
2849 // Read the language options.
2850 LangOptions LOpts;
2851 LOpts.Read(D);
2852
Ted Kremenek68228a92007-10-31 22:44:07 +00002853 SourceManager &SM = D.ReadRef<SourceManager>();
2854 TargetInfo &t = D.ReadRef<TargetInfo>();
2855 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2856 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002857
Ted Kremenek68228a92007-10-31 22:44:07 +00002858 unsigned size_reserve = D.ReadInt();
2859
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002860 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2861 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002862
Ted Kremenek034a78c2007-11-13 22:02:55 +00002863 for (unsigned i = 0; i < size_reserve; ++i)
2864 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002865
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002866 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2867
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002868 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002869
2870 return A;
2871}