blob: 643d4753250226c4f60700632e4c0cc9c4d64e69 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Daniel Dunbarde300732008-08-11 04:54:23 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
31 IdentifierTable &idents, SelectorTable &sels,
32 unsigned size_reserve) :
33 CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t),
34 Idents(idents), Selectors(sels)
35{
36 if (size_reserve > 0) Types.reserve(size_reserve);
37 InitBuiltinTypes();
38 BuiltinInfo.InitializeBuiltins(idents, Target);
39 TUDecl = TranslationUnitDecl::Create(*this);
40}
41
Chris Lattner4b009652007-07-25 00:24:17 +000042ASTContext::~ASTContext() {
43 // Deallocate all the types.
44 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000045 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000046 Types.pop_back();
47 }
Eli Friedman65489b72008-05-27 03:08:09 +000048
49 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050}
51
52void ASTContext::PrintStats() const {
53 fprintf(stderr, "*** AST Context Stats:\n");
54 fprintf(stderr, " %d types total.\n", (int)Types.size());
55 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
56 unsigned NumVector = 0, NumComplex = 0;
57 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
58
59 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000060 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
61 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000062 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000063
64 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
65 Type *T = Types[i];
66 if (isa<BuiltinType>(T))
67 ++NumBuiltin;
68 else if (isa<PointerType>(T))
69 ++NumPointer;
70 else if (isa<ReferenceType>(T))
71 ++NumReference;
72 else if (isa<ComplexType>(T))
73 ++NumComplex;
74 else if (isa<ArrayType>(T))
75 ++NumArray;
76 else if (isa<VectorType>(T))
77 ++NumVector;
78 else if (isa<FunctionTypeNoProto>(T))
79 ++NumFunctionNP;
80 else if (isa<FunctionTypeProto>(T))
81 ++NumFunctionP;
82 else if (isa<TypedefType>(T))
83 ++NumTypeName;
84 else if (TagType *TT = dyn_cast<TagType>(T)) {
85 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000086 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000087 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000088 case TagDecl::TK_struct: ++NumTagStruct; break;
89 case TagDecl::TK_union: ++NumTagUnion; break;
90 case TagDecl::TK_class: ++NumTagClass; break;
91 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000092 }
Ted Kremenek42730c52008-01-07 19:49:32 +000093 } else if (isa<ObjCInterfaceType>(T))
94 ++NumObjCInterfaces;
95 else if (isa<ObjCQualifiedInterfaceType>(T))
96 ++NumObjCQualifiedInterfaces;
97 else if (isa<ObjCQualifiedIdType>(T))
98 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000099 else if (isa<TypeOfType>(T))
100 ++NumTypeOfTypes;
101 else if (isa<TypeOfExpr>(T))
102 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000103 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000104 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000105 assert(0 && "Unknown type!");
106 }
107 }
108
109 fprintf(stderr, " %d builtin types\n", NumBuiltin);
110 fprintf(stderr, " %d pointer types\n", NumPointer);
111 fprintf(stderr, " %d reference types\n", NumReference);
112 fprintf(stderr, " %d complex types\n", NumComplex);
113 fprintf(stderr, " %d array types\n", NumArray);
114 fprintf(stderr, " %d vector types\n", NumVector);
115 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
116 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
117 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
118 fprintf(stderr, " %d tagged types\n", NumTagged);
119 fprintf(stderr, " %d struct types\n", NumTagStruct);
120 fprintf(stderr, " %d union types\n", NumTagUnion);
121 fprintf(stderr, " %d class types\n", NumTagClass);
122 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000123 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000124 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000125 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000126 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000127 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000128 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
129 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
130
Chris Lattner4b009652007-07-25 00:24:17 +0000131 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
132 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
133 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
134 NumFunctionP*sizeof(FunctionTypeProto)+
135 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000136 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
137 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000138}
139
140
141void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
142 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
143}
144
Chris Lattner4b009652007-07-25 00:24:17 +0000145void ASTContext::InitBuiltinTypes() {
146 assert(VoidTy.isNull() && "Context reinitialized?");
147
148 // C99 6.2.5p19.
149 InitBuiltinType(VoidTy, BuiltinType::Void);
150
151 // C99 6.2.5p2.
152 InitBuiltinType(BoolTy, BuiltinType::Bool);
153 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000154 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000155 InitBuiltinType(CharTy, BuiltinType::Char_S);
156 else
157 InitBuiltinType(CharTy, BuiltinType::Char_U);
158 // C99 6.2.5p4.
159 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
160 InitBuiltinType(ShortTy, BuiltinType::Short);
161 InitBuiltinType(IntTy, BuiltinType::Int);
162 InitBuiltinType(LongTy, BuiltinType::Long);
163 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
164
165 // C99 6.2.5p6.
166 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
167 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
168 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
169 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
170 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
171
172 // C99 6.2.5p10.
173 InitBuiltinType(FloatTy, BuiltinType::Float);
174 InitBuiltinType(DoubleTy, BuiltinType::Double);
175 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000176
177 // C++ 3.9.1p5
178 InitBuiltinType(WCharTy, BuiltinType::WChar);
179
Chris Lattner4b009652007-07-25 00:24:17 +0000180 // C99 6.2.5p11.
181 FloatComplexTy = getComplexType(FloatTy);
182 DoubleComplexTy = getComplexType(DoubleTy);
183 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000184
185 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000186 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000187 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000189 ClassStructType = 0;
190
Ted Kremenek42730c52008-01-07 19:49:32 +0000191 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000192
193 // void * type
194 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000195}
196
197//===----------------------------------------------------------------------===//
198// Type Sizing and Analysis
199//===----------------------------------------------------------------------===//
200
Chris Lattner2a674dc2008-06-30 18:32:54 +0000201/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
202/// scalar floating point type.
203const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
204 const BuiltinType *BT = T->getAsBuiltinType();
205 assert(BT && "Not a floating point type!");
206 switch (BT->getKind()) {
207 default: assert(0 && "Not a floating point type!");
208 case BuiltinType::Float: return Target.getFloatFormat();
209 case BuiltinType::Double: return Target.getDoubleFormat();
210 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
211 }
212}
213
214
Chris Lattner4b009652007-07-25 00:24:17 +0000215/// getTypeSize - Return the size of the specified type, in bits. This method
216/// does not work on incomplete types.
217std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000218ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000219 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000220 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000221 unsigned Align;
222 switch (T->getTypeClass()) {
223 case Type::TypeName: assert(0 && "Not a canonical type!");
224 case Type::FunctionNoProto:
225 case Type::FunctionProto:
226 default:
227 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000228 case Type::VariableArray:
229 assert(0 && "VLAs not implemented yet!");
230 case Type::ConstantArray: {
231 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
232
Chris Lattner8cd0e932008-03-05 18:54:05 +0000233 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000234 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000235 Align = EltInfo.second;
236 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000237 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000238 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case Type::Vector: {
240 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000241 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000242 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000243 // FIXME: This isn't right for unusual vectors
244 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000245 break;
246 }
247
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000248 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000249 switch (cast<BuiltinType>(T)->getKind()) {
250 default: assert(0 && "Unknown builtin type!");
251 case BuiltinType::Void:
252 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000253 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000254 Width = Target.getBoolWidth();
255 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000256 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000257 case BuiltinType::Char_S:
258 case BuiltinType::Char_U:
259 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000261 Width = Target.getCharWidth();
262 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000263 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000264 case BuiltinType::WChar:
265 Width = Target.getWCharWidth();
266 Align = Target.getWCharAlign();
267 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000268 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000269 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000270 Width = Target.getShortWidth();
271 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000272 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000273 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000274 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000275 Width = Target.getIntWidth();
276 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000277 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000278 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000279 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000280 Width = Target.getLongWidth();
281 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000282 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000283 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000284 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000285 Width = Target.getLongLongWidth();
286 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000287 break;
288 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 Width = Target.getFloatWidth();
290 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000291 break;
292 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000293 Width = Target.getDoubleWidth();
294 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000295 break;
296 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000297 Width = Target.getLongDoubleWidth();
298 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000299 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 }
301 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000302 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000303 // FIXME: Pointers into different addr spaces could have different sizes and
304 // alignment requirements: getPointerInfo should take an AddrSpace.
305 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000306 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000307 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000308 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000309 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000310 case Type::Pointer: {
311 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000312 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000313 Align = Target.getPointerAlign(AS);
314 break;
315 }
Chris Lattner4b009652007-07-25 00:24:17 +0000316 case Type::Reference:
317 // "When applied to a reference or a reference type, the result is the size
318 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000319 // FIXME: This is wrong for struct layout: a reference in a struct has
320 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000321 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000322
323 case Type::Complex: {
324 // Complex types have the same alignment as their elements, but twice the
325 // size.
326 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000327 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000328 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000329 Align = EltInfo.second;
330 break;
331 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000332 case Type::ObjCInterface: {
333 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
334 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
335 Width = Layout.getSize();
336 Align = Layout.getAlignment();
337 break;
338 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000339 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000340 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
341 Width = 1;
342 Align = 1;
343 break;
344 }
345
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000346 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
347 return getTypeInfo(ET->getDecl()->getIntegerType());
348
349 RecordType *RT = cast<RecordType>(T);
350 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
351 Width = Layout.getSize();
352 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000353 break;
354 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000355 }
Chris Lattner4b009652007-07-25 00:24:17 +0000356
357 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000358 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000359}
360
Devang Patelbfe323c2008-06-04 21:22:16 +0000361/// LayoutField - Field layout.
362void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
363 bool IsUnion, bool StructIsPacked,
364 ASTContext &Context) {
365 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
366 uint64_t FieldOffset = IsUnion ? 0 : Size;
367 uint64_t FieldSize;
368 unsigned FieldAlign;
369
370 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
371 // TODO: Need to check this algorithm on other targets!
372 // (tested on Linux-X86)
373 llvm::APSInt I(32);
374 bool BitWidthIsICE =
375 BitWidthExpr->isIntegerConstantExpr(I, Context);
376 assert (BitWidthIsICE && "Invalid BitField size expression");
377 FieldSize = I.getZExtValue();
378
379 std::pair<uint64_t, unsigned> FieldInfo =
380 Context.getTypeInfo(FD->getType());
381 uint64_t TypeSize = FieldInfo.first;
382
383 FieldAlign = FieldInfo.second;
384 if (FieldIsPacked)
385 FieldAlign = 1;
386 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
387 FieldAlign = std::max(FieldAlign, AA->getAlignment());
388
389 // Check if we need to add padding to give the field the correct
390 // alignment.
391 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
392 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
393
394 // Padding members don't affect overall alignment
395 if (!FD->getIdentifier())
396 FieldAlign = 1;
397 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000398 if (FD->getType()->isIncompleteArrayType()) {
399 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000400 // query getTypeInfo about these, so we figure it out here.
401 // Flexible array members don't have any size, but they
402 // have to be aligned appropriately for their element type.
403 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000404 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000405 FieldAlign = Context.getTypeAlign(ATy->getElementType());
406 } else {
407 std::pair<uint64_t, unsigned> FieldInfo =
408 Context.getTypeInfo(FD->getType());
409 FieldSize = FieldInfo.first;
410 FieldAlign = FieldInfo.second;
411 }
412
413 if (FieldIsPacked)
414 FieldAlign = 8;
415 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
416 FieldAlign = std::max(FieldAlign, AA->getAlignment());
417
418 // Round up the current record size to the field's alignment boundary.
419 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
420 }
421
422 // Place this field at the current location.
423 FieldOffsets[FieldNo] = FieldOffset;
424
425 // Reserve space for this field.
426 if (IsUnion) {
427 Size = std::max(Size, FieldSize);
428 } else {
429 Size = FieldOffset + FieldSize;
430 }
431
432 // Remember max struct/class alignment.
433 Alignment = std::max(Alignment, FieldAlign);
434}
435
Devang Patel4b6bf702008-06-04 21:54:36 +0000436
437/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
438/// specified Objective C, which indicates its size and ivar
439/// position information.
440const ASTRecordLayout &
441ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
442 // Look up this layout, if already laid out, return what we have.
443 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
444 if (Entry) return *Entry;
445
446 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
447 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000448 ASTRecordLayout *NewEntry = NULL;
449 unsigned FieldCount = D->ivar_size();
450 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
451 FieldCount++;
452 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
453 unsigned Alignment = SL.getAlignment();
454 uint64_t Size = SL.getSize();
455 NewEntry = new ASTRecordLayout(Size, Alignment);
456 NewEntry->InitializeLayout(FieldCount);
457 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
458 } else {
459 NewEntry = new ASTRecordLayout();
460 NewEntry->InitializeLayout(FieldCount);
461 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000462 Entry = NewEntry;
463
Devang Patel4b6bf702008-06-04 21:54:36 +0000464 bool IsPacked = D->getAttr<PackedAttr>();
465
466 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
467 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
468 AA->getAlignment()));
469
470 // Layout each ivar sequentially.
471 unsigned i = 0;
472 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
473 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
474 const ObjCIvarDecl* Ivar = (*IVI);
475 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
476 }
477
478 // Finally, round the size of the total struct up to the alignment of the
479 // struct itself.
480 NewEntry->FinalizeLayout();
481 return *NewEntry;
482}
483
Devang Patel7a78e432007-11-01 19:11:01 +0000484/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000485/// specified record (struct/union/class), which indicates its size and field
486/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000487const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000488 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000489
Chris Lattner4b009652007-07-25 00:24:17 +0000490 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000491 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000492 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000493
Devang Patel7a78e432007-11-01 19:11:01 +0000494 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
495 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
496 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000497 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000498
Devang Patelbfe323c2008-06-04 21:22:16 +0000499 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000500 bool StructIsPacked = D->getAttr<PackedAttr>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000501 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000502
Eli Friedman5949a022008-05-30 09:31:38 +0000503 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000504 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
505 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000506
Eli Friedman5949a022008-05-30 09:31:38 +0000507 // Layout each field, for now, just sequentially, respecting alignment. In
508 // the future, this will need to be tweakable by targets.
509 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
510 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000511 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000512 }
Eli Friedman5949a022008-05-30 09:31:38 +0000513
514 // Finally, round the size of the total struct up to the alignment of the
515 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000516 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000517 return *NewEntry;
518}
519
Chris Lattner4b009652007-07-25 00:24:17 +0000520//===----------------------------------------------------------------------===//
521// Type creation/memoization methods
522//===----------------------------------------------------------------------===//
523
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000524QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000525 QualType CanT = getCanonicalType(T);
526 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000527 return T;
528
529 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
530 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000531 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000532 "Type is already address space qualified");
533
534 // Check if we've already instantiated an address space qual'd type of this
535 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000536 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000537 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000538 void *InsertPos = 0;
539 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
540 return QualType(ASQy, 0);
541
542 // If the base type isn't canonical, this won't be a canonical type either,
543 // so fill in the canonical type field.
544 QualType Canonical;
545 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000546 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000547
548 // Get the new insert position for the node we care about.
549 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
550 assert(NewIP == 0 && "Shouldn't be in the map!");
551 }
Chris Lattner35fef522008-02-20 20:55:12 +0000552 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000553 ASQualTypes.InsertNode(New, InsertPos);
554 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000555 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000556}
557
Chris Lattner4b009652007-07-25 00:24:17 +0000558
559/// getComplexType - Return the uniqued reference to the type for a complex
560/// number with the specified element type.
561QualType ASTContext::getComplexType(QualType T) {
562 // Unique pointers, to guarantee there is only one pointer of a particular
563 // structure.
564 llvm::FoldingSetNodeID ID;
565 ComplexType::Profile(ID, T);
566
567 void *InsertPos = 0;
568 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
569 return QualType(CT, 0);
570
571 // If the pointee type isn't canonical, this won't be a canonical type either,
572 // so fill in the canonical type field.
573 QualType Canonical;
574 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000575 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000576
577 // Get the new insert position for the node we care about.
578 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
579 assert(NewIP == 0 && "Shouldn't be in the map!");
580 }
581 ComplexType *New = new ComplexType(T, Canonical);
582 Types.push_back(New);
583 ComplexTypes.InsertNode(New, InsertPos);
584 return QualType(New, 0);
585}
586
587
588/// getPointerType - Return the uniqued reference to the type for a pointer to
589/// the specified type.
590QualType ASTContext::getPointerType(QualType T) {
591 // Unique pointers, to guarantee there is only one pointer of a particular
592 // structure.
593 llvm::FoldingSetNodeID ID;
594 PointerType::Profile(ID, T);
595
596 void *InsertPos = 0;
597 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
598 return QualType(PT, 0);
599
600 // If the pointee type isn't canonical, this won't be a canonical type either,
601 // so fill in the canonical type field.
602 QualType Canonical;
603 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000604 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000605
606 // Get the new insert position for the node we care about.
607 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
608 assert(NewIP == 0 && "Shouldn't be in the map!");
609 }
610 PointerType *New = new PointerType(T, Canonical);
611 Types.push_back(New);
612 PointerTypes.InsertNode(New, InsertPos);
613 return QualType(New, 0);
614}
615
616/// getReferenceType - Return the uniqued reference to the type for a reference
617/// to the specified type.
618QualType ASTContext::getReferenceType(QualType T) {
619 // Unique pointers, to guarantee there is only one pointer of a particular
620 // structure.
621 llvm::FoldingSetNodeID ID;
622 ReferenceType::Profile(ID, T);
623
624 void *InsertPos = 0;
625 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
626 return QualType(RT, 0);
627
628 // If the referencee type isn't canonical, this won't be a canonical type
629 // either, so fill in the canonical type field.
630 QualType Canonical;
631 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000632 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000633
634 // Get the new insert position for the node we care about.
635 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
636 assert(NewIP == 0 && "Shouldn't be in the map!");
637 }
638
639 ReferenceType *New = new ReferenceType(T, Canonical);
640 Types.push_back(New);
641 ReferenceTypes.InsertNode(New, InsertPos);
642 return QualType(New, 0);
643}
644
Steve Naroff83c13012007-08-30 01:06:46 +0000645/// getConstantArrayType - Return the unique reference to the type for an
646/// array of the specified element type.
647QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000648 const llvm::APInt &ArySize,
649 ArrayType::ArraySizeModifier ASM,
650 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000651 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000652 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000653
654 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000655 if (ConstantArrayType *ATP =
656 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000657 return QualType(ATP, 0);
658
659 // If the element type isn't canonical, this won't be a canonical type either,
660 // so fill in the canonical type field.
661 QualType Canonical;
662 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000663 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000664 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000665 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000666 ConstantArrayType *NewIP =
667 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
668
Chris Lattner4b009652007-07-25 00:24:17 +0000669 assert(NewIP == 0 && "Shouldn't be in the map!");
670 }
671
Steve Naroff24c9b982007-08-30 18:10:14 +0000672 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
673 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000674 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000675 Types.push_back(New);
676 return QualType(New, 0);
677}
678
Steve Naroffe2579e32007-08-30 18:14:25 +0000679/// getVariableArrayType - Returns a non-unique reference to the type for a
680/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000681QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
682 ArrayType::ArraySizeModifier ASM,
683 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000684 // Since we don't unique expressions, it isn't possible to unique VLA's
685 // that have an expression provided for their size.
686
687 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
688 ASM, EltTypeQuals);
689
690 VariableArrayTypes.push_back(New);
691 Types.push_back(New);
692 return QualType(New, 0);
693}
694
695QualType ASTContext::getIncompleteArrayType(QualType EltTy,
696 ArrayType::ArraySizeModifier ASM,
697 unsigned EltTypeQuals) {
698 llvm::FoldingSetNodeID ID;
699 IncompleteArrayType::Profile(ID, EltTy);
700
701 void *InsertPos = 0;
702 if (IncompleteArrayType *ATP =
703 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
704 return QualType(ATP, 0);
705
706 // If the element type isn't canonical, this won't be a canonical type
707 // either, so fill in the canonical type field.
708 QualType Canonical;
709
710 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000711 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000712 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000713
714 // Get the new insert position for the node we care about.
715 IncompleteArrayType *NewIP =
716 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
717
718 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000719 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000720
721 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
722 ASM, EltTypeQuals);
723
724 IncompleteArrayTypes.InsertNode(New, InsertPos);
725 Types.push_back(New);
726 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000727}
728
Chris Lattner4b009652007-07-25 00:24:17 +0000729/// getVectorType - Return the unique reference to a vector type of
730/// the specified element type and size. VectorType must be a built-in type.
731QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
732 BuiltinType *baseType;
733
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000734 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000735 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
736
737 // Check if we've already instantiated a vector of this type.
738 llvm::FoldingSetNodeID ID;
739 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
740 void *InsertPos = 0;
741 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
742 return QualType(VTP, 0);
743
744 // If the element type isn't canonical, this won't be a canonical type either,
745 // so fill in the canonical type field.
746 QualType Canonical;
747 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000748 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000749
750 // Get the new insert position for the node we care about.
751 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
752 assert(NewIP == 0 && "Shouldn't be in the map!");
753 }
754 VectorType *New = new VectorType(vecType, NumElts, Canonical);
755 VectorTypes.InsertNode(New, InsertPos);
756 Types.push_back(New);
757 return QualType(New, 0);
758}
759
Nate Begemanaf6ed502008-04-18 23:10:10 +0000760/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000761/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000762QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000763 BuiltinType *baseType;
764
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000765 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000766 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000767
768 // Check if we've already instantiated a vector of this type.
769 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000770 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000771 void *InsertPos = 0;
772 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
773 return QualType(VTP, 0);
774
775 // If the element type isn't canonical, this won't be a canonical type either,
776 // so fill in the canonical type field.
777 QualType Canonical;
778 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000779 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000780
781 // Get the new insert position for the node we care about.
782 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
783 assert(NewIP == 0 && "Shouldn't be in the map!");
784 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000785 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000786 VectorTypes.InsertNode(New, InsertPos);
787 Types.push_back(New);
788 return QualType(New, 0);
789}
790
791/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
792///
793QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
794 // Unique functions, to guarantee there is only one function of a particular
795 // structure.
796 llvm::FoldingSetNodeID ID;
797 FunctionTypeNoProto::Profile(ID, ResultTy);
798
799 void *InsertPos = 0;
800 if (FunctionTypeNoProto *FT =
801 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
802 return QualType(FT, 0);
803
804 QualType Canonical;
805 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000806 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000807
808 // Get the new insert position for the node we care about.
809 FunctionTypeNoProto *NewIP =
810 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
811 assert(NewIP == 0 && "Shouldn't be in the map!");
812 }
813
814 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
815 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000816 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000817 return QualType(New, 0);
818}
819
820/// getFunctionType - Return a normal function type with a typed argument
821/// list. isVariadic indicates whether the argument list includes '...'.
822QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
823 unsigned NumArgs, bool isVariadic) {
824 // Unique functions, to guarantee there is only one function of a particular
825 // structure.
826 llvm::FoldingSetNodeID ID;
827 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
828
829 void *InsertPos = 0;
830 if (FunctionTypeProto *FTP =
831 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
832 return QualType(FTP, 0);
833
834 // Determine whether the type being created is already canonical or not.
835 bool isCanonical = ResultTy->isCanonical();
836 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
837 if (!ArgArray[i]->isCanonical())
838 isCanonical = false;
839
840 // If this type isn't canonical, get the canonical version of it.
841 QualType Canonical;
842 if (!isCanonical) {
843 llvm::SmallVector<QualType, 16> CanonicalArgs;
844 CanonicalArgs.reserve(NumArgs);
845 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000846 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000847
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000848 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000849 &CanonicalArgs[0], NumArgs,
850 isVariadic);
851
852 // Get the new insert position for the node we care about.
853 FunctionTypeProto *NewIP =
854 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
855 assert(NewIP == 0 && "Shouldn't be in the map!");
856 }
857
858 // FunctionTypeProto objects are not allocated with new because they have a
859 // variable size array (for parameter types) at the end of them.
860 FunctionTypeProto *FTP =
861 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
862 NumArgs*sizeof(QualType));
863 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
864 Canonical);
865 Types.push_back(FTP);
866 FunctionTypeProtos.InsertNode(FTP, InsertPos);
867 return QualType(FTP, 0);
868}
869
Douglas Gregor1d661552008-04-13 21:07:44 +0000870/// getTypeDeclType - Return the unique reference to the type for the
871/// specified type declaration.
872QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
873 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
874
875 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
876 return getTypedefType(Typedef);
877 else if (ObjCInterfaceDecl *ObjCInterface
878 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
879 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000880
881 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
882 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
883 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000884 Decl->TypeForDecl = new RecordType(Record);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000885 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000886 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000887 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000888 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000889
890 Types.push_back(Decl->TypeForDecl);
891 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000892}
893
Chris Lattner4b009652007-07-25 00:24:17 +0000894/// getTypedefType - Return the unique reference to the type for the
895/// specified typename decl.
896QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
897 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
898
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000899 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000900 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000901 Types.push_back(Decl->TypeForDecl);
902 return QualType(Decl->TypeForDecl, 0);
903}
904
Ted Kremenek42730c52008-01-07 19:49:32 +0000905/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000906/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000907QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000908 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
909
Ted Kremenek42730c52008-01-07 19:49:32 +0000910 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000911 Types.push_back(Decl->TypeForDecl);
912 return QualType(Decl->TypeForDecl, 0);
913}
914
Chris Lattnere1352302008-04-07 04:56:42 +0000915/// CmpProtocolNames - Comparison predicate for sorting protocols
916/// alphabetically.
917static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
918 const ObjCProtocolDecl *RHS) {
919 return strcmp(LHS->getName(), RHS->getName()) < 0;
920}
921
922static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
923 unsigned &NumProtocols) {
924 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
925
926 // Sort protocols, keyed by name.
927 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
928
929 // Remove duplicates.
930 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
931 NumProtocols = ProtocolsEnd-Protocols;
932}
933
934
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000935/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
936/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000937QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
938 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000939 // Sort the protocol list alphabetically to canonicalize it.
940 SortAndUniqueProtocols(Protocols, NumProtocols);
941
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000942 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000943 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000944
945 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000946 if (ObjCQualifiedInterfaceType *QT =
947 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000948 return QualType(QT, 0);
949
950 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000951 ObjCQualifiedInterfaceType *QType =
952 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000953 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000954 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000955 return QualType(QType, 0);
956}
957
Chris Lattnere1352302008-04-07 04:56:42 +0000958/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
959/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +0000960QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000961 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000962 // Sort the protocol list alphabetically to canonicalize it.
963 SortAndUniqueProtocols(Protocols, NumProtocols);
964
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000965 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000966 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000967
968 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000969 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +0000970 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000971 return QualType(QT, 0);
972
973 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000974 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000975 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000976 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000977 return QualType(QType, 0);
978}
979
Steve Naroff0604dd92007-08-01 18:02:17 +0000980/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
981/// TypeOfExpr AST's (since expression's are never shared). For example,
982/// multiple declarations that refer to "typeof(x)" all contain different
983/// DeclRefExpr's. This doesn't effect the type checker, since it operates
984/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000985QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000986 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000987 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
988 Types.push_back(toe);
989 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000990}
991
Steve Naroff0604dd92007-08-01 18:02:17 +0000992/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
993/// TypeOfType AST's. The only motivation to unique these nodes would be
994/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
995/// an issue. This doesn't effect the type checker, since it operates
996/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000997QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000998 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000999 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1000 Types.push_back(tot);
1001 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001002}
1003
Chris Lattner4b009652007-07-25 00:24:17 +00001004/// getTagDeclType - Return the unique reference to the type for the
1005/// specified TagDecl (struct/union/class/enum) decl.
1006QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001007 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001008 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001009}
1010
1011/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1012/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1013/// needs to agree with the definition in <stddef.h>.
1014QualType ASTContext::getSizeType() const {
1015 // On Darwin, size_t is defined as a "long unsigned int".
1016 // FIXME: should derive from "Target".
1017 return UnsignedLongTy;
1018}
1019
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001020/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001021/// width of characters in wide strings, The value is target dependent and
1022/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001023QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001024 if (LangOpts.CPlusPlus)
1025 return WCharTy;
1026
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001027 // On Darwin, wchar_t is defined as a "int".
1028 // FIXME: should derive from "Target".
1029 return IntTy;
1030}
1031
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001032/// getSignedWCharType - Return the type of "signed wchar_t".
1033/// Used when in C++, as a GCC extension.
1034QualType ASTContext::getSignedWCharType() const {
1035 // FIXME: derive from "Target" ?
1036 return WCharTy;
1037}
1038
1039/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1040/// Used when in C++, as a GCC extension.
1041QualType ASTContext::getUnsignedWCharType() const {
1042 // FIXME: derive from "Target" ?
1043 return UnsignedIntTy;
1044}
1045
Chris Lattner4b009652007-07-25 00:24:17 +00001046/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1047/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1048QualType ASTContext::getPointerDiffType() const {
1049 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1050 // FIXME: should derive from "Target".
1051 return IntTy;
1052}
1053
Chris Lattner19eb97e2008-04-02 05:18:44 +00001054//===----------------------------------------------------------------------===//
1055// Type Operators
1056//===----------------------------------------------------------------------===//
1057
Chris Lattner3dae6f42008-04-06 22:41:35 +00001058/// getCanonicalType - Return the canonical (structural) type corresponding to
1059/// the specified potentially non-canonical type. The non-canonical version
1060/// of a type may have many "decorated" versions of types. Decorators can
1061/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1062/// to be free of any of these, allowing two canonical types to be compared
1063/// for exact equality with a simple pointer comparison.
1064QualType ASTContext::getCanonicalType(QualType T) {
1065 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001066
1067 // If the result has type qualifiers, make sure to canonicalize them as well.
1068 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1069 if (TypeQuals == 0) return CanType;
1070
1071 // If the type qualifiers are on an array type, get the canonical type of the
1072 // array with the qualifiers applied to the element type.
1073 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1074 if (!AT)
1075 return CanType.getQualifiedType(TypeQuals);
1076
1077 // Get the canonical version of the element with the extra qualifiers on it.
1078 // This can recursively sink qualifiers through multiple levels of arrays.
1079 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1080 NewEltTy = getCanonicalType(NewEltTy);
1081
1082 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1083 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1084 CAT->getIndexTypeQualifier());
1085 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1086 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1087 IAT->getIndexTypeQualifier());
1088
1089 // FIXME: What is the ownership of size expressions in VLAs?
1090 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1091 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1092 VAT->getSizeModifier(),
1093 VAT->getIndexTypeQualifier());
1094}
1095
1096
1097const ArrayType *ASTContext::getAsArrayType(QualType T) {
1098 // Handle the non-qualified case efficiently.
1099 if (T.getCVRQualifiers() == 0) {
1100 // Handle the common positive case fast.
1101 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1102 return AT;
1103 }
1104
1105 // Handle the common negative case fast, ignoring CVR qualifiers.
1106 QualType CType = T->getCanonicalTypeInternal();
1107
1108 // Make sure to look through type qualifiers (like ASQuals) for the negative
1109 // test.
1110 if (!isa<ArrayType>(CType) &&
1111 !isa<ArrayType>(CType.getUnqualifiedType()))
1112 return 0;
1113
1114 // Apply any CVR qualifiers from the array type to the element type. This
1115 // implements C99 6.7.3p8: "If the specification of an array type includes
1116 // any type qualifiers, the element type is so qualified, not the array type."
1117
1118 // If we get here, we either have type qualifiers on the type, or we have
1119 // sugar such as a typedef in the way. If we have type qualifiers on the type
1120 // we must propagate them down into the elemeng type.
1121 unsigned CVRQuals = T.getCVRQualifiers();
1122 unsigned AddrSpace = 0;
1123 Type *Ty = T.getTypePtr();
1124
1125 // Rip through ASQualType's and typedefs to get to a concrete type.
1126 while (1) {
1127 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1128 AddrSpace = ASQT->getAddressSpace();
1129 Ty = ASQT->getBaseType();
1130 } else {
1131 T = Ty->getDesugaredType();
1132 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1133 break;
1134 CVRQuals |= T.getCVRQualifiers();
1135 Ty = T.getTypePtr();
1136 }
1137 }
1138
1139 // If we have a simple case, just return now.
1140 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1141 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1142 return ATy;
1143
1144 // Otherwise, we have an array and we have qualifiers on it. Push the
1145 // qualifiers into the array element type and return a new array type.
1146 // Get the canonical version of the element with the extra qualifiers on it.
1147 // This can recursively sink qualifiers through multiple levels of arrays.
1148 QualType NewEltTy = ATy->getElementType();
1149 if (AddrSpace)
1150 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1151 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1152
1153 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1154 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1155 CAT->getSizeModifier(),
1156 CAT->getIndexTypeQualifier()));
1157 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1158 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1159 IAT->getSizeModifier(),
1160 IAT->getIndexTypeQualifier()));
1161
1162 // FIXME: What is the ownership of size expressions in VLAs?
1163 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1164 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1165 VAT->getSizeModifier(),
1166 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001167}
1168
1169
Chris Lattner19eb97e2008-04-02 05:18:44 +00001170/// getArrayDecayedType - Return the properly qualified result of decaying the
1171/// specified array type to a pointer. This operation is non-trivial when
1172/// handling typedefs etc. The canonical type of "T" must be an array type,
1173/// this returns a pointer to a properly qualified element of the array.
1174///
1175/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1176QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001177 // Get the element type with 'getAsArrayType' so that we don't lose any
1178 // typedefs in the element type of the array. This also handles propagation
1179 // of type qualifiers from the array type into the element type if present
1180 // (C99 6.7.3p8).
1181 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1182 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001183
Chris Lattnera1923f62008-08-04 07:31:14 +00001184 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001185
1186 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001187 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001188}
1189
Chris Lattner4b009652007-07-25 00:24:17 +00001190/// getFloatingRank - Return a relative rank for floating point types.
1191/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001192static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001193 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001194 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001195
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001196 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001197 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001198 case BuiltinType::Float: return FloatRank;
1199 case BuiltinType::Double: return DoubleRank;
1200 case BuiltinType::LongDouble: return LongDoubleRank;
1201 }
1202}
1203
Steve Narofffa0c4532007-08-27 01:41:48 +00001204/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1205/// point or a complex type (based on typeDomain/typeSize).
1206/// 'typeDomain' is a real floating point or complex type.
1207/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001208QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1209 QualType Domain) const {
1210 FloatingRank EltRank = getFloatingRank(Size);
1211 if (Domain->isComplexType()) {
1212 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001213 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001214 case FloatRank: return FloatComplexTy;
1215 case DoubleRank: return DoubleComplexTy;
1216 case LongDoubleRank: return LongDoubleComplexTy;
1217 }
Chris Lattner4b009652007-07-25 00:24:17 +00001218 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001219
1220 assert(Domain->isRealFloatingType() && "Unknown domain!");
1221 switch (EltRank) {
1222 default: assert(0 && "getFloatingRank(): illegal value for rank");
1223 case FloatRank: return FloatTy;
1224 case DoubleRank: return DoubleTy;
1225 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001226 }
Chris Lattner4b009652007-07-25 00:24:17 +00001227}
1228
Chris Lattner51285d82008-04-06 23:55:33 +00001229/// getFloatingTypeOrder - Compare the rank of the two specified floating
1230/// point types, ignoring the domain of the type (i.e. 'double' ==
1231/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1232/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001233int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1234 FloatingRank LHSR = getFloatingRank(LHS);
1235 FloatingRank RHSR = getFloatingRank(RHS);
1236
1237 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001238 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001239 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001240 return 1;
1241 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001242}
1243
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001244/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1245/// routine will assert if passed a built-in type that isn't an integer or enum,
1246/// or if it is not canonicalized.
1247static unsigned getIntegerRank(Type *T) {
1248 assert(T->isCanonical() && "T should be canonicalized");
1249 if (isa<EnumType>(T))
1250 return 4;
1251
1252 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001253 default: assert(0 && "getIntegerRank(): not a built-in integer");
1254 case BuiltinType::Bool:
1255 return 1;
1256 case BuiltinType::Char_S:
1257 case BuiltinType::Char_U:
1258 case BuiltinType::SChar:
1259 case BuiltinType::UChar:
1260 return 2;
1261 case BuiltinType::Short:
1262 case BuiltinType::UShort:
1263 return 3;
1264 case BuiltinType::Int:
1265 case BuiltinType::UInt:
1266 return 4;
1267 case BuiltinType::Long:
1268 case BuiltinType::ULong:
1269 return 5;
1270 case BuiltinType::LongLong:
1271 case BuiltinType::ULongLong:
1272 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001273 }
1274}
1275
Chris Lattner51285d82008-04-06 23:55:33 +00001276/// getIntegerTypeOrder - Returns the highest ranked integer type:
1277/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1278/// LHS < RHS, return -1.
1279int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001280 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1281 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001282 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001283
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001284 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1285 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001286
Chris Lattner51285d82008-04-06 23:55:33 +00001287 unsigned LHSRank = getIntegerRank(LHSC);
1288 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001289
Chris Lattner51285d82008-04-06 23:55:33 +00001290 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1291 if (LHSRank == RHSRank) return 0;
1292 return LHSRank > RHSRank ? 1 : -1;
1293 }
Chris Lattner4b009652007-07-25 00:24:17 +00001294
Chris Lattner51285d82008-04-06 23:55:33 +00001295 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1296 if (LHSUnsigned) {
1297 // If the unsigned [LHS] type is larger, return it.
1298 if (LHSRank >= RHSRank)
1299 return 1;
1300
1301 // If the signed type can represent all values of the unsigned type, it
1302 // wins. Because we are dealing with 2's complement and types that are
1303 // powers of two larger than each other, this is always safe.
1304 return -1;
1305 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001306
Chris Lattner51285d82008-04-06 23:55:33 +00001307 // If the unsigned [RHS] type is larger, return it.
1308 if (RHSRank >= LHSRank)
1309 return -1;
1310
1311 // If the signed type can represent all values of the unsigned type, it
1312 // wins. Because we are dealing with 2's complement and types that are
1313 // powers of two larger than each other, this is always safe.
1314 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001315}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001316
1317// getCFConstantStringType - Return the type used for constant CFStrings.
1318QualType ASTContext::getCFConstantStringType() {
1319 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001320 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001321 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001322 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001323 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001324
1325 // const int *isa;
1326 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001327 // int flags;
1328 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001329 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001330 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001331 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001332 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001333 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001334 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001335
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001336 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001337 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001338 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001339
1340 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1341 }
1342
1343 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001344}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001345
Anders Carlssone3f02572007-10-29 06:33:42 +00001346// This returns true if a type has been typedefed to BOOL:
1347// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001348static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001349 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001350 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001351
1352 return false;
1353}
1354
Ted Kremenek42730c52008-01-07 19:49:32 +00001355/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001356/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001357int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001358 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001359
1360 // Make all integer and enum types at least as large as an int
1361 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001362 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001363 // Treat arrays as pointers, since that's how they're passed in.
1364 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001365 sz = getTypeSize(VoidPtrTy);
1366 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001367}
1368
Ted Kremenek42730c52008-01-07 19:49:32 +00001369/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001370/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001371void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001372 std::string& S)
1373{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001374 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001375 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001376 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001377 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001378 // Compute size of all parameters.
1379 // Start with computing size of a pointer in number of bytes.
1380 // FIXME: There might(should) be a better way of doing this computation!
1381 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001382 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001383 // The first two arguments (self and _cmd) are pointers; account for
1384 // their size.
1385 int ParmOffset = 2 * PtrSize;
1386 int NumOfParams = Decl->getNumParams();
1387 for (int i = 0; i < NumOfParams; i++) {
1388 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 int sz = getObjCEncodingTypeSize (PType);
1390 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001391 ParmOffset += sz;
1392 }
1393 S += llvm::utostr(ParmOffset);
1394 S += "@0:";
1395 S += llvm::utostr(PtrSize);
1396
1397 // Argument types.
1398 ParmOffset = 2 * PtrSize;
1399 for (int i = 0; i < NumOfParams; i++) {
1400 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001401 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001402 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001403 getObjCEncodingForTypeQualifier(
1404 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001405 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001406 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001407 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001408 }
1409}
1410
Fariborz Jahanian248db262008-01-22 22:44:46 +00001411void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnera1923f62008-08-04 07:31:14 +00001412 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001413 // FIXME: This currently doesn't encode:
1414 // @ An object (whether statically typed or typed id)
1415 // # A class object (Class)
1416 // : A method selector (SEL)
1417 // {name=type...} A structure
1418 // (name=type...) A union
1419 // bnum A bit field of num bits
1420
1421 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001422 char encoding;
1423 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001424 default: assert(0 && "Unhandled builtin type kind");
1425 case BuiltinType::Void: encoding = 'v'; break;
1426 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001427 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001428 case BuiltinType::UChar: encoding = 'C'; break;
1429 case BuiltinType::UShort: encoding = 'S'; break;
1430 case BuiltinType::UInt: encoding = 'I'; break;
1431 case BuiltinType::ULong: encoding = 'L'; break;
1432 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001433 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001434 case BuiltinType::SChar: encoding = 'c'; break;
1435 case BuiltinType::Short: encoding = 's'; break;
1436 case BuiltinType::Int: encoding = 'i'; break;
1437 case BuiltinType::Long: encoding = 'l'; break;
1438 case BuiltinType::LongLong: encoding = 'q'; break;
1439 case BuiltinType::Float: encoding = 'f'; break;
1440 case BuiltinType::Double: encoding = 'd'; break;
1441 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001442 }
1443
1444 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001445 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001446 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001447 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001448 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001449
1450 }
1451 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001452 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001453 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001454 S += '@';
1455 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001456 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001457 S += '#';
1458 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001459 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001460 S += ':';
1461 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001462 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001463
1464 if (PointeeTy->isCharType()) {
1465 // char pointer types should be encoded as '*' unless it is a
1466 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001467 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001468 S += '*';
1469 return;
1470 }
1471 }
1472
1473 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001474 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnera1923f62008-08-04 07:31:14 +00001475 } else if (const ArrayType *AT =
1476 // Ignore type qualifiers etc.
1477 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001478 S += '[';
1479
1480 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1481 S += llvm::utostr(CAT->getSize().getZExtValue());
1482 else
1483 assert(0 && "Unhandled array type!");
1484
Fariborz Jahanian248db262008-01-22 22:44:46 +00001485 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001486 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001487 } else if (T->getAsFunctionType()) {
1488 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001489 } else if (const RecordType *RTy = T->getAsRecordType()) {
1490 RecordDecl *RDecl= RTy->getDecl();
1491 S += '{';
1492 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001493 bool found = false;
1494 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1495 if (ERType[i] == RTy) {
1496 found = true;
1497 break;
1498 }
1499 if (!found) {
1500 ERType.push_back(RTy);
1501 S += '=';
1502 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1503 FieldDecl *field = RDecl->getMember(i);
1504 getObjCEncodingForType(field->getType(), S, ERType);
1505 }
1506 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1507 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001508 }
1509 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001510 } else if (T->isEnumeralType()) {
1511 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001512 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001513 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001514}
1515
Ted Kremenek42730c52008-01-07 19:49:32 +00001516void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001517 std::string& S) const {
1518 if (QT & Decl::OBJC_TQ_In)
1519 S += 'n';
1520 if (QT & Decl::OBJC_TQ_Inout)
1521 S += 'N';
1522 if (QT & Decl::OBJC_TQ_Out)
1523 S += 'o';
1524 if (QT & Decl::OBJC_TQ_Bycopy)
1525 S += 'O';
1526 if (QT & Decl::OBJC_TQ_Byref)
1527 S += 'R';
1528 if (QT & Decl::OBJC_TQ_Oneway)
1529 S += 'V';
1530}
1531
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001532void ASTContext::setBuiltinVaListType(QualType T)
1533{
1534 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1535
1536 BuiltinVaListType = T;
1537}
1538
Ted Kremenek42730c52008-01-07 19:49:32 +00001539void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001540{
Ted Kremenek42730c52008-01-07 19:49:32 +00001541 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001542
Ted Kremenek42730c52008-01-07 19:49:32 +00001543 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001544
1545 // typedef struct objc_object *id;
1546 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1547 assert(ptr && "'id' incorrectly typed");
1548 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1549 assert(rec && "'id' incorrectly typed");
1550 IdStructType = rec;
1551}
1552
Ted Kremenek42730c52008-01-07 19:49:32 +00001553void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001554{
Ted Kremenek42730c52008-01-07 19:49:32 +00001555 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001556
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001558
1559 // typedef struct objc_selector *SEL;
1560 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1561 assert(ptr && "'SEL' incorrectly typed");
1562 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1563 assert(rec && "'SEL' incorrectly typed");
1564 SelStructType = rec;
1565}
1566
Ted Kremenek42730c52008-01-07 19:49:32 +00001567void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001568{
Ted Kremenek42730c52008-01-07 19:49:32 +00001569 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1570 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001571}
1572
Ted Kremenek42730c52008-01-07 19:49:32 +00001573void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001574{
Ted Kremenek42730c52008-01-07 19:49:32 +00001575 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001576
Ted Kremenek42730c52008-01-07 19:49:32 +00001577 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001578
1579 // typedef struct objc_class *Class;
1580 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1581 assert(ptr && "'Class' incorrectly typed");
1582 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1583 assert(rec && "'Class' incorrectly typed");
1584 ClassStructType = rec;
1585}
1586
Ted Kremenek42730c52008-01-07 19:49:32 +00001587void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1588 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001589 "'NSConstantString' type already set!");
1590
Ted Kremenek42730c52008-01-07 19:49:32 +00001591 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001592}
1593
Ted Kremenek118930e2008-07-24 23:58:27 +00001594
1595//===----------------------------------------------------------------------===//
1596// Type Predicates.
1597//===----------------------------------------------------------------------===//
1598
1599/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1600/// to an object type. This includes "id" and "Class" (two 'special' pointers
1601/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1602/// ID type).
1603bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1604 if (Ty->isObjCQualifiedIdType())
1605 return true;
1606
1607 if (!Ty->isPointerType())
1608 return false;
1609
1610 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1611 // pointer types. This looks for the typedef specifically, not for the
1612 // underlying type.
1613 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1614 return true;
1615
1616 // If this a pointer to an interface (e.g. NSString*), it is ok.
1617 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1618}
1619
Chris Lattner6ff358b2008-04-07 06:51:04 +00001620//===----------------------------------------------------------------------===//
1621// Type Compatibility Testing
1622//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001623
Chris Lattner390564e2008-04-07 06:49:41 +00001624/// C99 6.2.7p1: If both are complete types, then the following additional
1625/// requirements apply.
1626/// FIXME (handle compatibility across source files).
1627static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1628 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001629 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001630 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1631 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001632 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001633
Chris Lattner390564e2008-04-07 06:49:41 +00001634 // Within a translation unit a tag type is only compatible with itself. Self
1635 // equality is already handled by the time we get here.
1636 assert(LHS != RHS && "Self equality not handled!");
1637 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001638}
1639
1640bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1641 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1642 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001643 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1644 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001645 return false;
1646
Chris Lattner25168a52008-07-26 21:30:36 +00001647 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1648 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001649
1650 return typesAreCompatible(ltype, rtype);
1651}
1652
Steve Naroff85f0dc52007-10-15 20:41:53 +00001653bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner25168a52008-07-26 21:30:36 +00001654 const FunctionType *lbase = lhs->getAsFunctionType();
1655 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001656 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1657 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1658
1659 // first check the return types (common between C99 and K&R).
1660 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1661 return false;
1662
1663 if (lproto && rproto) { // two C99 style function prototypes
1664 unsigned lproto_nargs = lproto->getNumArgs();
1665 unsigned rproto_nargs = rproto->getNumArgs();
1666
1667 if (lproto_nargs != rproto_nargs)
1668 return false;
1669
1670 // both prototypes have the same number of arguments.
1671 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1672 (rproto->isVariadic() && !lproto->isVariadic()))
1673 return false;
1674
1675 // The use of ellipsis agree...now check the argument types.
1676 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001677 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1678 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001679 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001680 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001681 return false;
1682 return true;
1683 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001684
Steve Naroff85f0dc52007-10-15 20:41:53 +00001685 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1686 return true;
1687
1688 // we have a mixture of K&R style with C99 prototypes
1689 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001690 if (proto->isVariadic())
1691 return false;
1692
1693 // FIXME: Each parameter type T in the prototype must be compatible with the
1694 // type resulting from applying the usual argument conversions to T.
1695 return true;
1696}
1697
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001698// C99 6.7.5.2p6
1699static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001700 // Constant arrays must be the same size to be compatible.
1701 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1702 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1703 if (RCAT->getSize() != LCAT->getSize())
1704 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001705
Chris Lattnerc8971d72008-04-07 06:58:21 +00001706 // Compatible arrays must have compatible element types
1707 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001708}
1709
Chris Lattner6ff358b2008-04-07 06:51:04 +00001710/// areCompatVectorTypes - Return true if the two specified vector types are
1711/// compatible.
1712static bool areCompatVectorTypes(const VectorType *LHS,
1713 const VectorType *RHS) {
1714 assert(LHS->isCanonical() && RHS->isCanonical());
1715 return LHS->getElementType() == RHS->getElementType() &&
1716 LHS->getNumElements() == RHS->getNumElements();
1717}
1718
1719/// areCompatObjCInterfaces - Return true if the two interface types are
1720/// compatible for assignment from RHS to LHS. This handles validation of any
1721/// protocol qualifiers on the LHS or RHS.
1722///
Chris Lattner1d78a862008-04-07 07:01:58 +00001723static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1724 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001725 // Verify that the base decls are compatible: the RHS must be a subclass of
1726 // the LHS.
1727 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1728 return false;
1729
1730 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1731 // protocol qualified at all, then we are good.
1732 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1733 return true;
1734
1735 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1736 // isn't a superset.
1737 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1738 return true; // FIXME: should return false!
1739
1740 // Finally, we must have two protocol-qualified interfaces.
1741 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1742 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1743 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1744 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1745 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1746 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1747
1748 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1749 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1750 // LHS in a single parallel scan until we run out of elements in LHS.
1751 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1752 ObjCProtocolDecl *LHSProto = *LHSPI;
1753
1754 while (RHSPI != RHSPE) {
1755 ObjCProtocolDecl *RHSProto = *RHSPI++;
1756 // If the RHS has a protocol that the LHS doesn't, ignore it.
1757 if (RHSProto != LHSProto)
1758 continue;
1759
1760 // Otherwise, the RHS does have this element.
1761 ++LHSPI;
1762 if (LHSPI == LHSPE)
1763 return true; // All protocols in LHS exist in RHS.
1764
1765 LHSProto = *LHSPI;
1766 }
1767
1768 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1769 return false;
1770}
1771
1772
Steve Naroff85f0dc52007-10-15 20:41:53 +00001773/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1774/// both shall have the identically qualified version of a compatible type.
1775/// C99 6.2.7p1: Two types have compatible types if their types are the
1776/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001777bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner25168a52008-07-26 21:30:36 +00001778 QualType LHS = getCanonicalType(LHS_NC);
1779 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner4d5670b2008-04-03 05:07:04 +00001780
Bill Wendling6a9d8542007-12-03 07:33:35 +00001781 // C++ [expr]: If an expression initially has the type "reference to T", the
1782 // type is adjusted to "T" prior to any further analysis, the expression
1783 // designates the object or function denoted by the reference, and the
1784 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001785 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1786 LHS = RT->getPointeeType();
1787 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1788 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001789
Chris Lattnerd47d6042008-04-07 05:37:56 +00001790 // If two types are identical, they are compatible.
1791 if (LHS == RHS)
1792 return true;
1793
1794 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001795 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1796 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001797 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001798
1799 // Strip off ASQual's if present.
1800 if (LHSAS) {
1801 LHS = LHS.getUnqualifiedType();
1802 RHS = RHS.getUnqualifiedType();
1803 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001804
Chris Lattner855fed42008-04-07 04:07:56 +00001805 Type::TypeClass LHSClass = LHS->getTypeClass();
1806 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001807
1808 // We want to consider the two function types to be the same for these
1809 // comparisons, just force one to the other.
1810 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1811 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001812
1813 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001814 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1815 LHSClass = Type::ConstantArray;
1816 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1817 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001818
Nate Begemanaf6ed502008-04-18 23:10:10 +00001819 // Canonicalize ExtVector -> Vector.
1820 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1821 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001822
Chris Lattner7cdcb252008-04-07 06:38:24 +00001823 // Consider qualified interfaces and interfaces the same.
1824 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1825 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1826
Chris Lattnerb5709e22008-04-07 05:43:21 +00001827 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001828 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001829 // ID is compatible with all interface types.
1830 if (isa<ObjCInterfaceType>(LHS))
1831 return isObjCIdType(RHS);
1832 if (isa<ObjCInterfaceType>(RHS))
1833 return isObjCIdType(LHS);
Steve Naroff44549772008-06-04 15:07:33 +00001834
1835 // ID is compatible with all qualified id types.
1836 if (isa<ObjCQualifiedIdType>(LHS)) {
1837 if (const PointerType *PT = RHS->getAsPointerType())
1838 return isObjCIdType(PT->getPointeeType());
1839 }
1840 if (isa<ObjCQualifiedIdType>(RHS)) {
1841 if (const PointerType *PT = LHS->getAsPointerType())
1842 return isObjCIdType(PT->getPointeeType());
1843 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001844 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1845 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001846 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1847 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1848 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001849 }
Chris Lattner855fed42008-04-07 04:07:56 +00001850 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1851 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1852 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001853 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001854
Steve Naroff85f0dc52007-10-15 20:41:53 +00001855 return false;
1856 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001857
Steve Naroffc88babe2008-01-09 22:43:08 +00001858 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001859 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001860 case Type::ASQual:
1861 case Type::FunctionProto:
1862 case Type::VariableArray:
1863 case Type::IncompleteArray:
1864 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001865 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001866 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001867 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001868 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001869 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001870 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1871 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001872 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001873 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001874 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001875 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001876 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001877 // Only exactly equal builtin types are compatible, which is tested above.
1878 return false;
1879 case Type::Vector:
1880 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001881 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001882 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1883 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001884 default:
1885 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001886 }
1887 return true; // should never get here...
1888}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001889
Chris Lattner1d78a862008-04-07 07:01:58 +00001890//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00001891// Integer Predicates
1892//===----------------------------------------------------------------------===//
1893unsigned ASTContext::getIntWidth(QualType T) {
1894 if (T == BoolTy)
1895 return 1;
1896 // At the moment, only bool has padding bits
1897 return (unsigned)getTypeSize(T);
1898}
1899
1900QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1901 assert(T->isSignedIntegerType() && "Unexpected type");
1902 if (const EnumType* ETy = T->getAsEnumType())
1903 T = ETy->getDecl()->getIntegerType();
1904 const BuiltinType* BTy = T->getAsBuiltinType();
1905 assert (BTy && "Unexpected signed integer type");
1906 switch (BTy->getKind()) {
1907 case BuiltinType::Char_S:
1908 case BuiltinType::SChar:
1909 return UnsignedCharTy;
1910 case BuiltinType::Short:
1911 return UnsignedShortTy;
1912 case BuiltinType::Int:
1913 return UnsignedIntTy;
1914 case BuiltinType::Long:
1915 return UnsignedLongTy;
1916 case BuiltinType::LongLong:
1917 return UnsignedLongLongTy;
1918 default:
1919 assert(0 && "Unexpected signed integer type");
1920 return QualType();
1921 }
1922}
1923
1924
1925//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00001926// Serialization Support
1927//===----------------------------------------------------------------------===//
1928
Ted Kremenek738e6c02007-10-31 17:10:13 +00001929/// Emit - Serialize an ASTContext object to Bitcode.
1930void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00001931 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001932 S.EmitRef(SourceMgr);
1933 S.EmitRef(Target);
1934 S.EmitRef(Idents);
1935 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001936
Ted Kremenek68228a92007-10-31 22:44:07 +00001937 // Emit the size of the type vector so that we can reserve that size
1938 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001939 S.EmitInt(Types.size());
1940
Ted Kremenek034a78c2007-11-13 22:02:55 +00001941 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1942 I!=E;++I)
1943 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001944
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001945 S.EmitOwnedPtr(TUDecl);
1946
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001947 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001948}
1949
Ted Kremenekacba3612007-11-13 00:25:37 +00001950ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00001951
1952 // Read the language options.
1953 LangOptions LOpts;
1954 LOpts.Read(D);
1955
Ted Kremenek68228a92007-10-31 22:44:07 +00001956 SourceManager &SM = D.ReadRef<SourceManager>();
1957 TargetInfo &t = D.ReadRef<TargetInfo>();
1958 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1959 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001960
Ted Kremenek68228a92007-10-31 22:44:07 +00001961 unsigned size_reserve = D.ReadInt();
1962
Ted Kremenek842126e2008-06-04 15:55:15 +00001963 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00001964
Ted Kremenek034a78c2007-11-13 22:02:55 +00001965 for (unsigned i = 0; i < size_reserve; ++i)
1966 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001967
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001968 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1969
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001970 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001971
1972 return A;
1973}