blob: 122ba270c62c5d92d7368681f8137778b644c29a [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"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000023#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000024
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
27enum FloatingRank {
28 FloatRank, DoubleRank, LongDoubleRank
29};
30
Chris Lattner2fda0ed2008-10-05 17:34:18 +000031ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
32 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000033 IdentifierTable &idents, SelectorTable &sels,
34 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
36 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000037 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000038{
39 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
41 BuiltinInfo.InitializeBuiltins(idents, Target);
42 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Chris Lattner4b009652007-07-25 00:24:17 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000049 Types.pop_back();
50 }
Eli Friedman65489b72008-05-27 03:08:09 +000051
Nuno Lopes355a8682008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Eli Friedman65489b72008-05-27 03:08:09 +000079 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000080}
81
82void ASTContext::PrintStats() const {
83 fprintf(stderr, "*** AST Context Stats:\n");
84 fprintf(stderr, " %d types total.\n", (int)Types.size());
85 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000086 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000087 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
88
89 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000090 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
91 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000092 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000093
94 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
95 Type *T = Types[i];
96 if (isa<BuiltinType>(T))
97 ++NumBuiltin;
98 else if (isa<PointerType>(T))
99 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000100 else if (isa<BlockPointerType>(T))
101 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 else if (isa<ReferenceType>(T))
103 ++NumReference;
104 else if (isa<ComplexType>(T))
105 ++NumComplex;
106 else if (isa<ArrayType>(T))
107 ++NumArray;
108 else if (isa<VectorType>(T))
109 ++NumVector;
110 else if (isa<FunctionTypeNoProto>(T))
111 ++NumFunctionNP;
112 else if (isa<FunctionTypeProto>(T))
113 ++NumFunctionP;
114 else if (isa<TypedefType>(T))
115 ++NumTypeName;
116 else if (TagType *TT = dyn_cast<TagType>(T)) {
117 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000118 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000119 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000120 case TagDecl::TK_struct: ++NumTagStruct; break;
121 case TagDecl::TK_union: ++NumTagUnion; break;
122 case TagDecl::TK_class: ++NumTagClass; break;
123 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000124 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000125 } else if (isa<ObjCInterfaceType>(T))
126 ++NumObjCInterfaces;
127 else if (isa<ObjCQualifiedInterfaceType>(T))
128 ++NumObjCQualifiedInterfaces;
129 else if (isa<ObjCQualifiedIdType>(T))
130 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000131 else if (isa<TypeOfType>(T))
132 ++NumTypeOfTypes;
133 else if (isa<TypeOfExpr>(T))
134 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000135 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000136 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000137 assert(0 && "Unknown type!");
138 }
139 }
140
141 fprintf(stderr, " %d builtin types\n", NumBuiltin);
142 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000143 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000144 fprintf(stderr, " %d reference types\n", NumReference);
145 fprintf(stderr, " %d complex types\n", NumComplex);
146 fprintf(stderr, " %d array types\n", NumArray);
147 fprintf(stderr, " %d vector types\n", NumVector);
148 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
149 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
150 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
151 fprintf(stderr, " %d tagged types\n", NumTagged);
152 fprintf(stderr, " %d struct types\n", NumTagStruct);
153 fprintf(stderr, " %d union types\n", NumTagUnion);
154 fprintf(stderr, " %d class types\n", NumTagClass);
155 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000156 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000157 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000158 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000159 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000161 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
162 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
163
Chris Lattner4b009652007-07-25 00:24:17 +0000164 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
165 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
166 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
167 NumFunctionP*sizeof(FunctionTypeProto)+
168 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000169 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
170 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000171}
172
173
174void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
175 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
176}
177
Chris Lattner4b009652007-07-25 00:24:17 +0000178void ASTContext::InitBuiltinTypes() {
179 assert(VoidTy.isNull() && "Context reinitialized?");
180
181 // C99 6.2.5p19.
182 InitBuiltinType(VoidTy, BuiltinType::Void);
183
184 // C99 6.2.5p2.
185 InitBuiltinType(BoolTy, BuiltinType::Bool);
186 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000187 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000188 InitBuiltinType(CharTy, BuiltinType::Char_S);
189 else
190 InitBuiltinType(CharTy, BuiltinType::Char_U);
191 // C99 6.2.5p4.
192 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
193 InitBuiltinType(ShortTy, BuiltinType::Short);
194 InitBuiltinType(IntTy, BuiltinType::Int);
195 InitBuiltinType(LongTy, BuiltinType::Long);
196 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
197
198 // C99 6.2.5p6.
199 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
200 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
201 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
202 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
203 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
204
205 // C99 6.2.5p10.
206 InitBuiltinType(FloatTy, BuiltinType::Float);
207 InitBuiltinType(DoubleTy, BuiltinType::Double);
208 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000209
210 // C++ 3.9.1p5
211 InitBuiltinType(WCharTy, BuiltinType::WChar);
212
Douglas Gregord2baafd2008-10-21 16:13:35 +0000213 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000214 InitBuiltinType(OverloadTy, BuiltinType::Overload);
215
216 // Placeholder type for type-dependent expressions whose type is
217 // completely unknown. No code should ever check a type against
218 // DependentTy and users should never see it; however, it is here to
219 // help diagnose failures to properly check for type-dependent
220 // expressions.
221 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000222
Chris Lattner4b009652007-07-25 00:24:17 +0000223 // C99 6.2.5p11.
224 FloatComplexTy = getComplexType(FloatTy);
225 DoubleComplexTy = getComplexType(DoubleTy);
226 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000227
Steve Naroff9d12c902007-10-15 14:41:52 +0000228 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000229 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000230 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000231 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000232 ClassStructType = 0;
233
Ted Kremenek42730c52008-01-07 19:49:32 +0000234 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000235
236 // void * type
237 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000238}
239
240//===----------------------------------------------------------------------===//
241// Type Sizing and Analysis
242//===----------------------------------------------------------------------===//
243
Chris Lattner2a674dc2008-06-30 18:32:54 +0000244/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
245/// scalar floating point type.
246const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
247 const BuiltinType *BT = T->getAsBuiltinType();
248 assert(BT && "Not a floating point type!");
249 switch (BT->getKind()) {
250 default: assert(0 && "Not a floating point type!");
251 case BuiltinType::Float: return Target.getFloatFormat();
252 case BuiltinType::Double: return Target.getDoubleFormat();
253 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
254 }
255}
256
257
Chris Lattner4b009652007-07-25 00:24:17 +0000258/// getTypeSize - Return the size of the specified type, in bits. This method
259/// does not work on incomplete types.
260std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000261ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000262 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000264 unsigned Align;
265 switch (T->getTypeClass()) {
266 case Type::TypeName: assert(0 && "Not a canonical type!");
267 case Type::FunctionNoProto:
268 case Type::FunctionProto:
269 default:
270 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000271 case Type::VariableArray:
272 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000273 case Type::DependentSizedArray:
274 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000275 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000276 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000277
Chris Lattner8cd0e932008-03-05 18:54:05 +0000278 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000279 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000280 Align = EltInfo.second;
281 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000282 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000283 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000284 case Type::Vector: {
285 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000286 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000287 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000288 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000289 // If the alignment is not a power of 2, round up to the next power of 2.
290 // This happens for non-power-of-2 length vectors.
291 // FIXME: this should probably be a target property.
292 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000293 break;
294 }
295
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000296 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000297 switch (cast<BuiltinType>(T)->getKind()) {
298 default: assert(0 && "Unknown builtin type!");
299 case BuiltinType::Void:
300 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000301 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000302 Width = Target.getBoolWidth();
303 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000304 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000305 case BuiltinType::Char_S:
306 case BuiltinType::Char_U:
307 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000308 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000309 Width = Target.getCharWidth();
310 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000311 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000312 case BuiltinType::WChar:
313 Width = Target.getWCharWidth();
314 Align = Target.getWCharAlign();
315 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000316 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000317 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000318 Width = Target.getShortWidth();
319 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000320 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000321 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000322 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000323 Width = Target.getIntWidth();
324 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000325 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000326 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000327 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000328 Width = Target.getLongWidth();
329 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000330 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000332 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000333 Width = Target.getLongLongWidth();
334 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000335 break;
336 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000337 Width = Target.getFloatWidth();
338 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000339 break;
340 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000341 Width = Target.getDoubleWidth();
342 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 break;
344 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000345 Width = Target.getLongDoubleWidth();
346 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000347 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000348 }
349 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000350 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000351 // FIXME: Pointers into different addr spaces could have different sizes and
352 // alignment requirements: getPointerInfo should take an AddrSpace.
353 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000354 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000355 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000356 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000357 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000358 case Type::BlockPointer: {
359 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
360 Width = Target.getPointerWidth(AS);
361 Align = Target.getPointerAlign(AS);
362 break;
363 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000364 case Type::Pointer: {
365 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000366 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000367 Align = Target.getPointerAlign(AS);
368 break;
369 }
Chris Lattner4b009652007-07-25 00:24:17 +0000370 case Type::Reference:
371 // "When applied to a reference or a reference type, the result is the size
372 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 // FIXME: This is wrong for struct layout: a reference in a struct has
374 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000375 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000376
377 case Type::Complex: {
378 // Complex types have the same alignment as their elements, but twice the
379 // size.
380 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000381 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000382 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000383 Align = EltInfo.second;
384 break;
385 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000386 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000387 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000388 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
389 Width = Layout.getSize();
390 Align = Layout.getAlignment();
391 break;
392 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000393 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000394 const TagType *TT = cast<TagType>(T);
395
396 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000397 Width = 1;
398 Align = 1;
399 break;
400 }
401
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000402 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000403 return getTypeInfo(ET->getDecl()->getIntegerType());
404
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000405 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000406 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
407 Width = Layout.getSize();
408 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000409 break;
410 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000411 }
Chris Lattner4b009652007-07-25 00:24:17 +0000412
413 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000414 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000415}
416
Devang Patelbfe323c2008-06-04 21:22:16 +0000417/// LayoutField - Field layout.
418void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000419 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000420 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000421 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000422 uint64_t FieldOffset = IsUnion ? 0 : Size;
423 uint64_t FieldSize;
424 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000425
426 // FIXME: Should this override struct packing? Probably we want to
427 // take the minimum?
428 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
429 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000430
431 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
432 // TODO: Need to check this algorithm on other targets!
433 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000434 FieldSize =
435 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000436
437 std::pair<uint64_t, unsigned> FieldInfo =
438 Context.getTypeInfo(FD->getType());
439 uint64_t TypeSize = FieldInfo.first;
440
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000441 // Determine the alignment of this bitfield. The packing
442 // attributes define a maximum and the alignment attribute defines
443 // a minimum.
444 // FIXME: What is the right behavior when the specified alignment
445 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000446 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000447 if (FieldPacking)
448 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000449 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
450 FieldAlign = std::max(FieldAlign, AA->getAlignment());
451
452 // Check if we need to add padding to give the field the correct
453 // alignment.
454 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
455 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
456
457 // Padding members don't affect overall alignment
458 if (!FD->getIdentifier())
459 FieldAlign = 1;
460 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000461 if (FD->getType()->isIncompleteArrayType()) {
462 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000463 // query getTypeInfo about these, so we figure it out here.
464 // Flexible array members don't have any size, but they
465 // have to be aligned appropriately for their element type.
466 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000467 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000468 FieldAlign = Context.getTypeAlign(ATy->getElementType());
469 } else {
470 std::pair<uint64_t, unsigned> FieldInfo =
471 Context.getTypeInfo(FD->getType());
472 FieldSize = FieldInfo.first;
473 FieldAlign = FieldInfo.second;
474 }
475
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000476 // Determine the alignment of this bitfield. The packing
477 // attributes define a maximum and the alignment attribute defines
478 // a minimum. Additionally, the packing alignment must be at least
479 // a byte for non-bitfields.
480 //
481 // FIXME: What is the right behavior when the specified alignment
482 // is smaller than the specified packing?
483 if (FieldPacking)
484 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000485 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
486 FieldAlign = std::max(FieldAlign, AA->getAlignment());
487
488 // Round up the current record size to the field's alignment boundary.
489 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
490 }
491
492 // Place this field at the current location.
493 FieldOffsets[FieldNo] = FieldOffset;
494
495 // Reserve space for this field.
496 if (IsUnion) {
497 Size = std::max(Size, FieldSize);
498 } else {
499 Size = FieldOffset + FieldSize;
500 }
501
502 // Remember max struct/class alignment.
503 Alignment = std::max(Alignment, FieldAlign);
504}
505
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000506static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
507 std::vector<FieldDecl*> &Fields) {
508 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
509 if (SuperClass)
510 CollectObjCIvars(SuperClass, Fields);
511 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
512 E = OI->ivar_end(); I != E; ++I) {
513 ObjCIvarDecl *IVDecl = (*I);
514 if (!IVDecl->isInvalidDecl())
515 Fields.push_back(cast<FieldDecl>(IVDecl));
516 }
517}
518
519/// addRecordToClass - produces record info. for the class for its
520/// ivars and all those inherited.
521///
522const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
523{
524 const RecordDecl *&RD = ASTRecordForInterface[D];
525 if (RD)
526 return RD;
527 std::vector<FieldDecl*> RecFields;
528 CollectObjCIvars(D, RecFields);
529 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
530 D->getLocation(),
531 D->getIdentifier());
532 /// FIXME! Can do collection of ivars and adding to the record while
533 /// doing it.
534 for (unsigned int i = 0; i != RecFields.size(); i++) {
535 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
536 RecFields[i]->getLocation(),
537 RecFields[i]->getIdentifier(),
538 RecFields[i]->getType(),
539 RecFields[i]->getBitWidth(), false, 0);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000540 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000541 }
542 NewRD->completeDefinition(*this);
543 RD = NewRD;
544 return RD;
545}
Devang Patel4b6bf702008-06-04 21:54:36 +0000546
Fariborz Jahanianea944842008-12-18 17:29:46 +0000547/// setFieldDecl - maps a field for the given Ivar reference node.
548//
549void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
550 const ObjCIvarDecl *Ivar,
551 const ObjCIvarRefExpr *MRef) {
552 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
553 lookupFieldDeclForIvar(*this, Ivar);
554 ASTFieldForIvarRef[MRef] = FD;
555}
556
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000557/// getASTObjcInterfaceLayout - Get or compute information about the layout of
558/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000559/// position information.
560const ASTRecordLayout &
561ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
562 // Look up this layout, if already laid out, return what we have.
563 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
564 if (Entry) return *Entry;
565
566 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
567 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000568 ASTRecordLayout *NewEntry = NULL;
569 unsigned FieldCount = D->ivar_size();
570 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
571 FieldCount++;
572 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
573 unsigned Alignment = SL.getAlignment();
574 uint64_t Size = SL.getSize();
575 NewEntry = new ASTRecordLayout(Size, Alignment);
576 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000577 // Super class is at the beginning of the layout.
578 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000579 } else {
580 NewEntry = new ASTRecordLayout();
581 NewEntry->InitializeLayout(FieldCount);
582 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000583 Entry = NewEntry;
584
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000585 unsigned StructPacking = 0;
586 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
587 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000588
589 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
590 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
591 AA->getAlignment()));
592
593 // Layout each ivar sequentially.
594 unsigned i = 0;
595 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
596 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
597 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000598 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000599 }
600
601 // Finally, round the size of the total struct up to the alignment of the
602 // struct itself.
603 NewEntry->FinalizeLayout();
604 return *NewEntry;
605}
606
Devang Patel7a78e432007-11-01 19:11:01 +0000607/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000608/// specified record (struct/union/class), which indicates its size and field
609/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000610const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000611 D = D->getDefinition(*this);
612 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000613
Chris Lattner4b009652007-07-25 00:24:17 +0000614 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000615 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000616 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000617
Devang Patel7a78e432007-11-01 19:11:01 +0000618 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
619 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
620 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000621 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000622
Douglas Gregor39677622008-12-11 20:41:00 +0000623 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000624 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000625 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000626
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000627 unsigned StructPacking = 0;
628 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
629 StructPacking = PA->getAlignment();
630
Eli Friedman5949a022008-05-30 09:31:38 +0000631 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000632 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
633 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000634
Eli Friedman5949a022008-05-30 09:31:38 +0000635 // Layout each field, for now, just sequentially, respecting alignment. In
636 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000637 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000638 for (RecordDecl::field_iterator Field = D->field_begin(),
639 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000640 Field != FieldEnd; (void)++Field, ++FieldIdx)
641 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000642
643 // Finally, round the size of the total struct up to the alignment of the
644 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000645 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000646 return *NewEntry;
647}
648
Chris Lattner4b009652007-07-25 00:24:17 +0000649//===----------------------------------------------------------------------===//
650// Type creation/memoization methods
651//===----------------------------------------------------------------------===//
652
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000653QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000654 QualType CanT = getCanonicalType(T);
655 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000656 return T;
657
658 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
659 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000660 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000661 "Type is already address space qualified");
662
663 // Check if we've already instantiated an address space qual'd type of this
664 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000665 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000666 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000667 void *InsertPos = 0;
668 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
669 return QualType(ASQy, 0);
670
671 // If the base type isn't canonical, this won't be a canonical type either,
672 // so fill in the canonical type field.
673 QualType Canonical;
674 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000675 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000676
677 // Get the new insert position for the node we care about.
678 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000679 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000680 }
Chris Lattner35fef522008-02-20 20:55:12 +0000681 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000682 ASQualTypes.InsertNode(New, InsertPos);
683 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000684 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000685}
686
Chris Lattner4b009652007-07-25 00:24:17 +0000687
688/// getComplexType - Return the uniqued reference to the type for a complex
689/// number with the specified element type.
690QualType ASTContext::getComplexType(QualType T) {
691 // Unique pointers, to guarantee there is only one pointer of a particular
692 // structure.
693 llvm::FoldingSetNodeID ID;
694 ComplexType::Profile(ID, T);
695
696 void *InsertPos = 0;
697 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
698 return QualType(CT, 0);
699
700 // If the pointee type isn't canonical, this won't be a canonical type either,
701 // so fill in the canonical type field.
702 QualType Canonical;
703 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000704 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000705
706 // Get the new insert position for the node we care about.
707 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000708 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000709 }
710 ComplexType *New = new ComplexType(T, Canonical);
711 Types.push_back(New);
712 ComplexTypes.InsertNode(New, InsertPos);
713 return QualType(New, 0);
714}
715
716
717/// getPointerType - Return the uniqued reference to the type for a pointer to
718/// the specified type.
719QualType ASTContext::getPointerType(QualType T) {
720 // Unique pointers, to guarantee there is only one pointer of a particular
721 // structure.
722 llvm::FoldingSetNodeID ID;
723 PointerType::Profile(ID, T);
724
725 void *InsertPos = 0;
726 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
727 return QualType(PT, 0);
728
729 // If the pointee type isn't canonical, this won't be a canonical type either,
730 // so fill in the canonical type field.
731 QualType Canonical;
732 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000733 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000734
735 // Get the new insert position for the node we care about.
736 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000737 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000738 }
739 PointerType *New = new PointerType(T, Canonical);
740 Types.push_back(New);
741 PointerTypes.InsertNode(New, InsertPos);
742 return QualType(New, 0);
743}
744
Steve Naroff7aa54752008-08-27 16:04:49 +0000745/// getBlockPointerType - Return the uniqued reference to the type for
746/// a pointer to the specified block.
747QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000748 assert(T->isFunctionType() && "block of function types only");
749 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000750 // structure.
751 llvm::FoldingSetNodeID ID;
752 BlockPointerType::Profile(ID, T);
753
754 void *InsertPos = 0;
755 if (BlockPointerType *PT =
756 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
757 return QualType(PT, 0);
758
Steve Narofffd5b19d2008-08-28 19:20:44 +0000759 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000760 // type either so fill in the canonical type field.
761 QualType Canonical;
762 if (!T->isCanonical()) {
763 Canonical = getBlockPointerType(getCanonicalType(T));
764
765 // Get the new insert position for the node we care about.
766 BlockPointerType *NewIP =
767 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000768 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000769 }
770 BlockPointerType *New = new BlockPointerType(T, Canonical);
771 Types.push_back(New);
772 BlockPointerTypes.InsertNode(New, InsertPos);
773 return QualType(New, 0);
774}
775
Chris Lattner4b009652007-07-25 00:24:17 +0000776/// getReferenceType - Return the uniqued reference to the type for a reference
777/// to the specified type.
778QualType ASTContext::getReferenceType(QualType T) {
779 // Unique pointers, to guarantee there is only one pointer of a particular
780 // structure.
781 llvm::FoldingSetNodeID ID;
782 ReferenceType::Profile(ID, T);
783
784 void *InsertPos = 0;
785 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
786 return QualType(RT, 0);
787
788 // If the referencee type isn't canonical, this won't be a canonical type
789 // either, so fill in the canonical type field.
790 QualType Canonical;
791 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000792 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000793
794 // Get the new insert position for the node we care about.
795 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000796 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000797 }
798
799 ReferenceType *New = new ReferenceType(T, Canonical);
800 Types.push_back(New);
801 ReferenceTypes.InsertNode(New, InsertPos);
802 return QualType(New, 0);
803}
804
Steve Naroff83c13012007-08-30 01:06:46 +0000805/// getConstantArrayType - Return the unique reference to the type for an
806/// array of the specified element type.
807QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000808 const llvm::APInt &ArySize,
809 ArrayType::ArraySizeModifier ASM,
810 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000811 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000812 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000813
814 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000815 if (ConstantArrayType *ATP =
816 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000817 return QualType(ATP, 0);
818
819 // If the element type isn't canonical, this won't be a canonical type either,
820 // so fill in the canonical type field.
821 QualType Canonical;
822 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000823 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000824 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000825 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000826 ConstantArrayType *NewIP =
827 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000828 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000829 }
830
Steve Naroff24c9b982007-08-30 18:10:14 +0000831 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
832 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000833 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000834 Types.push_back(New);
835 return QualType(New, 0);
836}
837
Steve Naroffe2579e32007-08-30 18:14:25 +0000838/// getVariableArrayType - Returns a non-unique reference to the type for a
839/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000840QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
841 ArrayType::ArraySizeModifier ASM,
842 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000843 // Since we don't unique expressions, it isn't possible to unique VLA's
844 // that have an expression provided for their size.
845
846 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
847 ASM, EltTypeQuals);
848
849 VariableArrayTypes.push_back(New);
850 Types.push_back(New);
851 return QualType(New, 0);
852}
853
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000854/// getDependentSizedArrayType - Returns a non-unique reference to
855/// the type for a dependently-sized array of the specified element
856/// type. FIXME: We will need these to be uniqued, or at least
857/// comparable, at some point.
858QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
859 ArrayType::ArraySizeModifier ASM,
860 unsigned EltTypeQuals) {
861 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
862 "Size must be type- or value-dependent!");
863
864 // Since we don't unique expressions, it isn't possible to unique
865 // dependently-sized array types.
866
867 DependentSizedArrayType *New
868 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
869 ASM, EltTypeQuals);
870
871 DependentSizedArrayTypes.push_back(New);
872 Types.push_back(New);
873 return QualType(New, 0);
874}
875
Eli Friedman8ff07782008-02-15 18:16:39 +0000876QualType ASTContext::getIncompleteArrayType(QualType EltTy,
877 ArrayType::ArraySizeModifier ASM,
878 unsigned EltTypeQuals) {
879 llvm::FoldingSetNodeID ID;
880 IncompleteArrayType::Profile(ID, EltTy);
881
882 void *InsertPos = 0;
883 if (IncompleteArrayType *ATP =
884 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
885 return QualType(ATP, 0);
886
887 // If the element type isn't canonical, this won't be a canonical type
888 // either, so fill in the canonical type field.
889 QualType Canonical;
890
891 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000892 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000893 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000894
895 // Get the new insert position for the node we care about.
896 IncompleteArrayType *NewIP =
897 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000898 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000899 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000900
901 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
902 ASM, EltTypeQuals);
903
904 IncompleteArrayTypes.InsertNode(New, InsertPos);
905 Types.push_back(New);
906 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000907}
908
Chris Lattner4b009652007-07-25 00:24:17 +0000909/// getVectorType - Return the unique reference to a vector type of
910/// the specified element type and size. VectorType must be a built-in type.
911QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
912 BuiltinType *baseType;
913
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000914 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000915 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
916
917 // Check if we've already instantiated a vector of this type.
918 llvm::FoldingSetNodeID ID;
919 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
920 void *InsertPos = 0;
921 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
922 return QualType(VTP, 0);
923
924 // If the element type isn't canonical, this won't be a canonical type either,
925 // so fill in the canonical type field.
926 QualType Canonical;
927 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000928 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000929
930 // Get the new insert position for the node we care about.
931 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000932 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000933 }
934 VectorType *New = new VectorType(vecType, NumElts, Canonical);
935 VectorTypes.InsertNode(New, InsertPos);
936 Types.push_back(New);
937 return QualType(New, 0);
938}
939
Nate Begemanaf6ed502008-04-18 23:10:10 +0000940/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000941/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000942QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000943 BuiltinType *baseType;
944
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000945 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000946 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000947
948 // Check if we've already instantiated a vector of this type.
949 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000950 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000951 void *InsertPos = 0;
952 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
953 return QualType(VTP, 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 (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000959 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000960
961 // Get the new insert position for the node we care about.
962 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000963 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000964 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000965 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000966 VectorTypes.InsertNode(New, InsertPos);
967 Types.push_back(New);
968 return QualType(New, 0);
969}
970
971/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
972///
973QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
974 // Unique functions, to guarantee there is only one function of a particular
975 // structure.
976 llvm::FoldingSetNodeID ID;
977 FunctionTypeNoProto::Profile(ID, ResultTy);
978
979 void *InsertPos = 0;
980 if (FunctionTypeNoProto *FT =
981 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
982 return QualType(FT, 0);
983
984 QualType Canonical;
985 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000986 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000987
988 // Get the new insert position for the node we care about.
989 FunctionTypeNoProto *NewIP =
990 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000991 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000992 }
993
994 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
995 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000996 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000997 return QualType(New, 0);
998}
999
1000/// getFunctionType - Return a normal function type with a typed argument
1001/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001002QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001003 unsigned NumArgs, bool isVariadic,
1004 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001005 // Unique functions, to guarantee there is only one function of a particular
1006 // structure.
1007 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001008 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1009 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001010
1011 void *InsertPos = 0;
1012 if (FunctionTypeProto *FTP =
1013 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1014 return QualType(FTP, 0);
1015
1016 // Determine whether the type being created is already canonical or not.
1017 bool isCanonical = ResultTy->isCanonical();
1018 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1019 if (!ArgArray[i]->isCanonical())
1020 isCanonical = false;
1021
1022 // If this type isn't canonical, get the canonical version of it.
1023 QualType Canonical;
1024 if (!isCanonical) {
1025 llvm::SmallVector<QualType, 16> CanonicalArgs;
1026 CanonicalArgs.reserve(NumArgs);
1027 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001028 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001029
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001030 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001031 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001032 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001033
1034 // Get the new insert position for the node we care about.
1035 FunctionTypeProto *NewIP =
1036 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001037 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001038 }
1039
1040 // FunctionTypeProto objects are not allocated with new because they have a
1041 // variable size array (for parameter types) at the end of them.
1042 FunctionTypeProto *FTP =
1043 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
1044 NumArgs*sizeof(QualType));
1045 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001046 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001047 Types.push_back(FTP);
1048 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1049 return QualType(FTP, 0);
1050}
1051
Douglas Gregor1d661552008-04-13 21:07:44 +00001052/// getTypeDeclType - Return the unique reference to the type for the
1053/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001054QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001055 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001056 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1057
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001058 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001059 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +00001060 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1061 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001062 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001063 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001064
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001065 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +00001066 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1067 : new CXXRecordType(CXXRecord);
1068 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001069 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +00001070 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1071 : new RecordType(Record);
1072 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001073 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregorae644892008-12-15 16:32:14 +00001074 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1075 : new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001076 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001077 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001078
Ted Kremenek46a837c2008-09-05 17:16:31 +00001079 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001080 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001081}
1082
Chris Lattner4b009652007-07-25 00:24:17 +00001083/// getTypedefType - Return the unique reference to the type for the
1084/// specified typename decl.
1085QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1086 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1087
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001088 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001089 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001090 Types.push_back(Decl->TypeForDecl);
1091 return QualType(Decl->TypeForDecl, 0);
1092}
1093
Douglas Gregordd861062008-12-05 18:15:24 +00001094/// getTemplateTypeParmType - Return the unique reference to the type
1095/// for the specified template type parameter declaration.
1096QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1097 if (!Decl->TypeForDecl) {
1098 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1099 Types.push_back(Decl->TypeForDecl);
1100 }
1101 return QualType(Decl->TypeForDecl, 0);
1102}
1103
Ted Kremenek42730c52008-01-07 19:49:32 +00001104/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001105/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001106QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001107 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1108
Ted Kremenek42730c52008-01-07 19:49:32 +00001109 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001110 Types.push_back(Decl->TypeForDecl);
1111 return QualType(Decl->TypeForDecl, 0);
1112}
1113
Chris Lattnere1352302008-04-07 04:56:42 +00001114/// CmpProtocolNames - Comparison predicate for sorting protocols
1115/// alphabetically.
1116static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1117 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001118 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001119}
1120
1121static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1122 unsigned &NumProtocols) {
1123 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1124
1125 // Sort protocols, keyed by name.
1126 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1127
1128 // Remove duplicates.
1129 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1130 NumProtocols = ProtocolsEnd-Protocols;
1131}
1132
1133
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001134/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1135/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001136QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1137 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001138 // Sort the protocol list alphabetically to canonicalize it.
1139 SortAndUniqueProtocols(Protocols, NumProtocols);
1140
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001141 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001142 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001143
1144 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001145 if (ObjCQualifiedInterfaceType *QT =
1146 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001147 return QualType(QT, 0);
1148
1149 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001150 ObjCQualifiedInterfaceType *QType =
1151 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001152 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001153 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001154 return QualType(QType, 0);
1155}
1156
Chris Lattnere1352302008-04-07 04:56:42 +00001157/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1158/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001159QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001160 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001161 // Sort the protocol list alphabetically to canonicalize it.
1162 SortAndUniqueProtocols(Protocols, NumProtocols);
1163
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001164 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001165 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001166
1167 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001168 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001169 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001170 return QualType(QT, 0);
1171
1172 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001173 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001174 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001175 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001176 return QualType(QType, 0);
1177}
1178
Steve Naroff0604dd92007-08-01 18:02:17 +00001179/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1180/// TypeOfExpr AST's (since expression's are never shared). For example,
1181/// multiple declarations that refer to "typeof(x)" all contain different
1182/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1183/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001184QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001185 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001186 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1187 Types.push_back(toe);
1188 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001189}
1190
Steve Naroff0604dd92007-08-01 18:02:17 +00001191/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1192/// TypeOfType AST's. The only motivation to unique these nodes would be
1193/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1194/// an issue. This doesn't effect the type checker, since it operates
1195/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001196QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001197 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001198 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1199 Types.push_back(tot);
1200 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001201}
1202
Chris Lattner4b009652007-07-25 00:24:17 +00001203/// getTagDeclType - Return the unique reference to the type for the
1204/// specified TagDecl (struct/union/class/enum) decl.
1205QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001206 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001207 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001208}
1209
1210/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1211/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1212/// needs to agree with the definition in <stddef.h>.
1213QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001214 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001215}
1216
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001217/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001218/// width of characters in wide strings, The value is target dependent and
1219/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001220QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001221 if (LangOpts.CPlusPlus)
1222 return WCharTy;
1223
Douglas Gregorc6507e42008-11-03 14:12:49 +00001224 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1225 // wide-character type?
1226 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001227}
1228
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001229/// getSignedWCharType - Return the type of "signed wchar_t".
1230/// Used when in C++, as a GCC extension.
1231QualType ASTContext::getSignedWCharType() const {
1232 // FIXME: derive from "Target" ?
1233 return WCharTy;
1234}
1235
1236/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1237/// Used when in C++, as a GCC extension.
1238QualType ASTContext::getUnsignedWCharType() const {
1239 // FIXME: derive from "Target" ?
1240 return UnsignedIntTy;
1241}
1242
Chris Lattner4b009652007-07-25 00:24:17 +00001243/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1244/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1245QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001246 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001247}
1248
Chris Lattner19eb97e2008-04-02 05:18:44 +00001249//===----------------------------------------------------------------------===//
1250// Type Operators
1251//===----------------------------------------------------------------------===//
1252
Chris Lattner3dae6f42008-04-06 22:41:35 +00001253/// getCanonicalType - Return the canonical (structural) type corresponding to
1254/// the specified potentially non-canonical type. The non-canonical version
1255/// of a type may have many "decorated" versions of types. Decorators can
1256/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1257/// to be free of any of these, allowing two canonical types to be compared
1258/// for exact equality with a simple pointer comparison.
1259QualType ASTContext::getCanonicalType(QualType T) {
1260 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001261
1262 // If the result has type qualifiers, make sure to canonicalize them as well.
1263 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1264 if (TypeQuals == 0) return CanType;
1265
1266 // If the type qualifiers are on an array type, get the canonical type of the
1267 // array with the qualifiers applied to the element type.
1268 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1269 if (!AT)
1270 return CanType.getQualifiedType(TypeQuals);
1271
1272 // Get the canonical version of the element with the extra qualifiers on it.
1273 // This can recursively sink qualifiers through multiple levels of arrays.
1274 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1275 NewEltTy = getCanonicalType(NewEltTy);
1276
1277 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1278 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1279 CAT->getIndexTypeQualifier());
1280 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1281 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1282 IAT->getIndexTypeQualifier());
1283
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001284 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1285 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1286 DSAT->getSizeModifier(),
1287 DSAT->getIndexTypeQualifier());
1288
Chris Lattnera1923f62008-08-04 07:31:14 +00001289 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1290 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1291 VAT->getSizeModifier(),
1292 VAT->getIndexTypeQualifier());
1293}
1294
1295
1296const ArrayType *ASTContext::getAsArrayType(QualType T) {
1297 // Handle the non-qualified case efficiently.
1298 if (T.getCVRQualifiers() == 0) {
1299 // Handle the common positive case fast.
1300 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1301 return AT;
1302 }
1303
1304 // Handle the common negative case fast, ignoring CVR qualifiers.
1305 QualType CType = T->getCanonicalTypeInternal();
1306
1307 // Make sure to look through type qualifiers (like ASQuals) for the negative
1308 // test.
1309 if (!isa<ArrayType>(CType) &&
1310 !isa<ArrayType>(CType.getUnqualifiedType()))
1311 return 0;
1312
1313 // Apply any CVR qualifiers from the array type to the element type. This
1314 // implements C99 6.7.3p8: "If the specification of an array type includes
1315 // any type qualifiers, the element type is so qualified, not the array type."
1316
1317 // If we get here, we either have type qualifiers on the type, or we have
1318 // sugar such as a typedef in the way. If we have type qualifiers on the type
1319 // we must propagate them down into the elemeng type.
1320 unsigned CVRQuals = T.getCVRQualifiers();
1321 unsigned AddrSpace = 0;
1322 Type *Ty = T.getTypePtr();
1323
1324 // Rip through ASQualType's and typedefs to get to a concrete type.
1325 while (1) {
1326 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1327 AddrSpace = ASQT->getAddressSpace();
1328 Ty = ASQT->getBaseType();
1329 } else {
1330 T = Ty->getDesugaredType();
1331 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1332 break;
1333 CVRQuals |= T.getCVRQualifiers();
1334 Ty = T.getTypePtr();
1335 }
1336 }
1337
1338 // If we have a simple case, just return now.
1339 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1340 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1341 return ATy;
1342
1343 // Otherwise, we have an array and we have qualifiers on it. Push the
1344 // qualifiers into the array element type and return a new array type.
1345 // Get the canonical version of the element with the extra qualifiers on it.
1346 // This can recursively sink qualifiers through multiple levels of arrays.
1347 QualType NewEltTy = ATy->getElementType();
1348 if (AddrSpace)
1349 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1350 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1351
1352 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1353 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1354 CAT->getSizeModifier(),
1355 CAT->getIndexTypeQualifier()));
1356 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1357 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1358 IAT->getSizeModifier(),
1359 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001360
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001361 if (const DependentSizedArrayType *DSAT
1362 = dyn_cast<DependentSizedArrayType>(ATy))
1363 return cast<ArrayType>(
1364 getDependentSizedArrayType(NewEltTy,
1365 DSAT->getSizeExpr(),
1366 DSAT->getSizeModifier(),
1367 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001368
Chris Lattnera1923f62008-08-04 07:31:14 +00001369 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1370 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1371 VAT->getSizeModifier(),
1372 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001373}
1374
1375
Chris Lattner19eb97e2008-04-02 05:18:44 +00001376/// getArrayDecayedType - Return the properly qualified result of decaying the
1377/// specified array type to a pointer. This operation is non-trivial when
1378/// handling typedefs etc. The canonical type of "T" must be an array type,
1379/// this returns a pointer to a properly qualified element of the array.
1380///
1381/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1382QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001383 // Get the element type with 'getAsArrayType' so that we don't lose any
1384 // typedefs in the element type of the array. This also handles propagation
1385 // of type qualifiers from the array type into the element type if present
1386 // (C99 6.7.3p8).
1387 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1388 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001389
Chris Lattnera1923f62008-08-04 07:31:14 +00001390 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001391
1392 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001393 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001394}
1395
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001396QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001397 QualType ElemTy = VAT->getElementType();
1398
1399 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1400 return getBaseElementType(VAT);
1401
1402 return ElemTy;
1403}
1404
Chris Lattner4b009652007-07-25 00:24:17 +00001405/// getFloatingRank - Return a relative rank for floating point types.
1406/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001407static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001408 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001409 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001410
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001411 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001412 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001413 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001414 case BuiltinType::Float: return FloatRank;
1415 case BuiltinType::Double: return DoubleRank;
1416 case BuiltinType::LongDouble: return LongDoubleRank;
1417 }
1418}
1419
Steve Narofffa0c4532007-08-27 01:41:48 +00001420/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1421/// point or a complex type (based on typeDomain/typeSize).
1422/// 'typeDomain' is a real floating point or complex type.
1423/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001424QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1425 QualType Domain) const {
1426 FloatingRank EltRank = getFloatingRank(Size);
1427 if (Domain->isComplexType()) {
1428 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001429 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001430 case FloatRank: return FloatComplexTy;
1431 case DoubleRank: return DoubleComplexTy;
1432 case LongDoubleRank: return LongDoubleComplexTy;
1433 }
Chris Lattner4b009652007-07-25 00:24:17 +00001434 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001435
1436 assert(Domain->isRealFloatingType() && "Unknown domain!");
1437 switch (EltRank) {
1438 default: assert(0 && "getFloatingRank(): illegal value for rank");
1439 case FloatRank: return FloatTy;
1440 case DoubleRank: return DoubleTy;
1441 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001442 }
Chris Lattner4b009652007-07-25 00:24:17 +00001443}
1444
Chris Lattner51285d82008-04-06 23:55:33 +00001445/// getFloatingTypeOrder - Compare the rank of the two specified floating
1446/// point types, ignoring the domain of the type (i.e. 'double' ==
1447/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1448/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001449int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1450 FloatingRank LHSR = getFloatingRank(LHS);
1451 FloatingRank RHSR = getFloatingRank(RHS);
1452
1453 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001454 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001455 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001456 return 1;
1457 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001458}
1459
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001460/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1461/// routine will assert if passed a built-in type that isn't an integer or enum,
1462/// or if it is not canonicalized.
1463static unsigned getIntegerRank(Type *T) {
1464 assert(T->isCanonical() && "T should be canonicalized");
1465 if (isa<EnumType>(T))
1466 return 4;
1467
1468 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001469 default: assert(0 && "getIntegerRank(): not a built-in integer");
1470 case BuiltinType::Bool:
1471 return 1;
1472 case BuiltinType::Char_S:
1473 case BuiltinType::Char_U:
1474 case BuiltinType::SChar:
1475 case BuiltinType::UChar:
1476 return 2;
1477 case BuiltinType::Short:
1478 case BuiltinType::UShort:
1479 return 3;
1480 case BuiltinType::Int:
1481 case BuiltinType::UInt:
1482 return 4;
1483 case BuiltinType::Long:
1484 case BuiltinType::ULong:
1485 return 5;
1486 case BuiltinType::LongLong:
1487 case BuiltinType::ULongLong:
1488 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001489 }
1490}
1491
Chris Lattner51285d82008-04-06 23:55:33 +00001492/// getIntegerTypeOrder - Returns the highest ranked integer type:
1493/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1494/// LHS < RHS, return -1.
1495int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001496 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1497 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001498 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001499
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001500 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1501 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001502
Chris Lattner51285d82008-04-06 23:55:33 +00001503 unsigned LHSRank = getIntegerRank(LHSC);
1504 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001505
Chris Lattner51285d82008-04-06 23:55:33 +00001506 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1507 if (LHSRank == RHSRank) return 0;
1508 return LHSRank > RHSRank ? 1 : -1;
1509 }
Chris Lattner4b009652007-07-25 00:24:17 +00001510
Chris Lattner51285d82008-04-06 23:55:33 +00001511 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1512 if (LHSUnsigned) {
1513 // If the unsigned [LHS] type is larger, return it.
1514 if (LHSRank >= RHSRank)
1515 return 1;
1516
1517 // If the signed type can represent all values of the unsigned type, it
1518 // wins. Because we are dealing with 2's complement and types that are
1519 // powers of two larger than each other, this is always safe.
1520 return -1;
1521 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001522
Chris Lattner51285d82008-04-06 23:55:33 +00001523 // If the unsigned [RHS] type is larger, return it.
1524 if (RHSRank >= LHSRank)
1525 return -1;
1526
1527 // If the signed type can represent all values of the unsigned type, it
1528 // wins. Because we are dealing with 2's complement and types that are
1529 // powers of two larger than each other, this is always safe.
1530 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001531}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001532
1533// getCFConstantStringType - Return the type used for constant CFStrings.
1534QualType ASTContext::getCFConstantStringType() {
1535 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001536 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001537 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001538 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001539 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001540
1541 // const int *isa;
1542 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001543 // int flags;
1544 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001545 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001546 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001547 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001548 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001549
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001550 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001551 for (unsigned i = 0; i < 4; ++i) {
1552 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1553 SourceLocation(), 0,
1554 FieldTypes[i], /*BitWidth=*/0,
1555 /*Mutable=*/false, /*PrevDecl=*/0);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001556 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001557 }
1558
1559 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001560 }
1561
1562 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001563}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001564
Anders Carlssonf58cac72008-08-30 19:34:46 +00001565QualType ASTContext::getObjCFastEnumerationStateType()
1566{
1567 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001568 ObjCFastEnumerationStateTypeDecl =
1569 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1570 &Idents.get("__objcFastEnumerationState"));
1571
Anders Carlssonf58cac72008-08-30 19:34:46 +00001572 QualType FieldTypes[] = {
1573 UnsignedLongTy,
1574 getPointerType(ObjCIdType),
1575 getPointerType(UnsignedLongTy),
1576 getConstantArrayType(UnsignedLongTy,
1577 llvm::APInt(32, 5), ArrayType::Normal, 0)
1578 };
1579
Douglas Gregor8acb7272008-12-11 16:49:14 +00001580 for (size_t i = 0; i < 4; ++i) {
1581 FieldDecl *Field = FieldDecl::Create(*this,
1582 ObjCFastEnumerationStateTypeDecl,
1583 SourceLocation(), 0,
1584 FieldTypes[i], /*BitWidth=*/0,
1585 /*Mutable=*/false, /*PrevDecl=*/0);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001586 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001587 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001588
Douglas Gregor8acb7272008-12-11 16:49:14 +00001589 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001590 }
1591
1592 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1593}
1594
Anders Carlssone3f02572007-10-29 06:33:42 +00001595// This returns true if a type has been typedefed to BOOL:
1596// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001597static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001598 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001599 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1600 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001601
1602 return false;
1603}
1604
Ted Kremenek42730c52008-01-07 19:49:32 +00001605/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001606/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001607int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001608 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001609
1610 // Make all integer and enum types at least as large as an int
1611 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001612 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001613 // Treat arrays as pointers, since that's how they're passed in.
1614 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001615 sz = getTypeSize(VoidPtrTy);
1616 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001617}
1618
Ted Kremenek42730c52008-01-07 19:49:32 +00001619/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001620/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001621void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001622 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001623 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001624 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001625 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001626 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001627 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001628 // Compute size of all parameters.
1629 // Start with computing size of a pointer in number of bytes.
1630 // FIXME: There might(should) be a better way of doing this computation!
1631 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001632 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001633 // The first two arguments (self and _cmd) are pointers; account for
1634 // their size.
1635 int ParmOffset = 2 * PtrSize;
1636 int NumOfParams = Decl->getNumParams();
1637 for (int i = 0; i < NumOfParams; i++) {
1638 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001639 int sz = getObjCEncodingTypeSize (PType);
1640 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001641 ParmOffset += sz;
1642 }
1643 S += llvm::utostr(ParmOffset);
1644 S += "@0:";
1645 S += llvm::utostr(PtrSize);
1646
1647 // Argument types.
1648 ParmOffset = 2 * PtrSize;
1649 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001650 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1651 QualType PType = PVDecl->getOriginalType();
1652 if (const ArrayType *AT =
1653 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1654 // Use array's original type only if it has known number of
1655 // elements.
1656 if (!dyn_cast<ConstantArrayType>(AT))
1657 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001658 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001659 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001660 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001661 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001662 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001663 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001664 }
1665}
1666
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001667/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1668/// method declaration. If non-NULL, Container must be either an
1669/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1670/// NULL when getting encodings for protocol properties.
1671void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1672 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001673 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001674 // Collect information from the property implementation decl(s).
1675 bool Dynamic = false;
1676 ObjCPropertyImplDecl *SynthesizePID = 0;
1677
1678 // FIXME: Duplicated code due to poor abstraction.
1679 if (Container) {
1680 if (const ObjCCategoryImplDecl *CID =
1681 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1682 for (ObjCCategoryImplDecl::propimpl_iterator
1683 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1684 ObjCPropertyImplDecl *PID = *i;
1685 if (PID->getPropertyDecl() == PD) {
1686 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1687 Dynamic = true;
1688 } else {
1689 SynthesizePID = PID;
1690 }
1691 }
1692 }
1693 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001694 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001695 for (ObjCCategoryImplDecl::propimpl_iterator
1696 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1697 ObjCPropertyImplDecl *PID = *i;
1698 if (PID->getPropertyDecl() == PD) {
1699 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1700 Dynamic = true;
1701 } else {
1702 SynthesizePID = PID;
1703 }
1704 }
1705 }
1706 }
1707 }
1708
1709 // FIXME: This is not very efficient.
1710 S = "T";
1711
1712 // Encode result type.
1713 // FIXME: GCC uses a generating_property_type_encoding mode during
1714 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001715 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001716
1717 if (PD->isReadOnly()) {
1718 S += ",R";
1719 } else {
1720 switch (PD->getSetterKind()) {
1721 case ObjCPropertyDecl::Assign: break;
1722 case ObjCPropertyDecl::Copy: S += ",C"; break;
1723 case ObjCPropertyDecl::Retain: S += ",&"; break;
1724 }
1725 }
1726
1727 // It really isn't clear at all what this means, since properties
1728 // are "dynamic by default".
1729 if (Dynamic)
1730 S += ",D";
1731
1732 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1733 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001734 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001735 }
1736
1737 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1738 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001739 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001740 }
1741
1742 if (SynthesizePID) {
1743 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1744 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001745 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001746 }
1747
1748 // FIXME: OBJCGC: weak & strong
1749}
1750
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001751/// getLegacyIntegralTypeEncoding -
1752/// Another legacy compatibility encoding: 32-bit longs are encoded as
1753/// 'l' or 'L', but not always. For typedefs, we need to use
1754/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1755///
1756void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1757 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1758 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1759 if (BT->getKind() == BuiltinType::ULong)
1760 PointeeTy = UnsignedIntTy;
1761 else if (BT->getKind() == BuiltinType::Long)
1762 PointeeTy = IntTy;
1763 }
1764 }
1765}
1766
Fariborz Jahanian248db262008-01-22 22:44:46 +00001767void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001768 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001769 // We follow the behavior of gcc, expanding structures which are
1770 // directly pointed to, and expanding embedded structures. Note that
1771 // these rules are sufficient to prevent recursive encoding of the
1772 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001773 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1774 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001775}
1776
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001777static void EncodeBitField(const ASTContext *Context, std::string& S,
1778 FieldDecl *FD) {
1779 const Expr *E = FD->getBitWidth();
1780 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1781 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1782 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1783 S += 'b';
1784 S += llvm::utostr(N);
1785}
1786
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001787void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1788 bool ExpandPointedToStructures,
1789 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001790 FieldDecl *FD,
1791 bool OutermostType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001792 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001793 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001794 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001795 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001796 else {
1797 char encoding;
1798 switch (BT->getKind()) {
1799 default: assert(0 && "Unhandled builtin type kind");
1800 case BuiltinType::Void: encoding = 'v'; break;
1801 case BuiltinType::Bool: encoding = 'B'; break;
1802 case BuiltinType::Char_U:
1803 case BuiltinType::UChar: encoding = 'C'; break;
1804 case BuiltinType::UShort: encoding = 'S'; break;
1805 case BuiltinType::UInt: encoding = 'I'; break;
1806 case BuiltinType::ULong: encoding = 'L'; break;
1807 case BuiltinType::ULongLong: encoding = 'Q'; break;
1808 case BuiltinType::Char_S:
1809 case BuiltinType::SChar: encoding = 'c'; break;
1810 case BuiltinType::Short: encoding = 's'; break;
1811 case BuiltinType::Int: encoding = 'i'; break;
1812 case BuiltinType::Long: encoding = 'l'; break;
1813 case BuiltinType::LongLong: encoding = 'q'; break;
1814 case BuiltinType::Float: encoding = 'f'; break;
1815 case BuiltinType::Double: encoding = 'd'; break;
1816 case BuiltinType::LongDouble: encoding = 'd'; break;
1817 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001818
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001819 S += encoding;
1820 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001821 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001822 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001823 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001824 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1825 ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001826 ExpandStructures, FD);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001827 }
1828 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001829 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001830 bool isReadOnly = false;
1831 // For historical/compatibility reasons, the read-only qualifier of the
1832 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1833 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1834 // Also, do not emit the 'r' for anything but the outermost type!
1835 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1836 if (OutermostType && T.isConstQualified()) {
1837 isReadOnly = true;
1838 S += 'r';
1839 }
1840 }
1841 else if (OutermostType) {
1842 QualType P = PointeeTy;
1843 while (P->getAsPointerType())
1844 P = P->getAsPointerType()->getPointeeType();
1845 if (P.isConstQualified()) {
1846 isReadOnly = true;
1847 S += 'r';
1848 }
1849 }
1850 if (isReadOnly) {
1851 // Another legacy compatibility encoding. Some ObjC qualifier and type
1852 // combinations need to be rearranged.
1853 // Rewrite "in const" from "nr" to "rn"
1854 const char * s = S.c_str();
1855 int len = S.length();
1856 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
1857 std::string replace = "rn";
1858 S.replace(S.end()-2, S.end(), replace);
1859 }
1860 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001861 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001862 S += '@';
1863 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001864 }
1865 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00001866 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1867 // Another historical/compatibility reason.
1868 // We encode the underlying type which comes out as
1869 // {...};
1870 S += '^';
1871 getObjCEncodingForTypeImpl(PointeeTy, S,
1872 false, ExpandPointedToStructures,
1873 NULL);
1874 return;
1875 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001876 S += '@';
Fariborz Jahanian320ac422008-12-20 19:17:01 +00001877 if (FD) {
1878 ObjCInterfaceDecl *OI = PointeeTy->getAsObjCInterfaceType()->getDecl();
1879 S += '"';
1880 S += OI->getNameAsCString();
1881 S += '"';
1882 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001883 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001884 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001885 S += '#';
1886 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001887 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001888 S += ':';
1889 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001890 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001891
1892 if (PointeeTy->isCharType()) {
1893 // char pointer types should be encoded as '*' unless it is a
1894 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001895 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001896 S += '*';
1897 return;
1898 }
1899 }
1900
1901 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001902 getLegacyIntegralTypeEncoding(PointeeTy);
1903
1904 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001905 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001906 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00001907 } else if (const ArrayType *AT =
1908 // Ignore type qualifiers etc.
1909 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001910 S += '[';
1911
1912 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1913 S += llvm::utostr(CAT->getSize().getZExtValue());
1914 else
1915 assert(0 && "Unhandled array type!");
1916
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001917 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001918 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001919 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001920 } else if (T->getAsFunctionType()) {
1921 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001922 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001923 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001924 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001925 // Anonymous structures print as '?'
1926 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1927 S += II->getName();
1928 } else {
1929 S += '?';
1930 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001931 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001932 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001933 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1934 FieldEnd = RDecl->field_end();
1935 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001936 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001937 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001938 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001939 S += '"';
1940 }
1941
1942 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001943 if (Field->isBitField()) {
1944 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1945 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00001946 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001947 QualType qt = Field->getType();
1948 getLegacyIntegralTypeEncoding(qt);
1949 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001950 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001951 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001952 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001953 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001954 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001955 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001956 if (FD && FD->isBitField())
1957 EncodeBitField(this, S, FD);
1958 else
1959 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001960 } else if (T->isBlockPointerType()) {
1961 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001962 } else if (T->isObjCInterfaceType()) {
1963 // @encode(class_name)
1964 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
1965 S += '{';
1966 const IdentifierInfo *II = OI->getIdentifier();
1967 S += II->getName();
1968 S += '=';
1969 std::vector<FieldDecl*> RecFields;
1970 CollectObjCIvars(OI, RecFields);
1971 for (unsigned int i = 0; i != RecFields.size(); i++) {
1972 if (RecFields[i]->isBitField())
1973 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
1974 RecFields[i]);
1975 else
1976 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
1977 FD);
1978 }
1979 S += '}';
1980 }
1981 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001982 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001983}
1984
Ted Kremenek42730c52008-01-07 19:49:32 +00001985void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001986 std::string& S) const {
1987 if (QT & Decl::OBJC_TQ_In)
1988 S += 'n';
1989 if (QT & Decl::OBJC_TQ_Inout)
1990 S += 'N';
1991 if (QT & Decl::OBJC_TQ_Out)
1992 S += 'o';
1993 if (QT & Decl::OBJC_TQ_Bycopy)
1994 S += 'O';
1995 if (QT & Decl::OBJC_TQ_Byref)
1996 S += 'R';
1997 if (QT & Decl::OBJC_TQ_Oneway)
1998 S += 'V';
1999}
2000
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002001void ASTContext::setBuiltinVaListType(QualType T)
2002{
2003 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2004
2005 BuiltinVaListType = T;
2006}
2007
Ted Kremenek42730c52008-01-07 19:49:32 +00002008void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002009{
Ted Kremenek42730c52008-01-07 19:49:32 +00002010 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002011
2012 // typedef struct objc_object *id;
2013 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002014 // User error - caller will issue diagnostics.
2015 if (!ptr)
2016 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002017 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002018 // User error - caller will issue diagnostics.
2019 if (!rec)
2020 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002021 IdStructType = rec;
2022}
2023
Ted Kremenek42730c52008-01-07 19:49:32 +00002024void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002025{
Ted Kremenek42730c52008-01-07 19:49:32 +00002026 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002027
2028 // typedef struct objc_selector *SEL;
2029 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002030 if (!ptr)
2031 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002032 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002033 if (!rec)
2034 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002035 SelStructType = rec;
2036}
2037
Ted Kremenek42730c52008-01-07 19:49:32 +00002038void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002039{
Ted Kremenek42730c52008-01-07 19:49:32 +00002040 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002041}
2042
Ted Kremenek42730c52008-01-07 19:49:32 +00002043void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002044{
Ted Kremenek42730c52008-01-07 19:49:32 +00002045 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002046
2047 // typedef struct objc_class *Class;
2048 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2049 assert(ptr && "'Class' incorrectly typed");
2050 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2051 assert(rec && "'Class' incorrectly typed");
2052 ClassStructType = rec;
2053}
2054
Ted Kremenek42730c52008-01-07 19:49:32 +00002055void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2056 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002057 "'NSConstantString' type already set!");
2058
Ted Kremenek42730c52008-01-07 19:49:32 +00002059 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002060}
2061
Douglas Gregorc6507e42008-11-03 14:12:49 +00002062/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002063/// TargetInfo, produce the corresponding type. The unsigned @p Type
2064/// is actually a value of type @c TargetInfo::IntType.
2065QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002066 switch (Type) {
2067 case TargetInfo::NoInt: return QualType();
2068 case TargetInfo::SignedShort: return ShortTy;
2069 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2070 case TargetInfo::SignedInt: return IntTy;
2071 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2072 case TargetInfo::SignedLong: return LongTy;
2073 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2074 case TargetInfo::SignedLongLong: return LongLongTy;
2075 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2076 }
2077
2078 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002079 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002080}
Ted Kremenek118930e2008-07-24 23:58:27 +00002081
2082//===----------------------------------------------------------------------===//
2083// Type Predicates.
2084//===----------------------------------------------------------------------===//
2085
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002086/// isObjCNSObjectType - Return true if this is an NSObject object using
2087/// NSObject attribute on a c-style pointer type.
2088/// FIXME - Make it work directly on types.
2089///
2090bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2091 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2092 if (TypedefDecl *TD = TDT->getDecl())
2093 if (TD->getAttr<ObjCNSObjectAttr>())
2094 return true;
2095 }
2096 return false;
2097}
2098
Ted Kremenek118930e2008-07-24 23:58:27 +00002099/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2100/// to an object type. This includes "id" and "Class" (two 'special' pointers
2101/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2102/// ID type).
2103bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2104 if (Ty->isObjCQualifiedIdType())
2105 return true;
2106
Steve Naroffd9e00802008-10-21 18:24:04 +00002107 // Blocks are objects.
2108 if (Ty->isBlockPointerType())
2109 return true;
2110
2111 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002112 if (!Ty->isPointerType())
2113 return false;
2114
2115 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2116 // pointer types. This looks for the typedef specifically, not for the
2117 // underlying type.
2118 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2119 return true;
2120
2121 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002122 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2123 return true;
2124
2125 // If is has NSObject attribute, OK as well.
2126 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002127}
2128
Chris Lattner6ff358b2008-04-07 06:51:04 +00002129//===----------------------------------------------------------------------===//
2130// Type Compatibility Testing
2131//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002132
Steve Naroff3454b6c2008-09-04 15:10:53 +00002133/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002134/// block types. Types must be strictly compatible here. For example,
2135/// C unfortunately doesn't produce an error for the following:
2136///
2137/// int (*emptyArgFunc)();
2138/// int (*intArgList)(int) = emptyArgFunc;
2139///
2140/// For blocks, we will produce an error for the following (similar to C++):
2141///
2142/// int (^emptyArgBlock)();
2143/// int (^intArgBlock)(int) = emptyArgBlock;
2144///
2145/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2146///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002147bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002148 const FunctionType *lbase = lhs->getAsFunctionType();
2149 const FunctionType *rbase = rhs->getAsFunctionType();
2150 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2151 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2152 if (lproto && rproto)
2153 return !mergeTypes(lhs, rhs).isNull();
2154 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002155}
2156
Chris Lattner6ff358b2008-04-07 06:51:04 +00002157/// areCompatVectorTypes - Return true if the two specified vector types are
2158/// compatible.
2159static bool areCompatVectorTypes(const VectorType *LHS,
2160 const VectorType *RHS) {
2161 assert(LHS->isCanonical() && RHS->isCanonical());
2162 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002163 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002164}
2165
Eli Friedman0d9549b2008-08-22 00:56:42 +00002166/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002167/// compatible for assignment from RHS to LHS. This handles validation of any
2168/// protocol qualifiers on the LHS or RHS.
2169///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002170bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2171 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002172 // Verify that the base decls are compatible: the RHS must be a subclass of
2173 // the LHS.
2174 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2175 return false;
2176
2177 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2178 // protocol qualified at all, then we are good.
2179 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2180 return true;
2181
2182 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2183 // isn't a superset.
2184 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2185 return true; // FIXME: should return false!
2186
2187 // Finally, we must have two protocol-qualified interfaces.
2188 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2189 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2190 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2191 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2192 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2193 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2194
2195 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2196 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2197 // LHS in a single parallel scan until we run out of elements in LHS.
2198 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2199 ObjCProtocolDecl *LHSProto = *LHSPI;
2200
2201 while (RHSPI != RHSPE) {
2202 ObjCProtocolDecl *RHSProto = *RHSPI++;
2203 // If the RHS has a protocol that the LHS doesn't, ignore it.
2204 if (RHSProto != LHSProto)
2205 continue;
2206
2207 // Otherwise, the RHS does have this element.
2208 ++LHSPI;
2209 if (LHSPI == LHSPE)
2210 return true; // All protocols in LHS exist in RHS.
2211
2212 LHSProto = *LHSPI;
2213 }
2214
2215 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2216 return false;
2217}
2218
Steve Naroff85f0dc52007-10-15 20:41:53 +00002219/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2220/// both shall have the identically qualified version of a compatible type.
2221/// C99 6.2.7p1: Two types have compatible types if their types are the
2222/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002223bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2224 return !mergeTypes(LHS, RHS).isNull();
2225}
2226
2227QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2228 const FunctionType *lbase = lhs->getAsFunctionType();
2229 const FunctionType *rbase = rhs->getAsFunctionType();
2230 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2231 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2232 bool allLTypes = true;
2233 bool allRTypes = true;
2234
2235 // Check return type
2236 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2237 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002238 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2239 allLTypes = false;
2240 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2241 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002242
2243 if (lproto && rproto) { // two C99 style function prototypes
2244 unsigned lproto_nargs = lproto->getNumArgs();
2245 unsigned rproto_nargs = rproto->getNumArgs();
2246
2247 // Compatible functions must have the same number of arguments
2248 if (lproto_nargs != rproto_nargs)
2249 return QualType();
2250
2251 // Variadic and non-variadic functions aren't compatible
2252 if (lproto->isVariadic() != rproto->isVariadic())
2253 return QualType();
2254
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002255 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2256 return QualType();
2257
Eli Friedman0d9549b2008-08-22 00:56:42 +00002258 // Check argument compatibility
2259 llvm::SmallVector<QualType, 10> types;
2260 for (unsigned i = 0; i < lproto_nargs; i++) {
2261 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2262 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2263 QualType argtype = mergeTypes(largtype, rargtype);
2264 if (argtype.isNull()) return QualType();
2265 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002266 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2267 allLTypes = false;
2268 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2269 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002270 }
2271 if (allLTypes) return lhs;
2272 if (allRTypes) return rhs;
2273 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002274 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002275 }
2276
2277 if (lproto) allRTypes = false;
2278 if (rproto) allLTypes = false;
2279
2280 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2281 if (proto) {
2282 if (proto->isVariadic()) return QualType();
2283 // Check that the types are compatible with the types that
2284 // would result from default argument promotions (C99 6.7.5.3p15).
2285 // The only types actually affected are promotable integer
2286 // types and floats, which would be passed as a different
2287 // type depending on whether the prototype is visible.
2288 unsigned proto_nargs = proto->getNumArgs();
2289 for (unsigned i = 0; i < proto_nargs; ++i) {
2290 QualType argTy = proto->getArgType(i);
2291 if (argTy->isPromotableIntegerType() ||
2292 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2293 return QualType();
2294 }
2295
2296 if (allLTypes) return lhs;
2297 if (allRTypes) return rhs;
2298 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002299 proto->getNumArgs(), lproto->isVariadic(),
2300 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002301 }
2302
2303 if (allLTypes) return lhs;
2304 if (allRTypes) return rhs;
2305 return getFunctionTypeNoProto(retType);
2306}
2307
2308QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002309 // C++ [expr]: If an expression initially has the type "reference to T", the
2310 // type is adjusted to "T" prior to any further analysis, the expression
2311 // designates the object or function denoted by the reference, and the
2312 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002313 // FIXME: C++ shouldn't be going through here! The rules are different
2314 // enough that they should be handled separately.
2315 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002316 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002317 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002318 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002319
Eli Friedman0d9549b2008-08-22 00:56:42 +00002320 QualType LHSCan = getCanonicalType(LHS),
2321 RHSCan = getCanonicalType(RHS);
2322
2323 // If two types are identical, they are compatible.
2324 if (LHSCan == RHSCan)
2325 return LHS;
2326
2327 // If the qualifiers are different, the types aren't compatible
2328 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2329 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2330 return QualType();
2331
2332 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2333 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2334
Chris Lattnerc38d4522008-01-14 05:45:46 +00002335 // We want to consider the two function types to be the same for these
2336 // comparisons, just force one to the other.
2337 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2338 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002339
2340 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002341 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2342 LHSClass = Type::ConstantArray;
2343 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2344 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002345
Nate Begemanaf6ed502008-04-18 23:10:10 +00002346 // Canonicalize ExtVector -> Vector.
2347 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2348 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002349
Chris Lattner7cdcb252008-04-07 06:38:24 +00002350 // Consider qualified interfaces and interfaces the same.
2351 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2352 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002353
Chris Lattnerb5709e22008-04-07 05:43:21 +00002354 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002355 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002356 // ID is compatible with all qualified id types.
2357 if (LHS->isObjCQualifiedIdType()) {
2358 if (const PointerType *PT = RHS->getAsPointerType()) {
2359 QualType pType = PT->getPointeeType();
2360 if (isObjCIdType(pType))
2361 return LHS;
2362 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2363 // Unfortunately, this API is part of Sema (which we don't have access
2364 // to. Need to refactor. The following check is insufficient, since we
2365 // need to make sure the class implements the protocol.
2366 if (pType->isObjCInterfaceType())
2367 return LHS;
2368 }
2369 }
2370 if (RHS->isObjCQualifiedIdType()) {
2371 if (const PointerType *PT = LHS->getAsPointerType()) {
2372 QualType pType = PT->getPointeeType();
2373 if (isObjCIdType(pType))
2374 return RHS;
2375 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2376 // Unfortunately, this API is part of Sema (which we don't have access
2377 // to. Need to refactor. The following check is insufficient, since we
2378 // need to make sure the class implements the protocol.
2379 if (pType->isObjCInterfaceType())
2380 return RHS;
2381 }
2382 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002383 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2384 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002385 if (const EnumType* ETy = LHS->getAsEnumType()) {
2386 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2387 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002388 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002389 if (const EnumType* ETy = RHS->getAsEnumType()) {
2390 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2391 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002392 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002393
Eli Friedman0d9549b2008-08-22 00:56:42 +00002394 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002395 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002396
Steve Naroffc88babe2008-01-09 22:43:08 +00002397 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002398 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002399 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002400 {
2401 // Merge two pointer types, while trying to preserve typedef info
2402 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2403 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2404 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2405 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002406 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2407 return LHS;
2408 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2409 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002410 return getPointerType(ResultType);
2411 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002412 case Type::BlockPointer:
2413 {
2414 // Merge two block pointer types, while trying to preserve typedef info
2415 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2416 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2417 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2418 if (ResultType.isNull()) return QualType();
2419 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2420 return LHS;
2421 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2422 return RHS;
2423 return getBlockPointerType(ResultType);
2424 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002425 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002426 {
2427 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2428 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2429 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2430 return QualType();
2431
2432 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2433 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2434 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2435 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002436 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2437 return LHS;
2438 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2439 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002440 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2441 ArrayType::ArraySizeModifier(), 0);
2442 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2443 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002444 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2445 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002446 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2447 return LHS;
2448 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2449 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002450 if (LVAT) {
2451 // FIXME: This isn't correct! But tricky to implement because
2452 // the array's size has to be the size of LHS, but the type
2453 // has to be different.
2454 return LHS;
2455 }
2456 if (RVAT) {
2457 // FIXME: This isn't correct! But tricky to implement because
2458 // the array's size has to be the size of RHS, but the type
2459 // has to be different.
2460 return RHS;
2461 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002462 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2463 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002464 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002465 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002466 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002467 return mergeFunctionTypes(LHS, RHS);
2468 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002469 // FIXME: Why are these compatible?
2470 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2471 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2472 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002473 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002474 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002475 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002476 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002477 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2478 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002479 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002480 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002481 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2482 // for checking assignment/comparison safety
2483 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002484 case Type::ObjCQualifiedId:
2485 // Distinct qualified id's are not compatible.
2486 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002487 default:
2488 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002489 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002490 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002491}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002492
Chris Lattner1d78a862008-04-07 07:01:58 +00002493//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002494// Integer Predicates
2495//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002496
Eli Friedman0832dbc2008-06-28 06:23:08 +00002497unsigned ASTContext::getIntWidth(QualType T) {
2498 if (T == BoolTy)
2499 return 1;
2500 // At the moment, only bool has padding bits
2501 return (unsigned)getTypeSize(T);
2502}
2503
2504QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2505 assert(T->isSignedIntegerType() && "Unexpected type");
2506 if (const EnumType* ETy = T->getAsEnumType())
2507 T = ETy->getDecl()->getIntegerType();
2508 const BuiltinType* BTy = T->getAsBuiltinType();
2509 assert (BTy && "Unexpected signed integer type");
2510 switch (BTy->getKind()) {
2511 case BuiltinType::Char_S:
2512 case BuiltinType::SChar:
2513 return UnsignedCharTy;
2514 case BuiltinType::Short:
2515 return UnsignedShortTy;
2516 case BuiltinType::Int:
2517 return UnsignedIntTy;
2518 case BuiltinType::Long:
2519 return UnsignedLongTy;
2520 case BuiltinType::LongLong:
2521 return UnsignedLongLongTy;
2522 default:
2523 assert(0 && "Unexpected signed integer type");
2524 return QualType();
2525 }
2526}
2527
2528
2529//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002530// Serialization Support
2531//===----------------------------------------------------------------------===//
2532
Ted Kremenek738e6c02007-10-31 17:10:13 +00002533/// Emit - Serialize an ASTContext object to Bitcode.
2534void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002535 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002536 S.EmitRef(SourceMgr);
2537 S.EmitRef(Target);
2538 S.EmitRef(Idents);
2539 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002540
Ted Kremenek68228a92007-10-31 22:44:07 +00002541 // Emit the size of the type vector so that we can reserve that size
2542 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002543 S.EmitInt(Types.size());
2544
Ted Kremenek034a78c2007-11-13 22:02:55 +00002545 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2546 I!=E;++I)
2547 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002548
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002549 S.EmitOwnedPtr(TUDecl);
2550
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002551 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002552}
2553
Ted Kremenekacba3612007-11-13 00:25:37 +00002554ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002555
2556 // Read the language options.
2557 LangOptions LOpts;
2558 LOpts.Read(D);
2559
Ted Kremenek68228a92007-10-31 22:44:07 +00002560 SourceManager &SM = D.ReadRef<SourceManager>();
2561 TargetInfo &t = D.ReadRef<TargetInfo>();
2562 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2563 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002564
Ted Kremenek68228a92007-10-31 22:44:07 +00002565 unsigned size_reserve = D.ReadInt();
2566
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002567 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2568 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002569
Ted Kremenek034a78c2007-11-13 22:02:55 +00002570 for (unsigned i = 0; i < size_reserve; ++i)
2571 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002572
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002573 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2574
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002575 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002576
2577 return A;
2578}