blob: c43ae78c9f587df6a051434d32871ee6933c9738 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000023#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000024
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27enum FloatingRank {
28 FloatRank, DoubleRank, LongDoubleRank
29};
30
Chris Lattner61710852008-10-05 17:34:18 +000031ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
32 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000033 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000034 bool FreeMem, unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroffc0ac4922009-01-27 23:20:32 +000036 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000037 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000038{
39 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
41 BuiltinInfo.InitializeBuiltins(idents, Target);
42 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 Types.pop_back();
50 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000051
Nuno Lopesb74668e2008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Eli Friedmanb26153c2008-05-27 03:08:09 +000079 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
82void ASTContext::PrintStats() const {
83 fprintf(stderr, "*** AST Context Stats:\n");
84 fprintf(stderr, " %d types total.\n", (int)Types.size());
85 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000086 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redlf30208a2009-01-24 21:16:55 +000088 unsigned NumMemberPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000089
90 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
92 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000093 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000094
95 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
96 Type *T = Types[i];
97 if (isa<BuiltinType>(T))
98 ++NumBuiltin;
99 else if (isa<PointerType>(T))
100 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 else if (isa<BlockPointerType>(T))
102 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 else if (isa<ReferenceType>(T))
104 ++NumReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000105 else if (isa<MemberPointerType>(T))
106 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000107 else if (isa<ComplexType>(T))
108 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 else if (isa<ArrayType>(T))
110 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000111 else if (isa<VectorType>(T))
112 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 else if (isa<FunctionTypeNoProto>(T))
114 ++NumFunctionNP;
115 else if (isa<FunctionTypeProto>(T))
116 ++NumFunctionP;
117 else if (isa<TypedefType>(T))
118 ++NumTypeName;
119 else if (TagType *TT = dyn_cast<TagType>(T)) {
120 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000121 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000123 case TagDecl::TK_struct: ++NumTagStruct; break;
124 case TagDecl::TK_union: ++NumTagUnion; break;
125 case TagDecl::TK_class: ++NumTagClass; break;
126 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 } else if (isa<ObjCInterfaceType>(T))
129 ++NumObjCInterfaces;
130 else if (isa<ObjCQualifiedInterfaceType>(T))
131 ++NumObjCQualifiedInterfaces;
132 else if (isa<ObjCQualifiedIdType>(T))
133 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000134 else if (isa<TypeOfType>(T))
135 ++NumTypeOfTypes;
136 else if (isa<TypeOfExpr>(T))
137 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000138 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000139 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 assert(0 && "Unknown type!");
141 }
142 }
143
144 fprintf(stderr, " %d builtin types\n", NumBuiltin);
145 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000146 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000148 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000149 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000151 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
153 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
154 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
155 fprintf(stderr, " %d tagged types\n", NumTagged);
156 fprintf(stderr, " %d struct types\n", NumTagStruct);
157 fprintf(stderr, " %d union types\n", NumTagUnion);
158 fprintf(stderr, " %d class types\n", NumTagClass);
159 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000160 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000161 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000163 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000164 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000165 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
166 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
169 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000171 NumMemberPointer*sizeof(MemberPointerType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 NumFunctionP*sizeof(FunctionTypeProto)+
173 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000174 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
175 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
178
179void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000180 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183void ASTContext::InitBuiltinTypes() {
184 assert(VoidTy.isNull() && "Context reinitialized?");
185
186 // C99 6.2.5p19.
187 InitBuiltinType(VoidTy, BuiltinType::Void);
188
189 // C99 6.2.5p2.
190 InitBuiltinType(BoolTy, BuiltinType::Bool);
191 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000192 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 InitBuiltinType(CharTy, BuiltinType::Char_S);
194 else
195 InitBuiltinType(CharTy, BuiltinType::Char_U);
196 // C99 6.2.5p4.
197 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
198 InitBuiltinType(ShortTy, BuiltinType::Short);
199 InitBuiltinType(IntTy, BuiltinType::Int);
200 InitBuiltinType(LongTy, BuiltinType::Long);
201 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
202
203 // C99 6.2.5p6.
204 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
205 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
206 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
207 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
208 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
209
210 // C99 6.2.5p10.
211 InitBuiltinType(FloatTy, BuiltinType::Float);
212 InitBuiltinType(DoubleTy, BuiltinType::Double);
213 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000214
215 // C++ 3.9.1p5
216 InitBuiltinType(WCharTy, BuiltinType::WChar);
217
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000218 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000219 InitBuiltinType(OverloadTy, BuiltinType::Overload);
220
221 // Placeholder type for type-dependent expressions whose type is
222 // completely unknown. No code should ever check a type against
223 // DependentTy and users should never see it; however, it is here to
224 // help diagnose failures to properly check for type-dependent
225 // expressions.
226 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // C99 6.2.5p11.
229 FloatComplexTy = getComplexType(FloatTy);
230 DoubleComplexTy = getComplexType(DoubleTy);
231 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000232
Steve Naroff7e219e42007-10-15 14:41:52 +0000233 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000234 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000235 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000237 ClassStructType = 0;
238
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000240
241 // void * type
242 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
Chris Lattner464175b2007-07-18 17:52:12 +0000245//===----------------------------------------------------------------------===//
246// Type Sizing and Analysis
247//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000248
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000249/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
250/// scalar floating point type.
251const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
252 const BuiltinType *BT = T->getAsBuiltinType();
253 assert(BT && "Not a floating point type!");
254 switch (BT->getKind()) {
255 default: assert(0 && "Not a floating point type!");
256 case BuiltinType::Float: return Target.getFloatFormat();
257 case BuiltinType::Double: return Target.getDoubleFormat();
258 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
259 }
260}
261
Chris Lattneraf707ab2009-01-24 21:53:27 +0000262/// getDeclAlign - Return a conservative estimate of the alignment of the
263/// specified decl. Note that bitfields do not have a valid alignment, so
264/// this method will assert on them.
265unsigned ASTContext::getDeclAlign(const Decl *D) {
266 // FIXME: If attribute(align) is specified on the decl, round up to it.
267
268 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
269 QualType T = VD->getType();
270 // Incomplete or function types default to 1.
271 if (T->isIncompleteType() || T->isFunctionType())
272 return 1;
273
274 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
275 T = cast<ArrayType>(T)->getElementType();
276
277 return getTypeAlign(T);
278 }
279
280 return 1;
281}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000282
Chris Lattnera7674d82007-07-13 22:13:22 +0000283/// getTypeSize - Return the size of the specified type, in bits. This method
284/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000285std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000286ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000287 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000288 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000289 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000290 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000291 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000292 case Type::FunctionNoProto:
293 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000294 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000295 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000296 case Type::VariableArray:
297 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000298 case Type::DependentSizedArray:
299 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000300 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000301 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000302
Chris Lattner98be4942008-03-05 18:54:05 +0000303 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000304 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000305 Align = EltInfo.second;
306 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000307 }
Nate Begeman213541a2008-04-18 23:10:10 +0000308 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000309 case Type::Vector: {
310 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000311 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000312 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000313 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000314 // If the alignment is not a power of 2, round up to the next power of 2.
315 // This happens for non-power-of-2 length vectors.
316 // FIXME: this should probably be a target property.
317 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000318 break;
319 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000320
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000321 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000322 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000323 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000324 case BuiltinType::Void:
325 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000326 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000327 Width = Target.getBoolWidth();
328 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000329 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000330 case BuiltinType::Char_S:
331 case BuiltinType::Char_U:
332 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000334 Width = Target.getCharWidth();
335 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000337 case BuiltinType::WChar:
338 Width = Target.getWCharWidth();
339 Align = Target.getWCharAlign();
340 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000341 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000342 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000343 Width = Target.getShortWidth();
344 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000345 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000346 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000347 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000348 Width = Target.getIntWidth();
349 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000350 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000351 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getLongWidth();
354 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getLongLongWidth();
359 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
361 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000362 Width = Target.getFloatWidth();
363 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000364 break;
365 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000366 Width = Target.getDoubleWidth();
367 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000368 break;
369 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000370 Width = Target.getLongDoubleWidth();
371 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000372 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000373 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000374 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000375 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000376 // FIXME: Pointers into different addr spaces could have different sizes and
377 // alignment requirements: getPointerInfo should take an AddrSpace.
378 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000379 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000380 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000381 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000383 case Type::BlockPointer: {
384 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
385 Width = Target.getPointerWidth(AS);
386 Align = Target.getPointerAlign(AS);
387 break;
388 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000389 case Type::Pointer: {
390 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(AS);
393 break;
394 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000395 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000396 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000397 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 // FIXME: This is wrong for struct layout: a reference in a struct has
399 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000400 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000401 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000402 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000403 // the GCC ABI, where pointers to data are one pointer large, pointers to
404 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000405 // other compilers too, we need to delegate this completely to TargetInfo
406 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000407 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
408 unsigned AS = Pointee.getAddressSpace();
409 Width = Target.getPointerWidth(AS);
410 if (Pointee->isFunctionType())
411 Width *= 2;
412 Align = Target.getPointerAlign(AS);
413 // GCC aligns at single pointer width.
414 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000415 case Type::Complex: {
416 // Complex types have the same alignment as their elements, but twice the
417 // size.
418 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000419 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000420 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000421 Align = EltInfo.second;
422 break;
423 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000424 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000425 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000426 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
427 Width = Layout.getSize();
428 Align = Layout.getAlignment();
429 break;
430 }
Chris Lattner71763312008-04-06 22:05:18 +0000431 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000432 const TagType *TT = cast<TagType>(T);
433
434 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000435 Width = 1;
436 Align = 1;
437 break;
438 }
439
Daniel Dunbar1d751182008-11-08 05:48:37 +0000440 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000441 return getTypeInfo(ET->getDecl()->getIntegerType());
442
Daniel Dunbar1d751182008-11-08 05:48:37 +0000443 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000444 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
445 Width = Layout.getSize();
446 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000447 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000448 }
Chris Lattner71763312008-04-06 22:05:18 +0000449 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000450
Chris Lattner464175b2007-07-18 17:52:12 +0000451 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000452 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000453}
454
Chris Lattner34ebde42009-01-27 18:08:34 +0000455/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
456/// type for the current target in bits. This can be different than the ABI
457/// alignment in cases where it is beneficial for performance to overalign
458/// a data type.
459unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
460 unsigned ABIAlign = getTypeAlign(T);
461
462 // Doubles should be naturally aligned if possible.
463 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
464 if (BT->getKind() == BuiltinType::Double)
Fariborz Jahanian32b978c2009-01-27 18:55:00 +0000465 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000466
467 return ABIAlign;
468}
469
470
Devang Patel8b277042008-06-04 21:22:16 +0000471/// LayoutField - Field layout.
472void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000473 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000474 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000475 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000476 uint64_t FieldOffset = IsUnion ? 0 : Size;
477 uint64_t FieldSize;
478 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000479
480 // FIXME: Should this override struct packing? Probably we want to
481 // take the minimum?
482 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
483 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000484
485 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
486 // TODO: Need to check this algorithm on other targets!
487 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000488 FieldSize =
489 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000490
491 std::pair<uint64_t, unsigned> FieldInfo =
492 Context.getTypeInfo(FD->getType());
493 uint64_t TypeSize = FieldInfo.first;
494
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000495 // Determine the alignment of this bitfield. The packing
496 // attributes define a maximum and the alignment attribute defines
497 // a minimum.
498 // FIXME: What is the right behavior when the specified alignment
499 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000500 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000501 if (FieldPacking)
502 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000503 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
504 FieldAlign = std::max(FieldAlign, AA->getAlignment());
505
506 // Check if we need to add padding to give the field the correct
507 // alignment.
508 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
509 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
510
511 // Padding members don't affect overall alignment
512 if (!FD->getIdentifier())
513 FieldAlign = 1;
514 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000515 if (FD->getType()->isIncompleteArrayType()) {
516 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000517 // query getTypeInfo about these, so we figure it out here.
518 // Flexible array members don't have any size, but they
519 // have to be aligned appropriately for their element type.
520 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000521 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000522 FieldAlign = Context.getTypeAlign(ATy->getElementType());
523 } else {
524 std::pair<uint64_t, unsigned> FieldInfo =
525 Context.getTypeInfo(FD->getType());
526 FieldSize = FieldInfo.first;
527 FieldAlign = FieldInfo.second;
528 }
529
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000530 // Determine the alignment of this bitfield. The packing
531 // attributes define a maximum and the alignment attribute defines
532 // a minimum. Additionally, the packing alignment must be at least
533 // a byte for non-bitfields.
534 //
535 // FIXME: What is the right behavior when the specified alignment
536 // is smaller than the specified packing?
537 if (FieldPacking)
538 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000539 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
540 FieldAlign = std::max(FieldAlign, AA->getAlignment());
541
542 // Round up the current record size to the field's alignment boundary.
543 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
544 }
545
546 // Place this field at the current location.
547 FieldOffsets[FieldNo] = FieldOffset;
548
549 // Reserve space for this field.
550 if (IsUnion) {
551 Size = std::max(Size, FieldSize);
552 } else {
553 Size = FieldOffset + FieldSize;
554 }
555
556 // Remember max struct/class alignment.
557 Alignment = std::max(Alignment, FieldAlign);
558}
559
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000560static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
561 std::vector<FieldDecl*> &Fields) {
562 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
563 if (SuperClass)
564 CollectObjCIvars(SuperClass, Fields);
565 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
566 E = OI->ivar_end(); I != E; ++I) {
567 ObjCIvarDecl *IVDecl = (*I);
568 if (!IVDecl->isInvalidDecl())
569 Fields.push_back(cast<FieldDecl>(IVDecl));
570 }
571}
572
573/// addRecordToClass - produces record info. for the class for its
574/// ivars and all those inherited.
575///
576const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
577{
578 const RecordDecl *&RD = ASTRecordForInterface[D];
579 if (RD)
580 return RD;
581 std::vector<FieldDecl*> RecFields;
582 CollectObjCIvars(D, RecFields);
583 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
584 D->getLocation(),
585 D->getIdentifier());
586 /// FIXME! Can do collection of ivars and adding to the record while
587 /// doing it.
588 for (unsigned int i = 0; i != RecFields.size(); i++) {
589 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
590 RecFields[i]->getLocation(),
591 RecFields[i]->getIdentifier(),
592 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000593 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000594 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000595 }
596 NewRD->completeDefinition(*this);
597 RD = NewRD;
598 return RD;
599}
Devang Patel44a3dde2008-06-04 21:54:36 +0000600
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000601/// setFieldDecl - maps a field for the given Ivar reference node.
602//
603void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
604 const ObjCIvarDecl *Ivar,
605 const ObjCIvarRefExpr *MRef) {
606 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
607 lookupFieldDeclForIvar(*this, Ivar);
608 ASTFieldForIvarRef[MRef] = FD;
609}
610
Chris Lattner61710852008-10-05 17:34:18 +0000611/// getASTObjcInterfaceLayout - Get or compute information about the layout of
612/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000613/// position information.
614const ASTRecordLayout &
615ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
616 // Look up this layout, if already laid out, return what we have.
617 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
618 if (Entry) return *Entry;
619
620 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
621 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000622 ASTRecordLayout *NewEntry = NULL;
623 unsigned FieldCount = D->ivar_size();
624 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
625 FieldCount++;
626 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
627 unsigned Alignment = SL.getAlignment();
628 uint64_t Size = SL.getSize();
629 NewEntry = new ASTRecordLayout(Size, Alignment);
630 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000631 // Super class is at the beginning of the layout.
632 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000633 } else {
634 NewEntry = new ASTRecordLayout();
635 NewEntry->InitializeLayout(FieldCount);
636 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000637 Entry = NewEntry;
638
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000639 unsigned StructPacking = 0;
640 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
641 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000642
643 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
644 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
645 AA->getAlignment()));
646
647 // Layout each ivar sequentially.
648 unsigned i = 0;
649 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
650 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
651 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000652 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000653 }
654
655 // Finally, round the size of the total struct up to the alignment of the
656 // struct itself.
657 NewEntry->FinalizeLayout();
658 return *NewEntry;
659}
660
Devang Patel88a981b2007-11-01 19:11:01 +0000661/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000662/// specified record (struct/union/class), which indicates its size and field
663/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000664const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000665 D = D->getDefinition(*this);
666 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000667
Chris Lattner464175b2007-07-18 17:52:12 +0000668 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000669 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000670 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000671
Devang Patel88a981b2007-11-01 19:11:01 +0000672 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
673 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
674 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000675 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000676
Douglas Gregore267ff32008-12-11 20:41:00 +0000677 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000678 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000679 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000680
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000681 unsigned StructPacking = 0;
682 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
683 StructPacking = PA->getAlignment();
684
Eli Friedman4bd998b2008-05-30 09:31:38 +0000685 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000686 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
687 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000688
Eli Friedman4bd998b2008-05-30 09:31:38 +0000689 // Layout each field, for now, just sequentially, respecting alignment. In
690 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000691 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000692 for (RecordDecl::field_iterator Field = D->field_begin(),
693 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000694 Field != FieldEnd; (void)++Field, ++FieldIdx)
695 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000696
697 // Finally, round the size of the total struct up to the alignment of the
698 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000699 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000700 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000701}
702
Chris Lattnera7674d82007-07-13 22:13:22 +0000703//===----------------------------------------------------------------------===//
704// Type creation/memoization methods
705//===----------------------------------------------------------------------===//
706
Christopher Lambebb97e92008-02-04 02:31:56 +0000707QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000708 QualType CanT = getCanonicalType(T);
709 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000710 return T;
711
712 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
713 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000714 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000715 "Type is already address space qualified");
716
717 // Check if we've already instantiated an address space qual'd type of this
718 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000719 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000720 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000721 void *InsertPos = 0;
722 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
723 return QualType(ASQy, 0);
724
725 // If the base 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 Lattnerf52ab252008-04-06 22:59:24 +0000729 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000730
731 // Get the new insert position for the node we care about.
732 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000733 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000734 }
Steve Narofff83820b2009-01-27 22:08:43 +0000735 ASQualType *New = new (*this, 8) ASQualType(T.getTypePtr(), Canonical,
736 AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000737 ASQualTypes.InsertNode(New, InsertPos);
738 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000739 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000740}
741
Chris Lattnera7674d82007-07-13 22:13:22 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743/// getComplexType - Return the uniqued reference to the type for a complex
744/// number with the specified element type.
745QualType ASTContext::getComplexType(QualType T) {
746 // Unique pointers, to guarantee there is only one pointer of a particular
747 // structure.
748 llvm::FoldingSetNodeID ID;
749 ComplexType::Profile(ID, T);
750
751 void *InsertPos = 0;
752 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
753 return QualType(CT, 0);
754
755 // If the pointee type isn't canonical, this won't be a canonical type either,
756 // so fill in the canonical type field.
757 QualType Canonical;
758 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000759 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000760
761 // Get the new insert position for the node we care about.
762 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000763 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 }
Steve Narofff83820b2009-01-27 22:08:43 +0000765 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 Types.push_back(New);
767 ComplexTypes.InsertNode(New, InsertPos);
768 return QualType(New, 0);
769}
770
771
772/// getPointerType - Return the uniqued reference to the type for a pointer to
773/// the specified type.
774QualType ASTContext::getPointerType(QualType T) {
775 // Unique pointers, to guarantee there is only one pointer of a particular
776 // structure.
777 llvm::FoldingSetNodeID ID;
778 PointerType::Profile(ID, T);
779
780 void *InsertPos = 0;
781 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
782 return QualType(PT, 0);
783
784 // If the pointee type isn't canonical, this won't be a canonical type either,
785 // so fill in the canonical type field.
786 QualType Canonical;
787 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000788 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
790 // Get the new insert position for the node we care about.
791 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000792 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 }
Steve Narofff83820b2009-01-27 22:08:43 +0000794 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 Types.push_back(New);
796 PointerTypes.InsertNode(New, InsertPos);
797 return QualType(New, 0);
798}
799
Steve Naroff5618bd42008-08-27 16:04:49 +0000800/// getBlockPointerType - Return the uniqued reference to the type for
801/// a pointer to the specified block.
802QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000803 assert(T->isFunctionType() && "block of function types only");
804 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000805 // structure.
806 llvm::FoldingSetNodeID ID;
807 BlockPointerType::Profile(ID, T);
808
809 void *InsertPos = 0;
810 if (BlockPointerType *PT =
811 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
812 return QualType(PT, 0);
813
Steve Naroff296e8d52008-08-28 19:20:44 +0000814 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000815 // type either so fill in the canonical type field.
816 QualType Canonical;
817 if (!T->isCanonical()) {
818 Canonical = getBlockPointerType(getCanonicalType(T));
819
820 // Get the new insert position for the node we care about.
821 BlockPointerType *NewIP =
822 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000823 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000824 }
Steve Narofff83820b2009-01-27 22:08:43 +0000825 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000826 Types.push_back(New);
827 BlockPointerTypes.InsertNode(New, InsertPos);
828 return QualType(New, 0);
829}
830
Reid Spencer5f016e22007-07-11 17:01:13 +0000831/// getReferenceType - Return the uniqued reference to the type for a reference
832/// to the specified type.
833QualType ASTContext::getReferenceType(QualType T) {
834 // Unique pointers, to guarantee there is only one pointer of a particular
835 // structure.
836 llvm::FoldingSetNodeID ID;
837 ReferenceType::Profile(ID, T);
838
839 void *InsertPos = 0;
840 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
841 return QualType(RT, 0);
842
843 // If the referencee type isn't canonical, this won't be a canonical type
844 // either, so fill in the canonical type field.
845 QualType Canonical;
846 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000847 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000848
849 // Get the new insert position for the node we care about.
850 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000851 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 }
853
Steve Narofff83820b2009-01-27 22:08:43 +0000854 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 Types.push_back(New);
856 ReferenceTypes.InsertNode(New, InsertPos);
857 return QualType(New, 0);
858}
859
Sebastian Redlf30208a2009-01-24 21:16:55 +0000860/// getMemberPointerType - Return the uniqued reference to the type for a
861/// member pointer to the specified type, in the specified class.
862QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
863{
864 // Unique pointers, to guarantee there is only one pointer of a particular
865 // structure.
866 llvm::FoldingSetNodeID ID;
867 MemberPointerType::Profile(ID, T, Cls);
868
869 void *InsertPos = 0;
870 if (MemberPointerType *PT =
871 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
872 return QualType(PT, 0);
873
874 // If the pointee or class type isn't canonical, this won't be a canonical
875 // type either, so fill in the canonical type field.
876 QualType Canonical;
877 if (!T->isCanonical()) {
878 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
879
880 // Get the new insert position for the node we care about.
881 MemberPointerType *NewIP =
882 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
883 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
884 }
Steve Narofff83820b2009-01-27 22:08:43 +0000885 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000886 Types.push_back(New);
887 MemberPointerTypes.InsertNode(New, InsertPos);
888 return QualType(New, 0);
889}
890
Steve Narofffb22d962007-08-30 01:06:46 +0000891/// getConstantArrayType - Return the unique reference to the type for an
892/// array of the specified element type.
893QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000894 const llvm::APInt &ArySize,
895 ArrayType::ArraySizeModifier ASM,
896 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000898 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000899
900 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000901 if (ConstantArrayType *ATP =
902 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 return QualType(ATP, 0);
904
905 // If the element type isn't canonical, this won't be a canonical type either,
906 // so fill in the canonical type field.
907 QualType Canonical;
908 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000909 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000910 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000912 ConstantArrayType *NewIP =
913 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000914 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
916
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000917 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000918 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000919 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 Types.push_back(New);
921 return QualType(New, 0);
922}
923
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000924/// getVariableArrayType - Returns a non-unique reference to the type for a
925/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000926QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
927 ArrayType::ArraySizeModifier ASM,
928 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000929 // Since we don't unique expressions, it isn't possible to unique VLA's
930 // that have an expression provided for their size.
931
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000932 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000933 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000934
935 VariableArrayTypes.push_back(New);
936 Types.push_back(New);
937 return QualType(New, 0);
938}
939
Douglas Gregor898574e2008-12-05 23:32:09 +0000940/// getDependentSizedArrayType - Returns a non-unique reference to
941/// the type for a dependently-sized array of the specified element
942/// type. FIXME: We will need these to be uniqued, or at least
943/// comparable, at some point.
944QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
945 ArrayType::ArraySizeModifier ASM,
946 unsigned EltTypeQuals) {
947 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
948 "Size must be type- or value-dependent!");
949
950 // Since we don't unique expressions, it isn't possible to unique
951 // dependently-sized array types.
952
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000953 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000954 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
955 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000956
957 DependentSizedArrayTypes.push_back(New);
958 Types.push_back(New);
959 return QualType(New, 0);
960}
961
Eli Friedmanc5773c42008-02-15 18:16:39 +0000962QualType ASTContext::getIncompleteArrayType(QualType EltTy,
963 ArrayType::ArraySizeModifier ASM,
964 unsigned EltTypeQuals) {
965 llvm::FoldingSetNodeID ID;
966 IncompleteArrayType::Profile(ID, EltTy);
967
968 void *InsertPos = 0;
969 if (IncompleteArrayType *ATP =
970 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
971 return QualType(ATP, 0);
972
973 // If the element type isn't canonical, this won't be a canonical type
974 // either, so fill in the canonical type field.
975 QualType Canonical;
976
977 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000978 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000979 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000980
981 // Get the new insert position for the node we care about.
982 IncompleteArrayType *NewIP =
983 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000984 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000985 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000986
Steve Narofff83820b2009-01-27 22:08:43 +0000987 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000988 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000989
990 IncompleteArrayTypes.InsertNode(New, InsertPos);
991 Types.push_back(New);
992 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000993}
994
Steve Naroff73322922007-07-18 18:00:27 +0000995/// getVectorType - Return the unique reference to a vector type of
996/// the specified element type and size. VectorType must be a built-in type.
997QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 BuiltinType *baseType;
999
Chris Lattnerf52ab252008-04-06 22:59:24 +00001000 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001001 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001002
1003 // Check if we've already instantiated a vector of this type.
1004 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001005 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 void *InsertPos = 0;
1007 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1008 return QualType(VTP, 0);
1009
1010 // If the element type isn't canonical, this won't be a canonical type either,
1011 // so fill in the canonical type field.
1012 QualType Canonical;
1013 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001014 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015
1016 // Get the new insert position for the node we care about.
1017 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001018 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 }
Steve Narofff83820b2009-01-27 22:08:43 +00001020 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 VectorTypes.InsertNode(New, InsertPos);
1022 Types.push_back(New);
1023 return QualType(New, 0);
1024}
1025
Nate Begeman213541a2008-04-18 23:10:10 +00001026/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001027/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001028QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001029 BuiltinType *baseType;
1030
Chris Lattnerf52ab252008-04-06 22:59:24 +00001031 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001032 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001033
1034 // Check if we've already instantiated a vector of this type.
1035 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001036 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001037 void *InsertPos = 0;
1038 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1039 return QualType(VTP, 0);
1040
1041 // If the element type isn't canonical, this won't be a canonical type either,
1042 // so fill in the canonical type field.
1043 QualType Canonical;
1044 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001045 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001046
1047 // Get the new insert position for the node we care about.
1048 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001049 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001050 }
Steve Narofff83820b2009-01-27 22:08:43 +00001051 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001052 VectorTypes.InsertNode(New, InsertPos);
1053 Types.push_back(New);
1054 return QualType(New, 0);
1055}
1056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1058///
1059QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1060 // Unique functions, to guarantee there is only one function of a particular
1061 // structure.
1062 llvm::FoldingSetNodeID ID;
1063 FunctionTypeNoProto::Profile(ID, ResultTy);
1064
1065 void *InsertPos = 0;
1066 if (FunctionTypeNoProto *FT =
1067 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1068 return QualType(FT, 0);
1069
1070 QualType Canonical;
1071 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001072 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074 // Get the new insert position for the node we care about.
1075 FunctionTypeNoProto *NewIP =
1076 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001077 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 }
1079
Steve Narofff83820b2009-01-27 22:08:43 +00001080 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001082 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 return QualType(New, 0);
1084}
1085
1086/// getFunctionType - Return a normal function type with a typed argument
1087/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001088QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001089 unsigned NumArgs, bool isVariadic,
1090 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 // Unique functions, to guarantee there is only one function of a particular
1092 // structure.
1093 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001094 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1095 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096
1097 void *InsertPos = 0;
1098 if (FunctionTypeProto *FTP =
1099 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1100 return QualType(FTP, 0);
1101
1102 // Determine whether the type being created is already canonical or not.
1103 bool isCanonical = ResultTy->isCanonical();
1104 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1105 if (!ArgArray[i]->isCanonical())
1106 isCanonical = false;
1107
1108 // If this type isn't canonical, get the canonical version of it.
1109 QualType Canonical;
1110 if (!isCanonical) {
1111 llvm::SmallVector<QualType, 16> CanonicalArgs;
1112 CanonicalArgs.reserve(NumArgs);
1113 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001114 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001115
Chris Lattnerf52ab252008-04-06 22:59:24 +00001116 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001118 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001119
1120 // Get the new insert position for the node we care about.
1121 FunctionTypeProto *NewIP =
1122 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001123 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 }
1125
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001126 // FunctionTypeProto objects are allocated with extra bytes after them
1127 // for a variable size array (for parameter types) at the end of them.
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 FunctionTypeProto *FTP =
Steve Naroffc0ac4922009-01-27 23:20:32 +00001129 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1130 NumArgs*sizeof(QualType), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001132 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 Types.push_back(FTP);
1134 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1135 return QualType(FTP, 0);
1136}
1137
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001138/// getTypeDeclType - Return the unique reference to the type for the
1139/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001140QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001141 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001142 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1143
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001144 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001145 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001146 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1147 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001148 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001149 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001150
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001151 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001152 if (PrevDecl)
1153 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001154 else
1155 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001156 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001157 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001158 if (PrevDecl)
1159 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001160 else
1161 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001162 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001163 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1164 if (PrevDecl)
1165 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001166 else
1167 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001168 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001169 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001170 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001171
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001172 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001173 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001174}
1175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176/// getTypedefType - Return the unique reference to the type for the
1177/// specified typename decl.
1178QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1179 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1180
Chris Lattnerf52ab252008-04-06 22:59:24 +00001181 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Narofff83820b2009-01-27 22:08:43 +00001182 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 Types.push_back(Decl->TypeForDecl);
1184 return QualType(Decl->TypeForDecl, 0);
1185}
1186
Douglas Gregor72c3f312008-12-05 18:15:24 +00001187/// getTemplateTypeParmType - Return the unique reference to the type
1188/// for the specified template type parameter declaration.
1189QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1190 if (!Decl->TypeForDecl) {
Steve Narofff83820b2009-01-27 22:08:43 +00001191 Decl->TypeForDecl = new (*this,8) TemplateTypeParmType(Decl);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001192 Types.push_back(Decl->TypeForDecl);
1193 }
1194 return QualType(Decl->TypeForDecl, 0);
1195}
1196
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001197/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001198/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001199QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001200 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1201
Steve Narofff83820b2009-01-27 22:08:43 +00001202 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001203 Types.push_back(Decl->TypeForDecl);
1204 return QualType(Decl->TypeForDecl, 0);
1205}
1206
Chris Lattner88cb27a2008-04-07 04:56:42 +00001207/// CmpProtocolNames - Comparison predicate for sorting protocols
1208/// alphabetically.
1209static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1210 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001211 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001212}
1213
1214static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1215 unsigned &NumProtocols) {
1216 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1217
1218 // Sort protocols, keyed by name.
1219 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1220
1221 // Remove duplicates.
1222 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1223 NumProtocols = ProtocolsEnd-Protocols;
1224}
1225
1226
Chris Lattner065f0d72008-04-07 04:44:08 +00001227/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1228/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001229QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1230 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001231 // Sort the protocol list alphabetically to canonicalize it.
1232 SortAndUniqueProtocols(Protocols, NumProtocols);
1233
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001234 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001235 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001236
1237 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001238 if (ObjCQualifiedInterfaceType *QT =
1239 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001240 return QualType(QT, 0);
1241
1242 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001243 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001244 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001245
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001246 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001247 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001248 return QualType(QType, 0);
1249}
1250
Chris Lattner88cb27a2008-04-07 04:56:42 +00001251/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1252/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001253QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001254 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001255 // Sort the protocol list alphabetically to canonicalize it.
1256 SortAndUniqueProtocols(Protocols, NumProtocols);
1257
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001258 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001260
1261 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001262 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001263 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001264 return QualType(QT, 0);
1265
1266 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001267 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001268 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001269 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001270 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001271 return QualType(QType, 0);
1272}
1273
Steve Naroff9752f252007-08-01 18:02:17 +00001274/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1275/// TypeOfExpr AST's (since expression's are never shared). For example,
1276/// multiple declarations that refer to "typeof(x)" all contain different
1277/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1278/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001279QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001280 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Narofff83820b2009-01-27 22:08:43 +00001281 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001282 Types.push_back(toe);
1283 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001284}
1285
Steve Naroff9752f252007-08-01 18:02:17 +00001286/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1287/// TypeOfType AST's. The only motivation to unique these nodes would be
1288/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1289/// an issue. This doesn't effect the type checker, since it operates
1290/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001291QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001292 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001293 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001294 Types.push_back(tot);
1295 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001296}
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298/// getTagDeclType - Return the unique reference to the type for the
1299/// specified TagDecl (struct/union/class/enum) decl.
1300QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001301 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001302 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001303}
1304
1305/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1306/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1307/// needs to agree with the definition in <stddef.h>.
1308QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001309 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001310}
1311
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001312/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001313/// width of characters in wide strings, The value is target dependent and
1314/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001315QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001316 if (LangOpts.CPlusPlus)
1317 return WCharTy;
1318
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001319 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1320 // wide-character type?
1321 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001322}
1323
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001324/// getSignedWCharType - Return the type of "signed wchar_t".
1325/// Used when in C++, as a GCC extension.
1326QualType ASTContext::getSignedWCharType() const {
1327 // FIXME: derive from "Target" ?
1328 return WCharTy;
1329}
1330
1331/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1332/// Used when in C++, as a GCC extension.
1333QualType ASTContext::getUnsignedWCharType() const {
1334 // FIXME: derive from "Target" ?
1335 return UnsignedIntTy;
1336}
1337
Chris Lattner8b9023b2007-07-13 03:05:23 +00001338/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1339/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1340QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001341 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001342}
1343
Chris Lattnere6327742008-04-02 05:18:44 +00001344//===----------------------------------------------------------------------===//
1345// Type Operators
1346//===----------------------------------------------------------------------===//
1347
Chris Lattner77c96472008-04-06 22:41:35 +00001348/// getCanonicalType - Return the canonical (structural) type corresponding to
1349/// the specified potentially non-canonical type. The non-canonical version
1350/// of a type may have many "decorated" versions of types. Decorators can
1351/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1352/// to be free of any of these, allowing two canonical types to be compared
1353/// for exact equality with a simple pointer comparison.
1354QualType ASTContext::getCanonicalType(QualType T) {
1355 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001356
1357 // If the result has type qualifiers, make sure to canonicalize them as well.
1358 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1359 if (TypeQuals == 0) return CanType;
1360
1361 // If the type qualifiers are on an array type, get the canonical type of the
1362 // array with the qualifiers applied to the element type.
1363 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1364 if (!AT)
1365 return CanType.getQualifiedType(TypeQuals);
1366
1367 // Get the canonical version of the element with the extra qualifiers on it.
1368 // This can recursively sink qualifiers through multiple levels of arrays.
1369 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1370 NewEltTy = getCanonicalType(NewEltTy);
1371
1372 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1373 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1374 CAT->getIndexTypeQualifier());
1375 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1376 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1377 IAT->getIndexTypeQualifier());
1378
Douglas Gregor898574e2008-12-05 23:32:09 +00001379 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1380 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1381 DSAT->getSizeModifier(),
1382 DSAT->getIndexTypeQualifier());
1383
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001384 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1385 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1386 VAT->getSizeModifier(),
1387 VAT->getIndexTypeQualifier());
1388}
1389
1390
1391const ArrayType *ASTContext::getAsArrayType(QualType T) {
1392 // Handle the non-qualified case efficiently.
1393 if (T.getCVRQualifiers() == 0) {
1394 // Handle the common positive case fast.
1395 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1396 return AT;
1397 }
1398
1399 // Handle the common negative case fast, ignoring CVR qualifiers.
1400 QualType CType = T->getCanonicalTypeInternal();
1401
1402 // Make sure to look through type qualifiers (like ASQuals) for the negative
1403 // test.
1404 if (!isa<ArrayType>(CType) &&
1405 !isa<ArrayType>(CType.getUnqualifiedType()))
1406 return 0;
1407
1408 // Apply any CVR qualifiers from the array type to the element type. This
1409 // implements C99 6.7.3p8: "If the specification of an array type includes
1410 // any type qualifiers, the element type is so qualified, not the array type."
1411
1412 // If we get here, we either have type qualifiers on the type, or we have
1413 // sugar such as a typedef in the way. If we have type qualifiers on the type
1414 // we must propagate them down into the elemeng type.
1415 unsigned CVRQuals = T.getCVRQualifiers();
1416 unsigned AddrSpace = 0;
1417 Type *Ty = T.getTypePtr();
1418
1419 // Rip through ASQualType's and typedefs to get to a concrete type.
1420 while (1) {
1421 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1422 AddrSpace = ASQT->getAddressSpace();
1423 Ty = ASQT->getBaseType();
1424 } else {
1425 T = Ty->getDesugaredType();
1426 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1427 break;
1428 CVRQuals |= T.getCVRQualifiers();
1429 Ty = T.getTypePtr();
1430 }
1431 }
1432
1433 // If we have a simple case, just return now.
1434 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1435 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1436 return ATy;
1437
1438 // Otherwise, we have an array and we have qualifiers on it. Push the
1439 // qualifiers into the array element type and return a new array type.
1440 // Get the canonical version of the element with the extra qualifiers on it.
1441 // This can recursively sink qualifiers through multiple levels of arrays.
1442 QualType NewEltTy = ATy->getElementType();
1443 if (AddrSpace)
1444 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1445 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1446
1447 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1448 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1449 CAT->getSizeModifier(),
1450 CAT->getIndexTypeQualifier()));
1451 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1452 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1453 IAT->getSizeModifier(),
1454 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001455
Douglas Gregor898574e2008-12-05 23:32:09 +00001456 if (const DependentSizedArrayType *DSAT
1457 = dyn_cast<DependentSizedArrayType>(ATy))
1458 return cast<ArrayType>(
1459 getDependentSizedArrayType(NewEltTy,
1460 DSAT->getSizeExpr(),
1461 DSAT->getSizeModifier(),
1462 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001463
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001464 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1465 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1466 VAT->getSizeModifier(),
1467 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001468}
1469
1470
Chris Lattnere6327742008-04-02 05:18:44 +00001471/// getArrayDecayedType - Return the properly qualified result of decaying the
1472/// specified array type to a pointer. This operation is non-trivial when
1473/// handling typedefs etc. The canonical type of "T" must be an array type,
1474/// this returns a pointer to a properly qualified element of the array.
1475///
1476/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1477QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001478 // Get the element type with 'getAsArrayType' so that we don't lose any
1479 // typedefs in the element type of the array. This also handles propagation
1480 // of type qualifiers from the array type into the element type if present
1481 // (C99 6.7.3p8).
1482 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1483 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001484
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001485 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001486
1487 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001488 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001489}
1490
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001491QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001492 QualType ElemTy = VAT->getElementType();
1493
1494 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1495 return getBaseElementType(VAT);
1496
1497 return ElemTy;
1498}
1499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500/// getFloatingRank - Return a relative rank for floating point types.
1501/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001502static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001503 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001505
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001506 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001507 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001508 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 case BuiltinType::Float: return FloatRank;
1510 case BuiltinType::Double: return DoubleRank;
1511 case BuiltinType::LongDouble: return LongDoubleRank;
1512 }
1513}
1514
Steve Naroff716c7302007-08-27 01:41:48 +00001515/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1516/// point or a complex type (based on typeDomain/typeSize).
1517/// 'typeDomain' is a real floating point or complex type.
1518/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001519QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1520 QualType Domain) const {
1521 FloatingRank EltRank = getFloatingRank(Size);
1522 if (Domain->isComplexType()) {
1523 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001524 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001525 case FloatRank: return FloatComplexTy;
1526 case DoubleRank: return DoubleComplexTy;
1527 case LongDoubleRank: return LongDoubleComplexTy;
1528 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 }
Chris Lattner1361b112008-04-06 23:58:54 +00001530
1531 assert(Domain->isRealFloatingType() && "Unknown domain!");
1532 switch (EltRank) {
1533 default: assert(0 && "getFloatingRank(): illegal value for rank");
1534 case FloatRank: return FloatTy;
1535 case DoubleRank: return DoubleTy;
1536 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001537 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001538}
1539
Chris Lattner7cfeb082008-04-06 23:55:33 +00001540/// getFloatingTypeOrder - Compare the rank of the two specified floating
1541/// point types, ignoring the domain of the type (i.e. 'double' ==
1542/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1543/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001544int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1545 FloatingRank LHSR = getFloatingRank(LHS);
1546 FloatingRank RHSR = getFloatingRank(RHS);
1547
1548 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001549 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001550 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001551 return 1;
1552 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001553}
1554
Chris Lattnerf52ab252008-04-06 22:59:24 +00001555/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1556/// routine will assert if passed a built-in type that isn't an integer or enum,
1557/// or if it is not canonicalized.
1558static unsigned getIntegerRank(Type *T) {
1559 assert(T->isCanonical() && "T should be canonicalized");
1560 if (isa<EnumType>(T))
1561 return 4;
1562
1563 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001564 default: assert(0 && "getIntegerRank(): not a built-in integer");
1565 case BuiltinType::Bool:
1566 return 1;
1567 case BuiltinType::Char_S:
1568 case BuiltinType::Char_U:
1569 case BuiltinType::SChar:
1570 case BuiltinType::UChar:
1571 return 2;
1572 case BuiltinType::Short:
1573 case BuiltinType::UShort:
1574 return 3;
1575 case BuiltinType::Int:
1576 case BuiltinType::UInt:
1577 return 4;
1578 case BuiltinType::Long:
1579 case BuiltinType::ULong:
1580 return 5;
1581 case BuiltinType::LongLong:
1582 case BuiltinType::ULongLong:
1583 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001584 }
1585}
1586
Chris Lattner7cfeb082008-04-06 23:55:33 +00001587/// getIntegerTypeOrder - Returns the highest ranked integer type:
1588/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1589/// LHS < RHS, return -1.
1590int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001591 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1592 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001593 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001594
Chris Lattnerf52ab252008-04-06 22:59:24 +00001595 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1596 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001597
Chris Lattner7cfeb082008-04-06 23:55:33 +00001598 unsigned LHSRank = getIntegerRank(LHSC);
1599 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001600
Chris Lattner7cfeb082008-04-06 23:55:33 +00001601 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1602 if (LHSRank == RHSRank) return 0;
1603 return LHSRank > RHSRank ? 1 : -1;
1604 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001605
Chris Lattner7cfeb082008-04-06 23:55:33 +00001606 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1607 if (LHSUnsigned) {
1608 // If the unsigned [LHS] type is larger, return it.
1609 if (LHSRank >= RHSRank)
1610 return 1;
1611
1612 // If the signed type can represent all values of the unsigned type, it
1613 // wins. Because we are dealing with 2's complement and types that are
1614 // powers of two larger than each other, this is always safe.
1615 return -1;
1616 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001617
Chris Lattner7cfeb082008-04-06 23:55:33 +00001618 // If the unsigned [RHS] type is larger, return it.
1619 if (RHSRank >= LHSRank)
1620 return -1;
1621
1622 // If the signed type can represent all values of the unsigned type, it
1623 // wins. Because we are dealing with 2's complement and types that are
1624 // powers of two larger than each other, this is always safe.
1625 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001626}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001627
1628// getCFConstantStringType - Return the type used for constant CFStrings.
1629QualType ASTContext::getCFConstantStringType() {
1630 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001631 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001632 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001633 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001634 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001635
1636 // const int *isa;
1637 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001638 // int flags;
1639 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001640 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001641 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001642 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001643 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001644
Anders Carlsson71993dd2007-08-17 05:31:46 +00001645 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001646 for (unsigned i = 0; i < 4; ++i) {
1647 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1648 SourceLocation(), 0,
1649 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001650 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001651 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001652 }
1653
1654 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001655 }
1656
1657 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001658}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001659
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001660QualType ASTContext::getObjCFastEnumerationStateType()
1661{
1662 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001663 ObjCFastEnumerationStateTypeDecl =
1664 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1665 &Idents.get("__objcFastEnumerationState"));
1666
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001667 QualType FieldTypes[] = {
1668 UnsignedLongTy,
1669 getPointerType(ObjCIdType),
1670 getPointerType(UnsignedLongTy),
1671 getConstantArrayType(UnsignedLongTy,
1672 llvm::APInt(32, 5), ArrayType::Normal, 0)
1673 };
1674
Douglas Gregor44b43212008-12-11 16:49:14 +00001675 for (size_t i = 0; i < 4; ++i) {
1676 FieldDecl *Field = FieldDecl::Create(*this,
1677 ObjCFastEnumerationStateTypeDecl,
1678 SourceLocation(), 0,
1679 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001680 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001681 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001682 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001683
Douglas Gregor44b43212008-12-11 16:49:14 +00001684 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001685 }
1686
1687 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1688}
1689
Anders Carlssone8c49532007-10-29 06:33:42 +00001690// This returns true if a type has been typedefed to BOOL:
1691// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001692static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001693 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001694 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1695 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001696
1697 return false;
1698}
1699
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001700/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001701/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001702int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001703 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001704
1705 // Make all integer and enum types at least as large as an int
1706 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001707 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001708 // Treat arrays as pointers, since that's how they're passed in.
1709 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001710 sz = getTypeSize(VoidPtrTy);
1711 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001712}
1713
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001714/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001715/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001716void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001717 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001718 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001719 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001720 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001721 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001722 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001723 // Compute size of all parameters.
1724 // Start with computing size of a pointer in number of bytes.
1725 // FIXME: There might(should) be a better way of doing this computation!
1726 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001727 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001728 // The first two arguments (self and _cmd) are pointers; account for
1729 // their size.
1730 int ParmOffset = 2 * PtrSize;
1731 int NumOfParams = Decl->getNumParams();
1732 for (int i = 0; i < NumOfParams; i++) {
1733 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 int sz = getObjCEncodingTypeSize (PType);
1735 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001736 ParmOffset += sz;
1737 }
1738 S += llvm::utostr(ParmOffset);
1739 S += "@0:";
1740 S += llvm::utostr(PtrSize);
1741
1742 // Argument types.
1743 ParmOffset = 2 * PtrSize;
1744 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001745 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1746 QualType PType = PVDecl->getOriginalType();
1747 if (const ArrayType *AT =
1748 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1749 // Use array's original type only if it has known number of
1750 // elements.
1751 if (!dyn_cast<ConstantArrayType>(AT))
1752 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001753 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001754 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001755 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001756 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001757 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001758 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001759 }
1760}
1761
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001762/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001763/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001764/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1765/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001766/// Property attributes are stored as a comma-delimited C string. The simple
1767/// attributes readonly and bycopy are encoded as single characters. The
1768/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1769/// encoded as single characters, followed by an identifier. Property types
1770/// are also encoded as a parametrized attribute. The characters used to encode
1771/// these attributes are defined by the following enumeration:
1772/// @code
1773/// enum PropertyAttributes {
1774/// kPropertyReadOnly = 'R', // property is read-only.
1775/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1776/// kPropertyByref = '&', // property is a reference to the value last assigned
1777/// kPropertyDynamic = 'D', // property is dynamic
1778/// kPropertyGetter = 'G', // followed by getter selector name
1779/// kPropertySetter = 'S', // followed by setter selector name
1780/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1781/// kPropertyType = 't' // followed by old-style type encoding.
1782/// kPropertyWeak = 'W' // 'weak' property
1783/// kPropertyStrong = 'P' // property GC'able
1784/// kPropertyNonAtomic = 'N' // property non-atomic
1785/// };
1786/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001787void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1788 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001789 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001790 // Collect information from the property implementation decl(s).
1791 bool Dynamic = false;
1792 ObjCPropertyImplDecl *SynthesizePID = 0;
1793
1794 // FIXME: Duplicated code due to poor abstraction.
1795 if (Container) {
1796 if (const ObjCCategoryImplDecl *CID =
1797 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1798 for (ObjCCategoryImplDecl::propimpl_iterator
1799 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1800 ObjCPropertyImplDecl *PID = *i;
1801 if (PID->getPropertyDecl() == PD) {
1802 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1803 Dynamic = true;
1804 } else {
1805 SynthesizePID = PID;
1806 }
1807 }
1808 }
1809 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001810 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001811 for (ObjCCategoryImplDecl::propimpl_iterator
1812 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1813 ObjCPropertyImplDecl *PID = *i;
1814 if (PID->getPropertyDecl() == PD) {
1815 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1816 Dynamic = true;
1817 } else {
1818 SynthesizePID = PID;
1819 }
1820 }
1821 }
1822 }
1823 }
1824
1825 // FIXME: This is not very efficient.
1826 S = "T";
1827
1828 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001829 // GCC has some special rules regarding encoding of properties which
1830 // closely resembles encoding of ivars.
1831 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1832 true /* outermost type */,
1833 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001834
1835 if (PD->isReadOnly()) {
1836 S += ",R";
1837 } else {
1838 switch (PD->getSetterKind()) {
1839 case ObjCPropertyDecl::Assign: break;
1840 case ObjCPropertyDecl::Copy: S += ",C"; break;
1841 case ObjCPropertyDecl::Retain: S += ",&"; break;
1842 }
1843 }
1844
1845 // It really isn't clear at all what this means, since properties
1846 // are "dynamic by default".
1847 if (Dynamic)
1848 S += ",D";
1849
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001850 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1851 S += ",N";
1852
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001853 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1854 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001855 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001856 }
1857
1858 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1859 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001860 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001861 }
1862
1863 if (SynthesizePID) {
1864 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1865 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001866 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001867 }
1868
1869 // FIXME: OBJCGC: weak & strong
1870}
1871
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001872/// getLegacyIntegralTypeEncoding -
1873/// Another legacy compatibility encoding: 32-bit longs are encoded as
1874/// 'l' or 'L', but not always. For typedefs, we need to use
1875/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1876///
1877void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1878 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1879 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1880 if (BT->getKind() == BuiltinType::ULong)
1881 PointeeTy = UnsignedIntTy;
1882 else if (BT->getKind() == BuiltinType::Long)
1883 PointeeTy = IntTy;
1884 }
1885 }
1886}
1887
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001888void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001889 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001890 // We follow the behavior of gcc, expanding structures which are
1891 // directly pointed to, and expanding embedded structures. Note that
1892 // these rules are sufficient to prevent recursive encoding of the
1893 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001894 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1895 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001896}
1897
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001898static void EncodeBitField(const ASTContext *Context, std::string& S,
1899 FieldDecl *FD) {
1900 const Expr *E = FD->getBitWidth();
1901 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1902 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1903 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1904 S += 'b';
1905 S += llvm::utostr(N);
1906}
1907
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001908void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1909 bool ExpandPointedToStructures,
1910 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001911 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001912 bool OutermostType,
1913 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001914 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001915 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001916 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001917 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001918 else {
1919 char encoding;
1920 switch (BT->getKind()) {
1921 default: assert(0 && "Unhandled builtin type kind");
1922 case BuiltinType::Void: encoding = 'v'; break;
1923 case BuiltinType::Bool: encoding = 'B'; break;
1924 case BuiltinType::Char_U:
1925 case BuiltinType::UChar: encoding = 'C'; break;
1926 case BuiltinType::UShort: encoding = 'S'; break;
1927 case BuiltinType::UInt: encoding = 'I'; break;
1928 case BuiltinType::ULong: encoding = 'L'; break;
1929 case BuiltinType::ULongLong: encoding = 'Q'; break;
1930 case BuiltinType::Char_S:
1931 case BuiltinType::SChar: encoding = 'c'; break;
1932 case BuiltinType::Short: encoding = 's'; break;
1933 case BuiltinType::Int: encoding = 'i'; break;
1934 case BuiltinType::Long: encoding = 'l'; break;
1935 case BuiltinType::LongLong: encoding = 'q'; break;
1936 case BuiltinType::Float: encoding = 'f'; break;
1937 case BuiltinType::Double: encoding = 'd'; break;
1938 case BuiltinType::LongDouble: encoding = 'd'; break;
1939 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001940
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001941 S += encoding;
1942 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001943 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001944 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001945 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1946 ExpandPointedToStructures,
1947 ExpandStructures, FD);
1948 if (FD || EncodingProperty) {
1949 // Note that we do extended encoding of protocol qualifer list
1950 // Only when doing ivar or property encoding.
1951 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1952 S += '"';
1953 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1954 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1955 S += '<';
1956 S += Proto->getNameAsString();
1957 S += '>';
1958 }
1959 S += '"';
1960 }
1961 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001962 }
1963 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001964 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001965 bool isReadOnly = false;
1966 // For historical/compatibility reasons, the read-only qualifier of the
1967 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1968 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1969 // Also, do not emit the 'r' for anything but the outermost type!
1970 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1971 if (OutermostType && T.isConstQualified()) {
1972 isReadOnly = true;
1973 S += 'r';
1974 }
1975 }
1976 else if (OutermostType) {
1977 QualType P = PointeeTy;
1978 while (P->getAsPointerType())
1979 P = P->getAsPointerType()->getPointeeType();
1980 if (P.isConstQualified()) {
1981 isReadOnly = true;
1982 S += 'r';
1983 }
1984 }
1985 if (isReadOnly) {
1986 // Another legacy compatibility encoding. Some ObjC qualifier and type
1987 // combinations need to be rearranged.
1988 // Rewrite "in const" from "nr" to "rn"
1989 const char * s = S.c_str();
1990 int len = S.length();
1991 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
1992 std::string replace = "rn";
1993 S.replace(S.end()-2, S.end(), replace);
1994 }
1995 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001996 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001997 S += '@';
1998 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001999 }
2000 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002001 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2002 // Another historical/compatibility reason.
2003 // We encode the underlying type which comes out as
2004 // {...};
2005 S += '^';
2006 getObjCEncodingForTypeImpl(PointeeTy, S,
2007 false, ExpandPointedToStructures,
2008 NULL);
2009 return;
2010 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002011 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002012 if (FD || EncodingProperty) {
2013 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2014 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002015 S += '"';
2016 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002017 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2018 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2019 S += '<';
2020 S += Proto->getNameAsString();
2021 S += '>';
2022 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002023 S += '"';
2024 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002025 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002026 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002027 S += '#';
2028 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002029 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002030 S += ':';
2031 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002032 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002033
2034 if (PointeeTy->isCharType()) {
2035 // char pointer types should be encoded as '*' unless it is a
2036 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002037 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002038 S += '*';
2039 return;
2040 }
2041 }
2042
2043 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002044 getLegacyIntegralTypeEncoding(PointeeTy);
2045
2046 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002047 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002048 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002049 } else if (const ArrayType *AT =
2050 // Ignore type qualifiers etc.
2051 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002052 S += '[';
2053
2054 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2055 S += llvm::utostr(CAT->getSize().getZExtValue());
2056 else
2057 assert(0 && "Unhandled array type!");
2058
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002059 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002060 false, ExpandStructures, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002061 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002062 } else if (T->getAsFunctionType()) {
2063 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002064 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002065 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002066 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002067 // Anonymous structures print as '?'
2068 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2069 S += II->getName();
2070 } else {
2071 S += '?';
2072 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002073 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002074 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002075 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2076 FieldEnd = RDecl->field_end();
2077 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002078 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002079 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002080 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002081 S += '"';
2082 }
2083
2084 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002085 if (Field->isBitField()) {
2086 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2087 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002088 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002089 QualType qt = Field->getType();
2090 getLegacyIntegralTypeEncoding(qt);
2091 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002092 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002093 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002094 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002095 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002096 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002097 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002098 if (FD && FD->isBitField())
2099 EncodeBitField(this, S, FD);
2100 else
2101 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002102 } else if (T->isBlockPointerType()) {
2103 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002104 } else if (T->isObjCInterfaceType()) {
2105 // @encode(class_name)
2106 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2107 S += '{';
2108 const IdentifierInfo *II = OI->getIdentifier();
2109 S += II->getName();
2110 S += '=';
2111 std::vector<FieldDecl*> RecFields;
2112 CollectObjCIvars(OI, RecFields);
2113 for (unsigned int i = 0; i != RecFields.size(); i++) {
2114 if (RecFields[i]->isBitField())
2115 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2116 RecFields[i]);
2117 else
2118 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2119 FD);
2120 }
2121 S += '}';
2122 }
2123 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002124 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002125}
2126
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002127void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002128 std::string& S) const {
2129 if (QT & Decl::OBJC_TQ_In)
2130 S += 'n';
2131 if (QT & Decl::OBJC_TQ_Inout)
2132 S += 'N';
2133 if (QT & Decl::OBJC_TQ_Out)
2134 S += 'o';
2135 if (QT & Decl::OBJC_TQ_Bycopy)
2136 S += 'O';
2137 if (QT & Decl::OBJC_TQ_Byref)
2138 S += 'R';
2139 if (QT & Decl::OBJC_TQ_Oneway)
2140 S += 'V';
2141}
2142
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002143void ASTContext::setBuiltinVaListType(QualType T)
2144{
2145 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2146
2147 BuiltinVaListType = T;
2148}
2149
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002150void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002151{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002152 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002153
2154 // typedef struct objc_object *id;
2155 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002156 // User error - caller will issue diagnostics.
2157 if (!ptr)
2158 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002159 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002160 // User error - caller will issue diagnostics.
2161 if (!rec)
2162 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002163 IdStructType = rec;
2164}
2165
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002166void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002167{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002168 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002169
2170 // typedef struct objc_selector *SEL;
2171 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002172 if (!ptr)
2173 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002174 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002175 if (!rec)
2176 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002177 SelStructType = rec;
2178}
2179
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002180void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002181{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002182 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002183}
2184
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002185void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002186{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002187 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002188
2189 // typedef struct objc_class *Class;
2190 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2191 assert(ptr && "'Class' incorrectly typed");
2192 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2193 assert(rec && "'Class' incorrectly typed");
2194 ClassStructType = rec;
2195}
2196
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002197void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2198 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002199 "'NSConstantString' type already set!");
2200
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002201 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002202}
2203
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002204/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002205/// TargetInfo, produce the corresponding type. The unsigned @p Type
2206/// is actually a value of type @c TargetInfo::IntType.
2207QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002208 switch (Type) {
2209 case TargetInfo::NoInt: return QualType();
2210 case TargetInfo::SignedShort: return ShortTy;
2211 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2212 case TargetInfo::SignedInt: return IntTy;
2213 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2214 case TargetInfo::SignedLong: return LongTy;
2215 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2216 case TargetInfo::SignedLongLong: return LongLongTy;
2217 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2218 }
2219
2220 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002221 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002222}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002223
2224//===----------------------------------------------------------------------===//
2225// Type Predicates.
2226//===----------------------------------------------------------------------===//
2227
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002228/// isObjCNSObjectType - Return true if this is an NSObject object using
2229/// NSObject attribute on a c-style pointer type.
2230/// FIXME - Make it work directly on types.
2231///
2232bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2233 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2234 if (TypedefDecl *TD = TDT->getDecl())
2235 if (TD->getAttr<ObjCNSObjectAttr>())
2236 return true;
2237 }
2238 return false;
2239}
2240
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002241/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2242/// to an object type. This includes "id" and "Class" (two 'special' pointers
2243/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2244/// ID type).
2245bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2246 if (Ty->isObjCQualifiedIdType())
2247 return true;
2248
Steve Naroff6ae98502008-10-21 18:24:04 +00002249 // Blocks are objects.
2250 if (Ty->isBlockPointerType())
2251 return true;
2252
2253 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002254 if (!Ty->isPointerType())
2255 return false;
2256
2257 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2258 // pointer types. This looks for the typedef specifically, not for the
2259 // underlying type.
2260 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2261 return true;
2262
2263 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002264 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2265 return true;
2266
2267 // If is has NSObject attribute, OK as well.
2268 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002269}
2270
Chris Lattner6ac46a42008-04-07 06:51:04 +00002271//===----------------------------------------------------------------------===//
2272// Type Compatibility Testing
2273//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002274
Steve Naroff1c7d0672008-09-04 15:10:53 +00002275/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002276/// block types. Types must be strictly compatible here. For example,
2277/// C unfortunately doesn't produce an error for the following:
2278///
2279/// int (*emptyArgFunc)();
2280/// int (*intArgList)(int) = emptyArgFunc;
2281///
2282/// For blocks, we will produce an error for the following (similar to C++):
2283///
2284/// int (^emptyArgBlock)();
2285/// int (^intArgBlock)(int) = emptyArgBlock;
2286///
2287/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2288///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002289bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002290 const FunctionType *lbase = lhs->getAsFunctionType();
2291 const FunctionType *rbase = rhs->getAsFunctionType();
2292 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2293 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2294 if (lproto && rproto)
2295 return !mergeTypes(lhs, rhs).isNull();
2296 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002297}
2298
Chris Lattner6ac46a42008-04-07 06:51:04 +00002299/// areCompatVectorTypes - Return true if the two specified vector types are
2300/// compatible.
2301static bool areCompatVectorTypes(const VectorType *LHS,
2302 const VectorType *RHS) {
2303 assert(LHS->isCanonical() && RHS->isCanonical());
2304 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002305 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002306}
2307
Eli Friedman3d815e72008-08-22 00:56:42 +00002308/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002309/// compatible for assignment from RHS to LHS. This handles validation of any
2310/// protocol qualifiers on the LHS or RHS.
2311///
Eli Friedman3d815e72008-08-22 00:56:42 +00002312bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2313 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002314 // Verify that the base decls are compatible: the RHS must be a subclass of
2315 // the LHS.
2316 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2317 return false;
2318
2319 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2320 // protocol qualified at all, then we are good.
2321 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2322 return true;
2323
2324 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2325 // isn't a superset.
2326 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2327 return true; // FIXME: should return false!
2328
2329 // Finally, we must have two protocol-qualified interfaces.
2330 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2331 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2332 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2333 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2334 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2335 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2336
2337 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2338 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2339 // LHS in a single parallel scan until we run out of elements in LHS.
2340 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2341 ObjCProtocolDecl *LHSProto = *LHSPI;
2342
2343 while (RHSPI != RHSPE) {
2344 ObjCProtocolDecl *RHSProto = *RHSPI++;
2345 // If the RHS has a protocol that the LHS doesn't, ignore it.
2346 if (RHSProto != LHSProto)
2347 continue;
2348
2349 // Otherwise, the RHS does have this element.
2350 ++LHSPI;
2351 if (LHSPI == LHSPE)
2352 return true; // All protocols in LHS exist in RHS.
2353
2354 LHSProto = *LHSPI;
2355 }
2356
2357 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2358 return false;
2359}
2360
Steve Naroffec0550f2007-10-15 20:41:53 +00002361/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2362/// both shall have the identically qualified version of a compatible type.
2363/// C99 6.2.7p1: Two types have compatible types if their types are the
2364/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002365bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2366 return !mergeTypes(LHS, RHS).isNull();
2367}
2368
2369QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2370 const FunctionType *lbase = lhs->getAsFunctionType();
2371 const FunctionType *rbase = rhs->getAsFunctionType();
2372 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2373 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2374 bool allLTypes = true;
2375 bool allRTypes = true;
2376
2377 // Check return type
2378 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2379 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002380 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2381 allLTypes = false;
2382 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2383 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002384
2385 if (lproto && rproto) { // two C99 style function prototypes
2386 unsigned lproto_nargs = lproto->getNumArgs();
2387 unsigned rproto_nargs = rproto->getNumArgs();
2388
2389 // Compatible functions must have the same number of arguments
2390 if (lproto_nargs != rproto_nargs)
2391 return QualType();
2392
2393 // Variadic and non-variadic functions aren't compatible
2394 if (lproto->isVariadic() != rproto->isVariadic())
2395 return QualType();
2396
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002397 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2398 return QualType();
2399
Eli Friedman3d815e72008-08-22 00:56:42 +00002400 // Check argument compatibility
2401 llvm::SmallVector<QualType, 10> types;
2402 for (unsigned i = 0; i < lproto_nargs; i++) {
2403 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2404 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2405 QualType argtype = mergeTypes(largtype, rargtype);
2406 if (argtype.isNull()) return QualType();
2407 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002408 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2409 allLTypes = false;
2410 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2411 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002412 }
2413 if (allLTypes) return lhs;
2414 if (allRTypes) return rhs;
2415 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002416 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002417 }
2418
2419 if (lproto) allRTypes = false;
2420 if (rproto) allLTypes = false;
2421
2422 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2423 if (proto) {
2424 if (proto->isVariadic()) return QualType();
2425 // Check that the types are compatible with the types that
2426 // would result from default argument promotions (C99 6.7.5.3p15).
2427 // The only types actually affected are promotable integer
2428 // types and floats, which would be passed as a different
2429 // type depending on whether the prototype is visible.
2430 unsigned proto_nargs = proto->getNumArgs();
2431 for (unsigned i = 0; i < proto_nargs; ++i) {
2432 QualType argTy = proto->getArgType(i);
2433 if (argTy->isPromotableIntegerType() ||
2434 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2435 return QualType();
2436 }
2437
2438 if (allLTypes) return lhs;
2439 if (allRTypes) return rhs;
2440 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002441 proto->getNumArgs(), lproto->isVariadic(),
2442 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002443 }
2444
2445 if (allLTypes) return lhs;
2446 if (allRTypes) return rhs;
2447 return getFunctionTypeNoProto(retType);
2448}
2449
2450QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002451 // C++ [expr]: If an expression initially has the type "reference to T", the
2452 // type is adjusted to "T" prior to any further analysis, the expression
2453 // designates the object or function denoted by the reference, and the
2454 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002455 // FIXME: C++ shouldn't be going through here! The rules are different
2456 // enough that they should be handled separately.
2457 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002458 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002459 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002460 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002461
Eli Friedman3d815e72008-08-22 00:56:42 +00002462 QualType LHSCan = getCanonicalType(LHS),
2463 RHSCan = getCanonicalType(RHS);
2464
2465 // If two types are identical, they are compatible.
2466 if (LHSCan == RHSCan)
2467 return LHS;
2468
2469 // If the qualifiers are different, the types aren't compatible
2470 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2471 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2472 return QualType();
2473
2474 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2475 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2476
Chris Lattner1adb8832008-01-14 05:45:46 +00002477 // We want to consider the two function types to be the same for these
2478 // comparisons, just force one to the other.
2479 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2480 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002481
2482 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002483 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2484 LHSClass = Type::ConstantArray;
2485 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2486 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002487
Nate Begeman213541a2008-04-18 23:10:10 +00002488 // Canonicalize ExtVector -> Vector.
2489 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2490 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002491
Chris Lattnerb0489812008-04-07 06:38:24 +00002492 // Consider qualified interfaces and interfaces the same.
2493 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2494 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002495
Chris Lattnera36a61f2008-04-07 05:43:21 +00002496 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002497 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002498 // ID is compatible with all qualified id types.
2499 if (LHS->isObjCQualifiedIdType()) {
2500 if (const PointerType *PT = RHS->getAsPointerType()) {
2501 QualType pType = PT->getPointeeType();
2502 if (isObjCIdType(pType))
2503 return LHS;
2504 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2505 // Unfortunately, this API is part of Sema (which we don't have access
2506 // to. Need to refactor. The following check is insufficient, since we
2507 // need to make sure the class implements the protocol.
2508 if (pType->isObjCInterfaceType())
2509 return LHS;
2510 }
2511 }
2512 if (RHS->isObjCQualifiedIdType()) {
2513 if (const PointerType *PT = LHS->getAsPointerType()) {
2514 QualType pType = PT->getPointeeType();
2515 if (isObjCIdType(pType))
2516 return RHS;
2517 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2518 // Unfortunately, this API is part of Sema (which we don't have access
2519 // to. Need to refactor. The following check is insufficient, since we
2520 // need to make sure the class implements the protocol.
2521 if (pType->isObjCInterfaceType())
2522 return RHS;
2523 }
2524 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002525 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2526 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002527 if (const EnumType* ETy = LHS->getAsEnumType()) {
2528 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2529 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002530 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002531 if (const EnumType* ETy = RHS->getAsEnumType()) {
2532 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2533 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002534 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002535
Eli Friedman3d815e72008-08-22 00:56:42 +00002536 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002537 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002538
Steve Naroff4a746782008-01-09 22:43:08 +00002539 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002540 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002541 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002542 {
2543 // Merge two pointer types, while trying to preserve typedef info
2544 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2545 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2546 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2547 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002548 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2549 return LHS;
2550 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2551 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002552 return getPointerType(ResultType);
2553 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002554 case Type::BlockPointer:
2555 {
2556 // Merge two block pointer types, while trying to preserve typedef info
2557 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2558 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2559 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2560 if (ResultType.isNull()) return QualType();
2561 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2562 return LHS;
2563 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2564 return RHS;
2565 return getBlockPointerType(ResultType);
2566 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002567 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002568 {
2569 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2570 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2571 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2572 return QualType();
2573
2574 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2575 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2576 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2577 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002578 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2579 return LHS;
2580 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2581 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002582 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2583 ArrayType::ArraySizeModifier(), 0);
2584 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2585 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002586 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2587 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002588 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2589 return LHS;
2590 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2591 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002592 if (LVAT) {
2593 // FIXME: This isn't correct! But tricky to implement because
2594 // the array's size has to be the size of LHS, but the type
2595 // has to be different.
2596 return LHS;
2597 }
2598 if (RVAT) {
2599 // FIXME: This isn't correct! But tricky to implement because
2600 // the array's size has to be the size of RHS, but the type
2601 // has to be different.
2602 return RHS;
2603 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002604 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2605 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002606 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002607 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002608 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002609 return mergeFunctionTypes(LHS, RHS);
2610 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002611 // FIXME: Why are these compatible?
2612 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2613 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2614 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002615 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002616 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002617 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002618 case Type::Complex:
2619 // Distinct complex types are incompatible.
2620 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002621 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002622 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2623 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002624 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002625 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002626 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2627 // for checking assignment/comparison safety
2628 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002629 case Type::ObjCQualifiedId:
2630 // Distinct qualified id's are not compatible.
2631 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002632 default:
2633 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002634 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002635 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002636}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002637
Chris Lattner5426bf62008-04-07 07:01:58 +00002638//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002639// Integer Predicates
2640//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002641
Eli Friedmanad74a752008-06-28 06:23:08 +00002642unsigned ASTContext::getIntWidth(QualType T) {
2643 if (T == BoolTy)
2644 return 1;
2645 // At the moment, only bool has padding bits
2646 return (unsigned)getTypeSize(T);
2647}
2648
2649QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2650 assert(T->isSignedIntegerType() && "Unexpected type");
2651 if (const EnumType* ETy = T->getAsEnumType())
2652 T = ETy->getDecl()->getIntegerType();
2653 const BuiltinType* BTy = T->getAsBuiltinType();
2654 assert (BTy && "Unexpected signed integer type");
2655 switch (BTy->getKind()) {
2656 case BuiltinType::Char_S:
2657 case BuiltinType::SChar:
2658 return UnsignedCharTy;
2659 case BuiltinType::Short:
2660 return UnsignedShortTy;
2661 case BuiltinType::Int:
2662 return UnsignedIntTy;
2663 case BuiltinType::Long:
2664 return UnsignedLongTy;
2665 case BuiltinType::LongLong:
2666 return UnsignedLongLongTy;
2667 default:
2668 assert(0 && "Unexpected signed integer type");
2669 return QualType();
2670 }
2671}
2672
2673
2674//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002675// Serialization Support
2676//===----------------------------------------------------------------------===//
2677
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002678/// Emit - Serialize an ASTContext object to Bitcode.
2679void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002680 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002681 S.EmitRef(SourceMgr);
2682 S.EmitRef(Target);
2683 S.EmitRef(Idents);
2684 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002685
Ted Kremenekfee04522007-10-31 22:44:07 +00002686 // Emit the size of the type vector so that we can reserve that size
2687 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002688 S.EmitInt(Types.size());
2689
Ted Kremenek03ed4402007-11-13 22:02:55 +00002690 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2691 I!=E;++I)
2692 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002693
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002694 S.EmitOwnedPtr(TUDecl);
2695
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002696 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002697}
2698
Ted Kremenek0f84c002007-11-13 00:25:37 +00002699ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002700
2701 // Read the language options.
2702 LangOptions LOpts;
2703 LOpts.Read(D);
2704
Ted Kremenekfee04522007-10-31 22:44:07 +00002705 SourceManager &SM = D.ReadRef<SourceManager>();
2706 TargetInfo &t = D.ReadRef<TargetInfo>();
2707 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2708 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002709
Ted Kremenekfee04522007-10-31 22:44:07 +00002710 unsigned size_reserve = D.ReadInt();
2711
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002712 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2713 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002714
Ted Kremenek03ed4402007-11-13 22:02:55 +00002715 for (unsigned i = 0; i < size_reserve; ++i)
2716 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002717
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002718 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2719
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002720 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002721
2722 return A;
2723}