blob: 002a51f877a3a7f7a5fce77c0542bab22b3bc27b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000032 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000033 Types.pop_back();
34 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000035
36 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000037}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000047 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
48 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000049 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000059 else if (isa<ComplexType>(T))
60 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 else if (isa<ArrayType>(T))
62 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000063 else if (isa<VectorType>(T))
64 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000073 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000074 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000075 case TagDecl::TK_struct: ++NumTagStruct; break;
76 case TagDecl::TK_union: ++NumTagUnion; break;
77 case TagDecl::TK_class: ++NumTagClass; break;
78 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 } else if (isa<ObjCInterfaceType>(T))
81 ++NumObjCInterfaces;
82 else if (isa<ObjCQualifiedInterfaceType>(T))
83 ++NumObjCQualifiedInterfaces;
84 else if (isa<ObjCQualifiedIdType>(T))
85 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +000086 else if (isa<TypeOfType>(T))
87 ++NumTypeOfTypes;
88 else if (isa<TypeOfExpr>(T))
89 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +000090 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000091 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +000092 assert(0 && "Unknown type!");
93 }
94 }
95
96 fprintf(stderr, " %d builtin types\n", NumBuiltin);
97 fprintf(stderr, " %d pointer types\n", NumPointer);
98 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000099 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000101 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
103 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
104 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
105 fprintf(stderr, " %d tagged types\n", NumTagged);
106 fprintf(stderr, " %d struct types\n", NumTagStruct);
107 fprintf(stderr, " %d union types\n", NumTagUnion);
108 fprintf(stderr, " %d class types\n", NumTagClass);
109 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000111 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000113 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000115 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
116 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
119 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000120 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 NumFunctionP*sizeof(FunctionTypeProto)+
122 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000123 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
124 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
129 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000141 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
163
164 // C99 6.2.5p11.
165 FloatComplexTy = getComplexType(FloatTy);
166 DoubleComplexTy = getComplexType(DoubleTy);
167 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000168
169 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000171 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000172 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000173 ClassStructType = 0;
174
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000176
177 // void * type
178 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Chris Lattner464175b2007-07-18 17:52:12 +0000181//===----------------------------------------------------------------------===//
182// Type Sizing and Analysis
183//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000184
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000185/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
186/// scalar floating point type.
187const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
188 const BuiltinType *BT = T->getAsBuiltinType();
189 assert(BT && "Not a floating point type!");
190 switch (BT->getKind()) {
191 default: assert(0 && "Not a floating point type!");
192 case BuiltinType::Float: return Target.getFloatFormat();
193 case BuiltinType::Double: return Target.getDoubleFormat();
194 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
195 }
196}
197
198
Chris Lattnera7674d82007-07-13 22:13:22 +0000199/// getTypeSize - Return the size of the specified type, in bits. This method
200/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000201std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000202ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000203 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000204 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000205 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000206 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000207 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000208 case Type::FunctionNoProto:
209 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000210 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000211 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000212 case Type::VariableArray:
213 assert(0 && "VLAs not implemented yet!");
214 case Type::ConstantArray: {
215 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
216
Chris Lattner98be4942008-03-05 18:54:05 +0000217 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000218 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000219 Align = EltInfo.second;
220 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000221 }
Nate Begeman213541a2008-04-18 23:10:10 +0000222 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000223 case Type::Vector: {
224 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000225 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000226 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000227 // FIXME: This isn't right for unusual vectors
228 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000229 break;
230 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000231
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000232 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000233 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000234 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000235 case BuiltinType::Void:
236 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000237 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000238 Width = Target.getBoolWidth();
239 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000241 case BuiltinType::Char_S:
242 case BuiltinType::Char_U:
243 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000244 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000245 Width = Target.getCharWidth();
246 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000247 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000248 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000249 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000250 Width = Target.getShortWidth();
251 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000252 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000253 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000254 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000255 Width = Target.getIntWidth();
256 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000257 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000258 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000259 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000260 Width = Target.getLongWidth();
261 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000262 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000263 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000264 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000265 Width = Target.getLongLongWidth();
266 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000267 break;
268 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000269 Width = Target.getFloatWidth();
270 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000271 break;
272 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000273 Width = Target.getDoubleWidth();
274 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000275 break;
276 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000277 Width = Target.getLongDoubleWidth();
278 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000279 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000280 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000281 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000282 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000283 // FIXME: Pointers into different addr spaces could have different sizes and
284 // alignment requirements: getPointerInfo should take an AddrSpace.
285 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000287 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000288 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000289 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000290 case Type::Pointer: {
291 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000292 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000293 Align = Target.getPointerAlign(AS);
294 break;
295 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000296 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000297 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000298 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000299 // FIXME: This is wrong for struct layout: a reference in a struct has
300 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000301 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000302
303 case Type::Complex: {
304 // Complex types have the same alignment as their elements, but twice the
305 // size.
306 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000307 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000308 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000309 Align = EltInfo.second;
310 break;
311 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000312 case Type::ObjCInterface: {
313 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
314 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
315 Width = Layout.getSize();
316 Align = Layout.getAlignment();
317 break;
318 }
Chris Lattner71763312008-04-06 22:05:18 +0000319 case Type::Tagged: {
320 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
321 return getTypeInfo(ET->getDecl()->getIntegerType());
322
323 RecordType *RT = cast<RecordType>(T);
324 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
325 Width = Layout.getSize();
326 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000327 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000328 }
Chris Lattner71763312008-04-06 22:05:18 +0000329 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000330
Chris Lattner464175b2007-07-18 17:52:12 +0000331 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000332 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000333}
334
Devang Patel8b277042008-06-04 21:22:16 +0000335/// LayoutField - Field layout.
336void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
337 bool IsUnion, bool StructIsPacked,
338 ASTContext &Context) {
339 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
340 uint64_t FieldOffset = IsUnion ? 0 : Size;
341 uint64_t FieldSize;
342 unsigned FieldAlign;
343
344 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
345 // TODO: Need to check this algorithm on other targets!
346 // (tested on Linux-X86)
347 llvm::APSInt I(32);
348 bool BitWidthIsICE =
349 BitWidthExpr->isIntegerConstantExpr(I, Context);
350 assert (BitWidthIsICE && "Invalid BitField size expression");
351 FieldSize = I.getZExtValue();
352
353 std::pair<uint64_t, unsigned> FieldInfo =
354 Context.getTypeInfo(FD->getType());
355 uint64_t TypeSize = FieldInfo.first;
356
357 FieldAlign = FieldInfo.second;
358 if (FieldIsPacked)
359 FieldAlign = 1;
360 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
361 FieldAlign = std::max(FieldAlign, AA->getAlignment());
362
363 // Check if we need to add padding to give the field the correct
364 // alignment.
365 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
366 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
367
368 // Padding members don't affect overall alignment
369 if (!FD->getIdentifier())
370 FieldAlign = 1;
371 } else {
372 if (FD->getType()->isIncompleteType()) {
373 // This must be a flexible array member; we can't directly
374 // query getTypeInfo about these, so we figure it out here.
375 // Flexible array members don't have any size, but they
376 // have to be aligned appropriately for their element type.
377 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000378 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000379 FieldAlign = Context.getTypeAlign(ATy->getElementType());
380 } else {
381 std::pair<uint64_t, unsigned> FieldInfo =
382 Context.getTypeInfo(FD->getType());
383 FieldSize = FieldInfo.first;
384 FieldAlign = FieldInfo.second;
385 }
386
387 if (FieldIsPacked)
388 FieldAlign = 8;
389 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
390 FieldAlign = std::max(FieldAlign, AA->getAlignment());
391
392 // Round up the current record size to the field's alignment boundary.
393 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
394 }
395
396 // Place this field at the current location.
397 FieldOffsets[FieldNo] = FieldOffset;
398
399 // Reserve space for this field.
400 if (IsUnion) {
401 Size = std::max(Size, FieldSize);
402 } else {
403 Size = FieldOffset + FieldSize;
404 }
405
406 // Remember max struct/class alignment.
407 Alignment = std::max(Alignment, FieldAlign);
408}
409
Devang Patel44a3dde2008-06-04 21:54:36 +0000410
411/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
412/// specified Objective C, which indicates its size and ivar
413/// position information.
414const ASTRecordLayout &
415ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
416 // Look up this layout, if already laid out, return what we have.
417 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
418 if (Entry) return *Entry;
419
420 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
421 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000422 ASTRecordLayout *NewEntry = NULL;
423 unsigned FieldCount = D->ivar_size();
424 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
425 FieldCount++;
426 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
427 unsigned Alignment = SL.getAlignment();
428 uint64_t Size = SL.getSize();
429 NewEntry = new ASTRecordLayout(Size, Alignment);
430 NewEntry->InitializeLayout(FieldCount);
431 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
432 } else {
433 NewEntry = new ASTRecordLayout();
434 NewEntry->InitializeLayout(FieldCount);
435 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000436 Entry = NewEntry;
437
Devang Patel44a3dde2008-06-04 21:54:36 +0000438 bool IsPacked = D->getAttr<PackedAttr>();
439
440 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
441 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
442 AA->getAlignment()));
443
444 // Layout each ivar sequentially.
445 unsigned i = 0;
446 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
447 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
448 const ObjCIvarDecl* Ivar = (*IVI);
449 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
450 }
451
452 // Finally, round the size of the total struct up to the alignment of the
453 // struct itself.
454 NewEntry->FinalizeLayout();
455 return *NewEntry;
456}
457
Devang Patel88a981b2007-11-01 19:11:01 +0000458/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000459/// specified record (struct/union/class), which indicates its size and field
460/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000461const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000462 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000463
Chris Lattner464175b2007-07-18 17:52:12 +0000464 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000465 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000466 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000467
Devang Patel88a981b2007-11-01 19:11:01 +0000468 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
469 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
470 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000471 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000472
Devang Patel8b277042008-06-04 21:22:16 +0000473 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000474 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000475 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000476
Eli Friedman4bd998b2008-05-30 09:31:38 +0000477 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000478 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
479 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000480
Eli Friedman4bd998b2008-05-30 09:31:38 +0000481 // Layout each field, for now, just sequentially, respecting alignment. In
482 // the future, this will need to be tweakable by targets.
483 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
484 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000485 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000486 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000487
488 // Finally, round the size of the total struct up to the alignment of the
489 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000490 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000492}
493
Chris Lattnera7674d82007-07-13 22:13:22 +0000494//===----------------------------------------------------------------------===//
495// Type creation/memoization methods
496//===----------------------------------------------------------------------===//
497
Christopher Lambebb97e92008-02-04 02:31:56 +0000498QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000499 QualType CanT = getCanonicalType(T);
500 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000501 return T;
502
503 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
504 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000505 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000506 "Type is already address space qualified");
507
508 // Check if we've already instantiated an address space qual'd type of this
509 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000510 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000511 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000512 void *InsertPos = 0;
513 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
514 return QualType(ASQy, 0);
515
516 // If the base type isn't canonical, this won't be a canonical type either,
517 // so fill in the canonical type field.
518 QualType Canonical;
519 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000520 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000521
522 // Get the new insert position for the node we care about.
523 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
524 assert(NewIP == 0 && "Shouldn't be in the map!");
525 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000526 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000527 ASQualTypes.InsertNode(New, InsertPos);
528 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000529 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000530}
531
Chris Lattnera7674d82007-07-13 22:13:22 +0000532
Reid Spencer5f016e22007-07-11 17:01:13 +0000533/// getComplexType - Return the uniqued reference to the type for a complex
534/// number with the specified element type.
535QualType ASTContext::getComplexType(QualType T) {
536 // Unique pointers, to guarantee there is only one pointer of a particular
537 // structure.
538 llvm::FoldingSetNodeID ID;
539 ComplexType::Profile(ID, T);
540
541 void *InsertPos = 0;
542 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
543 return QualType(CT, 0);
544
545 // If the pointee type isn't canonical, this won't be a canonical type either,
546 // so fill in the canonical type field.
547 QualType Canonical;
548 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000549 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
551 // Get the new insert position for the node we care about.
552 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
553 assert(NewIP == 0 && "Shouldn't be in the map!");
554 }
555 ComplexType *New = new ComplexType(T, Canonical);
556 Types.push_back(New);
557 ComplexTypes.InsertNode(New, InsertPos);
558 return QualType(New, 0);
559}
560
561
562/// getPointerType - Return the uniqued reference to the type for a pointer to
563/// the specified type.
564QualType ASTContext::getPointerType(QualType T) {
565 // Unique pointers, to guarantee there is only one pointer of a particular
566 // structure.
567 llvm::FoldingSetNodeID ID;
568 PointerType::Profile(ID, T);
569
570 void *InsertPos = 0;
571 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
572 return QualType(PT, 0);
573
574 // If the pointee type isn't canonical, this won't be a canonical type either,
575 // so fill in the canonical type field.
576 QualType Canonical;
577 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000578 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
580 // Get the new insert position for the node we care about.
581 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
582 assert(NewIP == 0 && "Shouldn't be in the map!");
583 }
584 PointerType *New = new PointerType(T, Canonical);
585 Types.push_back(New);
586 PointerTypes.InsertNode(New, InsertPos);
587 return QualType(New, 0);
588}
589
590/// getReferenceType - Return the uniqued reference to the type for a reference
591/// to the specified type.
592QualType ASTContext::getReferenceType(QualType T) {
593 // Unique pointers, to guarantee there is only one pointer of a particular
594 // structure.
595 llvm::FoldingSetNodeID ID;
596 ReferenceType::Profile(ID, T);
597
598 void *InsertPos = 0;
599 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
600 return QualType(RT, 0);
601
602 // If the referencee type isn't canonical, this won't be a canonical type
603 // either, so fill in the canonical type field.
604 QualType Canonical;
605 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000606 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608 // Get the new insert position for the node we care about.
609 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
610 assert(NewIP == 0 && "Shouldn't be in the map!");
611 }
612
613 ReferenceType *New = new ReferenceType(T, Canonical);
614 Types.push_back(New);
615 ReferenceTypes.InsertNode(New, InsertPos);
616 return QualType(New, 0);
617}
618
Steve Narofffb22d962007-08-30 01:06:46 +0000619/// getConstantArrayType - Return the unique reference to the type for an
620/// array of the specified element type.
621QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000622 const llvm::APInt &ArySize,
623 ArrayType::ArraySizeModifier ASM,
624 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000626 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000627
628 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000629 if (ConstantArrayType *ATP =
630 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 return QualType(ATP, 0);
632
633 // If the element type isn't canonical, this won't be a canonical type either,
634 // so fill in the canonical type field.
635 QualType Canonical;
636 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000637 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000638 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000640 ConstantArrayType *NewIP =
641 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 assert(NewIP == 0 && "Shouldn't be in the map!");
644 }
645
Steve Naroffc9406122007-08-30 18:10:14 +0000646 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
647 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000648 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 Types.push_back(New);
650 return QualType(New, 0);
651}
652
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000653/// getVariableArrayType - Returns a non-unique reference to the type for a
654/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000655QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
656 ArrayType::ArraySizeModifier ASM,
657 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000658 // Since we don't unique expressions, it isn't possible to unique VLA's
659 // that have an expression provided for their size.
660
661 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
662 ASM, EltTypeQuals);
663
664 VariableArrayTypes.push_back(New);
665 Types.push_back(New);
666 return QualType(New, 0);
667}
668
669QualType ASTContext::getIncompleteArrayType(QualType EltTy,
670 ArrayType::ArraySizeModifier ASM,
671 unsigned EltTypeQuals) {
672 llvm::FoldingSetNodeID ID;
673 IncompleteArrayType::Profile(ID, EltTy);
674
675 void *InsertPos = 0;
676 if (IncompleteArrayType *ATP =
677 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
678 return QualType(ATP, 0);
679
680 // If the element type isn't canonical, this won't be a canonical type
681 // either, so fill in the canonical type field.
682 QualType Canonical;
683
684 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000685 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000686 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000687
688 // Get the new insert position for the node we care about.
689 IncompleteArrayType *NewIP =
690 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
691
692 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000693 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000694
695 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
696 ASM, EltTypeQuals);
697
698 IncompleteArrayTypes.InsertNode(New, InsertPos);
699 Types.push_back(New);
700 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000701}
702
Steve Naroff73322922007-07-18 18:00:27 +0000703/// getVectorType - Return the unique reference to a vector type of
704/// the specified element type and size. VectorType must be a built-in type.
705QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 BuiltinType *baseType;
707
Chris Lattnerf52ab252008-04-06 22:59:24 +0000708 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000709 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000710
711 // Check if we've already instantiated a vector of this type.
712 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000713 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 void *InsertPos = 0;
715 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
716 return QualType(VTP, 0);
717
718 // If the element type isn't canonical, this won't be a canonical type either,
719 // so fill in the canonical type field.
720 QualType Canonical;
721 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000722 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723
724 // Get the new insert position for the node we care about.
725 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
726 assert(NewIP == 0 && "Shouldn't be in the map!");
727 }
728 VectorType *New = new VectorType(vecType, NumElts, Canonical);
729 VectorTypes.InsertNode(New, InsertPos);
730 Types.push_back(New);
731 return QualType(New, 0);
732}
733
Nate Begeman213541a2008-04-18 23:10:10 +0000734/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000735/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000736QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000737 BuiltinType *baseType;
738
Chris Lattnerf52ab252008-04-06 22:59:24 +0000739 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000740 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000741
742 // Check if we've already instantiated a vector of this type.
743 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000744 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000745 void *InsertPos = 0;
746 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
747 return QualType(VTP, 0);
748
749 // If the element type isn't canonical, this won't be a canonical type either,
750 // so fill in the canonical type field.
751 QualType Canonical;
752 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000753 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000754
755 // Get the new insert position for the node we care about.
756 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
757 assert(NewIP == 0 && "Shouldn't be in the map!");
758 }
Nate Begeman213541a2008-04-18 23:10:10 +0000759 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000760 VectorTypes.InsertNode(New, InsertPos);
761 Types.push_back(New);
762 return QualType(New, 0);
763}
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
766///
767QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
768 // Unique functions, to guarantee there is only one function of a particular
769 // structure.
770 llvm::FoldingSetNodeID ID;
771 FunctionTypeNoProto::Profile(ID, ResultTy);
772
773 void *InsertPos = 0;
774 if (FunctionTypeNoProto *FT =
775 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
776 return QualType(FT, 0);
777
778 QualType Canonical;
779 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000780 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000781
782 // Get the new insert position for the node we care about.
783 FunctionTypeNoProto *NewIP =
784 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
785 assert(NewIP == 0 && "Shouldn't be in the map!");
786 }
787
788 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
789 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000790 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 return QualType(New, 0);
792}
793
794/// getFunctionType - Return a normal function type with a typed argument
795/// list. isVariadic indicates whether the argument list includes '...'.
796QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
797 unsigned NumArgs, bool isVariadic) {
798 // Unique functions, to guarantee there is only one function of a particular
799 // structure.
800 llvm::FoldingSetNodeID ID;
801 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
802
803 void *InsertPos = 0;
804 if (FunctionTypeProto *FTP =
805 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
806 return QualType(FTP, 0);
807
808 // Determine whether the type being created is already canonical or not.
809 bool isCanonical = ResultTy->isCanonical();
810 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
811 if (!ArgArray[i]->isCanonical())
812 isCanonical = false;
813
814 // If this type isn't canonical, get the canonical version of it.
815 QualType Canonical;
816 if (!isCanonical) {
817 llvm::SmallVector<QualType, 16> CanonicalArgs;
818 CanonicalArgs.reserve(NumArgs);
819 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000820 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000821
Chris Lattnerf52ab252008-04-06 22:59:24 +0000822 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 &CanonicalArgs[0], NumArgs,
824 isVariadic);
825
826 // Get the new insert position for the node we care about.
827 FunctionTypeProto *NewIP =
828 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
829 assert(NewIP == 0 && "Shouldn't be in the map!");
830 }
831
832 // FunctionTypeProto objects are not allocated with new because they have a
833 // variable size array (for parameter types) at the end of them.
834 FunctionTypeProto *FTP =
835 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000836 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
838 Canonical);
839 Types.push_back(FTP);
840 FunctionTypeProtos.InsertNode(FTP, InsertPos);
841 return QualType(FTP, 0);
842}
843
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000844/// getTypeDeclType - Return the unique reference to the type for the
845/// specified type declaration.
846QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
847 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
848
849 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
850 return getTypedefType(Typedef);
851 else if (ObjCInterfaceDecl *ObjCInterface
852 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
853 return getObjCInterfaceType(ObjCInterface);
854 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
855 Decl->TypeForDecl = new RecordType(Record);
856 Types.push_back(Decl->TypeForDecl);
857 return QualType(Decl->TypeForDecl, 0);
858 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
859 Decl->TypeForDecl = new EnumType(Enum);
860 Types.push_back(Decl->TypeForDecl);
861 return QualType(Decl->TypeForDecl, 0);
862 } else
863 assert(false && "TypeDecl without a type?");
864}
865
Reid Spencer5f016e22007-07-11 17:01:13 +0000866/// getTypedefType - Return the unique reference to the type for the
867/// specified typename decl.
868QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
869 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
870
Chris Lattnerf52ab252008-04-06 22:59:24 +0000871 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000872 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 Types.push_back(Decl->TypeForDecl);
874 return QualType(Decl->TypeForDecl, 0);
875}
876
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000878/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000880 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
881
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000883 Types.push_back(Decl->TypeForDecl);
884 return QualType(Decl->TypeForDecl, 0);
885}
886
Chris Lattner88cb27a2008-04-07 04:56:42 +0000887/// CmpProtocolNames - Comparison predicate for sorting protocols
888/// alphabetically.
889static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
890 const ObjCProtocolDecl *RHS) {
891 return strcmp(LHS->getName(), RHS->getName()) < 0;
892}
893
894static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
895 unsigned &NumProtocols) {
896 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
897
898 // Sort protocols, keyed by name.
899 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
900
901 // Remove duplicates.
902 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
903 NumProtocols = ProtocolsEnd-Protocols;
904}
905
906
Chris Lattner065f0d72008-04-07 04:44:08 +0000907/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
908/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
910 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000911 // Sort the protocol list alphabetically to canonicalize it.
912 SortAndUniqueProtocols(Protocols, NumProtocols);
913
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000914 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000915 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000916
917 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 if (ObjCQualifiedInterfaceType *QT =
919 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000920 return QualType(QT, 0);
921
922 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923 ObjCQualifiedInterfaceType *QType =
924 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000925 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000927 return QualType(QType, 0);
928}
929
Chris Lattner88cb27a2008-04-07 04:56:42 +0000930/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
931/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000932QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000933 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000934 // Sort the protocol list alphabetically to canonicalize it.
935 SortAndUniqueProtocols(Protocols, NumProtocols);
936
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000937 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000939
940 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000942 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000943 return QualType(QT, 0);
944
945 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000946 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000947 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000949 return QualType(QType, 0);
950}
951
Steve Naroff9752f252007-08-01 18:02:17 +0000952/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
953/// TypeOfExpr AST's (since expression's are never shared). For example,
954/// multiple declarations that refer to "typeof(x)" all contain different
955/// DeclRefExpr's. This doesn't effect the type checker, since it operates
956/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000957QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000958 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +0000959 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
960 Types.push_back(toe);
961 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000962}
963
Steve Naroff9752f252007-08-01 18:02:17 +0000964/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
965/// TypeOfType AST's. The only motivation to unique these nodes would be
966/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
967/// an issue. This doesn't effect the type checker, since it operates
968/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000969QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000970 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +0000971 TypeOfType *tot = new TypeOfType(tofType, Canonical);
972 Types.push_back(tot);
973 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000974}
975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976/// getTagDeclType - Return the unique reference to the type for the
977/// specified TagDecl (struct/union/class/enum) decl.
978QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000979 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000980 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000981}
982
983/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
984/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
985/// needs to agree with the definition in <stddef.h>.
986QualType ASTContext::getSizeType() const {
987 // On Darwin, size_t is defined as a "long unsigned int".
988 // FIXME: should derive from "Target".
989 return UnsignedLongTy;
990}
991
Eli Friedmanfd888a52008-02-12 08:29:21 +0000992/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
993/// width of characters in wide strings, The value is target dependent and
994/// needs to agree with the definition in <stddef.h>.
995QualType ASTContext::getWcharType() const {
996 // On Darwin, wchar_t is defined as a "int".
997 // FIXME: should derive from "Target".
998 return IntTy;
999}
1000
Chris Lattner8b9023b2007-07-13 03:05:23 +00001001/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1002/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1003QualType ASTContext::getPointerDiffType() const {
1004 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1005 // FIXME: should derive from "Target".
1006 return IntTy;
1007}
1008
Chris Lattnere6327742008-04-02 05:18:44 +00001009//===----------------------------------------------------------------------===//
1010// Type Operators
1011//===----------------------------------------------------------------------===//
1012
Chris Lattner77c96472008-04-06 22:41:35 +00001013/// getCanonicalType - Return the canonical (structural) type corresponding to
1014/// the specified potentially non-canonical type. The non-canonical version
1015/// of a type may have many "decorated" versions of types. Decorators can
1016/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1017/// to be free of any of these, allowing two canonical types to be compared
1018/// for exact equality with a simple pointer comparison.
1019QualType ASTContext::getCanonicalType(QualType T) {
1020 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001021
1022 // If the result has type qualifiers, make sure to canonicalize them as well.
1023 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1024 if (TypeQuals == 0) return CanType;
1025
1026 // If the type qualifiers are on an array type, get the canonical type of the
1027 // array with the qualifiers applied to the element type.
1028 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1029 if (!AT)
1030 return CanType.getQualifiedType(TypeQuals);
1031
1032 // Get the canonical version of the element with the extra qualifiers on it.
1033 // This can recursively sink qualifiers through multiple levels of arrays.
1034 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1035 NewEltTy = getCanonicalType(NewEltTy);
1036
1037 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1038 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1039 CAT->getIndexTypeQualifier());
1040 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1041 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1042 IAT->getIndexTypeQualifier());
1043
1044 // FIXME: What is the ownership of size expressions in VLAs?
1045 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1046 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1047 VAT->getSizeModifier(),
1048 VAT->getIndexTypeQualifier());
1049}
1050
1051
1052const ArrayType *ASTContext::getAsArrayType(QualType T) {
1053 // Handle the non-qualified case efficiently.
1054 if (T.getCVRQualifiers() == 0) {
1055 // Handle the common positive case fast.
1056 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1057 return AT;
1058 }
1059
1060 // Handle the common negative case fast, ignoring CVR qualifiers.
1061 QualType CType = T->getCanonicalTypeInternal();
1062
1063 // Make sure to look through type qualifiers (like ASQuals) for the negative
1064 // test.
1065 if (!isa<ArrayType>(CType) &&
1066 !isa<ArrayType>(CType.getUnqualifiedType()))
1067 return 0;
1068
1069 // Apply any CVR qualifiers from the array type to the element type. This
1070 // implements C99 6.7.3p8: "If the specification of an array type includes
1071 // any type qualifiers, the element type is so qualified, not the array type."
1072
1073 // If we get here, we either have type qualifiers on the type, or we have
1074 // sugar such as a typedef in the way. If we have type qualifiers on the type
1075 // we must propagate them down into the elemeng type.
1076 unsigned CVRQuals = T.getCVRQualifiers();
1077 unsigned AddrSpace = 0;
1078 Type *Ty = T.getTypePtr();
1079
1080 // Rip through ASQualType's and typedefs to get to a concrete type.
1081 while (1) {
1082 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1083 AddrSpace = ASQT->getAddressSpace();
1084 Ty = ASQT->getBaseType();
1085 } else {
1086 T = Ty->getDesugaredType();
1087 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1088 break;
1089 CVRQuals |= T.getCVRQualifiers();
1090 Ty = T.getTypePtr();
1091 }
1092 }
1093
1094 // If we have a simple case, just return now.
1095 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1096 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1097 return ATy;
1098
1099 // Otherwise, we have an array and we have qualifiers on it. Push the
1100 // qualifiers into the array element type and return a new array type.
1101 // Get the canonical version of the element with the extra qualifiers on it.
1102 // This can recursively sink qualifiers through multiple levels of arrays.
1103 QualType NewEltTy = ATy->getElementType();
1104 if (AddrSpace)
1105 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1106 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1107
1108 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1109 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1110 CAT->getSizeModifier(),
1111 CAT->getIndexTypeQualifier()));
1112 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1113 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1114 IAT->getSizeModifier(),
1115 IAT->getIndexTypeQualifier()));
1116
1117 // FIXME: What is the ownership of size expressions in VLAs?
1118 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1119 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1120 VAT->getSizeModifier(),
1121 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001122}
1123
1124
Chris Lattnere6327742008-04-02 05:18:44 +00001125/// getArrayDecayedType - Return the properly qualified result of decaying the
1126/// specified array type to a pointer. This operation is non-trivial when
1127/// handling typedefs etc. The canonical type of "T" must be an array type,
1128/// this returns a pointer to a properly qualified element of the array.
1129///
1130/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1131QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001132 // Get the element type with 'getAsArrayType' so that we don't lose any
1133 // typedefs in the element type of the array. This also handles propagation
1134 // of type qualifiers from the array type into the element type if present
1135 // (C99 6.7.3p8).
1136 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1137 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001138
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001139 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001140
1141 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001142 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001143}
1144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145/// getFloatingRank - Return a relative rank for floating point types.
1146/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001147static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001148 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001150
Christopher Lambebb97e92008-02-04 02:31:56 +00001151 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001152 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 case BuiltinType::Float: return FloatRank;
1154 case BuiltinType::Double: return DoubleRank;
1155 case BuiltinType::LongDouble: return LongDoubleRank;
1156 }
1157}
1158
Steve Naroff716c7302007-08-27 01:41:48 +00001159/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1160/// point or a complex type (based on typeDomain/typeSize).
1161/// 'typeDomain' is a real floating point or complex type.
1162/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001163QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1164 QualType Domain) const {
1165 FloatingRank EltRank = getFloatingRank(Size);
1166 if (Domain->isComplexType()) {
1167 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001168 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001169 case FloatRank: return FloatComplexTy;
1170 case DoubleRank: return DoubleComplexTy;
1171 case LongDoubleRank: return LongDoubleComplexTy;
1172 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 }
Chris Lattner1361b112008-04-06 23:58:54 +00001174
1175 assert(Domain->isRealFloatingType() && "Unknown domain!");
1176 switch (EltRank) {
1177 default: assert(0 && "getFloatingRank(): illegal value for rank");
1178 case FloatRank: return FloatTy;
1179 case DoubleRank: return DoubleTy;
1180 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001181 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001182}
1183
Chris Lattner7cfeb082008-04-06 23:55:33 +00001184/// getFloatingTypeOrder - Compare the rank of the two specified floating
1185/// point types, ignoring the domain of the type (i.e. 'double' ==
1186/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1187/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001188int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1189 FloatingRank LHSR = getFloatingRank(LHS);
1190 FloatingRank RHSR = getFloatingRank(RHS);
1191
1192 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001193 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001194 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001195 return 1;
1196 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197}
1198
Chris Lattnerf52ab252008-04-06 22:59:24 +00001199/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1200/// routine will assert if passed a built-in type that isn't an integer or enum,
1201/// or if it is not canonicalized.
1202static unsigned getIntegerRank(Type *T) {
1203 assert(T->isCanonical() && "T should be canonicalized");
1204 if (isa<EnumType>(T))
1205 return 4;
1206
1207 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001208 default: assert(0 && "getIntegerRank(): not a built-in integer");
1209 case BuiltinType::Bool:
1210 return 1;
1211 case BuiltinType::Char_S:
1212 case BuiltinType::Char_U:
1213 case BuiltinType::SChar:
1214 case BuiltinType::UChar:
1215 return 2;
1216 case BuiltinType::Short:
1217 case BuiltinType::UShort:
1218 return 3;
1219 case BuiltinType::Int:
1220 case BuiltinType::UInt:
1221 return 4;
1222 case BuiltinType::Long:
1223 case BuiltinType::ULong:
1224 return 5;
1225 case BuiltinType::LongLong:
1226 case BuiltinType::ULongLong:
1227 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001228 }
1229}
1230
Chris Lattner7cfeb082008-04-06 23:55:33 +00001231/// getIntegerTypeOrder - Returns the highest ranked integer type:
1232/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1233/// LHS < RHS, return -1.
1234int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001235 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1236 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001237 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001238
Chris Lattnerf52ab252008-04-06 22:59:24 +00001239 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1240 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001241
Chris Lattner7cfeb082008-04-06 23:55:33 +00001242 unsigned LHSRank = getIntegerRank(LHSC);
1243 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001244
Chris Lattner7cfeb082008-04-06 23:55:33 +00001245 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1246 if (LHSRank == RHSRank) return 0;
1247 return LHSRank > RHSRank ? 1 : -1;
1248 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001249
Chris Lattner7cfeb082008-04-06 23:55:33 +00001250 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1251 if (LHSUnsigned) {
1252 // If the unsigned [LHS] type is larger, return it.
1253 if (LHSRank >= RHSRank)
1254 return 1;
1255
1256 // If the signed type can represent all values of the unsigned type, it
1257 // wins. Because we are dealing with 2's complement and types that are
1258 // powers of two larger than each other, this is always safe.
1259 return -1;
1260 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001261
Chris Lattner7cfeb082008-04-06 23:55:33 +00001262 // If the unsigned [RHS] type is larger, return it.
1263 if (RHSRank >= LHSRank)
1264 return -1;
1265
1266 // If the signed type can represent all values of the unsigned type, it
1267 // wins. Because we are dealing with 2's complement and types that are
1268 // powers of two larger than each other, this is always safe.
1269 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001270}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001271
1272// getCFConstantStringType - Return the type used for constant CFStrings.
1273QualType ASTContext::getCFConstantStringType() {
1274 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001275 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001276 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001277 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001278 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001279
1280 // const int *isa;
1281 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001282 // int flags;
1283 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001284 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001285 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001286 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001287 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001288 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001289 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001290
Anders Carlssonf06273f2007-11-19 00:25:30 +00001291 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001292 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001293 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001294
1295 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1296 }
1297
1298 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001299}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001300
Anders Carlssone8c49532007-10-29 06:33:42 +00001301// This returns true if a type has been typedefed to BOOL:
1302// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001303static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001304 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001305 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001306
1307 return false;
1308}
1309
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001310/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001311/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001313 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001314
1315 // Make all integer and enum types at least as large as an int
1316 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001317 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001318 // Treat arrays as pointers, since that's how they're passed in.
1319 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001320 sz = getTypeSize(VoidPtrTy);
1321 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001322}
1323
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001324/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001325/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001326void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001327 std::string& S)
1328{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001329 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001330 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001331 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001332 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001333 // Compute size of all parameters.
1334 // Start with computing size of a pointer in number of bytes.
1335 // FIXME: There might(should) be a better way of doing this computation!
1336 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001337 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001338 // The first two arguments (self and _cmd) are pointers; account for
1339 // their size.
1340 int ParmOffset = 2 * PtrSize;
1341 int NumOfParams = Decl->getNumParams();
1342 for (int i = 0; i < NumOfParams; i++) {
1343 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344 int sz = getObjCEncodingTypeSize (PType);
1345 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001346 ParmOffset += sz;
1347 }
1348 S += llvm::utostr(ParmOffset);
1349 S += "@0:";
1350 S += llvm::utostr(PtrSize);
1351
1352 // Argument types.
1353 ParmOffset = 2 * PtrSize;
1354 for (int i = 0; i < NumOfParams; i++) {
1355 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001356 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001357 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358 getObjCEncodingForTypeQualifier(
1359 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001360 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001361 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001362 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001363 }
1364}
1365
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001366void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001367 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001368 // FIXME: This currently doesn't encode:
1369 // @ An object (whether statically typed or typed id)
1370 // # A class object (Class)
1371 // : A method selector (SEL)
1372 // {name=type...} A structure
1373 // (name=type...) A union
1374 // bnum A bit field of num bits
1375
1376 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001377 char encoding;
1378 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001379 default: assert(0 && "Unhandled builtin type kind");
1380 case BuiltinType::Void: encoding = 'v'; break;
1381 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001382 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001383 case BuiltinType::UChar: encoding = 'C'; break;
1384 case BuiltinType::UShort: encoding = 'S'; break;
1385 case BuiltinType::UInt: encoding = 'I'; break;
1386 case BuiltinType::ULong: encoding = 'L'; break;
1387 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001388 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001389 case BuiltinType::SChar: encoding = 'c'; break;
1390 case BuiltinType::Short: encoding = 's'; break;
1391 case BuiltinType::Int: encoding = 'i'; break;
1392 case BuiltinType::Long: encoding = 'l'; break;
1393 case BuiltinType::LongLong: encoding = 'q'; break;
1394 case BuiltinType::Float: encoding = 'f'; break;
1395 case BuiltinType::Double: encoding = 'd'; break;
1396 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001397 }
1398
1399 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001400 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001402 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001403 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001404
1405 }
1406 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001407 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001409 S += '@';
1410 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001412 S += '#';
1413 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001415 S += ':';
1416 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001417 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001418
1419 if (PointeeTy->isCharType()) {
1420 // char pointer types should be encoded as '*' unless it is a
1421 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001422 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001423 S += '*';
1424 return;
1425 }
1426 }
1427
1428 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001429 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001430 } else if (const ArrayType *AT =
1431 // Ignore type qualifiers etc.
1432 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001433 S += '[';
1434
1435 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1436 S += llvm::utostr(CAT->getSize().getZExtValue());
1437 else
1438 assert(0 && "Unhandled array type!");
1439
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001440 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001441 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001442 } else if (T->getAsFunctionType()) {
1443 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001444 } else if (const RecordType *RTy = T->getAsRecordType()) {
1445 RecordDecl *RDecl= RTy->getDecl();
1446 S += '{';
1447 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001448 bool found = false;
1449 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1450 if (ERType[i] == RTy) {
1451 found = true;
1452 break;
1453 }
1454 if (!found) {
1455 ERType.push_back(RTy);
1456 S += '=';
1457 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1458 FieldDecl *field = RDecl->getMember(i);
1459 getObjCEncodingForType(field->getType(), S, ERType);
1460 }
1461 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1462 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001463 }
1464 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001465 } else if (T->isEnumeralType()) {
1466 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001467 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001468 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001469}
1470
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001472 std::string& S) const {
1473 if (QT & Decl::OBJC_TQ_In)
1474 S += 'n';
1475 if (QT & Decl::OBJC_TQ_Inout)
1476 S += 'N';
1477 if (QT & Decl::OBJC_TQ_Out)
1478 S += 'o';
1479 if (QT & Decl::OBJC_TQ_Bycopy)
1480 S += 'O';
1481 if (QT & Decl::OBJC_TQ_Byref)
1482 S += 'R';
1483 if (QT & Decl::OBJC_TQ_Oneway)
1484 S += 'V';
1485}
1486
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001487void ASTContext::setBuiltinVaListType(QualType T)
1488{
1489 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1490
1491 BuiltinVaListType = T;
1492}
1493
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001495{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001496 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001497
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001498 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001499
1500 // typedef struct objc_object *id;
1501 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1502 assert(ptr && "'id' incorrectly typed");
1503 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1504 assert(rec && "'id' incorrectly typed");
1505 IdStructType = rec;
1506}
1507
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001509{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001511
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001513
1514 // typedef struct objc_selector *SEL;
1515 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1516 assert(ptr && "'SEL' incorrectly typed");
1517 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1518 assert(rec && "'SEL' incorrectly typed");
1519 SelStructType = rec;
1520}
1521
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001522void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001523{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001524 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1525 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001526}
1527
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001528void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001529{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001531
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001532 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001533
1534 // typedef struct objc_class *Class;
1535 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1536 assert(ptr && "'Class' incorrectly typed");
1537 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1538 assert(rec && "'Class' incorrectly typed");
1539 ClassStructType = rec;
1540}
1541
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001542void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1543 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001544 "'NSConstantString' type already set!");
1545
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001547}
1548
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001549
1550//===----------------------------------------------------------------------===//
1551// Type Predicates.
1552//===----------------------------------------------------------------------===//
1553
1554/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1555/// to an object type. This includes "id" and "Class" (two 'special' pointers
1556/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1557/// ID type).
1558bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1559 if (Ty->isObjCQualifiedIdType())
1560 return true;
1561
1562 if (!Ty->isPointerType())
1563 return false;
1564
1565 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1566 // pointer types. This looks for the typedef specifically, not for the
1567 // underlying type.
1568 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1569 return true;
1570
1571 // If this a pointer to an interface (e.g. NSString*), it is ok.
1572 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1573}
1574
Chris Lattner6ac46a42008-04-07 06:51:04 +00001575//===----------------------------------------------------------------------===//
1576// Type Compatibility Testing
1577//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001578
Chris Lattner78eca282008-04-07 06:49:41 +00001579/// C99 6.2.7p1: If both are complete types, then the following additional
1580/// requirements apply.
1581/// FIXME (handle compatibility across source files).
1582static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1583 const ASTContext &C) {
Steve Naroffab373092007-11-07 06:03:51 +00001584 // "Class" and "id" are compatible built-in structure types.
Chris Lattner78eca282008-04-07 06:49:41 +00001585 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1586 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroffab373092007-11-07 06:03:51 +00001587 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001588
Chris Lattner78eca282008-04-07 06:49:41 +00001589 // Within a translation unit a tag type is only compatible with itself. Self
1590 // equality is already handled by the time we get here.
1591 assert(LHS != RHS && "Self equality not handled!");
1592 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001593}
1594
1595bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1596 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1597 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001598 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1599 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001600 return false;
1601
Chris Lattner28be73f2008-07-26 21:30:36 +00001602 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1603 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001604
1605 return typesAreCompatible(ltype, rtype);
1606}
1607
Steve Naroffec0550f2007-10-15 20:41:53 +00001608bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001609 const FunctionType *lbase = lhs->getAsFunctionType();
1610 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001611 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1612 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1613
1614 // first check the return types (common between C99 and K&R).
1615 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1616 return false;
1617
1618 if (lproto && rproto) { // two C99 style function prototypes
1619 unsigned lproto_nargs = lproto->getNumArgs();
1620 unsigned rproto_nargs = rproto->getNumArgs();
1621
1622 if (lproto_nargs != rproto_nargs)
1623 return false;
1624
1625 // both prototypes have the same number of arguments.
1626 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1627 (rproto->isVariadic() && !lproto->isVariadic()))
1628 return false;
1629
1630 // The use of ellipsis agree...now check the argument types.
1631 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001632 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1633 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001634 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001635 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001636 return false;
1637 return true;
1638 }
Chris Lattner5426bf62008-04-07 07:01:58 +00001639
Steve Naroffec0550f2007-10-15 20:41:53 +00001640 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1641 return true;
1642
1643 // we have a mixture of K&R style with C99 prototypes
1644 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroffec0550f2007-10-15 20:41:53 +00001645 if (proto->isVariadic())
1646 return false;
1647
1648 // FIXME: Each parameter type T in the prototype must be compatible with the
1649 // type resulting from applying the usual argument conversions to T.
1650 return true;
1651}
1652
Chris Lattneracc99722008-04-07 06:56:55 +00001653// C99 6.7.5.2p6
1654static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattneracc99722008-04-07 06:56:55 +00001655 // Constant arrays must be the same size to be compatible.
1656 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1657 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1658 if (RCAT->getSize() != LCAT->getSize())
1659 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001660
Chris Lattner8c7bbb52008-04-07 06:58:21 +00001661 // Compatible arrays must have compatible element types
1662 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroffec0550f2007-10-15 20:41:53 +00001663}
1664
Chris Lattner6ac46a42008-04-07 06:51:04 +00001665/// areCompatVectorTypes - Return true if the two specified vector types are
1666/// compatible.
1667static bool areCompatVectorTypes(const VectorType *LHS,
1668 const VectorType *RHS) {
1669 assert(LHS->isCanonical() && RHS->isCanonical());
1670 return LHS->getElementType() == RHS->getElementType() &&
1671 LHS->getNumElements() == RHS->getNumElements();
1672}
1673
1674/// areCompatObjCInterfaces - Return true if the two interface types are
1675/// compatible for assignment from RHS to LHS. This handles validation of any
1676/// protocol qualifiers on the LHS or RHS.
1677///
Chris Lattner5426bf62008-04-07 07:01:58 +00001678static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1679 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001680 // Verify that the base decls are compatible: the RHS must be a subclass of
1681 // the LHS.
1682 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1683 return false;
1684
1685 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1686 // protocol qualified at all, then we are good.
1687 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1688 return true;
1689
1690 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1691 // isn't a superset.
1692 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1693 return true; // FIXME: should return false!
1694
1695 // Finally, we must have two protocol-qualified interfaces.
1696 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1697 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1698 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1699 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1700 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1701 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1702
1703 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1704 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1705 // LHS in a single parallel scan until we run out of elements in LHS.
1706 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1707 ObjCProtocolDecl *LHSProto = *LHSPI;
1708
1709 while (RHSPI != RHSPE) {
1710 ObjCProtocolDecl *RHSProto = *RHSPI++;
1711 // If the RHS has a protocol that the LHS doesn't, ignore it.
1712 if (RHSProto != LHSProto)
1713 continue;
1714
1715 // Otherwise, the RHS does have this element.
1716 ++LHSPI;
1717 if (LHSPI == LHSPE)
1718 return true; // All protocols in LHS exist in RHS.
1719
1720 LHSProto = *LHSPI;
1721 }
1722
1723 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1724 return false;
1725}
1726
1727
Steve Naroffec0550f2007-10-15 20:41:53 +00001728/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1729/// both shall have the identically qualified version of a compatible type.
1730/// C99 6.2.7p1: Two types have compatible types if their types are the
1731/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001732bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001733 QualType LHS = getCanonicalType(LHS_NC);
1734 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner988ee6e2008-04-03 05:07:04 +00001735
Bill Wendling43d69752007-12-03 07:33:35 +00001736 // C++ [expr]: If an expression initially has the type "reference to T", the
1737 // type is adjusted to "T" prior to any further analysis, the expression
1738 // designates the object or function denoted by the reference, and the
1739 // expression is an lvalue.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001740 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1741 LHS = RT->getPointeeType();
1742 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1743 RHS = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001744
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001745 // If two types are identical, they are compatible.
1746 if (LHS == RHS)
1747 return true;
1748
1749 // If qualifiers differ, the types are different.
Chris Lattnera36a61f2008-04-07 05:43:21 +00001750 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1751 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001752 return false;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001753
1754 // Strip off ASQual's if present.
1755 if (LHSAS) {
1756 LHS = LHS.getUnqualifiedType();
1757 RHS = RHS.getUnqualifiedType();
1758 }
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001759
Chris Lattnerc4e40592008-04-07 04:07:56 +00001760 Type::TypeClass LHSClass = LHS->getTypeClass();
1761 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattner1adb8832008-01-14 05:45:46 +00001762
1763 // We want to consider the two function types to be the same for these
1764 // comparisons, just force one to the other.
1765 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1766 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001767
1768 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001769 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1770 LHSClass = Type::ConstantArray;
1771 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1772 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001773
Nate Begeman213541a2008-04-18 23:10:10 +00001774 // Canonicalize ExtVector -> Vector.
1775 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1776 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001777
Chris Lattnerb0489812008-04-07 06:38:24 +00001778 // Consider qualified interfaces and interfaces the same.
1779 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1780 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1781
Chris Lattnera36a61f2008-04-07 05:43:21 +00001782 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001783 if (LHSClass != RHSClass) {
Chris Lattnerb0489812008-04-07 06:38:24 +00001784 // ID is compatible with all interface types.
1785 if (isa<ObjCInterfaceType>(LHS))
1786 return isObjCIdType(RHS);
1787 if (isa<ObjCInterfaceType>(RHS))
1788 return isObjCIdType(LHS);
Steve Naroff97341622008-06-04 15:07:33 +00001789
1790 // ID is compatible with all qualified id types.
1791 if (isa<ObjCQualifiedIdType>(LHS)) {
1792 if (const PointerType *PT = RHS->getAsPointerType())
1793 return isObjCIdType(PT->getPointeeType());
1794 }
1795 if (isa<ObjCQualifiedIdType>(RHS)) {
1796 if (const PointerType *PT = LHS->getAsPointerType())
1797 return isObjCIdType(PT->getPointeeType());
1798 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001799 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1800 // a signed integer type, or an unsigned integer type.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001801 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1802 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1803 return EDecl->getIntegerType() == RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001804 }
Chris Lattnerc4e40592008-04-07 04:07:56 +00001805 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1806 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1807 return EDecl->getIntegerType() == LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001808 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001809
Steve Naroffec0550f2007-10-15 20:41:53 +00001810 return false;
1811 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00001812
Steve Naroff4a746782008-01-09 22:43:08 +00001813 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001814 switch (LHSClass) {
Chris Lattnera36a61f2008-04-07 05:43:21 +00001815 case Type::ASQual:
1816 case Type::FunctionProto:
1817 case Type::VariableArray:
1818 case Type::IncompleteArray:
1819 case Type::Reference:
Chris Lattnerb0489812008-04-07 06:38:24 +00001820 case Type::ObjCQualifiedInterface:
Chris Lattnera36a61f2008-04-07 05:43:21 +00001821 assert(0 && "Canonicalized away above");
Chris Lattner1adb8832008-01-14 05:45:46 +00001822 case Type::Pointer:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001823 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001824 case Type::ConstantArray:
Chris Lattneracc99722008-04-07 06:56:55 +00001825 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1826 *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001827 case Type::FunctionNoProto:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001828 return functionTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001829 case Type::Tagged: // handle structures, unions
Chris Lattner78eca282008-04-07 06:49:41 +00001830 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001831 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00001832 // Only exactly equal builtin types are compatible, which is tested above.
1833 return false;
1834 case Type::Vector:
1835 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001836 case Type::ObjCInterface:
Chris Lattnerb0489812008-04-07 06:38:24 +00001837 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1838 cast<ObjCInterfaceType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001839 default:
1840 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001841 }
1842 return true; // should never get here...
1843}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001844
Chris Lattner5426bf62008-04-07 07:01:58 +00001845//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00001846// Integer Predicates
1847//===----------------------------------------------------------------------===//
1848unsigned ASTContext::getIntWidth(QualType T) {
1849 if (T == BoolTy)
1850 return 1;
1851 // At the moment, only bool has padding bits
1852 return (unsigned)getTypeSize(T);
1853}
1854
1855QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1856 assert(T->isSignedIntegerType() && "Unexpected type");
1857 if (const EnumType* ETy = T->getAsEnumType())
1858 T = ETy->getDecl()->getIntegerType();
1859 const BuiltinType* BTy = T->getAsBuiltinType();
1860 assert (BTy && "Unexpected signed integer type");
1861 switch (BTy->getKind()) {
1862 case BuiltinType::Char_S:
1863 case BuiltinType::SChar:
1864 return UnsignedCharTy;
1865 case BuiltinType::Short:
1866 return UnsignedShortTy;
1867 case BuiltinType::Int:
1868 return UnsignedIntTy;
1869 case BuiltinType::Long:
1870 return UnsignedLongTy;
1871 case BuiltinType::LongLong:
1872 return UnsignedLongLongTy;
1873 default:
1874 assert(0 && "Unexpected signed integer type");
1875 return QualType();
1876 }
1877}
1878
1879
1880//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00001881// Serialization Support
1882//===----------------------------------------------------------------------===//
1883
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001884/// Emit - Serialize an ASTContext object to Bitcode.
1885void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001886 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00001887 S.EmitRef(SourceMgr);
1888 S.EmitRef(Target);
1889 S.EmitRef(Idents);
1890 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001891
Ted Kremenekfee04522007-10-31 22:44:07 +00001892 // Emit the size of the type vector so that we can reserve that size
1893 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001894 S.EmitInt(Types.size());
1895
Ted Kremenek03ed4402007-11-13 22:02:55 +00001896 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1897 I!=E;++I)
1898 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001899
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001900 S.EmitOwnedPtr(TUDecl);
1901
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001902 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001903}
1904
Ted Kremenek0f84c002007-11-13 00:25:37 +00001905ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001906
1907 // Read the language options.
1908 LangOptions LOpts;
1909 LOpts.Read(D);
1910
Ted Kremenekfee04522007-10-31 22:44:07 +00001911 SourceManager &SM = D.ReadRef<SourceManager>();
1912 TargetInfo &t = D.ReadRef<TargetInfo>();
1913 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1914 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001915
Ted Kremenekfee04522007-10-31 22:44:07 +00001916 unsigned size_reserve = D.ReadInt();
1917
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001918 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00001919
Ted Kremenek03ed4402007-11-13 22:02:55 +00001920 for (unsigned i = 0; i < size_reserve; ++i)
1921 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001922
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001923 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1924
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001925 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001926
1927 return A;
1928}