blob: 057dfb7bcf170338b983d1f5a847daf6be052fa3 [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,
34 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
36 SourceMgr(SM), LangOpts(LOpts), 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;
88
89 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000090 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
91 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000092 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000093
94 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
95 Type *T = Types[i];
96 if (isa<BuiltinType>(T))
97 ++NumBuiltin;
98 else if (isa<PointerType>(T))
99 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000100 else if (isa<BlockPointerType>(T))
101 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 else if (isa<ReferenceType>(T))
103 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000104 else if (isa<ComplexType>(T))
105 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 else if (isa<ArrayType>(T))
107 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000108 else if (isa<VectorType>(T))
109 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 else if (isa<FunctionTypeNoProto>(T))
111 ++NumFunctionNP;
112 else if (isa<FunctionTypeProto>(T))
113 ++NumFunctionP;
114 else if (isa<TypedefType>(T))
115 ++NumTypeName;
116 else if (TagType *TT = dyn_cast<TagType>(T)) {
117 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000118 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000120 case TagDecl::TK_struct: ++NumTagStruct; break;
121 case TagDecl::TK_union: ++NumTagUnion; break;
122 case TagDecl::TK_class: ++NumTagClass; break;
123 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000125 } else if (isa<ObjCInterfaceType>(T))
126 ++NumObjCInterfaces;
127 else if (isa<ObjCQualifiedInterfaceType>(T))
128 ++NumObjCQualifiedInterfaces;
129 else if (isa<ObjCQualifiedIdType>(T))
130 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000131 else if (isa<TypeOfType>(T))
132 ++NumTypeOfTypes;
133 else if (isa<TypeOfExpr>(T))
134 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000135 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000136 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 assert(0 && "Unknown type!");
138 }
139 }
140
141 fprintf(stderr, " %d builtin types\n", NumBuiltin);
142 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000143 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000145 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000147 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
149 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
150 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
151 fprintf(stderr, " %d tagged types\n", NumTagged);
152 fprintf(stderr, " %d struct types\n", NumTagStruct);
153 fprintf(stderr, " %d union types\n", NumTagUnion);
154 fprintf(stderr, " %d class types\n", NumTagClass);
155 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000156 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000157 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000158 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000159 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000160 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000161 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
162 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
165 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000166 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 NumFunctionP*sizeof(FunctionTypeProto)+
168 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000169 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
170 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
173
174void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff506010b2009-01-19 22:45:10 +0000175 void *Mem = Allocator.Allocate(sizeof(BuiltinType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000176 Types.push_back((R = QualType(new (Mem) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000177}
178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179void ASTContext::InitBuiltinTypes() {
180 assert(VoidTy.isNull() && "Context reinitialized?");
181
182 // C99 6.2.5p19.
183 InitBuiltinType(VoidTy, BuiltinType::Void);
184
185 // C99 6.2.5p2.
186 InitBuiltinType(BoolTy, BuiltinType::Bool);
187 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000188 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 InitBuiltinType(CharTy, BuiltinType::Char_S);
190 else
191 InitBuiltinType(CharTy, BuiltinType::Char_U);
192 // C99 6.2.5p4.
193 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
194 InitBuiltinType(ShortTy, BuiltinType::Short);
195 InitBuiltinType(IntTy, BuiltinType::Int);
196 InitBuiltinType(LongTy, BuiltinType::Long);
197 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
198
199 // C99 6.2.5p6.
200 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
201 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
202 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
203 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
204 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
205
206 // C99 6.2.5p10.
207 InitBuiltinType(FloatTy, BuiltinType::Float);
208 InitBuiltinType(DoubleTy, BuiltinType::Double);
209 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000210
211 // C++ 3.9.1p5
212 InitBuiltinType(WCharTy, BuiltinType::WChar);
213
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000214 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000215 InitBuiltinType(OverloadTy, BuiltinType::Overload);
216
217 // Placeholder type for type-dependent expressions whose type is
218 // completely unknown. No code should ever check a type against
219 // DependentTy and users should never see it; however, it is here to
220 // help diagnose failures to properly check for type-dependent
221 // expressions.
222 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // C99 6.2.5p11.
225 FloatComplexTy = getComplexType(FloatTy);
226 DoubleComplexTy = getComplexType(DoubleTy);
227 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000228
Steve Naroff7e219e42007-10-15 14:41:52 +0000229 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000230 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000231 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000232 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000233 ClassStructType = 0;
234
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000236
237 // void * type
238 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239}
240
Chris Lattner464175b2007-07-18 17:52:12 +0000241//===----------------------------------------------------------------------===//
242// Type Sizing and Analysis
243//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000244
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000245/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
246/// scalar floating point type.
247const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
248 const BuiltinType *BT = T->getAsBuiltinType();
249 assert(BT && "Not a floating point type!");
250 switch (BT->getKind()) {
251 default: assert(0 && "Not a floating point type!");
252 case BuiltinType::Float: return Target.getFloatFormat();
253 case BuiltinType::Double: return Target.getDoubleFormat();
254 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
255 }
256}
257
258
Chris Lattnera7674d82007-07-13 22:13:22 +0000259/// getTypeSize - Return the size of the specified type, in bits. This method
260/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000261std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000262ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000263 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000264 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000265 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000266 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000267 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000268 case Type::FunctionNoProto:
269 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000270 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000271 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000272 case Type::VariableArray:
273 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000274 case Type::DependentSizedArray:
275 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000276 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000277 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000278
Chris Lattner98be4942008-03-05 18:54:05 +0000279 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000280 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000281 Align = EltInfo.second;
282 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000283 }
Nate Begeman213541a2008-04-18 23:10:10 +0000284 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000285 case Type::Vector: {
286 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000287 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000288 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000289 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000290 // If the alignment is not a power of 2, round up to the next power of 2.
291 // This happens for non-power-of-2 length vectors.
292 // FIXME: this should probably be a target property.
293 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000294 break;
295 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000296
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000297 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000298 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000299 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000300 case BuiltinType::Void:
301 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000302 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000303 Width = Target.getBoolWidth();
304 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000305 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000306 case BuiltinType::Char_S:
307 case BuiltinType::Char_U:
308 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000309 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000310 Width = Target.getCharWidth();
311 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000312 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000313 case BuiltinType::WChar:
314 Width = Target.getWCharWidth();
315 Align = Target.getWCharAlign();
316 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000317 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000318 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000319 Width = Target.getShortWidth();
320 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000321 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000322 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000323 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000324 Width = Target.getIntWidth();
325 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000326 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000327 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000329 Width = Target.getLongWidth();
330 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000331 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000332 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000334 Width = Target.getLongLongWidth();
335 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 break;
337 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000338 Width = Target.getFloatWidth();
339 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000340 break;
341 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000342 Width = Target.getDoubleWidth();
343 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000344 break;
345 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000346 Width = Target.getLongDoubleWidth();
347 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000348 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000349 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000350 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000352 // FIXME: Pointers into different addr spaces could have different sizes and
353 // alignment requirements: getPointerInfo should take an AddrSpace.
354 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000355 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000356 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000357 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000358 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000359 case Type::BlockPointer: {
360 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
361 Width = Target.getPointerWidth(AS);
362 Align = Target.getPointerAlign(AS);
363 break;
364 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000365 case Type::Pointer: {
366 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000367 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000368 Align = Target.getPointerAlign(AS);
369 break;
370 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000371 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000372 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000373 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000374 // FIXME: This is wrong for struct layout: a reference in a struct has
375 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000376 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000377
378 case Type::Complex: {
379 // Complex types have the same alignment as their elements, but twice the
380 // size.
381 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000382 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000383 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000384 Align = EltInfo.second;
385 break;
386 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000387 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000388 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000389 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
390 Width = Layout.getSize();
391 Align = Layout.getAlignment();
392 break;
393 }
Chris Lattner71763312008-04-06 22:05:18 +0000394 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000395 const TagType *TT = cast<TagType>(T);
396
397 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000398 Width = 1;
399 Align = 1;
400 break;
401 }
402
Daniel Dunbar1d751182008-11-08 05:48:37 +0000403 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000404 return getTypeInfo(ET->getDecl()->getIntegerType());
405
Daniel Dunbar1d751182008-11-08 05:48:37 +0000406 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000407 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
408 Width = Layout.getSize();
409 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000410 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000411 }
Chris Lattner71763312008-04-06 22:05:18 +0000412 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000413
Chris Lattner464175b2007-07-18 17:52:12 +0000414 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000415 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000416}
417
Devang Patel8b277042008-06-04 21:22:16 +0000418/// LayoutField - Field layout.
419void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000420 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000421 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000422 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000423 uint64_t FieldOffset = IsUnion ? 0 : Size;
424 uint64_t FieldSize;
425 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000426
427 // FIXME: Should this override struct packing? Probably we want to
428 // take the minimum?
429 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
430 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000431
432 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
433 // TODO: Need to check this algorithm on other targets!
434 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000435 FieldSize =
436 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000437
438 std::pair<uint64_t, unsigned> FieldInfo =
439 Context.getTypeInfo(FD->getType());
440 uint64_t TypeSize = FieldInfo.first;
441
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000442 // Determine the alignment of this bitfield. The packing
443 // attributes define a maximum and the alignment attribute defines
444 // a minimum.
445 // FIXME: What is the right behavior when the specified alignment
446 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000447 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000448 if (FieldPacking)
449 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000450 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
451 FieldAlign = std::max(FieldAlign, AA->getAlignment());
452
453 // Check if we need to add padding to give the field the correct
454 // alignment.
455 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
456 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
457
458 // Padding members don't affect overall alignment
459 if (!FD->getIdentifier())
460 FieldAlign = 1;
461 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000462 if (FD->getType()->isIncompleteArrayType()) {
463 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000464 // query getTypeInfo about these, so we figure it out here.
465 // Flexible array members don't have any size, but they
466 // have to be aligned appropriately for their element type.
467 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000468 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000469 FieldAlign = Context.getTypeAlign(ATy->getElementType());
470 } else {
471 std::pair<uint64_t, unsigned> FieldInfo =
472 Context.getTypeInfo(FD->getType());
473 FieldSize = FieldInfo.first;
474 FieldAlign = FieldInfo.second;
475 }
476
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000477 // Determine the alignment of this bitfield. The packing
478 // attributes define a maximum and the alignment attribute defines
479 // a minimum. Additionally, the packing alignment must be at least
480 // a byte for non-bitfields.
481 //
482 // FIXME: What is the right behavior when the specified alignment
483 // is smaller than the specified packing?
484 if (FieldPacking)
485 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000486 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
487 FieldAlign = std::max(FieldAlign, AA->getAlignment());
488
489 // Round up the current record size to the field's alignment boundary.
490 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
491 }
492
493 // Place this field at the current location.
494 FieldOffsets[FieldNo] = FieldOffset;
495
496 // Reserve space for this field.
497 if (IsUnion) {
498 Size = std::max(Size, FieldSize);
499 } else {
500 Size = FieldOffset + FieldSize;
501 }
502
503 // Remember max struct/class alignment.
504 Alignment = std::max(Alignment, FieldAlign);
505}
506
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000507static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
508 std::vector<FieldDecl*> &Fields) {
509 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
510 if (SuperClass)
511 CollectObjCIvars(SuperClass, Fields);
512 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
513 E = OI->ivar_end(); I != E; ++I) {
514 ObjCIvarDecl *IVDecl = (*I);
515 if (!IVDecl->isInvalidDecl())
516 Fields.push_back(cast<FieldDecl>(IVDecl));
517 }
518}
519
520/// addRecordToClass - produces record info. for the class for its
521/// ivars and all those inherited.
522///
523const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
524{
525 const RecordDecl *&RD = ASTRecordForInterface[D];
526 if (RD)
527 return RD;
528 std::vector<FieldDecl*> RecFields;
529 CollectObjCIvars(D, RecFields);
530 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
531 D->getLocation(),
532 D->getIdentifier());
533 /// FIXME! Can do collection of ivars and adding to the record while
534 /// doing it.
535 for (unsigned int i = 0; i != RecFields.size(); i++) {
536 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
537 RecFields[i]->getLocation(),
538 RecFields[i]->getIdentifier(),
539 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000540 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000541 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000542 }
543 NewRD->completeDefinition(*this);
544 RD = NewRD;
545 return RD;
546}
Devang Patel44a3dde2008-06-04 21:54:36 +0000547
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000548/// setFieldDecl - maps a field for the given Ivar reference node.
549//
550void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
551 const ObjCIvarDecl *Ivar,
552 const ObjCIvarRefExpr *MRef) {
553 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
554 lookupFieldDeclForIvar(*this, Ivar);
555 ASTFieldForIvarRef[MRef] = FD;
556}
557
Chris Lattner61710852008-10-05 17:34:18 +0000558/// getASTObjcInterfaceLayout - Get or compute information about the layout of
559/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000560/// position information.
561const ASTRecordLayout &
562ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
563 // Look up this layout, if already laid out, return what we have.
564 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
565 if (Entry) return *Entry;
566
567 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
568 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000569 ASTRecordLayout *NewEntry = NULL;
570 unsigned FieldCount = D->ivar_size();
571 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
572 FieldCount++;
573 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
574 unsigned Alignment = SL.getAlignment();
575 uint64_t Size = SL.getSize();
576 NewEntry = new ASTRecordLayout(Size, Alignment);
577 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000578 // Super class is at the beginning of the layout.
579 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000580 } else {
581 NewEntry = new ASTRecordLayout();
582 NewEntry->InitializeLayout(FieldCount);
583 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000584 Entry = NewEntry;
585
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000586 unsigned StructPacking = 0;
587 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
588 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000589
590 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
591 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
592 AA->getAlignment()));
593
594 // Layout each ivar sequentially.
595 unsigned i = 0;
596 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
597 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
598 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000599 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000600 }
601
602 // Finally, round the size of the total struct up to the alignment of the
603 // struct itself.
604 NewEntry->FinalizeLayout();
605 return *NewEntry;
606}
607
Devang Patel88a981b2007-11-01 19:11:01 +0000608/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000609/// specified record (struct/union/class), which indicates its size and field
610/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000611const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000612 D = D->getDefinition(*this);
613 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000614
Chris Lattner464175b2007-07-18 17:52:12 +0000615 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000616 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000617 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000618
Devang Patel88a981b2007-11-01 19:11:01 +0000619 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
620 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
621 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000622 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000623
Douglas Gregore267ff32008-12-11 20:41:00 +0000624 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000625 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000626 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000627
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000628 unsigned StructPacking = 0;
629 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
630 StructPacking = PA->getAlignment();
631
Eli Friedman4bd998b2008-05-30 09:31:38 +0000632 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000633 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
634 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000635
Eli Friedman4bd998b2008-05-30 09:31:38 +0000636 // Layout each field, for now, just sequentially, respecting alignment. In
637 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000638 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000639 for (RecordDecl::field_iterator Field = D->field_begin(),
640 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000641 Field != FieldEnd; (void)++Field, ++FieldIdx)
642 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000643
644 // Finally, round the size of the total struct up to the alignment of the
645 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000646 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000647 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000648}
649
Chris Lattnera7674d82007-07-13 22:13:22 +0000650//===----------------------------------------------------------------------===//
651// Type creation/memoization methods
652//===----------------------------------------------------------------------===//
653
Christopher Lambebb97e92008-02-04 02:31:56 +0000654QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000655 QualType CanT = getCanonicalType(T);
656 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000657 return T;
658
659 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
660 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000661 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000662 "Type is already address space qualified");
663
664 // Check if we've already instantiated an address space qual'd type of this
665 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000666 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000667 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000668 void *InsertPos = 0;
669 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
670 return QualType(ASQy, 0);
671
672 // If the base type isn't canonical, this won't be a canonical type either,
673 // so fill in the canonical type field.
674 QualType Canonical;
675 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000676 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000677
678 // Get the new insert position for the node we care about.
679 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000680 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000681 }
Steve Naroff506010b2009-01-19 22:45:10 +0000682 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000683 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000684 ASQualTypes.InsertNode(New, InsertPos);
685 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000686 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000687}
688
Chris Lattnera7674d82007-07-13 22:13:22 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690/// getComplexType - Return the uniqued reference to the type for a complex
691/// number with the specified element type.
692QualType ASTContext::getComplexType(QualType T) {
693 // Unique pointers, to guarantee there is only one pointer of a particular
694 // structure.
695 llvm::FoldingSetNodeID ID;
696 ComplexType::Profile(ID, T);
697
698 void *InsertPos = 0;
699 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
700 return QualType(CT, 0);
701
702 // If the pointee type isn't canonical, this won't be a canonical type either,
703 // so fill in the canonical type field.
704 QualType Canonical;
705 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000706 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000707
708 // Get the new insert position for the node we care about.
709 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000710 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 }
Steve Naroff506010b2009-01-19 22:45:10 +0000712 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000713 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 Types.push_back(New);
715 ComplexTypes.InsertNode(New, InsertPos);
716 return QualType(New, 0);
717}
718
719
720/// getPointerType - Return the uniqued reference to the type for a pointer to
721/// the specified type.
722QualType ASTContext::getPointerType(QualType T) {
723 // Unique pointers, to guarantee there is only one pointer of a particular
724 // structure.
725 llvm::FoldingSetNodeID ID;
726 PointerType::Profile(ID, T);
727
728 void *InsertPos = 0;
729 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
730 return QualType(PT, 0);
731
732 // If the pointee type isn't canonical, this won't be a canonical type either,
733 // so fill in the canonical type field.
734 QualType Canonical;
735 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000736 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
738 // Get the new insert position for the node we care about.
739 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000740 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
Steve Naroff506010b2009-01-19 22:45:10 +0000742 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000743 PointerType *New = new (Mem) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 Types.push_back(New);
745 PointerTypes.InsertNode(New, InsertPos);
746 return QualType(New, 0);
747}
748
Steve Naroff5618bd42008-08-27 16:04:49 +0000749/// getBlockPointerType - Return the uniqued reference to the type for
750/// a pointer to the specified block.
751QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000752 assert(T->isFunctionType() && "block of function types only");
753 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000754 // structure.
755 llvm::FoldingSetNodeID ID;
756 BlockPointerType::Profile(ID, T);
757
758 void *InsertPos = 0;
759 if (BlockPointerType *PT =
760 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
761 return QualType(PT, 0);
762
Steve Naroff296e8d52008-08-28 19:20:44 +0000763 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000764 // type either so fill in the canonical type field.
765 QualType Canonical;
766 if (!T->isCanonical()) {
767 Canonical = getBlockPointerType(getCanonicalType(T));
768
769 // Get the new insert position for the node we care about.
770 BlockPointerType *NewIP =
771 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000772 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000773 }
Steve Naroff506010b2009-01-19 22:45:10 +0000774 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000775 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000776 Types.push_back(New);
777 BlockPointerTypes.InsertNode(New, InsertPos);
778 return QualType(New, 0);
779}
780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781/// getReferenceType - Return the uniqued reference to the type for a reference
782/// to the specified type.
783QualType ASTContext::getReferenceType(QualType T) {
784 // Unique pointers, to guarantee there is only one pointer of a particular
785 // structure.
786 llvm::FoldingSetNodeID ID;
787 ReferenceType::Profile(ID, T);
788
789 void *InsertPos = 0;
790 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
791 return QualType(RT, 0);
792
793 // If the referencee type isn't canonical, this won't be a canonical type
794 // either, so fill in the canonical type field.
795 QualType Canonical;
796 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000797 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000798
799 // Get the new insert position for the node we care about.
800 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000801 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 }
803
Steve Naroff506010b2009-01-19 22:45:10 +0000804 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000805 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 Types.push_back(New);
807 ReferenceTypes.InsertNode(New, InsertPos);
808 return QualType(New, 0);
809}
810
Steve Narofffb22d962007-08-30 01:06:46 +0000811/// getConstantArrayType - Return the unique reference to the type for an
812/// array of the specified element type.
813QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000814 const llvm::APInt &ArySize,
815 ArrayType::ArraySizeModifier ASM,
816 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000818 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819
820 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000821 if (ConstantArrayType *ATP =
822 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 return QualType(ATP, 0);
824
825 // If the element type isn't canonical, this won't be a canonical type either,
826 // so fill in the canonical type field.
827 QualType Canonical;
828 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000829 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000830 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000832 ConstantArrayType *NewIP =
833 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000834 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 }
836
Steve Naroff506010b2009-01-19 22:45:10 +0000837 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000838 ConstantArrayType *New =
839 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000840 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 Types.push_back(New);
842 return QualType(New, 0);
843}
844
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000845/// getVariableArrayType - Returns a non-unique reference to the type for a
846/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000847QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
848 ArrayType::ArraySizeModifier ASM,
849 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000850 // Since we don't unique expressions, it isn't possible to unique VLA's
851 // that have an expression provided for their size.
852
Steve Naroff506010b2009-01-19 22:45:10 +0000853 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000854 VariableArrayType *New =
855 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000856
857 VariableArrayTypes.push_back(New);
858 Types.push_back(New);
859 return QualType(New, 0);
860}
861
Douglas Gregor898574e2008-12-05 23:32:09 +0000862/// getDependentSizedArrayType - Returns a non-unique reference to
863/// the type for a dependently-sized array of the specified element
864/// type. FIXME: We will need these to be uniqued, or at least
865/// comparable, at some point.
866QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
867 ArrayType::ArraySizeModifier ASM,
868 unsigned EltTypeQuals) {
869 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
870 "Size must be type- or value-dependent!");
871
872 // Since we don't unique expressions, it isn't possible to unique
873 // dependently-sized array types.
874
Steve Naroff506010b2009-01-19 22:45:10 +0000875 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000876 DependentSizedArrayType *New =
877 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
878 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000879
880 DependentSizedArrayTypes.push_back(New);
881 Types.push_back(New);
882 return QualType(New, 0);
883}
884
Eli Friedmanc5773c42008-02-15 18:16:39 +0000885QualType ASTContext::getIncompleteArrayType(QualType EltTy,
886 ArrayType::ArraySizeModifier ASM,
887 unsigned EltTypeQuals) {
888 llvm::FoldingSetNodeID ID;
889 IncompleteArrayType::Profile(ID, EltTy);
890
891 void *InsertPos = 0;
892 if (IncompleteArrayType *ATP =
893 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
894 return QualType(ATP, 0);
895
896 // If the element type isn't canonical, this won't be a canonical type
897 // either, so fill in the canonical type field.
898 QualType Canonical;
899
900 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000901 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000902 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000903
904 // Get the new insert position for the node we care about.
905 IncompleteArrayType *NewIP =
906 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000907 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000908 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000909
Steve Naroff506010b2009-01-19 22:45:10 +0000910 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000911 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
912 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000913
914 IncompleteArrayTypes.InsertNode(New, InsertPos);
915 Types.push_back(New);
916 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000917}
918
Steve Naroff73322922007-07-18 18:00:27 +0000919/// getVectorType - Return the unique reference to a vector type of
920/// the specified element type and size. VectorType must be a built-in type.
921QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 BuiltinType *baseType;
923
Chris Lattnerf52ab252008-04-06 22:59:24 +0000924 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000925 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
927 // Check if we've already instantiated a vector of this type.
928 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000929 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 void *InsertPos = 0;
931 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
932 return QualType(VTP, 0);
933
934 // If the element type isn't canonical, this won't be a canonical type either,
935 // so fill in the canonical type field.
936 QualType Canonical;
937 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000938 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000939
940 // Get the new insert position for the node we care about.
941 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000942 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 }
Steve Naroff506010b2009-01-19 22:45:10 +0000944 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000945 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 VectorTypes.InsertNode(New, InsertPos);
947 Types.push_back(New);
948 return QualType(New, 0);
949}
950
Nate Begeman213541a2008-04-18 23:10:10 +0000951/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000952/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000953QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000954 BuiltinType *baseType;
955
Chris Lattnerf52ab252008-04-06 22:59:24 +0000956 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000957 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000958
959 // Check if we've already instantiated a vector of this type.
960 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000961 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000962 void *InsertPos = 0;
963 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
964 return QualType(VTP, 0);
965
966 // If the element type isn't canonical, this won't be a canonical type either,
967 // so fill in the canonical type field.
968 QualType Canonical;
969 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000970 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000971
972 // Get the new insert position for the node we care about.
973 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000974 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +0000975 }
Steve Naroff506010b2009-01-19 22:45:10 +0000976 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000977 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000978 VectorTypes.InsertNode(New, InsertPos);
979 Types.push_back(New);
980 return QualType(New, 0);
981}
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
984///
985QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
986 // Unique functions, to guarantee there is only one function of a particular
987 // structure.
988 llvm::FoldingSetNodeID ID;
989 FunctionTypeNoProto::Profile(ID, ResultTy);
990
991 void *InsertPos = 0;
992 if (FunctionTypeNoProto *FT =
993 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
994 return QualType(FT, 0);
995
996 QualType Canonical;
997 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000998 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000999
1000 // Get the new insert position for the node we care about.
1001 FunctionTypeNoProto *NewIP =
1002 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001003 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 }
1005
1006 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
1007 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001008 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 return QualType(New, 0);
1010}
1011
1012/// getFunctionType - Return a normal function type with a typed argument
1013/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001014QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001015 unsigned NumArgs, bool isVariadic,
1016 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 // Unique functions, to guarantee there is only one function of a particular
1018 // structure.
1019 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001020 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1021 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022
1023 void *InsertPos = 0;
1024 if (FunctionTypeProto *FTP =
1025 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1026 return QualType(FTP, 0);
1027
1028 // Determine whether the type being created is already canonical or not.
1029 bool isCanonical = ResultTy->isCanonical();
1030 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1031 if (!ArgArray[i]->isCanonical())
1032 isCanonical = false;
1033
1034 // If this type isn't canonical, get the canonical version of it.
1035 QualType Canonical;
1036 if (!isCanonical) {
1037 llvm::SmallVector<QualType, 16> CanonicalArgs;
1038 CanonicalArgs.reserve(NumArgs);
1039 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001040 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001041
Chris Lattnerf52ab252008-04-06 22:59:24 +00001042 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001044 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045
1046 // Get the new insert position for the node we care about.
1047 FunctionTypeProto *NewIP =
1048 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001049 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 }
1051
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001052 // FunctionTypeProto objects are allocated with extra bytes after them
1053 // for a variable size array (for parameter types) at the end of them.
1054 // FIXME: Can we do better than forcing a 16-byte alignment?
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 FunctionTypeProto *FTP =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001056 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1057 NumArgs*sizeof(QualType), 16);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001059 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 Types.push_back(FTP);
1061 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1062 return QualType(FTP, 0);
1063}
1064
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001065/// getTypeDeclType - Return the unique reference to the type for the
1066/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001067QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001068 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001069 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1070
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001071 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001072 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001073 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1074 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001075 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001076 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001077
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001078 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001079 if (PrevDecl)
1080 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1081 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001082 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001083 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1084 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001085 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001086 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001087 if (PrevDecl)
1088 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1089 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001090 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001091 Decl->TypeForDecl = new (Mem) RecordType(Record);
1092 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001093 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001094 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1095 if (PrevDecl)
1096 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1097 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001098 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001099 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1100 }
1101 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001102 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001103 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001104
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001105 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001106 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001107}
1108
Reid Spencer5f016e22007-07-11 17:01:13 +00001109/// getTypedefType - Return the unique reference to the type for the
1110/// specified typename decl.
1111QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1112 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1113
Chris Lattnerf52ab252008-04-06 22:59:24 +00001114 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff506010b2009-01-19 22:45:10 +00001115 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001116 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 Types.push_back(Decl->TypeForDecl);
1118 return QualType(Decl->TypeForDecl, 0);
1119}
1120
Douglas Gregor72c3f312008-12-05 18:15:24 +00001121/// getTemplateTypeParmType - Return the unique reference to the type
1122/// for the specified template type parameter declaration.
1123QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1124 if (!Decl->TypeForDecl) {
Steve Naroff506010b2009-01-19 22:45:10 +00001125 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001126 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001127 Types.push_back(Decl->TypeForDecl);
1128 }
1129 return QualType(Decl->TypeForDecl, 0);
1130}
1131
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001133/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001134QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001135 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1136
Steve Naroff506010b2009-01-19 22:45:10 +00001137 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001138 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001139 Types.push_back(Decl->TypeForDecl);
1140 return QualType(Decl->TypeForDecl, 0);
1141}
1142
Chris Lattner88cb27a2008-04-07 04:56:42 +00001143/// CmpProtocolNames - Comparison predicate for sorting protocols
1144/// alphabetically.
1145static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1146 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001147 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001148}
1149
1150static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1151 unsigned &NumProtocols) {
1152 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1153
1154 // Sort protocols, keyed by name.
1155 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1156
1157 // Remove duplicates.
1158 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1159 NumProtocols = ProtocolsEnd-Protocols;
1160}
1161
1162
Chris Lattner065f0d72008-04-07 04:44:08 +00001163/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1164/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001165QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1166 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001167 // Sort the protocol list alphabetically to canonicalize it.
1168 SortAndUniqueProtocols(Protocols, NumProtocols);
1169
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001170 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001171 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001172
1173 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001174 if (ObjCQualifiedInterfaceType *QT =
1175 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001176 return QualType(QT, 0);
1177
1178 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001179 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001180 ObjCQualifiedInterfaceType *QType =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001181 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1182
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001183 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001184 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001185 return QualType(QType, 0);
1186}
1187
Chris Lattner88cb27a2008-04-07 04:56:42 +00001188/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1189/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001190QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001191 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001192 // Sort the protocol list alphabetically to canonicalize it.
1193 SortAndUniqueProtocols(Protocols, NumProtocols);
1194
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001195 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001196 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001197
1198 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001199 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001200 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001201 return QualType(QT, 0);
1202
1203 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001204 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001205 ObjCQualifiedIdType *QType =
1206 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001207 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001208 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001209 return QualType(QType, 0);
1210}
1211
Steve Naroff9752f252007-08-01 18:02:17 +00001212/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1213/// TypeOfExpr AST's (since expression's are never shared). For example,
1214/// multiple declarations that refer to "typeof(x)" all contain different
1215/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1216/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001217QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001218 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001219 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1220 Types.push_back(toe);
1221 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001222}
1223
Steve Naroff9752f252007-08-01 18:02:17 +00001224/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1225/// TypeOfType AST's. The only motivation to unique these nodes would be
1226/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1227/// an issue. This doesn't effect the type checker, since it operates
1228/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001229QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001230 QualType Canonical = getCanonicalType(tofType);
Steve Naroff506010b2009-01-19 22:45:10 +00001231 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001232 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001233 Types.push_back(tot);
1234 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001235}
1236
Reid Spencer5f016e22007-07-11 17:01:13 +00001237/// getTagDeclType - Return the unique reference to the type for the
1238/// specified TagDecl (struct/union/class/enum) decl.
1239QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001240 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001241 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242}
1243
1244/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1245/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1246/// needs to agree with the definition in <stddef.h>.
1247QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001248 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001249}
1250
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001251/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001252/// width of characters in wide strings, The value is target dependent and
1253/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001254QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001255 if (LangOpts.CPlusPlus)
1256 return WCharTy;
1257
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001258 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1259 // wide-character type?
1260 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001261}
1262
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001263/// getSignedWCharType - Return the type of "signed wchar_t".
1264/// Used when in C++, as a GCC extension.
1265QualType ASTContext::getSignedWCharType() const {
1266 // FIXME: derive from "Target" ?
1267 return WCharTy;
1268}
1269
1270/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1271/// Used when in C++, as a GCC extension.
1272QualType ASTContext::getUnsignedWCharType() const {
1273 // FIXME: derive from "Target" ?
1274 return UnsignedIntTy;
1275}
1276
Chris Lattner8b9023b2007-07-13 03:05:23 +00001277/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1278/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1279QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001280 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001281}
1282
Chris Lattnere6327742008-04-02 05:18:44 +00001283//===----------------------------------------------------------------------===//
1284// Type Operators
1285//===----------------------------------------------------------------------===//
1286
Chris Lattner77c96472008-04-06 22:41:35 +00001287/// getCanonicalType - Return the canonical (structural) type corresponding to
1288/// the specified potentially non-canonical type. The non-canonical version
1289/// of a type may have many "decorated" versions of types. Decorators can
1290/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1291/// to be free of any of these, allowing two canonical types to be compared
1292/// for exact equality with a simple pointer comparison.
1293QualType ASTContext::getCanonicalType(QualType T) {
1294 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001295
1296 // If the result has type qualifiers, make sure to canonicalize them as well.
1297 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1298 if (TypeQuals == 0) return CanType;
1299
1300 // If the type qualifiers are on an array type, get the canonical type of the
1301 // array with the qualifiers applied to the element type.
1302 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1303 if (!AT)
1304 return CanType.getQualifiedType(TypeQuals);
1305
1306 // Get the canonical version of the element with the extra qualifiers on it.
1307 // This can recursively sink qualifiers through multiple levels of arrays.
1308 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1309 NewEltTy = getCanonicalType(NewEltTy);
1310
1311 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1312 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1313 CAT->getIndexTypeQualifier());
1314 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1315 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1316 IAT->getIndexTypeQualifier());
1317
Douglas Gregor898574e2008-12-05 23:32:09 +00001318 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1319 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1320 DSAT->getSizeModifier(),
1321 DSAT->getIndexTypeQualifier());
1322
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001323 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1324 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1325 VAT->getSizeModifier(),
1326 VAT->getIndexTypeQualifier());
1327}
1328
1329
1330const ArrayType *ASTContext::getAsArrayType(QualType T) {
1331 // Handle the non-qualified case efficiently.
1332 if (T.getCVRQualifiers() == 0) {
1333 // Handle the common positive case fast.
1334 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1335 return AT;
1336 }
1337
1338 // Handle the common negative case fast, ignoring CVR qualifiers.
1339 QualType CType = T->getCanonicalTypeInternal();
1340
1341 // Make sure to look through type qualifiers (like ASQuals) for the negative
1342 // test.
1343 if (!isa<ArrayType>(CType) &&
1344 !isa<ArrayType>(CType.getUnqualifiedType()))
1345 return 0;
1346
1347 // Apply any CVR qualifiers from the array type to the element type. This
1348 // implements C99 6.7.3p8: "If the specification of an array type includes
1349 // any type qualifiers, the element type is so qualified, not the array type."
1350
1351 // If we get here, we either have type qualifiers on the type, or we have
1352 // sugar such as a typedef in the way. If we have type qualifiers on the type
1353 // we must propagate them down into the elemeng type.
1354 unsigned CVRQuals = T.getCVRQualifiers();
1355 unsigned AddrSpace = 0;
1356 Type *Ty = T.getTypePtr();
1357
1358 // Rip through ASQualType's and typedefs to get to a concrete type.
1359 while (1) {
1360 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1361 AddrSpace = ASQT->getAddressSpace();
1362 Ty = ASQT->getBaseType();
1363 } else {
1364 T = Ty->getDesugaredType();
1365 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1366 break;
1367 CVRQuals |= T.getCVRQualifiers();
1368 Ty = T.getTypePtr();
1369 }
1370 }
1371
1372 // If we have a simple case, just return now.
1373 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1374 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1375 return ATy;
1376
1377 // Otherwise, we have an array and we have qualifiers on it. Push the
1378 // qualifiers into the array element type and return a new array type.
1379 // Get the canonical version of the element with the extra qualifiers on it.
1380 // This can recursively sink qualifiers through multiple levels of arrays.
1381 QualType NewEltTy = ATy->getElementType();
1382 if (AddrSpace)
1383 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1384 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1385
1386 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1387 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1388 CAT->getSizeModifier(),
1389 CAT->getIndexTypeQualifier()));
1390 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1391 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1392 IAT->getSizeModifier(),
1393 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001394
Douglas Gregor898574e2008-12-05 23:32:09 +00001395 if (const DependentSizedArrayType *DSAT
1396 = dyn_cast<DependentSizedArrayType>(ATy))
1397 return cast<ArrayType>(
1398 getDependentSizedArrayType(NewEltTy,
1399 DSAT->getSizeExpr(),
1400 DSAT->getSizeModifier(),
1401 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001402
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001403 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1404 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1405 VAT->getSizeModifier(),
1406 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001407}
1408
1409
Chris Lattnere6327742008-04-02 05:18:44 +00001410/// getArrayDecayedType - Return the properly qualified result of decaying the
1411/// specified array type to a pointer. This operation is non-trivial when
1412/// handling typedefs etc. The canonical type of "T" must be an array type,
1413/// this returns a pointer to a properly qualified element of the array.
1414///
1415/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1416QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001417 // Get the element type with 'getAsArrayType' so that we don't lose any
1418 // typedefs in the element type of the array. This also handles propagation
1419 // of type qualifiers from the array type into the element type if present
1420 // (C99 6.7.3p8).
1421 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1422 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001423
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001424 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001425
1426 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001427 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001428}
1429
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001430QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001431 QualType ElemTy = VAT->getElementType();
1432
1433 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1434 return getBaseElementType(VAT);
1435
1436 return ElemTy;
1437}
1438
Reid Spencer5f016e22007-07-11 17:01:13 +00001439/// getFloatingRank - Return a relative rank for floating point types.
1440/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001441static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001442 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001444
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001445 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001446 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001447 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 case BuiltinType::Float: return FloatRank;
1449 case BuiltinType::Double: return DoubleRank;
1450 case BuiltinType::LongDouble: return LongDoubleRank;
1451 }
1452}
1453
Steve Naroff716c7302007-08-27 01:41:48 +00001454/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1455/// point or a complex type (based on typeDomain/typeSize).
1456/// 'typeDomain' is a real floating point or complex type.
1457/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001458QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1459 QualType Domain) const {
1460 FloatingRank EltRank = getFloatingRank(Size);
1461 if (Domain->isComplexType()) {
1462 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001463 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001464 case FloatRank: return FloatComplexTy;
1465 case DoubleRank: return DoubleComplexTy;
1466 case LongDoubleRank: return LongDoubleComplexTy;
1467 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 }
Chris Lattner1361b112008-04-06 23:58:54 +00001469
1470 assert(Domain->isRealFloatingType() && "Unknown domain!");
1471 switch (EltRank) {
1472 default: assert(0 && "getFloatingRank(): illegal value for rank");
1473 case FloatRank: return FloatTy;
1474 case DoubleRank: return DoubleTy;
1475 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001476 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001477}
1478
Chris Lattner7cfeb082008-04-06 23:55:33 +00001479/// getFloatingTypeOrder - Compare the rank of the two specified floating
1480/// point types, ignoring the domain of the type (i.e. 'double' ==
1481/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1482/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001483int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1484 FloatingRank LHSR = getFloatingRank(LHS);
1485 FloatingRank RHSR = getFloatingRank(RHS);
1486
1487 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001488 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001489 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001490 return 1;
1491 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001492}
1493
Chris Lattnerf52ab252008-04-06 22:59:24 +00001494/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1495/// routine will assert if passed a built-in type that isn't an integer or enum,
1496/// or if it is not canonicalized.
1497static unsigned getIntegerRank(Type *T) {
1498 assert(T->isCanonical() && "T should be canonicalized");
1499 if (isa<EnumType>(T))
1500 return 4;
1501
1502 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001503 default: assert(0 && "getIntegerRank(): not a built-in integer");
1504 case BuiltinType::Bool:
1505 return 1;
1506 case BuiltinType::Char_S:
1507 case BuiltinType::Char_U:
1508 case BuiltinType::SChar:
1509 case BuiltinType::UChar:
1510 return 2;
1511 case BuiltinType::Short:
1512 case BuiltinType::UShort:
1513 return 3;
1514 case BuiltinType::Int:
1515 case BuiltinType::UInt:
1516 return 4;
1517 case BuiltinType::Long:
1518 case BuiltinType::ULong:
1519 return 5;
1520 case BuiltinType::LongLong:
1521 case BuiltinType::ULongLong:
1522 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001523 }
1524}
1525
Chris Lattner7cfeb082008-04-06 23:55:33 +00001526/// getIntegerTypeOrder - Returns the highest ranked integer type:
1527/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1528/// LHS < RHS, return -1.
1529int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001530 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1531 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001532 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001533
Chris Lattnerf52ab252008-04-06 22:59:24 +00001534 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1535 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001536
Chris Lattner7cfeb082008-04-06 23:55:33 +00001537 unsigned LHSRank = getIntegerRank(LHSC);
1538 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001539
Chris Lattner7cfeb082008-04-06 23:55:33 +00001540 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1541 if (LHSRank == RHSRank) return 0;
1542 return LHSRank > RHSRank ? 1 : -1;
1543 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001544
Chris Lattner7cfeb082008-04-06 23:55:33 +00001545 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1546 if (LHSUnsigned) {
1547 // If the unsigned [LHS] type is larger, return it.
1548 if (LHSRank >= RHSRank)
1549 return 1;
1550
1551 // If the signed type can represent all values of the unsigned type, it
1552 // wins. Because we are dealing with 2's complement and types that are
1553 // powers of two larger than each other, this is always safe.
1554 return -1;
1555 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001556
Chris Lattner7cfeb082008-04-06 23:55:33 +00001557 // If the unsigned [RHS] type is larger, return it.
1558 if (RHSRank >= LHSRank)
1559 return -1;
1560
1561 // If the signed type can represent all values of the unsigned type, it
1562 // wins. Because we are dealing with 2's complement and types that are
1563 // powers of two larger than each other, this is always safe.
1564 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001565}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001566
1567// getCFConstantStringType - Return the type used for constant CFStrings.
1568QualType ASTContext::getCFConstantStringType() {
1569 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001570 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001571 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001572 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001573 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001574
1575 // const int *isa;
1576 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001577 // int flags;
1578 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001579 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001580 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001581 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001582 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001583
Anders Carlsson71993dd2007-08-17 05:31:46 +00001584 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001585 for (unsigned i = 0; i < 4; ++i) {
1586 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1587 SourceLocation(), 0,
1588 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001589 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001590 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001591 }
1592
1593 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001594 }
1595
1596 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001597}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001598
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001599QualType ASTContext::getObjCFastEnumerationStateType()
1600{
1601 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001602 ObjCFastEnumerationStateTypeDecl =
1603 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1604 &Idents.get("__objcFastEnumerationState"));
1605
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001606 QualType FieldTypes[] = {
1607 UnsignedLongTy,
1608 getPointerType(ObjCIdType),
1609 getPointerType(UnsignedLongTy),
1610 getConstantArrayType(UnsignedLongTy,
1611 llvm::APInt(32, 5), ArrayType::Normal, 0)
1612 };
1613
Douglas Gregor44b43212008-12-11 16:49:14 +00001614 for (size_t i = 0; i < 4; ++i) {
1615 FieldDecl *Field = FieldDecl::Create(*this,
1616 ObjCFastEnumerationStateTypeDecl,
1617 SourceLocation(), 0,
1618 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001619 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001620 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001621 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001622
Douglas Gregor44b43212008-12-11 16:49:14 +00001623 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001624 }
1625
1626 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1627}
1628
Anders Carlssone8c49532007-10-29 06:33:42 +00001629// This returns true if a type has been typedefed to BOOL:
1630// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001631static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001632 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001633 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1634 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001635
1636 return false;
1637}
1638
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001640/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001641int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001642 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001643
1644 // Make all integer and enum types at least as large as an int
1645 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001646 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001647 // Treat arrays as pointers, since that's how they're passed in.
1648 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001649 sz = getTypeSize(VoidPtrTy);
1650 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001651}
1652
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001654/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001655void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001656 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001657 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001658 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001659 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001660 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001661 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001662 // Compute size of all parameters.
1663 // Start with computing size of a pointer in number of bytes.
1664 // FIXME: There might(should) be a better way of doing this computation!
1665 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001666 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001667 // The first two arguments (self and _cmd) are pointers; account for
1668 // their size.
1669 int ParmOffset = 2 * PtrSize;
1670 int NumOfParams = Decl->getNumParams();
1671 for (int i = 0; i < NumOfParams; i++) {
1672 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001673 int sz = getObjCEncodingTypeSize (PType);
1674 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001675 ParmOffset += sz;
1676 }
1677 S += llvm::utostr(ParmOffset);
1678 S += "@0:";
1679 S += llvm::utostr(PtrSize);
1680
1681 // Argument types.
1682 ParmOffset = 2 * PtrSize;
1683 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001684 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1685 QualType PType = PVDecl->getOriginalType();
1686 if (const ArrayType *AT =
1687 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1688 // Use array's original type only if it has known number of
1689 // elements.
1690 if (!dyn_cast<ConstantArrayType>(AT))
1691 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001692 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001693 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001694 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001695 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001696 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001697 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001698 }
1699}
1700
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001701/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1702/// method declaration. If non-NULL, Container must be either an
1703/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1704/// NULL when getting encodings for protocol properties.
1705void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1706 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001707 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001708 // Collect information from the property implementation decl(s).
1709 bool Dynamic = false;
1710 ObjCPropertyImplDecl *SynthesizePID = 0;
1711
1712 // FIXME: Duplicated code due to poor abstraction.
1713 if (Container) {
1714 if (const ObjCCategoryImplDecl *CID =
1715 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1716 for (ObjCCategoryImplDecl::propimpl_iterator
1717 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1718 ObjCPropertyImplDecl *PID = *i;
1719 if (PID->getPropertyDecl() == PD) {
1720 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1721 Dynamic = true;
1722 } else {
1723 SynthesizePID = PID;
1724 }
1725 }
1726 }
1727 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001728 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001729 for (ObjCCategoryImplDecl::propimpl_iterator
1730 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1731 ObjCPropertyImplDecl *PID = *i;
1732 if (PID->getPropertyDecl() == PD) {
1733 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1734 Dynamic = true;
1735 } else {
1736 SynthesizePID = PID;
1737 }
1738 }
1739 }
1740 }
1741 }
1742
1743 // FIXME: This is not very efficient.
1744 S = "T";
1745
1746 // Encode result type.
1747 // FIXME: GCC uses a generating_property_type_encoding mode during
1748 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001749 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001750
1751 if (PD->isReadOnly()) {
1752 S += ",R";
1753 } else {
1754 switch (PD->getSetterKind()) {
1755 case ObjCPropertyDecl::Assign: break;
1756 case ObjCPropertyDecl::Copy: S += ",C"; break;
1757 case ObjCPropertyDecl::Retain: S += ",&"; break;
1758 }
1759 }
1760
1761 // It really isn't clear at all what this means, since properties
1762 // are "dynamic by default".
1763 if (Dynamic)
1764 S += ",D";
1765
1766 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1767 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001768 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001769 }
1770
1771 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1772 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001773 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001774 }
1775
1776 if (SynthesizePID) {
1777 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1778 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001779 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001780 }
1781
1782 // FIXME: OBJCGC: weak & strong
1783}
1784
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001785/// getLegacyIntegralTypeEncoding -
1786/// Another legacy compatibility encoding: 32-bit longs are encoded as
1787/// 'l' or 'L', but not always. For typedefs, we need to use
1788/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1789///
1790void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1791 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1792 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1793 if (BT->getKind() == BuiltinType::ULong)
1794 PointeeTy = UnsignedIntTy;
1795 else if (BT->getKind() == BuiltinType::Long)
1796 PointeeTy = IntTy;
1797 }
1798 }
1799}
1800
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001801void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001802 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001803 // We follow the behavior of gcc, expanding structures which are
1804 // directly pointed to, and expanding embedded structures. Note that
1805 // these rules are sufficient to prevent recursive encoding of the
1806 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001807 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1808 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001809}
1810
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001811static void EncodeBitField(const ASTContext *Context, std::string& S,
1812 FieldDecl *FD) {
1813 const Expr *E = FD->getBitWidth();
1814 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1815 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1816 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1817 S += 'b';
1818 S += llvm::utostr(N);
1819}
1820
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001821void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1822 bool ExpandPointedToStructures,
1823 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001824 FieldDecl *FD,
1825 bool OutermostType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001826 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001827 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001828 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001829 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001830 else {
1831 char encoding;
1832 switch (BT->getKind()) {
1833 default: assert(0 && "Unhandled builtin type kind");
1834 case BuiltinType::Void: encoding = 'v'; break;
1835 case BuiltinType::Bool: encoding = 'B'; break;
1836 case BuiltinType::Char_U:
1837 case BuiltinType::UChar: encoding = 'C'; break;
1838 case BuiltinType::UShort: encoding = 'S'; break;
1839 case BuiltinType::UInt: encoding = 'I'; break;
1840 case BuiltinType::ULong: encoding = 'L'; break;
1841 case BuiltinType::ULongLong: encoding = 'Q'; break;
1842 case BuiltinType::Char_S:
1843 case BuiltinType::SChar: encoding = 'c'; break;
1844 case BuiltinType::Short: encoding = 's'; break;
1845 case BuiltinType::Int: encoding = 'i'; break;
1846 case BuiltinType::Long: encoding = 'l'; break;
1847 case BuiltinType::LongLong: encoding = 'q'; break;
1848 case BuiltinType::Float: encoding = 'f'; break;
1849 case BuiltinType::Double: encoding = 'd'; break;
1850 case BuiltinType::LongDouble: encoding = 'd'; break;
1851 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001852
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001853 S += encoding;
1854 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001855 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001856 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001857 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001858 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1859 ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001860 ExpandStructures, FD);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001861 }
1862 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001863 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001864 bool isReadOnly = false;
1865 // For historical/compatibility reasons, the read-only qualifier of the
1866 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1867 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1868 // Also, do not emit the 'r' for anything but the outermost type!
1869 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1870 if (OutermostType && T.isConstQualified()) {
1871 isReadOnly = true;
1872 S += 'r';
1873 }
1874 }
1875 else if (OutermostType) {
1876 QualType P = PointeeTy;
1877 while (P->getAsPointerType())
1878 P = P->getAsPointerType()->getPointeeType();
1879 if (P.isConstQualified()) {
1880 isReadOnly = true;
1881 S += 'r';
1882 }
1883 }
1884 if (isReadOnly) {
1885 // Another legacy compatibility encoding. Some ObjC qualifier and type
1886 // combinations need to be rearranged.
1887 // Rewrite "in const" from "nr" to "rn"
1888 const char * s = S.c_str();
1889 int len = S.length();
1890 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
1891 std::string replace = "rn";
1892 S.replace(S.end()-2, S.end(), replace);
1893 }
1894 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001895 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001896 S += '@';
1897 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001898 }
1899 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00001900 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1901 // Another historical/compatibility reason.
1902 // We encode the underlying type which comes out as
1903 // {...};
1904 S += '^';
1905 getObjCEncodingForTypeImpl(PointeeTy, S,
1906 false, ExpandPointedToStructures,
1907 NULL);
1908 return;
1909 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001910 S += '@';
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00001911 if (FD) {
1912 ObjCInterfaceDecl *OI = PointeeTy->getAsObjCInterfaceType()->getDecl();
1913 S += '"';
1914 S += OI->getNameAsCString();
1915 S += '"';
1916 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001917 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001918 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001919 S += '#';
1920 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001921 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001922 S += ':';
1923 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001924 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001925
1926 if (PointeeTy->isCharType()) {
1927 // char pointer types should be encoded as '*' unless it is a
1928 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001929 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001930 S += '*';
1931 return;
1932 }
1933 }
1934
1935 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001936 getLegacyIntegralTypeEncoding(PointeeTy);
1937
1938 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001939 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001940 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001941 } else if (const ArrayType *AT =
1942 // Ignore type qualifiers etc.
1943 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001944 S += '[';
1945
1946 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1947 S += llvm::utostr(CAT->getSize().getZExtValue());
1948 else
1949 assert(0 && "Unhandled array type!");
1950
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001951 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001952 false, ExpandStructures, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001953 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001954 } else if (T->getAsFunctionType()) {
1955 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001956 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001957 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001958 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001959 // Anonymous structures print as '?'
1960 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1961 S += II->getName();
1962 } else {
1963 S += '?';
1964 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001965 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001966 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00001967 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1968 FieldEnd = RDecl->field_end();
1969 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001970 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001971 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00001972 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001973 S += '"';
1974 }
1975
1976 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001977 if (Field->isBitField()) {
1978 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1979 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001980 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001981 QualType qt = Field->getType();
1982 getLegacyIntegralTypeEncoding(qt);
1983 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001984 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001985 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001986 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001987 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001988 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001989 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001990 if (FD && FD->isBitField())
1991 EncodeBitField(this, S, FD);
1992 else
1993 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001994 } else if (T->isBlockPointerType()) {
1995 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001996 } else if (T->isObjCInterfaceType()) {
1997 // @encode(class_name)
1998 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
1999 S += '{';
2000 const IdentifierInfo *II = OI->getIdentifier();
2001 S += II->getName();
2002 S += '=';
2003 std::vector<FieldDecl*> RecFields;
2004 CollectObjCIvars(OI, RecFields);
2005 for (unsigned int i = 0; i != RecFields.size(); i++) {
2006 if (RecFields[i]->isBitField())
2007 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2008 RecFields[i]);
2009 else
2010 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2011 FD);
2012 }
2013 S += '}';
2014 }
2015 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002016 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002017}
2018
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002019void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002020 std::string& S) const {
2021 if (QT & Decl::OBJC_TQ_In)
2022 S += 'n';
2023 if (QT & Decl::OBJC_TQ_Inout)
2024 S += 'N';
2025 if (QT & Decl::OBJC_TQ_Out)
2026 S += 'o';
2027 if (QT & Decl::OBJC_TQ_Bycopy)
2028 S += 'O';
2029 if (QT & Decl::OBJC_TQ_Byref)
2030 S += 'R';
2031 if (QT & Decl::OBJC_TQ_Oneway)
2032 S += 'V';
2033}
2034
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002035void ASTContext::setBuiltinVaListType(QualType T)
2036{
2037 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2038
2039 BuiltinVaListType = T;
2040}
2041
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002042void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002043{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002044 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002045
2046 // typedef struct objc_object *id;
2047 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002048 // User error - caller will issue diagnostics.
2049 if (!ptr)
2050 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002051 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002052 // User error - caller will issue diagnostics.
2053 if (!rec)
2054 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002055 IdStructType = rec;
2056}
2057
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002058void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002059{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002060 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002061
2062 // typedef struct objc_selector *SEL;
2063 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002064 if (!ptr)
2065 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002066 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002067 if (!rec)
2068 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002069 SelStructType = rec;
2070}
2071
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002072void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002073{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002074 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002075}
2076
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002077void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002078{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002079 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002080
2081 // typedef struct objc_class *Class;
2082 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2083 assert(ptr && "'Class' incorrectly typed");
2084 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2085 assert(rec && "'Class' incorrectly typed");
2086 ClassStructType = rec;
2087}
2088
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002089void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2090 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002091 "'NSConstantString' type already set!");
2092
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002093 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002094}
2095
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002096/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002097/// TargetInfo, produce the corresponding type. The unsigned @p Type
2098/// is actually a value of type @c TargetInfo::IntType.
2099QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002100 switch (Type) {
2101 case TargetInfo::NoInt: return QualType();
2102 case TargetInfo::SignedShort: return ShortTy;
2103 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2104 case TargetInfo::SignedInt: return IntTy;
2105 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2106 case TargetInfo::SignedLong: return LongTy;
2107 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2108 case TargetInfo::SignedLongLong: return LongLongTy;
2109 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2110 }
2111
2112 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002113 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002114}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002115
2116//===----------------------------------------------------------------------===//
2117// Type Predicates.
2118//===----------------------------------------------------------------------===//
2119
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002120/// isObjCNSObjectType - Return true if this is an NSObject object using
2121/// NSObject attribute on a c-style pointer type.
2122/// FIXME - Make it work directly on types.
2123///
2124bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2125 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2126 if (TypedefDecl *TD = TDT->getDecl())
2127 if (TD->getAttr<ObjCNSObjectAttr>())
2128 return true;
2129 }
2130 return false;
2131}
2132
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002133/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2134/// to an object type. This includes "id" and "Class" (two 'special' pointers
2135/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2136/// ID type).
2137bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2138 if (Ty->isObjCQualifiedIdType())
2139 return true;
2140
Steve Naroff6ae98502008-10-21 18:24:04 +00002141 // Blocks are objects.
2142 if (Ty->isBlockPointerType())
2143 return true;
2144
2145 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002146 if (!Ty->isPointerType())
2147 return false;
2148
2149 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2150 // pointer types. This looks for the typedef specifically, not for the
2151 // underlying type.
2152 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2153 return true;
2154
2155 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002156 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2157 return true;
2158
2159 // If is has NSObject attribute, OK as well.
2160 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002161}
2162
Chris Lattner6ac46a42008-04-07 06:51:04 +00002163//===----------------------------------------------------------------------===//
2164// Type Compatibility Testing
2165//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002166
Steve Naroff1c7d0672008-09-04 15:10:53 +00002167/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002168/// block types. Types must be strictly compatible here. For example,
2169/// C unfortunately doesn't produce an error for the following:
2170///
2171/// int (*emptyArgFunc)();
2172/// int (*intArgList)(int) = emptyArgFunc;
2173///
2174/// For blocks, we will produce an error for the following (similar to C++):
2175///
2176/// int (^emptyArgBlock)();
2177/// int (^intArgBlock)(int) = emptyArgBlock;
2178///
2179/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2180///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002181bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002182 const FunctionType *lbase = lhs->getAsFunctionType();
2183 const FunctionType *rbase = rhs->getAsFunctionType();
2184 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2185 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2186 if (lproto && rproto)
2187 return !mergeTypes(lhs, rhs).isNull();
2188 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002189}
2190
Chris Lattner6ac46a42008-04-07 06:51:04 +00002191/// areCompatVectorTypes - Return true if the two specified vector types are
2192/// compatible.
2193static bool areCompatVectorTypes(const VectorType *LHS,
2194 const VectorType *RHS) {
2195 assert(LHS->isCanonical() && RHS->isCanonical());
2196 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002197 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002198}
2199
Eli Friedman3d815e72008-08-22 00:56:42 +00002200/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002201/// compatible for assignment from RHS to LHS. This handles validation of any
2202/// protocol qualifiers on the LHS or RHS.
2203///
Eli Friedman3d815e72008-08-22 00:56:42 +00002204bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2205 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002206 // Verify that the base decls are compatible: the RHS must be a subclass of
2207 // the LHS.
2208 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2209 return false;
2210
2211 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2212 // protocol qualified at all, then we are good.
2213 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2214 return true;
2215
2216 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2217 // isn't a superset.
2218 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2219 return true; // FIXME: should return false!
2220
2221 // Finally, we must have two protocol-qualified interfaces.
2222 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2223 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2224 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2225 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2226 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2227 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2228
2229 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2230 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2231 // LHS in a single parallel scan until we run out of elements in LHS.
2232 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2233 ObjCProtocolDecl *LHSProto = *LHSPI;
2234
2235 while (RHSPI != RHSPE) {
2236 ObjCProtocolDecl *RHSProto = *RHSPI++;
2237 // If the RHS has a protocol that the LHS doesn't, ignore it.
2238 if (RHSProto != LHSProto)
2239 continue;
2240
2241 // Otherwise, the RHS does have this element.
2242 ++LHSPI;
2243 if (LHSPI == LHSPE)
2244 return true; // All protocols in LHS exist in RHS.
2245
2246 LHSProto = *LHSPI;
2247 }
2248
2249 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2250 return false;
2251}
2252
Steve Naroffec0550f2007-10-15 20:41:53 +00002253/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2254/// both shall have the identically qualified version of a compatible type.
2255/// C99 6.2.7p1: Two types have compatible types if their types are the
2256/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002257bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2258 return !mergeTypes(LHS, RHS).isNull();
2259}
2260
2261QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2262 const FunctionType *lbase = lhs->getAsFunctionType();
2263 const FunctionType *rbase = rhs->getAsFunctionType();
2264 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2265 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2266 bool allLTypes = true;
2267 bool allRTypes = true;
2268
2269 // Check return type
2270 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2271 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002272 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2273 allLTypes = false;
2274 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2275 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002276
2277 if (lproto && rproto) { // two C99 style function prototypes
2278 unsigned lproto_nargs = lproto->getNumArgs();
2279 unsigned rproto_nargs = rproto->getNumArgs();
2280
2281 // Compatible functions must have the same number of arguments
2282 if (lproto_nargs != rproto_nargs)
2283 return QualType();
2284
2285 // Variadic and non-variadic functions aren't compatible
2286 if (lproto->isVariadic() != rproto->isVariadic())
2287 return QualType();
2288
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002289 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2290 return QualType();
2291
Eli Friedman3d815e72008-08-22 00:56:42 +00002292 // Check argument compatibility
2293 llvm::SmallVector<QualType, 10> types;
2294 for (unsigned i = 0; i < lproto_nargs; i++) {
2295 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2296 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2297 QualType argtype = mergeTypes(largtype, rargtype);
2298 if (argtype.isNull()) return QualType();
2299 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002300 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2301 allLTypes = false;
2302 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2303 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002304 }
2305 if (allLTypes) return lhs;
2306 if (allRTypes) return rhs;
2307 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002308 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002309 }
2310
2311 if (lproto) allRTypes = false;
2312 if (rproto) allLTypes = false;
2313
2314 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2315 if (proto) {
2316 if (proto->isVariadic()) return QualType();
2317 // Check that the types are compatible with the types that
2318 // would result from default argument promotions (C99 6.7.5.3p15).
2319 // The only types actually affected are promotable integer
2320 // types and floats, which would be passed as a different
2321 // type depending on whether the prototype is visible.
2322 unsigned proto_nargs = proto->getNumArgs();
2323 for (unsigned i = 0; i < proto_nargs; ++i) {
2324 QualType argTy = proto->getArgType(i);
2325 if (argTy->isPromotableIntegerType() ||
2326 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2327 return QualType();
2328 }
2329
2330 if (allLTypes) return lhs;
2331 if (allRTypes) return rhs;
2332 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002333 proto->getNumArgs(), lproto->isVariadic(),
2334 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002335 }
2336
2337 if (allLTypes) return lhs;
2338 if (allRTypes) return rhs;
2339 return getFunctionTypeNoProto(retType);
2340}
2341
2342QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002343 // C++ [expr]: If an expression initially has the type "reference to T", the
2344 // type is adjusted to "T" prior to any further analysis, the expression
2345 // designates the object or function denoted by the reference, and the
2346 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002347 // FIXME: C++ shouldn't be going through here! The rules are different
2348 // enough that they should be handled separately.
2349 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002350 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002351 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002352 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002353
Eli Friedman3d815e72008-08-22 00:56:42 +00002354 QualType LHSCan = getCanonicalType(LHS),
2355 RHSCan = getCanonicalType(RHS);
2356
2357 // If two types are identical, they are compatible.
2358 if (LHSCan == RHSCan)
2359 return LHS;
2360
2361 // If the qualifiers are different, the types aren't compatible
2362 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2363 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2364 return QualType();
2365
2366 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2367 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2368
Chris Lattner1adb8832008-01-14 05:45:46 +00002369 // We want to consider the two function types to be the same for these
2370 // comparisons, just force one to the other.
2371 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2372 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002373
2374 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002375 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2376 LHSClass = Type::ConstantArray;
2377 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2378 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002379
Nate Begeman213541a2008-04-18 23:10:10 +00002380 // Canonicalize ExtVector -> Vector.
2381 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2382 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002383
Chris Lattnerb0489812008-04-07 06:38:24 +00002384 // Consider qualified interfaces and interfaces the same.
2385 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2386 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002387
Chris Lattnera36a61f2008-04-07 05:43:21 +00002388 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002389 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002390 // ID is compatible with all qualified id types.
2391 if (LHS->isObjCQualifiedIdType()) {
2392 if (const PointerType *PT = RHS->getAsPointerType()) {
2393 QualType pType = PT->getPointeeType();
2394 if (isObjCIdType(pType))
2395 return LHS;
2396 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2397 // Unfortunately, this API is part of Sema (which we don't have access
2398 // to. Need to refactor. The following check is insufficient, since we
2399 // need to make sure the class implements the protocol.
2400 if (pType->isObjCInterfaceType())
2401 return LHS;
2402 }
2403 }
2404 if (RHS->isObjCQualifiedIdType()) {
2405 if (const PointerType *PT = LHS->getAsPointerType()) {
2406 QualType pType = PT->getPointeeType();
2407 if (isObjCIdType(pType))
2408 return RHS;
2409 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2410 // Unfortunately, this API is part of Sema (which we don't have access
2411 // to. Need to refactor. The following check is insufficient, since we
2412 // need to make sure the class implements the protocol.
2413 if (pType->isObjCInterfaceType())
2414 return RHS;
2415 }
2416 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002417 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2418 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002419 if (const EnumType* ETy = LHS->getAsEnumType()) {
2420 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2421 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002422 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002423 if (const EnumType* ETy = RHS->getAsEnumType()) {
2424 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2425 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002426 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002427
Eli Friedman3d815e72008-08-22 00:56:42 +00002428 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002429 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002430
Steve Naroff4a746782008-01-09 22:43:08 +00002431 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002432 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002433 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002434 {
2435 // Merge two pointer types, while trying to preserve typedef info
2436 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2437 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2438 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2439 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002440 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2441 return LHS;
2442 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2443 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002444 return getPointerType(ResultType);
2445 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002446 case Type::BlockPointer:
2447 {
2448 // Merge two block pointer types, while trying to preserve typedef info
2449 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2450 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2451 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2452 if (ResultType.isNull()) return QualType();
2453 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2454 return LHS;
2455 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2456 return RHS;
2457 return getBlockPointerType(ResultType);
2458 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002459 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002460 {
2461 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2462 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2463 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2464 return QualType();
2465
2466 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2467 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2468 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2469 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002470 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2471 return LHS;
2472 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2473 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002474 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2475 ArrayType::ArraySizeModifier(), 0);
2476 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2477 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002478 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2479 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002480 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2481 return LHS;
2482 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2483 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002484 if (LVAT) {
2485 // FIXME: This isn't correct! But tricky to implement because
2486 // the array's size has to be the size of LHS, but the type
2487 // has to be different.
2488 return LHS;
2489 }
2490 if (RVAT) {
2491 // FIXME: This isn't correct! But tricky to implement because
2492 // the array's size has to be the size of RHS, but the type
2493 // has to be different.
2494 return RHS;
2495 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002496 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2497 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002498 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002499 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002500 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002501 return mergeFunctionTypes(LHS, RHS);
2502 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002503 // FIXME: Why are these compatible?
2504 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2505 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2506 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002507 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002508 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002509 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002510 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002511 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2512 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002513 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002514 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002515 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2516 // for checking assignment/comparison safety
2517 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002518 case Type::ObjCQualifiedId:
2519 // Distinct qualified id's are not compatible.
2520 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002521 default:
2522 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002523 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002524 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002525}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002526
Chris Lattner5426bf62008-04-07 07:01:58 +00002527//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002528// Integer Predicates
2529//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002530
Eli Friedmanad74a752008-06-28 06:23:08 +00002531unsigned ASTContext::getIntWidth(QualType T) {
2532 if (T == BoolTy)
2533 return 1;
2534 // At the moment, only bool has padding bits
2535 return (unsigned)getTypeSize(T);
2536}
2537
2538QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2539 assert(T->isSignedIntegerType() && "Unexpected type");
2540 if (const EnumType* ETy = T->getAsEnumType())
2541 T = ETy->getDecl()->getIntegerType();
2542 const BuiltinType* BTy = T->getAsBuiltinType();
2543 assert (BTy && "Unexpected signed integer type");
2544 switch (BTy->getKind()) {
2545 case BuiltinType::Char_S:
2546 case BuiltinType::SChar:
2547 return UnsignedCharTy;
2548 case BuiltinType::Short:
2549 return UnsignedShortTy;
2550 case BuiltinType::Int:
2551 return UnsignedIntTy;
2552 case BuiltinType::Long:
2553 return UnsignedLongTy;
2554 case BuiltinType::LongLong:
2555 return UnsignedLongLongTy;
2556 default:
2557 assert(0 && "Unexpected signed integer type");
2558 return QualType();
2559 }
2560}
2561
2562
2563//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002564// Serialization Support
2565//===----------------------------------------------------------------------===//
2566
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002567/// Emit - Serialize an ASTContext object to Bitcode.
2568void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002569 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002570 S.EmitRef(SourceMgr);
2571 S.EmitRef(Target);
2572 S.EmitRef(Idents);
2573 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002574
Ted Kremenekfee04522007-10-31 22:44:07 +00002575 // Emit the size of the type vector so that we can reserve that size
2576 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002577 S.EmitInt(Types.size());
2578
Ted Kremenek03ed4402007-11-13 22:02:55 +00002579 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2580 I!=E;++I)
2581 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002582
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002583 S.EmitOwnedPtr(TUDecl);
2584
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002585 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002586}
2587
Ted Kremenek0f84c002007-11-13 00:25:37 +00002588ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002589
2590 // Read the language options.
2591 LangOptions LOpts;
2592 LOpts.Read(D);
2593
Ted Kremenekfee04522007-10-31 22:44:07 +00002594 SourceManager &SM = D.ReadRef<SourceManager>();
2595 TargetInfo &t = D.ReadRef<TargetInfo>();
2596 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2597 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002598
Ted Kremenekfee04522007-10-31 22:44:07 +00002599 unsigned size_reserve = D.ReadInt();
2600
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002601 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2602 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002603
Ted Kremenek03ed4402007-11-13 22:02:55 +00002604 for (unsigned i = 0; i < size_reserve; ++i)
2605 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002606
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002607 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2608
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002609 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002610
2611 return A;
2612}