blob: 95f95ceb5e4c410e03536cf2ab3fbd87ea4ca347 [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"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Chris Lattner2fda0ed2008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000036 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000037{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Chris Lattner4b009652007-07-25 00:24:17 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000048 Types.pop_back();
49 }
Eli Friedman65489b72008-05-27 03:08:09 +000050
Nuno Lopes355a8682008-12-17 22:30:25 +000051 {
52 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
53 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
54 while (I != E) {
55 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
56 delete R;
57 }
58 }
59
60 {
61 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
62 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
63 while (I != E) {
64 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
65 delete R;
66 }
67 }
68
69 {
70 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
71 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
72 while (I != E) {
73 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
74 R->Destroy(*this);
75 }
76 }
77
Eli Friedman65489b72008-05-27 03:08:09 +000078 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000079}
80
81void ASTContext::PrintStats() const {
82 fprintf(stderr, "*** AST Context Stats:\n");
83 fprintf(stderr, " %d types total.\n", (int)Types.size());
84 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000085 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000086 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
87
88 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000089 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
90 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000091 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000092
93 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
94 Type *T = Types[i];
95 if (isa<BuiltinType>(T))
96 ++NumBuiltin;
97 else if (isa<PointerType>(T))
98 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +000099 else if (isa<BlockPointerType>(T))
100 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000101 else if (isa<ReferenceType>(T))
102 ++NumReference;
103 else if (isa<ComplexType>(T))
104 ++NumComplex;
105 else if (isa<ArrayType>(T))
106 ++NumArray;
107 else if (isa<VectorType>(T))
108 ++NumVector;
109 else if (isa<FunctionTypeNoProto>(T))
110 ++NumFunctionNP;
111 else if (isa<FunctionTypeProto>(T))
112 ++NumFunctionP;
113 else if (isa<TypedefType>(T))
114 ++NumTypeName;
115 else if (TagType *TT = dyn_cast<TagType>(T)) {
116 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000117 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000118 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000119 case TagDecl::TK_struct: ++NumTagStruct; break;
120 case TagDecl::TK_union: ++NumTagUnion; break;
121 case TagDecl::TK_class: ++NumTagClass; break;
122 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000123 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000124 } else if (isa<ObjCInterfaceType>(T))
125 ++NumObjCInterfaces;
126 else if (isa<ObjCQualifiedInterfaceType>(T))
127 ++NumObjCQualifiedInterfaces;
128 else if (isa<ObjCQualifiedIdType>(T))
129 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000130 else if (isa<TypeOfType>(T))
131 ++NumTypeOfTypes;
132 else if (isa<TypeOfExpr>(T))
133 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000134 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000135 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000136 assert(0 && "Unknown type!");
137 }
138 }
139
140 fprintf(stderr, " %d builtin types\n", NumBuiltin);
141 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000142 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000143 fprintf(stderr, " %d reference types\n", NumReference);
144 fprintf(stderr, " %d complex types\n", NumComplex);
145 fprintf(stderr, " %d array types\n", NumArray);
146 fprintf(stderr, " %d vector types\n", NumVector);
147 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
148 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
149 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
150 fprintf(stderr, " %d tagged types\n", NumTagged);
151 fprintf(stderr, " %d struct types\n", NumTagStruct);
152 fprintf(stderr, " %d union types\n", NumTagUnion);
153 fprintf(stderr, " %d class types\n", NumTagClass);
154 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000155 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000156 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000157 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000158 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000159 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000160 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
161 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
162
Chris Lattner4b009652007-07-25 00:24:17 +0000163 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
164 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
165 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
166 NumFunctionP*sizeof(FunctionTypeProto)+
167 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000168 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
169 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000170}
171
172
173void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
174 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
175}
176
Chris Lattner4b009652007-07-25 00:24:17 +0000177void ASTContext::InitBuiltinTypes() {
178 assert(VoidTy.isNull() && "Context reinitialized?");
179
180 // C99 6.2.5p19.
181 InitBuiltinType(VoidTy, BuiltinType::Void);
182
183 // C99 6.2.5p2.
184 InitBuiltinType(BoolTy, BuiltinType::Bool);
185 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000186 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000187 InitBuiltinType(CharTy, BuiltinType::Char_S);
188 else
189 InitBuiltinType(CharTy, BuiltinType::Char_U);
190 // C99 6.2.5p4.
191 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
192 InitBuiltinType(ShortTy, BuiltinType::Short);
193 InitBuiltinType(IntTy, BuiltinType::Int);
194 InitBuiltinType(LongTy, BuiltinType::Long);
195 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
196
197 // C99 6.2.5p6.
198 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
199 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
200 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
201 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
202 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
203
204 // C99 6.2.5p10.
205 InitBuiltinType(FloatTy, BuiltinType::Float);
206 InitBuiltinType(DoubleTy, BuiltinType::Double);
207 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000208
209 // C++ 3.9.1p5
210 InitBuiltinType(WCharTy, BuiltinType::WChar);
211
Douglas Gregord2baafd2008-10-21 16:13:35 +0000212 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000213 InitBuiltinType(OverloadTy, BuiltinType::Overload);
214
215 // Placeholder type for type-dependent expressions whose type is
216 // completely unknown. No code should ever check a type against
217 // DependentTy and users should never see it; however, it is here to
218 // help diagnose failures to properly check for type-dependent
219 // expressions.
220 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000221
Chris Lattner4b009652007-07-25 00:24:17 +0000222 // C99 6.2.5p11.
223 FloatComplexTy = getComplexType(FloatTy);
224 DoubleComplexTy = getComplexType(DoubleTy);
225 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000226
Steve Naroff9d12c902007-10-15 14:41:52 +0000227 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000228 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000229 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000230 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000231 ClassStructType = 0;
232
Ted Kremenek42730c52008-01-07 19:49:32 +0000233 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000234
235 // void * type
236 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000237}
238
239//===----------------------------------------------------------------------===//
240// Type Sizing and Analysis
241//===----------------------------------------------------------------------===//
242
Chris Lattner2a674dc2008-06-30 18:32:54 +0000243/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
244/// scalar floating point type.
245const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
246 const BuiltinType *BT = T->getAsBuiltinType();
247 assert(BT && "Not a floating point type!");
248 switch (BT->getKind()) {
249 default: assert(0 && "Not a floating point type!");
250 case BuiltinType::Float: return Target.getFloatFormat();
251 case BuiltinType::Double: return Target.getDoubleFormat();
252 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
253 }
254}
255
256
Chris Lattner4b009652007-07-25 00:24:17 +0000257/// getTypeSize - Return the size of the specified type, in bits. This method
258/// does not work on incomplete types.
259std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000260ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000261 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000262 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000263 unsigned Align;
264 switch (T->getTypeClass()) {
265 case Type::TypeName: assert(0 && "Not a canonical type!");
266 case Type::FunctionNoProto:
267 case Type::FunctionProto:
268 default:
269 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000270 case Type::VariableArray:
271 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000272 case Type::DependentSizedArray:
273 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000274 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000275 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000276
Chris Lattner8cd0e932008-03-05 18:54:05 +0000277 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000278 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000279 Align = EltInfo.second;
280 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000281 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000282 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000283 case Type::Vector: {
284 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000285 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000286 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000287 // FIXME: This isn't right for unusual vectors
288 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000289 break;
290 }
291
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000292 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000293 switch (cast<BuiltinType>(T)->getKind()) {
294 default: assert(0 && "Unknown builtin type!");
295 case BuiltinType::Void:
296 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000297 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000298 Width = Target.getBoolWidth();
299 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000300 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000301 case BuiltinType::Char_S:
302 case BuiltinType::Char_U:
303 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000304 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000305 Width = Target.getCharWidth();
306 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000307 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000308 case BuiltinType::WChar:
309 Width = Target.getWCharWidth();
310 Align = Target.getWCharAlign();
311 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000312 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000313 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000314 Width = Target.getShortWidth();
315 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000316 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000318 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000319 Width = Target.getIntWidth();
320 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000321 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000322 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000323 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000324 Width = Target.getLongWidth();
325 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000326 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000327 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000328 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000329 Width = Target.getLongLongWidth();
330 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000331 break;
332 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000333 Width = Target.getFloatWidth();
334 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000335 break;
336 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000337 Width = Target.getDoubleWidth();
338 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000339 break;
340 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000341 Width = Target.getLongDoubleWidth();
342 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000344 }
345 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000346 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000347 // FIXME: Pointers into different addr spaces could have different sizes and
348 // alignment requirements: getPointerInfo should take an AddrSpace.
349 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000350 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000351 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000352 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000353 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000354 case Type::BlockPointer: {
355 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
356 Width = Target.getPointerWidth(AS);
357 Align = Target.getPointerAlign(AS);
358 break;
359 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000360 case Type::Pointer: {
361 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000362 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000363 Align = Target.getPointerAlign(AS);
364 break;
365 }
Chris Lattner4b009652007-07-25 00:24:17 +0000366 case Type::Reference:
367 // "When applied to a reference or a reference type, the result is the size
368 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000369 // FIXME: This is wrong for struct layout: a reference in a struct has
370 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000371 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000372
373 case Type::Complex: {
374 // Complex types have the same alignment as their elements, but twice the
375 // size.
376 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000377 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000378 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000379 Align = EltInfo.second;
380 break;
381 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000382 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000383 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000384 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
385 Width = Layout.getSize();
386 Align = Layout.getAlignment();
387 break;
388 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000389 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000390 const TagType *TT = cast<TagType>(T);
391
392 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000393 Width = 1;
394 Align = 1;
395 break;
396 }
397
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000398 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000399 return getTypeInfo(ET->getDecl()->getIntegerType());
400
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000401 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000402 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
403 Width = Layout.getSize();
404 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000405 break;
406 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000407 }
Chris Lattner4b009652007-07-25 00:24:17 +0000408
409 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000410 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000411}
412
Devang Patelbfe323c2008-06-04 21:22:16 +0000413/// LayoutField - Field layout.
414void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000415 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000416 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000417 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000418 uint64_t FieldOffset = IsUnion ? 0 : Size;
419 uint64_t FieldSize;
420 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000421
422 // FIXME: Should this override struct packing? Probably we want to
423 // take the minimum?
424 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
425 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000426
427 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
428 // TODO: Need to check this algorithm on other targets!
429 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000430 FieldSize =
431 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000432
433 std::pair<uint64_t, unsigned> FieldInfo =
434 Context.getTypeInfo(FD->getType());
435 uint64_t TypeSize = FieldInfo.first;
436
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000437 // Determine the alignment of this bitfield. The packing
438 // attributes define a maximum and the alignment attribute defines
439 // a minimum.
440 // FIXME: What is the right behavior when the specified alignment
441 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000442 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000443 if (FieldPacking)
444 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000445 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
446 FieldAlign = std::max(FieldAlign, AA->getAlignment());
447
448 // Check if we need to add padding to give the field the correct
449 // alignment.
450 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
451 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
452
453 // Padding members don't affect overall alignment
454 if (!FD->getIdentifier())
455 FieldAlign = 1;
456 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000457 if (FD->getType()->isIncompleteArrayType()) {
458 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000459 // query getTypeInfo about these, so we figure it out here.
460 // Flexible array members don't have any size, but they
461 // have to be aligned appropriately for their element type.
462 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000463 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000464 FieldAlign = Context.getTypeAlign(ATy->getElementType());
465 } else {
466 std::pair<uint64_t, unsigned> FieldInfo =
467 Context.getTypeInfo(FD->getType());
468 FieldSize = FieldInfo.first;
469 FieldAlign = FieldInfo.second;
470 }
471
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000472 // Determine the alignment of this bitfield. The packing
473 // attributes define a maximum and the alignment attribute defines
474 // a minimum. Additionally, the packing alignment must be at least
475 // a byte for non-bitfields.
476 //
477 // FIXME: What is the right behavior when the specified alignment
478 // is smaller than the specified packing?
479 if (FieldPacking)
480 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000481 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
482 FieldAlign = std::max(FieldAlign, AA->getAlignment());
483
484 // Round up the current record size to the field's alignment boundary.
485 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
486 }
487
488 // Place this field at the current location.
489 FieldOffsets[FieldNo] = FieldOffset;
490
491 // Reserve space for this field.
492 if (IsUnion) {
493 Size = std::max(Size, FieldSize);
494 } else {
495 Size = FieldOffset + FieldSize;
496 }
497
498 // Remember max struct/class alignment.
499 Alignment = std::max(Alignment, FieldAlign);
500}
501
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000502static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
503 std::vector<FieldDecl*> &Fields) {
504 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
505 if (SuperClass)
506 CollectObjCIvars(SuperClass, Fields);
507 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
508 E = OI->ivar_end(); I != E; ++I) {
509 ObjCIvarDecl *IVDecl = (*I);
510 if (!IVDecl->isInvalidDecl())
511 Fields.push_back(cast<FieldDecl>(IVDecl));
512 }
513}
514
515/// addRecordToClass - produces record info. for the class for its
516/// ivars and all those inherited.
517///
518const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
519{
520 const RecordDecl *&RD = ASTRecordForInterface[D];
521 if (RD)
522 return RD;
523 std::vector<FieldDecl*> RecFields;
524 CollectObjCIvars(D, RecFields);
525 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
526 D->getLocation(),
527 D->getIdentifier());
528 /// FIXME! Can do collection of ivars and adding to the record while
529 /// doing it.
530 for (unsigned int i = 0; i != RecFields.size(); i++) {
531 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
532 RecFields[i]->getLocation(),
533 RecFields[i]->getIdentifier(),
534 RecFields[i]->getType(),
535 RecFields[i]->getBitWidth(), false, 0);
536 NewRD->addDecl(*this, Field);
537 }
538 NewRD->completeDefinition(*this);
539 RD = NewRD;
540 return RD;
541}
Devang Patel4b6bf702008-06-04 21:54:36 +0000542
Fariborz Jahanianea944842008-12-18 17:29:46 +0000543/// setFieldDecl - maps a field for the given Ivar reference node.
544//
545void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
546 const ObjCIvarDecl *Ivar,
547 const ObjCIvarRefExpr *MRef) {
548 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
549 lookupFieldDeclForIvar(*this, Ivar);
550 ASTFieldForIvarRef[MRef] = FD;
551}
552
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000553/// getASTObjcInterfaceLayout - Get or compute information about the layout of
554/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000555/// position information.
556const ASTRecordLayout &
557ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
558 // Look up this layout, if already laid out, return what we have.
559 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
560 if (Entry) return *Entry;
561
562 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
563 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000564 ASTRecordLayout *NewEntry = NULL;
565 unsigned FieldCount = D->ivar_size();
566 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
567 FieldCount++;
568 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
569 unsigned Alignment = SL.getAlignment();
570 uint64_t Size = SL.getSize();
571 NewEntry = new ASTRecordLayout(Size, Alignment);
572 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000573 // Super class is at the beginning of the layout.
574 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000575 } else {
576 NewEntry = new ASTRecordLayout();
577 NewEntry->InitializeLayout(FieldCount);
578 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000579 Entry = NewEntry;
580
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000581 unsigned StructPacking = 0;
582 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
583 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000584
585 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
586 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
587 AA->getAlignment()));
588
589 // Layout each ivar sequentially.
590 unsigned i = 0;
591 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
592 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
593 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000594 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000595 }
596
597 // Finally, round the size of the total struct up to the alignment of the
598 // struct itself.
599 NewEntry->FinalizeLayout();
600 return *NewEntry;
601}
602
Devang Patel7a78e432007-11-01 19:11:01 +0000603/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000604/// specified record (struct/union/class), which indicates its size and field
605/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000606const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000607 D = D->getDefinition(*this);
608 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000609
Chris Lattner4b009652007-07-25 00:24:17 +0000610 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000611 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000612 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000613
Devang Patel7a78e432007-11-01 19:11:01 +0000614 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
615 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
616 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000617 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000618
Douglas Gregor39677622008-12-11 20:41:00 +0000619 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000620 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000621 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000622
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000623 unsigned StructPacking = 0;
624 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
625 StructPacking = PA->getAlignment();
626
Eli Friedman5949a022008-05-30 09:31:38 +0000627 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000628 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
629 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000630
Eli Friedman5949a022008-05-30 09:31:38 +0000631 // Layout each field, for now, just sequentially, respecting alignment. In
632 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000633 unsigned FieldIdx = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000634 for (RecordDecl::field_const_iterator Field = D->field_begin(),
635 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000636 Field != FieldEnd; (void)++Field, ++FieldIdx)
637 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000638
639 // Finally, round the size of the total struct up to the alignment of the
640 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000641 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000642 return *NewEntry;
643}
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645//===----------------------------------------------------------------------===//
646// Type creation/memoization methods
647//===----------------------------------------------------------------------===//
648
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000649QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000650 QualType CanT = getCanonicalType(T);
651 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000652 return T;
653
654 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
655 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000656 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000657 "Type is already address space qualified");
658
659 // Check if we've already instantiated an address space qual'd type of this
660 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000661 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000662 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000663 void *InsertPos = 0;
664 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
665 return QualType(ASQy, 0);
666
667 // If the base type isn't canonical, this won't be a canonical type either,
668 // so fill in the canonical type field.
669 QualType Canonical;
670 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000671 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000672
673 // Get the new insert position for the node we care about.
674 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000675 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000676 }
Chris Lattner35fef522008-02-20 20:55:12 +0000677 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000678 ASQualTypes.InsertNode(New, InsertPos);
679 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000680 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000681}
682
Chris Lattner4b009652007-07-25 00:24:17 +0000683
684/// getComplexType - Return the uniqued reference to the type for a complex
685/// number with the specified element type.
686QualType ASTContext::getComplexType(QualType T) {
687 // Unique pointers, to guarantee there is only one pointer of a particular
688 // structure.
689 llvm::FoldingSetNodeID ID;
690 ComplexType::Profile(ID, T);
691
692 void *InsertPos = 0;
693 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
694 return QualType(CT, 0);
695
696 // If the pointee type isn't canonical, this won't be a canonical type either,
697 // so fill in the canonical type field.
698 QualType Canonical;
699 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000700 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000701
702 // Get the new insert position for the node we care about.
703 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000704 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000705 }
706 ComplexType *New = new ComplexType(T, Canonical);
707 Types.push_back(New);
708 ComplexTypes.InsertNode(New, InsertPos);
709 return QualType(New, 0);
710}
711
712
713/// getPointerType - Return the uniqued reference to the type for a pointer to
714/// the specified type.
715QualType ASTContext::getPointerType(QualType T) {
716 // Unique pointers, to guarantee there is only one pointer of a particular
717 // structure.
718 llvm::FoldingSetNodeID ID;
719 PointerType::Profile(ID, T);
720
721 void *InsertPos = 0;
722 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
723 return QualType(PT, 0);
724
725 // If the pointee type isn't canonical, this won't be a canonical type either,
726 // so fill in the canonical type field.
727 QualType Canonical;
728 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000729 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000730
731 // Get the new insert position for the node we care about.
732 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000733 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000734 }
735 PointerType *New = new PointerType(T, Canonical);
736 Types.push_back(New);
737 PointerTypes.InsertNode(New, InsertPos);
738 return QualType(New, 0);
739}
740
Steve Naroff7aa54752008-08-27 16:04:49 +0000741/// getBlockPointerType - Return the uniqued reference to the type for
742/// a pointer to the specified block.
743QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000744 assert(T->isFunctionType() && "block of function types only");
745 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000746 // structure.
747 llvm::FoldingSetNodeID ID;
748 BlockPointerType::Profile(ID, T);
749
750 void *InsertPos = 0;
751 if (BlockPointerType *PT =
752 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
753 return QualType(PT, 0);
754
Steve Narofffd5b19d2008-08-28 19:20:44 +0000755 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000756 // type either so fill in the canonical type field.
757 QualType Canonical;
758 if (!T->isCanonical()) {
759 Canonical = getBlockPointerType(getCanonicalType(T));
760
761 // Get the new insert position for the node we care about.
762 BlockPointerType *NewIP =
763 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000764 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000765 }
766 BlockPointerType *New = new BlockPointerType(T, Canonical);
767 Types.push_back(New);
768 BlockPointerTypes.InsertNode(New, InsertPos);
769 return QualType(New, 0);
770}
771
Chris Lattner4b009652007-07-25 00:24:17 +0000772/// getReferenceType - Return the uniqued reference to the type for a reference
773/// to the specified type.
774QualType ASTContext::getReferenceType(QualType T) {
775 // Unique pointers, to guarantee there is only one pointer of a particular
776 // structure.
777 llvm::FoldingSetNodeID ID;
778 ReferenceType::Profile(ID, T);
779
780 void *InsertPos = 0;
781 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
782 return QualType(RT, 0);
783
784 // If the referencee type isn't canonical, this won't be a canonical type
785 // either, so fill in the canonical type field.
786 QualType Canonical;
787 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000788 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000789
790 // Get the new insert position for the node we care about.
791 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000792 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000793 }
794
795 ReferenceType *New = new ReferenceType(T, Canonical);
796 Types.push_back(New);
797 ReferenceTypes.InsertNode(New, InsertPos);
798 return QualType(New, 0);
799}
800
Steve Naroff83c13012007-08-30 01:06:46 +0000801/// getConstantArrayType - Return the unique reference to the type for an
802/// array of the specified element type.
803QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000804 const llvm::APInt &ArySize,
805 ArrayType::ArraySizeModifier ASM,
806 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000807 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000808 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000809
810 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000811 if (ConstantArrayType *ATP =
812 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000813 return QualType(ATP, 0);
814
815 // If the element type isn't canonical, this won't be a canonical type either,
816 // so fill in the canonical type field.
817 QualType Canonical;
818 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000819 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000820 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000821 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000822 ConstantArrayType *NewIP =
823 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000824 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000825 }
826
Steve Naroff24c9b982007-08-30 18:10:14 +0000827 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
828 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000829 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000830 Types.push_back(New);
831 return QualType(New, 0);
832}
833
Steve Naroffe2579e32007-08-30 18:14:25 +0000834/// getVariableArrayType - Returns a non-unique reference to the type for a
835/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000836QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
837 ArrayType::ArraySizeModifier ASM,
838 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000839 // Since we don't unique expressions, it isn't possible to unique VLA's
840 // that have an expression provided for their size.
841
842 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
843 ASM, EltTypeQuals);
844
845 VariableArrayTypes.push_back(New);
846 Types.push_back(New);
847 return QualType(New, 0);
848}
849
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000850/// getDependentSizedArrayType - Returns a non-unique reference to
851/// the type for a dependently-sized array of the specified element
852/// type. FIXME: We will need these to be uniqued, or at least
853/// comparable, at some point.
854QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
855 ArrayType::ArraySizeModifier ASM,
856 unsigned EltTypeQuals) {
857 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
858 "Size must be type- or value-dependent!");
859
860 // Since we don't unique expressions, it isn't possible to unique
861 // dependently-sized array types.
862
863 DependentSizedArrayType *New
864 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
865 ASM, EltTypeQuals);
866
867 DependentSizedArrayTypes.push_back(New);
868 Types.push_back(New);
869 return QualType(New, 0);
870}
871
Eli Friedman8ff07782008-02-15 18:16:39 +0000872QualType ASTContext::getIncompleteArrayType(QualType EltTy,
873 ArrayType::ArraySizeModifier ASM,
874 unsigned EltTypeQuals) {
875 llvm::FoldingSetNodeID ID;
876 IncompleteArrayType::Profile(ID, EltTy);
877
878 void *InsertPos = 0;
879 if (IncompleteArrayType *ATP =
880 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
881 return QualType(ATP, 0);
882
883 // If the element type isn't canonical, this won't be a canonical type
884 // either, so fill in the canonical type field.
885 QualType Canonical;
886
887 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000888 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000889 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000890
891 // Get the new insert position for the node we care about.
892 IncompleteArrayType *NewIP =
893 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000894 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000895 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000896
897 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
898 ASM, EltTypeQuals);
899
900 IncompleteArrayTypes.InsertNode(New, InsertPos);
901 Types.push_back(New);
902 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000903}
904
Chris Lattner4b009652007-07-25 00:24:17 +0000905/// getVectorType - Return the unique reference to a vector type of
906/// the specified element type and size. VectorType must be a built-in type.
907QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
908 BuiltinType *baseType;
909
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000910 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000911 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
912
913 // Check if we've already instantiated a vector of this type.
914 llvm::FoldingSetNodeID ID;
915 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
916 void *InsertPos = 0;
917 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
918 return QualType(VTP, 0);
919
920 // If the element type isn't canonical, this won't be a canonical type either,
921 // so fill in the canonical type field.
922 QualType Canonical;
923 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000924 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000925
926 // Get the new insert position for the node we care about.
927 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000928 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000929 }
930 VectorType *New = new VectorType(vecType, NumElts, Canonical);
931 VectorTypes.InsertNode(New, InsertPos);
932 Types.push_back(New);
933 return QualType(New, 0);
934}
935
Nate Begemanaf6ed502008-04-18 23:10:10 +0000936/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000937/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000938QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000939 BuiltinType *baseType;
940
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000941 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000942 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000943
944 // Check if we've already instantiated a vector of this type.
945 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000946 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000947 void *InsertPos = 0;
948 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
949 return QualType(VTP, 0);
950
951 // If the element type isn't canonical, this won't be a canonical type either,
952 // so fill in the canonical type field.
953 QualType Canonical;
954 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000955 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000956
957 // Get the new insert position for the node we care about.
958 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000959 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000960 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000961 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 VectorTypes.InsertNode(New, InsertPos);
963 Types.push_back(New);
964 return QualType(New, 0);
965}
966
967/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
968///
969QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
970 // Unique functions, to guarantee there is only one function of a particular
971 // structure.
972 llvm::FoldingSetNodeID ID;
973 FunctionTypeNoProto::Profile(ID, ResultTy);
974
975 void *InsertPos = 0;
976 if (FunctionTypeNoProto *FT =
977 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
978 return QualType(FT, 0);
979
980 QualType Canonical;
981 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000982 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000983
984 // Get the new insert position for the node we care about.
985 FunctionTypeNoProto *NewIP =
986 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000987 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000988 }
989
990 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
991 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000992 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000993 return QualType(New, 0);
994}
995
996/// getFunctionType - Return a normal function type with a typed argument
997/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000998QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000999 unsigned NumArgs, bool isVariadic,
1000 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001001 // Unique functions, to guarantee there is only one function of a particular
1002 // structure.
1003 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001004 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1005 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001006
1007 void *InsertPos = 0;
1008 if (FunctionTypeProto *FTP =
1009 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1010 return QualType(FTP, 0);
1011
1012 // Determine whether the type being created is already canonical or not.
1013 bool isCanonical = ResultTy->isCanonical();
1014 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1015 if (!ArgArray[i]->isCanonical())
1016 isCanonical = false;
1017
1018 // If this type isn't canonical, get the canonical version of it.
1019 QualType Canonical;
1020 if (!isCanonical) {
1021 llvm::SmallVector<QualType, 16> CanonicalArgs;
1022 CanonicalArgs.reserve(NumArgs);
1023 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001024 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001025
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001026 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001027 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001028 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001029
1030 // Get the new insert position for the node we care about.
1031 FunctionTypeProto *NewIP =
1032 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001033 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001034 }
1035
1036 // FunctionTypeProto objects are not allocated with new because they have a
1037 // variable size array (for parameter types) at the end of them.
1038 FunctionTypeProto *FTP =
1039 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
1040 NumArgs*sizeof(QualType));
1041 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001042 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001043 Types.push_back(FTP);
1044 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1045 return QualType(FTP, 0);
1046}
1047
Douglas Gregor1d661552008-04-13 21:07:44 +00001048/// getTypeDeclType - Return the unique reference to the type for the
1049/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001050QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001051 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001052 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1053
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001054 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001055 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +00001056 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1057 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001058 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001059 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001060
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001061 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +00001062 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1063 : new CXXRecordType(CXXRecord);
1064 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001065 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +00001066 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1067 : new RecordType(Record);
1068 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001069 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregorae644892008-12-15 16:32:14 +00001070 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1071 : new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001072 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001073 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001074
Ted Kremenek46a837c2008-09-05 17:16:31 +00001075 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001076 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001077}
1078
Ted Kremenek46a837c2008-09-05 17:16:31 +00001079void ASTContext::setTagDefinition(TagDecl* D) {
1080 assert (D->isDefinition());
Douglas Gregorae644892008-12-15 16:32:14 +00001081 cast<TagType>(D->TypeForDecl)->decl = D;
Ted Kremenek46a837c2008-09-05 17:16:31 +00001082}
1083
Chris Lattner4b009652007-07-25 00:24:17 +00001084/// getTypedefType - Return the unique reference to the type for the
1085/// specified typename decl.
1086QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1087 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1088
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001089 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001090 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001091 Types.push_back(Decl->TypeForDecl);
1092 return QualType(Decl->TypeForDecl, 0);
1093}
1094
Douglas Gregordd861062008-12-05 18:15:24 +00001095/// getTemplateTypeParmType - Return the unique reference to the type
1096/// for the specified template type parameter declaration.
1097QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1098 if (!Decl->TypeForDecl) {
1099 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1100 Types.push_back(Decl->TypeForDecl);
1101 }
1102 return QualType(Decl->TypeForDecl, 0);
1103}
1104
Ted Kremenek42730c52008-01-07 19:49:32 +00001105/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001106/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001107QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001108 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1109
Ted Kremenek42730c52008-01-07 19:49:32 +00001110 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001111 Types.push_back(Decl->TypeForDecl);
1112 return QualType(Decl->TypeForDecl, 0);
1113}
1114
Chris Lattnere1352302008-04-07 04:56:42 +00001115/// CmpProtocolNames - Comparison predicate for sorting protocols
1116/// alphabetically.
1117static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1118 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001119 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001120}
1121
1122static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1123 unsigned &NumProtocols) {
1124 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1125
1126 // Sort protocols, keyed by name.
1127 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1128
1129 // Remove duplicates.
1130 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1131 NumProtocols = ProtocolsEnd-Protocols;
1132}
1133
1134
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001135/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1136/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001137QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1138 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001139 // Sort the protocol list alphabetically to canonicalize it.
1140 SortAndUniqueProtocols(Protocols, NumProtocols);
1141
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001142 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001143 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001144
1145 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001146 if (ObjCQualifiedInterfaceType *QT =
1147 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001148 return QualType(QT, 0);
1149
1150 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001151 ObjCQualifiedInterfaceType *QType =
1152 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001153 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001154 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001155 return QualType(QType, 0);
1156}
1157
Chris Lattnere1352302008-04-07 04:56:42 +00001158/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1159/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001160QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001161 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001162 // Sort the protocol list alphabetically to canonicalize it.
1163 SortAndUniqueProtocols(Protocols, NumProtocols);
1164
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001165 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001166 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001167
1168 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001169 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001170 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001171 return QualType(QT, 0);
1172
1173 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001174 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001175 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001176 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001177 return QualType(QType, 0);
1178}
1179
Steve Naroff0604dd92007-08-01 18:02:17 +00001180/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1181/// TypeOfExpr AST's (since expression's are never shared). For example,
1182/// multiple declarations that refer to "typeof(x)" all contain different
1183/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1184/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001185QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001186 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001187 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1188 Types.push_back(toe);
1189 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001190}
1191
Steve Naroff0604dd92007-08-01 18:02:17 +00001192/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1193/// TypeOfType AST's. The only motivation to unique these nodes would be
1194/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1195/// an issue. This doesn't effect the type checker, since it operates
1196/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001197QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001198 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001199 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1200 Types.push_back(tot);
1201 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001202}
1203
Chris Lattner4b009652007-07-25 00:24:17 +00001204/// getTagDeclType - Return the unique reference to the type for the
1205/// specified TagDecl (struct/union/class/enum) decl.
1206QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001207 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001208 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001209}
1210
1211/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1212/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1213/// needs to agree with the definition in <stddef.h>.
1214QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001215 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001216}
1217
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001218/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001219/// width of characters in wide strings, The value is target dependent and
1220/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001221QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001222 if (LangOpts.CPlusPlus)
1223 return WCharTy;
1224
Douglas Gregorc6507e42008-11-03 14:12:49 +00001225 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1226 // wide-character type?
1227 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001228}
1229
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001230/// getSignedWCharType - Return the type of "signed wchar_t".
1231/// Used when in C++, as a GCC extension.
1232QualType ASTContext::getSignedWCharType() const {
1233 // FIXME: derive from "Target" ?
1234 return WCharTy;
1235}
1236
1237/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1238/// Used when in C++, as a GCC extension.
1239QualType ASTContext::getUnsignedWCharType() const {
1240 // FIXME: derive from "Target" ?
1241 return UnsignedIntTy;
1242}
1243
Chris Lattner4b009652007-07-25 00:24:17 +00001244/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1245/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1246QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001247 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001248}
1249
Chris Lattner19eb97e2008-04-02 05:18:44 +00001250//===----------------------------------------------------------------------===//
1251// Type Operators
1252//===----------------------------------------------------------------------===//
1253
Chris Lattner3dae6f42008-04-06 22:41:35 +00001254/// getCanonicalType - Return the canonical (structural) type corresponding to
1255/// the specified potentially non-canonical type. The non-canonical version
1256/// of a type may have many "decorated" versions of types. Decorators can
1257/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1258/// to be free of any of these, allowing two canonical types to be compared
1259/// for exact equality with a simple pointer comparison.
1260QualType ASTContext::getCanonicalType(QualType T) {
1261 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001262
1263 // If the result has type qualifiers, make sure to canonicalize them as well.
1264 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1265 if (TypeQuals == 0) return CanType;
1266
1267 // If the type qualifiers are on an array type, get the canonical type of the
1268 // array with the qualifiers applied to the element type.
1269 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1270 if (!AT)
1271 return CanType.getQualifiedType(TypeQuals);
1272
1273 // Get the canonical version of the element with the extra qualifiers on it.
1274 // This can recursively sink qualifiers through multiple levels of arrays.
1275 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1276 NewEltTy = getCanonicalType(NewEltTy);
1277
1278 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1279 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1280 CAT->getIndexTypeQualifier());
1281 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1282 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1283 IAT->getIndexTypeQualifier());
1284
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001285 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1286 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1287 DSAT->getSizeModifier(),
1288 DSAT->getIndexTypeQualifier());
1289
Chris Lattnera1923f62008-08-04 07:31:14 +00001290 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1291 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1292 VAT->getSizeModifier(),
1293 VAT->getIndexTypeQualifier());
1294}
1295
1296
1297const ArrayType *ASTContext::getAsArrayType(QualType T) {
1298 // Handle the non-qualified case efficiently.
1299 if (T.getCVRQualifiers() == 0) {
1300 // Handle the common positive case fast.
1301 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1302 return AT;
1303 }
1304
1305 // Handle the common negative case fast, ignoring CVR qualifiers.
1306 QualType CType = T->getCanonicalTypeInternal();
1307
1308 // Make sure to look through type qualifiers (like ASQuals) for the negative
1309 // test.
1310 if (!isa<ArrayType>(CType) &&
1311 !isa<ArrayType>(CType.getUnqualifiedType()))
1312 return 0;
1313
1314 // Apply any CVR qualifiers from the array type to the element type. This
1315 // implements C99 6.7.3p8: "If the specification of an array type includes
1316 // any type qualifiers, the element type is so qualified, not the array type."
1317
1318 // If we get here, we either have type qualifiers on the type, or we have
1319 // sugar such as a typedef in the way. If we have type qualifiers on the type
1320 // we must propagate them down into the elemeng type.
1321 unsigned CVRQuals = T.getCVRQualifiers();
1322 unsigned AddrSpace = 0;
1323 Type *Ty = T.getTypePtr();
1324
1325 // Rip through ASQualType's and typedefs to get to a concrete type.
1326 while (1) {
1327 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1328 AddrSpace = ASQT->getAddressSpace();
1329 Ty = ASQT->getBaseType();
1330 } else {
1331 T = Ty->getDesugaredType();
1332 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1333 break;
1334 CVRQuals |= T.getCVRQualifiers();
1335 Ty = T.getTypePtr();
1336 }
1337 }
1338
1339 // If we have a simple case, just return now.
1340 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1341 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1342 return ATy;
1343
1344 // Otherwise, we have an array and we have qualifiers on it. Push the
1345 // qualifiers into the array element type and return a new array type.
1346 // Get the canonical version of the element with the extra qualifiers on it.
1347 // This can recursively sink qualifiers through multiple levels of arrays.
1348 QualType NewEltTy = ATy->getElementType();
1349 if (AddrSpace)
1350 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1351 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1352
1353 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1354 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1355 CAT->getSizeModifier(),
1356 CAT->getIndexTypeQualifier()));
1357 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1358 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1359 IAT->getSizeModifier(),
1360 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001361
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001362 if (const DependentSizedArrayType *DSAT
1363 = dyn_cast<DependentSizedArrayType>(ATy))
1364 return cast<ArrayType>(
1365 getDependentSizedArrayType(NewEltTy,
1366 DSAT->getSizeExpr(),
1367 DSAT->getSizeModifier(),
1368 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001369
Chris Lattnera1923f62008-08-04 07:31:14 +00001370 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1371 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1372 VAT->getSizeModifier(),
1373 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001374}
1375
1376
Chris Lattner19eb97e2008-04-02 05:18:44 +00001377/// getArrayDecayedType - Return the properly qualified result of decaying the
1378/// specified array type to a pointer. This operation is non-trivial when
1379/// handling typedefs etc. The canonical type of "T" must be an array type,
1380/// this returns a pointer to a properly qualified element of the array.
1381///
1382/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1383QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001384 // Get the element type with 'getAsArrayType' so that we don't lose any
1385 // typedefs in the element type of the array. This also handles propagation
1386 // of type qualifiers from the array type into the element type if present
1387 // (C99 6.7.3p8).
1388 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1389 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001390
Chris Lattnera1923f62008-08-04 07:31:14 +00001391 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001392
1393 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001394 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001395}
1396
Chris Lattner4b009652007-07-25 00:24:17 +00001397/// getFloatingRank - Return a relative rank for floating point types.
1398/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001399static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001400 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001401 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001402
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001403 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001404 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001405 case BuiltinType::Float: return FloatRank;
1406 case BuiltinType::Double: return DoubleRank;
1407 case BuiltinType::LongDouble: return LongDoubleRank;
1408 }
1409}
1410
Steve Narofffa0c4532007-08-27 01:41:48 +00001411/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1412/// point or a complex type (based on typeDomain/typeSize).
1413/// 'typeDomain' is a real floating point or complex type.
1414/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001415QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1416 QualType Domain) const {
1417 FloatingRank EltRank = getFloatingRank(Size);
1418 if (Domain->isComplexType()) {
1419 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001420 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001421 case FloatRank: return FloatComplexTy;
1422 case DoubleRank: return DoubleComplexTy;
1423 case LongDoubleRank: return LongDoubleComplexTy;
1424 }
Chris Lattner4b009652007-07-25 00:24:17 +00001425 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001426
1427 assert(Domain->isRealFloatingType() && "Unknown domain!");
1428 switch (EltRank) {
1429 default: assert(0 && "getFloatingRank(): illegal value for rank");
1430 case FloatRank: return FloatTy;
1431 case DoubleRank: return DoubleTy;
1432 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001433 }
Chris Lattner4b009652007-07-25 00:24:17 +00001434}
1435
Chris Lattner51285d82008-04-06 23:55:33 +00001436/// getFloatingTypeOrder - Compare the rank of the two specified floating
1437/// point types, ignoring the domain of the type (i.e. 'double' ==
1438/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1439/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001440int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1441 FloatingRank LHSR = getFloatingRank(LHS);
1442 FloatingRank RHSR = getFloatingRank(RHS);
1443
1444 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001445 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001446 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001447 return 1;
1448 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001449}
1450
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001451/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1452/// routine will assert if passed a built-in type that isn't an integer or enum,
1453/// or if it is not canonicalized.
1454static unsigned getIntegerRank(Type *T) {
1455 assert(T->isCanonical() && "T should be canonicalized");
1456 if (isa<EnumType>(T))
1457 return 4;
1458
1459 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001460 default: assert(0 && "getIntegerRank(): not a built-in integer");
1461 case BuiltinType::Bool:
1462 return 1;
1463 case BuiltinType::Char_S:
1464 case BuiltinType::Char_U:
1465 case BuiltinType::SChar:
1466 case BuiltinType::UChar:
1467 return 2;
1468 case BuiltinType::Short:
1469 case BuiltinType::UShort:
1470 return 3;
1471 case BuiltinType::Int:
1472 case BuiltinType::UInt:
1473 return 4;
1474 case BuiltinType::Long:
1475 case BuiltinType::ULong:
1476 return 5;
1477 case BuiltinType::LongLong:
1478 case BuiltinType::ULongLong:
1479 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001480 }
1481}
1482
Chris Lattner51285d82008-04-06 23:55:33 +00001483/// getIntegerTypeOrder - Returns the highest ranked integer type:
1484/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1485/// LHS < RHS, return -1.
1486int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001487 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1488 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001489 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001490
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001491 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1492 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001493
Chris Lattner51285d82008-04-06 23:55:33 +00001494 unsigned LHSRank = getIntegerRank(LHSC);
1495 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001496
Chris Lattner51285d82008-04-06 23:55:33 +00001497 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1498 if (LHSRank == RHSRank) return 0;
1499 return LHSRank > RHSRank ? 1 : -1;
1500 }
Chris Lattner4b009652007-07-25 00:24:17 +00001501
Chris Lattner51285d82008-04-06 23:55:33 +00001502 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1503 if (LHSUnsigned) {
1504 // If the unsigned [LHS] type is larger, return it.
1505 if (LHSRank >= RHSRank)
1506 return 1;
1507
1508 // If the signed type can represent all values of the unsigned type, it
1509 // wins. Because we are dealing with 2's complement and types that are
1510 // powers of two larger than each other, this is always safe.
1511 return -1;
1512 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001513
Chris Lattner51285d82008-04-06 23:55:33 +00001514 // If the unsigned [RHS] type is larger, return it.
1515 if (RHSRank >= LHSRank)
1516 return -1;
1517
1518 // If the signed type can represent all values of the unsigned type, it
1519 // wins. Because we are dealing with 2's complement and types that are
1520 // powers of two larger than each other, this is always safe.
1521 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001522}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001523
1524// getCFConstantStringType - Return the type used for constant CFStrings.
1525QualType ASTContext::getCFConstantStringType() {
1526 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001527 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001528 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001529 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001530 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001531
1532 // const int *isa;
1533 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001534 // int flags;
1535 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001536 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001537 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001538 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001539 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001540
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001541 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001542 for (unsigned i = 0; i < 4; ++i) {
1543 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1544 SourceLocation(), 0,
1545 FieldTypes[i], /*BitWidth=*/0,
1546 /*Mutable=*/false, /*PrevDecl=*/0);
1547 CFConstantStringTypeDecl->addDecl(*this, Field, true);
1548 }
1549
1550 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001551 }
1552
1553 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001554}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001555
Anders Carlssonf58cac72008-08-30 19:34:46 +00001556QualType ASTContext::getObjCFastEnumerationStateType()
1557{
1558 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001559 ObjCFastEnumerationStateTypeDecl =
1560 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1561 &Idents.get("__objcFastEnumerationState"));
1562
Anders Carlssonf58cac72008-08-30 19:34:46 +00001563 QualType FieldTypes[] = {
1564 UnsignedLongTy,
1565 getPointerType(ObjCIdType),
1566 getPointerType(UnsignedLongTy),
1567 getConstantArrayType(UnsignedLongTy,
1568 llvm::APInt(32, 5), ArrayType::Normal, 0)
1569 };
1570
Douglas Gregor8acb7272008-12-11 16:49:14 +00001571 for (size_t i = 0; i < 4; ++i) {
1572 FieldDecl *Field = FieldDecl::Create(*this,
1573 ObjCFastEnumerationStateTypeDecl,
1574 SourceLocation(), 0,
1575 FieldTypes[i], /*BitWidth=*/0,
1576 /*Mutable=*/false, /*PrevDecl=*/0);
1577 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field, true);
1578 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001579
Douglas Gregor8acb7272008-12-11 16:49:14 +00001580 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001581 }
1582
1583 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1584}
1585
Anders Carlssone3f02572007-10-29 06:33:42 +00001586// This returns true if a type has been typedefed to BOOL:
1587// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001588static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001589 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001590 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1591 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001592
1593 return false;
1594}
1595
Ted Kremenek42730c52008-01-07 19:49:32 +00001596/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001597/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001598int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001599 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001600
1601 // Make all integer and enum types at least as large as an int
1602 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001603 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001604 // Treat arrays as pointers, since that's how they're passed in.
1605 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001606 sz = getTypeSize(VoidPtrTy);
1607 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001608}
1609
Ted Kremenek42730c52008-01-07 19:49:32 +00001610/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001611/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001612void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001613 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001614 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001615 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001617 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001618 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001619 // Compute size of all parameters.
1620 // Start with computing size of a pointer in number of bytes.
1621 // FIXME: There might(should) be a better way of doing this computation!
1622 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001623 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001624 // The first two arguments (self and _cmd) are pointers; account for
1625 // their size.
1626 int ParmOffset = 2 * PtrSize;
1627 int NumOfParams = Decl->getNumParams();
1628 for (int i = 0; i < NumOfParams; i++) {
1629 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001630 int sz = getObjCEncodingTypeSize (PType);
1631 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001632 ParmOffset += sz;
1633 }
1634 S += llvm::utostr(ParmOffset);
1635 S += "@0:";
1636 S += llvm::utostr(PtrSize);
1637
1638 // Argument types.
1639 ParmOffset = 2 * PtrSize;
1640 for (int i = 0; i < NumOfParams; i++) {
1641 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001642 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001643 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001644 getObjCEncodingForTypeQualifier(
1645 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001646 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001647 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001648 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001649 }
1650}
1651
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001652/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1653/// method declaration. If non-NULL, Container must be either an
1654/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1655/// NULL when getting encodings for protocol properties.
1656void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1657 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001658 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001659 // Collect information from the property implementation decl(s).
1660 bool Dynamic = false;
1661 ObjCPropertyImplDecl *SynthesizePID = 0;
1662
1663 // FIXME: Duplicated code due to poor abstraction.
1664 if (Container) {
1665 if (const ObjCCategoryImplDecl *CID =
1666 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1667 for (ObjCCategoryImplDecl::propimpl_iterator
1668 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1669 ObjCPropertyImplDecl *PID = *i;
1670 if (PID->getPropertyDecl() == PD) {
1671 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1672 Dynamic = true;
1673 } else {
1674 SynthesizePID = PID;
1675 }
1676 }
1677 }
1678 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001679 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001680 for (ObjCCategoryImplDecl::propimpl_iterator
1681 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1682 ObjCPropertyImplDecl *PID = *i;
1683 if (PID->getPropertyDecl() == PD) {
1684 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1685 Dynamic = true;
1686 } else {
1687 SynthesizePID = PID;
1688 }
1689 }
1690 }
1691 }
1692 }
1693
1694 // FIXME: This is not very efficient.
1695 S = "T";
1696
1697 // Encode result type.
1698 // FIXME: GCC uses a generating_property_type_encoding mode during
1699 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001700 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001701
1702 if (PD->isReadOnly()) {
1703 S += ",R";
1704 } else {
1705 switch (PD->getSetterKind()) {
1706 case ObjCPropertyDecl::Assign: break;
1707 case ObjCPropertyDecl::Copy: S += ",C"; break;
1708 case ObjCPropertyDecl::Retain: S += ",&"; break;
1709 }
1710 }
1711
1712 // It really isn't clear at all what this means, since properties
1713 // are "dynamic by default".
1714 if (Dynamic)
1715 S += ",D";
1716
1717 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1718 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001719 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001720 }
1721
1722 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1723 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001724 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001725 }
1726
1727 if (SynthesizePID) {
1728 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1729 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001730 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001731 }
1732
1733 // FIXME: OBJCGC: weak & strong
1734}
1735
Fariborz Jahanian248db262008-01-22 22:44:46 +00001736void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001737 bool NameFields) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001738 // We follow the behavior of gcc, expanding structures which are
1739 // directly pointed to, and expanding embedded structures. Note that
1740 // these rules are sufficient to prevent recursive encoding of the
1741 // same type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001742 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001743}
1744
1745void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1746 bool ExpandPointedToStructures,
1747 bool ExpandStructures,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001748 bool NameFields) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001749 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001750 char encoding;
1751 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001752 default: assert(0 && "Unhandled builtin type kind");
1753 case BuiltinType::Void: encoding = 'v'; break;
1754 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001755 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001756 case BuiltinType::UChar: encoding = 'C'; break;
1757 case BuiltinType::UShort: encoding = 'S'; break;
1758 case BuiltinType::UInt: encoding = 'I'; break;
1759 case BuiltinType::ULong: encoding = 'L'; break;
1760 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001761 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001762 case BuiltinType::SChar: encoding = 'c'; break;
1763 case BuiltinType::Short: encoding = 's'; break;
1764 case BuiltinType::Int: encoding = 'i'; break;
1765 case BuiltinType::Long: encoding = 'l'; break;
1766 case BuiltinType::LongLong: encoding = 'q'; break;
1767 case BuiltinType::Float: encoding = 'f'; break;
1768 case BuiltinType::Double: encoding = 'd'; break;
1769 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001770 }
1771
1772 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001773 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001774 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001775 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001776 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1777 ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001778 ExpandStructures, NameFields);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001779 }
1780 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001781 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001782 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001783 S += '@';
1784 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001785 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001786 S += '#';
1787 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001788 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001789 S += ':';
1790 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001791 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001792
1793 if (PointeeTy->isCharType()) {
1794 // char pointer types should be encoded as '*' unless it is a
1795 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001796 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001797 S += '*';
1798 return;
1799 }
1800 }
1801
1802 S += '^';
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001803 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001804 false, ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001805 NameFields);
Chris Lattnera1923f62008-08-04 07:31:14 +00001806 } else if (const ArrayType *AT =
1807 // Ignore type qualifiers etc.
1808 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001809 S += '[';
1810
1811 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1812 S += llvm::utostr(CAT->getSize().getZExtValue());
1813 else
1814 assert(0 && "Unhandled array type!");
1815
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001816 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001817 false, ExpandStructures, NameFields);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001818 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001819 } else if (T->getAsFunctionType()) {
1820 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001821 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001822 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001823 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001824 // Anonymous structures print as '?'
1825 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1826 S += II->getName();
1827 } else {
1828 S += '?';
1829 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001830 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001831 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001832 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1833 FieldEnd = RDecl->field_end();
1834 Field != FieldEnd; ++Field) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001835 if (NameFields) {
1836 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001837 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001838 S += '"';
1839 }
1840
1841 // Special case bit-fields.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001842 if (const Expr *E = Field->getBitWidth()) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001843 // FIXME: Fix constness.
1844 ASTContext *Ctx = const_cast<ASTContext*>(this);
1845 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1846 // FIXME: Obj-C is losing information about the type size
1847 // here. Investigate if this is a problem.
1848 S += 'b';
1849 S += llvm::utostr(N);
1850 } else {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001851 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1852 NameFields);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001853 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001854 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001855 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001856 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001857 } else if (T->isEnumeralType()) {
1858 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001859 } else if (T->isBlockPointerType()) {
1860 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001861 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001862 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001863}
1864
Ted Kremenek42730c52008-01-07 19:49:32 +00001865void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001866 std::string& S) const {
1867 if (QT & Decl::OBJC_TQ_In)
1868 S += 'n';
1869 if (QT & Decl::OBJC_TQ_Inout)
1870 S += 'N';
1871 if (QT & Decl::OBJC_TQ_Out)
1872 S += 'o';
1873 if (QT & Decl::OBJC_TQ_Bycopy)
1874 S += 'O';
1875 if (QT & Decl::OBJC_TQ_Byref)
1876 S += 'R';
1877 if (QT & Decl::OBJC_TQ_Oneway)
1878 S += 'V';
1879}
1880
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001881void ASTContext::setBuiltinVaListType(QualType T)
1882{
1883 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1884
1885 BuiltinVaListType = T;
1886}
1887
Ted Kremenek42730c52008-01-07 19:49:32 +00001888void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001889{
Ted Kremenek42730c52008-01-07 19:49:32 +00001890 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001891
1892 // typedef struct objc_object *id;
1893 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1894 assert(ptr && "'id' incorrectly typed");
1895 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1896 assert(rec && "'id' incorrectly typed");
1897 IdStructType = rec;
1898}
1899
Ted Kremenek42730c52008-01-07 19:49:32 +00001900void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001901{
Ted Kremenek42730c52008-01-07 19:49:32 +00001902 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001903
1904 // typedef struct objc_selector *SEL;
1905 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1906 assert(ptr && "'SEL' incorrectly typed");
1907 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1908 assert(rec && "'SEL' incorrectly typed");
1909 SelStructType = rec;
1910}
1911
Ted Kremenek42730c52008-01-07 19:49:32 +00001912void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001913{
Ted Kremenek42730c52008-01-07 19:49:32 +00001914 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001915}
1916
Ted Kremenek42730c52008-01-07 19:49:32 +00001917void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001918{
Ted Kremenek42730c52008-01-07 19:49:32 +00001919 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001920
1921 // typedef struct objc_class *Class;
1922 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1923 assert(ptr && "'Class' incorrectly typed");
1924 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1925 assert(rec && "'Class' incorrectly typed");
1926 ClassStructType = rec;
1927}
1928
Ted Kremenek42730c52008-01-07 19:49:32 +00001929void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1930 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001931 "'NSConstantString' type already set!");
1932
Ted Kremenek42730c52008-01-07 19:49:32 +00001933 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001934}
1935
Douglas Gregorc6507e42008-11-03 14:12:49 +00001936/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00001937/// TargetInfo, produce the corresponding type. The unsigned @p Type
1938/// is actually a value of type @c TargetInfo::IntType.
1939QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001940 switch (Type) {
1941 case TargetInfo::NoInt: return QualType();
1942 case TargetInfo::SignedShort: return ShortTy;
1943 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1944 case TargetInfo::SignedInt: return IntTy;
1945 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1946 case TargetInfo::SignedLong: return LongTy;
1947 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1948 case TargetInfo::SignedLongLong: return LongLongTy;
1949 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1950 }
1951
1952 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00001953 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00001954}
Ted Kremenek118930e2008-07-24 23:58:27 +00001955
1956//===----------------------------------------------------------------------===//
1957// Type Predicates.
1958//===----------------------------------------------------------------------===//
1959
1960/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1961/// to an object type. This includes "id" and "Class" (two 'special' pointers
1962/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1963/// ID type).
1964bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1965 if (Ty->isObjCQualifiedIdType())
1966 return true;
1967
Steve Naroffd9e00802008-10-21 18:24:04 +00001968 // Blocks are objects.
1969 if (Ty->isBlockPointerType())
1970 return true;
1971
1972 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00001973 if (!Ty->isPointerType())
1974 return false;
1975
1976 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1977 // pointer types. This looks for the typedef specifically, not for the
1978 // underlying type.
1979 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1980 return true;
1981
1982 // If this a pointer to an interface (e.g. NSString*), it is ok.
1983 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1984}
1985
Chris Lattner6ff358b2008-04-07 06:51:04 +00001986//===----------------------------------------------------------------------===//
1987// Type Compatibility Testing
1988//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001989
Steve Naroff3454b6c2008-09-04 15:10:53 +00001990/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001991/// block types. Types must be strictly compatible here. For example,
1992/// C unfortunately doesn't produce an error for the following:
1993///
1994/// int (*emptyArgFunc)();
1995/// int (*intArgList)(int) = emptyArgFunc;
1996///
1997/// For blocks, we will produce an error for the following (similar to C++):
1998///
1999/// int (^emptyArgBlock)();
2000/// int (^intArgBlock)(int) = emptyArgBlock;
2001///
2002/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2003///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002004bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002005 const FunctionType *lbase = lhs->getAsFunctionType();
2006 const FunctionType *rbase = rhs->getAsFunctionType();
2007 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2008 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2009 if (lproto && rproto)
2010 return !mergeTypes(lhs, rhs).isNull();
2011 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002012}
2013
Chris Lattner6ff358b2008-04-07 06:51:04 +00002014/// areCompatVectorTypes - Return true if the two specified vector types are
2015/// compatible.
2016static bool areCompatVectorTypes(const VectorType *LHS,
2017 const VectorType *RHS) {
2018 assert(LHS->isCanonical() && RHS->isCanonical());
2019 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002020 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002021}
2022
Eli Friedman0d9549b2008-08-22 00:56:42 +00002023/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002024/// compatible for assignment from RHS to LHS. This handles validation of any
2025/// protocol qualifiers on the LHS or RHS.
2026///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002027bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2028 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002029 // Verify that the base decls are compatible: the RHS must be a subclass of
2030 // the LHS.
2031 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2032 return false;
2033
2034 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2035 // protocol qualified at all, then we are good.
2036 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2037 return true;
2038
2039 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2040 // isn't a superset.
2041 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2042 return true; // FIXME: should return false!
2043
2044 // Finally, we must have two protocol-qualified interfaces.
2045 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2046 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2047 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2048 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2049 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2050 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2051
2052 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2053 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2054 // LHS in a single parallel scan until we run out of elements in LHS.
2055 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2056 ObjCProtocolDecl *LHSProto = *LHSPI;
2057
2058 while (RHSPI != RHSPE) {
2059 ObjCProtocolDecl *RHSProto = *RHSPI++;
2060 // If the RHS has a protocol that the LHS doesn't, ignore it.
2061 if (RHSProto != LHSProto)
2062 continue;
2063
2064 // Otherwise, the RHS does have this element.
2065 ++LHSPI;
2066 if (LHSPI == LHSPE)
2067 return true; // All protocols in LHS exist in RHS.
2068
2069 LHSProto = *LHSPI;
2070 }
2071
2072 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2073 return false;
2074}
2075
Steve Naroff85f0dc52007-10-15 20:41:53 +00002076/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2077/// both shall have the identically qualified version of a compatible type.
2078/// C99 6.2.7p1: Two types have compatible types if their types are the
2079/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002080bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2081 return !mergeTypes(LHS, RHS).isNull();
2082}
2083
2084QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2085 const FunctionType *lbase = lhs->getAsFunctionType();
2086 const FunctionType *rbase = rhs->getAsFunctionType();
2087 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2088 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2089 bool allLTypes = true;
2090 bool allRTypes = true;
2091
2092 // Check return type
2093 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2094 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002095 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2096 allLTypes = false;
2097 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2098 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002099
2100 if (lproto && rproto) { // two C99 style function prototypes
2101 unsigned lproto_nargs = lproto->getNumArgs();
2102 unsigned rproto_nargs = rproto->getNumArgs();
2103
2104 // Compatible functions must have the same number of arguments
2105 if (lproto_nargs != rproto_nargs)
2106 return QualType();
2107
2108 // Variadic and non-variadic functions aren't compatible
2109 if (lproto->isVariadic() != rproto->isVariadic())
2110 return QualType();
2111
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002112 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2113 return QualType();
2114
Eli Friedman0d9549b2008-08-22 00:56:42 +00002115 // Check argument compatibility
2116 llvm::SmallVector<QualType, 10> types;
2117 for (unsigned i = 0; i < lproto_nargs; i++) {
2118 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2119 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2120 QualType argtype = mergeTypes(largtype, rargtype);
2121 if (argtype.isNull()) return QualType();
2122 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002123 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2124 allLTypes = false;
2125 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2126 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002127 }
2128 if (allLTypes) return lhs;
2129 if (allRTypes) return rhs;
2130 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002131 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002132 }
2133
2134 if (lproto) allRTypes = false;
2135 if (rproto) allLTypes = false;
2136
2137 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2138 if (proto) {
2139 if (proto->isVariadic()) return QualType();
2140 // Check that the types are compatible with the types that
2141 // would result from default argument promotions (C99 6.7.5.3p15).
2142 // The only types actually affected are promotable integer
2143 // types and floats, which would be passed as a different
2144 // type depending on whether the prototype is visible.
2145 unsigned proto_nargs = proto->getNumArgs();
2146 for (unsigned i = 0; i < proto_nargs; ++i) {
2147 QualType argTy = proto->getArgType(i);
2148 if (argTy->isPromotableIntegerType() ||
2149 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2150 return QualType();
2151 }
2152
2153 if (allLTypes) return lhs;
2154 if (allRTypes) return rhs;
2155 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002156 proto->getNumArgs(), lproto->isVariadic(),
2157 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002158 }
2159
2160 if (allLTypes) return lhs;
2161 if (allRTypes) return rhs;
2162 return getFunctionTypeNoProto(retType);
2163}
2164
2165QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002166 // C++ [expr]: If an expression initially has the type "reference to T", the
2167 // type is adjusted to "T" prior to any further analysis, the expression
2168 // designates the object or function denoted by the reference, and the
2169 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002170 // FIXME: C++ shouldn't be going through here! The rules are different
2171 // enough that they should be handled separately.
2172 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002173 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002174 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002175 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002176
Eli Friedman0d9549b2008-08-22 00:56:42 +00002177 QualType LHSCan = getCanonicalType(LHS),
2178 RHSCan = getCanonicalType(RHS);
2179
2180 // If two types are identical, they are compatible.
2181 if (LHSCan == RHSCan)
2182 return LHS;
2183
2184 // If the qualifiers are different, the types aren't compatible
2185 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2186 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2187 return QualType();
2188
2189 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2190 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2191
Chris Lattnerc38d4522008-01-14 05:45:46 +00002192 // We want to consider the two function types to be the same for these
2193 // comparisons, just force one to the other.
2194 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2195 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002196
2197 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002198 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2199 LHSClass = Type::ConstantArray;
2200 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2201 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002202
Nate Begemanaf6ed502008-04-18 23:10:10 +00002203 // Canonicalize ExtVector -> Vector.
2204 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2205 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002206
Chris Lattner7cdcb252008-04-07 06:38:24 +00002207 // Consider qualified interfaces and interfaces the same.
2208 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2209 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002210
Chris Lattnerb5709e22008-04-07 05:43:21 +00002211 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002212 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002213 // ID is compatible with all qualified id types.
2214 if (LHS->isObjCQualifiedIdType()) {
2215 if (const PointerType *PT = RHS->getAsPointerType()) {
2216 QualType pType = PT->getPointeeType();
2217 if (isObjCIdType(pType))
2218 return LHS;
2219 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2220 // Unfortunately, this API is part of Sema (which we don't have access
2221 // to. Need to refactor. The following check is insufficient, since we
2222 // need to make sure the class implements the protocol.
2223 if (pType->isObjCInterfaceType())
2224 return LHS;
2225 }
2226 }
2227 if (RHS->isObjCQualifiedIdType()) {
2228 if (const PointerType *PT = LHS->getAsPointerType()) {
2229 QualType pType = PT->getPointeeType();
2230 if (isObjCIdType(pType))
2231 return RHS;
2232 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2233 // Unfortunately, this API is part of Sema (which we don't have access
2234 // to. Need to refactor. The following check is insufficient, since we
2235 // need to make sure the class implements the protocol.
2236 if (pType->isObjCInterfaceType())
2237 return RHS;
2238 }
2239 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002240 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2241 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002242 if (const EnumType* ETy = LHS->getAsEnumType()) {
2243 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2244 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002245 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002246 if (const EnumType* ETy = RHS->getAsEnumType()) {
2247 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2248 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002249 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002250
Eli Friedman0d9549b2008-08-22 00:56:42 +00002251 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002252 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002253
Steve Naroffc88babe2008-01-09 22:43:08 +00002254 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002255 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002256 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002257 {
2258 // Merge two pointer types, while trying to preserve typedef info
2259 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2260 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2261 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2262 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002263 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2264 return LHS;
2265 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2266 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002267 return getPointerType(ResultType);
2268 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002269 case Type::BlockPointer:
2270 {
2271 // Merge two block pointer types, while trying to preserve typedef info
2272 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2273 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2274 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2275 if (ResultType.isNull()) return QualType();
2276 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2277 return LHS;
2278 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2279 return RHS;
2280 return getBlockPointerType(ResultType);
2281 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002282 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002283 {
2284 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2285 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2286 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2287 return QualType();
2288
2289 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2290 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2291 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2292 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002293 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2294 return LHS;
2295 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2296 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002297 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2298 ArrayType::ArraySizeModifier(), 0);
2299 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2300 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002301 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2302 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002303 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2304 return LHS;
2305 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2306 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002307 if (LVAT) {
2308 // FIXME: This isn't correct! But tricky to implement because
2309 // the array's size has to be the size of LHS, but the type
2310 // has to be different.
2311 return LHS;
2312 }
2313 if (RVAT) {
2314 // FIXME: This isn't correct! But tricky to implement because
2315 // the array's size has to be the size of RHS, but the type
2316 // has to be different.
2317 return RHS;
2318 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002319 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2320 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002321 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002322 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002323 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002324 return mergeFunctionTypes(LHS, RHS);
2325 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002326 // FIXME: Why are these compatible?
2327 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2328 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2329 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002330 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002331 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002332 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002333 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002334 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2335 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002336 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002337 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002338 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2339 // for checking assignment/comparison safety
2340 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002341 case Type::ObjCQualifiedId:
2342 // Distinct qualified id's are not compatible.
2343 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002344 default:
2345 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002346 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002347 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002348}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002349
Chris Lattner1d78a862008-04-07 07:01:58 +00002350//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002351// Integer Predicates
2352//===----------------------------------------------------------------------===//
2353unsigned ASTContext::getIntWidth(QualType T) {
2354 if (T == BoolTy)
2355 return 1;
2356 // At the moment, only bool has padding bits
2357 return (unsigned)getTypeSize(T);
2358}
2359
2360QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2361 assert(T->isSignedIntegerType() && "Unexpected type");
2362 if (const EnumType* ETy = T->getAsEnumType())
2363 T = ETy->getDecl()->getIntegerType();
2364 const BuiltinType* BTy = T->getAsBuiltinType();
2365 assert (BTy && "Unexpected signed integer type");
2366 switch (BTy->getKind()) {
2367 case BuiltinType::Char_S:
2368 case BuiltinType::SChar:
2369 return UnsignedCharTy;
2370 case BuiltinType::Short:
2371 return UnsignedShortTy;
2372 case BuiltinType::Int:
2373 return UnsignedIntTy;
2374 case BuiltinType::Long:
2375 return UnsignedLongTy;
2376 case BuiltinType::LongLong:
2377 return UnsignedLongLongTy;
2378 default:
2379 assert(0 && "Unexpected signed integer type");
2380 return QualType();
2381 }
2382}
2383
2384
2385//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002386// Serialization Support
2387//===----------------------------------------------------------------------===//
2388
Ted Kremenek738e6c02007-10-31 17:10:13 +00002389/// Emit - Serialize an ASTContext object to Bitcode.
2390void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002391 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002392 S.EmitRef(SourceMgr);
2393 S.EmitRef(Target);
2394 S.EmitRef(Idents);
2395 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002396
Ted Kremenek68228a92007-10-31 22:44:07 +00002397 // Emit the size of the type vector so that we can reserve that size
2398 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002399 S.EmitInt(Types.size());
2400
Ted Kremenek034a78c2007-11-13 22:02:55 +00002401 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2402 I!=E;++I)
2403 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002404
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002405 S.EmitOwnedPtr(TUDecl);
2406
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002407 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002408}
2409
Ted Kremenekacba3612007-11-13 00:25:37 +00002410ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002411
2412 // Read the language options.
2413 LangOptions LOpts;
2414 LOpts.Read(D);
2415
Ted Kremenek68228a92007-10-31 22:44:07 +00002416 SourceManager &SM = D.ReadRef<SourceManager>();
2417 TargetInfo &t = D.ReadRef<TargetInfo>();
2418 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2419 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002420
Ted Kremenek68228a92007-10-31 22:44:07 +00002421 unsigned size_reserve = D.ReadInt();
2422
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002423 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2424 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002425
Ted Kremenek034a78c2007-11-13 22:02:55 +00002426 for (unsigned i = 0; i < size_reserve; ++i)
2427 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002428
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002429 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2430
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002431 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002432
2433 return A;
2434}