blob: f8129969b7b6b8969f1702ac801f27e916b1d68f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Chris Lattner61710852008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Daniel Dunbare91593e2008-08-11 04:54:23 +000036 Idents(idents), Selectors(sels)
37{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 Types.pop_back();
49 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000050
51 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000052}
53
54void ASTContext::PrintStats() const {
55 fprintf(stderr, "*** AST Context Stats:\n");
56 fprintf(stderr, " %d types total.\n", (int)Types.size());
57 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000058 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
60
61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
63 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000064 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
67 Type *T = Types[i];
68 if (isa<BuiltinType>(T))
69 ++NumBuiltin;
70 else if (isa<PointerType>(T))
71 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000072 else if (isa<BlockPointerType>(T))
73 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 else if (isa<ReferenceType>(T))
75 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000076 else if (isa<ComplexType>(T))
77 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 else if (isa<ArrayType>(T))
79 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000080 else if (isa<VectorType>(T))
81 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 else if (isa<FunctionTypeNoProto>(T))
83 ++NumFunctionNP;
84 else if (isa<FunctionTypeProto>(T))
85 ++NumFunctionP;
86 else if (isa<TypedefType>(T))
87 ++NumTypeName;
88 else if (TagType *TT = dyn_cast<TagType>(T)) {
89 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000090 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000092 case TagDecl::TK_struct: ++NumTagStruct; break;
93 case TagDecl::TK_union: ++NumTagUnion; break;
94 case TagDecl::TK_class: ++NumTagClass; break;
95 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 } else if (isa<ObjCInterfaceType>(T))
98 ++NumObjCInterfaces;
99 else if (isa<ObjCQualifiedInterfaceType>(T))
100 ++NumObjCQualifiedInterfaces;
101 else if (isa<ObjCQualifiedIdType>(T))
102 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000103 else if (isa<TypeOfType>(T))
104 ++NumTypeOfTypes;
105 else if (isa<TypeOfExpr>(T))
106 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000107 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000108 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 assert(0 && "Unknown type!");
110 }
111 }
112
113 fprintf(stderr, " %d builtin types\n", NumBuiltin);
114 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000115 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000117 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000119 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
121 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
122 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
123 fprintf(stderr, " %d tagged types\n", NumTagged);
124 fprintf(stderr, " %d struct types\n", NumTagStruct);
125 fprintf(stderr, " %d union types\n", NumTagUnion);
126 fprintf(stderr, " %d class types\n", NumTagClass);
127 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000129 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000130 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000131 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000132 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000133 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
134 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
137 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000138 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 NumFunctionP*sizeof(FunctionTypeProto)+
140 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000141 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
142 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000143}
144
145
146void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
147 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
148}
149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150void ASTContext::InitBuiltinTypes() {
151 assert(VoidTy.isNull() && "Context reinitialized?");
152
153 // C99 6.2.5p19.
154 InitBuiltinType(VoidTy, BuiltinType::Void);
155
156 // C99 6.2.5p2.
157 InitBuiltinType(BoolTy, BuiltinType::Bool);
158 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000159 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 InitBuiltinType(CharTy, BuiltinType::Char_S);
161 else
162 InitBuiltinType(CharTy, BuiltinType::Char_U);
163 // C99 6.2.5p4.
164 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
165 InitBuiltinType(ShortTy, BuiltinType::Short);
166 InitBuiltinType(IntTy, BuiltinType::Int);
167 InitBuiltinType(LongTy, BuiltinType::Long);
168 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
169
170 // C99 6.2.5p6.
171 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
172 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
173 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
174 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
175 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
176
177 // C99 6.2.5p10.
178 InitBuiltinType(FloatTy, BuiltinType::Float);
179 InitBuiltinType(DoubleTy, BuiltinType::Double);
180 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000181
182 // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000185 // Placeholder type for functions.
186 InitBuiltinType(OverloadTy, BuiltinType::Overload);
187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // C99 6.2.5p11.
189 FloatComplexTy = getComplexType(FloatTy);
190 DoubleComplexTy = getComplexType(DoubleTy);
191 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000192
Steve Naroff7e219e42007-10-15 14:41:52 +0000193 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000195 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000197 ClassStructType = 0;
198
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000200
201 // void * type
202 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203}
204
Chris Lattner464175b2007-07-18 17:52:12 +0000205//===----------------------------------------------------------------------===//
206// Type Sizing and Analysis
207//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000208
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000209/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
210/// scalar floating point type.
211const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
212 const BuiltinType *BT = T->getAsBuiltinType();
213 assert(BT && "Not a floating point type!");
214 switch (BT->getKind()) {
215 default: assert(0 && "Not a floating point type!");
216 case BuiltinType::Float: return Target.getFloatFormat();
217 case BuiltinType::Double: return Target.getDoubleFormat();
218 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
219 }
220}
221
222
Chris Lattnera7674d82007-07-13 22:13:22 +0000223/// getTypeSize - Return the size of the specified type, in bits. This method
224/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000225std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000226ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000227 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000228 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000229 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000230 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000231 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000232 case Type::FunctionNoProto:
233 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000234 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000235 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000236 case Type::VariableArray:
237 assert(0 && "VLAs not implemented yet!");
238 case Type::ConstantArray: {
239 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
240
Chris Lattner98be4942008-03-05 18:54:05 +0000241 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000242 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000243 Align = EltInfo.second;
244 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000245 }
Nate Begeman213541a2008-04-18 23:10:10 +0000246 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000247 case Type::Vector: {
248 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000249 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000250 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000251 // FIXME: This isn't right for unusual vectors
252 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000253 break;
254 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000255
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000256 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000258 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000259 case BuiltinType::Void:
260 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000261 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000262 Width = Target.getBoolWidth();
263 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000264 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000265 case BuiltinType::Char_S:
266 case BuiltinType::Char_U:
267 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000268 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000269 Width = Target.getCharWidth();
270 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000271 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000272 case BuiltinType::WChar:
273 Width = Target.getWCharWidth();
274 Align = Target.getWCharAlign();
275 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000276 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000277 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000278 Width = Target.getShortWidth();
279 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000280 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000281 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000282 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000283 Width = Target.getIntWidth();
284 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000285 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000286 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000287 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000288 Width = Target.getLongWidth();
289 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000290 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000291 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000292 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000293 Width = Target.getLongLongWidth();
294 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000295 break;
296 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000297 Width = Target.getFloatWidth();
298 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000299 break;
300 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000301 Width = Target.getDoubleWidth();
302 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000303 break;
304 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000305 Width = Target.getLongDoubleWidth();
306 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000307 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000308 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000309 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000310 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000311 // FIXME: Pointers into different addr spaces could have different sizes and
312 // alignment requirements: getPointerInfo should take an AddrSpace.
313 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000314 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000315 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000316 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000317 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000318 case Type::BlockPointer: {
319 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
320 Width = Target.getPointerWidth(AS);
321 Align = Target.getPointerAlign(AS);
322 break;
323 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000324 case Type::Pointer: {
325 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000326 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000327 Align = Target.getPointerAlign(AS);
328 break;
329 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000330 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000331 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000332 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 // FIXME: This is wrong for struct layout: a reference in a struct has
334 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000335 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000336
337 case Type::Complex: {
338 // Complex types have the same alignment as their elements, but twice the
339 // size.
340 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000341 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000342 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000343 Align = EltInfo.second;
344 break;
345 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000346 case Type::ObjCInterface: {
347 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
348 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
349 Width = Layout.getSize();
350 Align = Layout.getAlignment();
351 break;
352 }
Chris Lattner71763312008-04-06 22:05:18 +0000353 case Type::Tagged: {
Chris Lattner8389eab2008-08-09 21:35:13 +0000354 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
355 Width = 1;
356 Align = 1;
357 break;
358 }
359
Chris Lattner71763312008-04-06 22:05:18 +0000360 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
361 return getTypeInfo(ET->getDecl()->getIntegerType());
362
363 RecordType *RT = cast<RecordType>(T);
364 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
365 Width = Layout.getSize();
366 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000367 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000368 }
Chris Lattner71763312008-04-06 22:05:18 +0000369 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000370
Chris Lattner464175b2007-07-18 17:52:12 +0000371 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000372 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000373}
374
Devang Patel8b277042008-06-04 21:22:16 +0000375/// LayoutField - Field layout.
376void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000377 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000378 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000379 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000380 uint64_t FieldOffset = IsUnion ? 0 : Size;
381 uint64_t FieldSize;
382 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000383
384 // FIXME: Should this override struct packing? Probably we want to
385 // take the minimum?
386 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
387 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000388
389 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
390 // TODO: Need to check this algorithm on other targets!
391 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000392 FieldSize =
393 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000394
395 std::pair<uint64_t, unsigned> FieldInfo =
396 Context.getTypeInfo(FD->getType());
397 uint64_t TypeSize = FieldInfo.first;
398
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000399 // Determine the alignment of this bitfield. The packing
400 // attributes define a maximum and the alignment attribute defines
401 // a minimum.
402 // FIXME: What is the right behavior when the specified alignment
403 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000404 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000405 if (FieldPacking)
406 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000407 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
408 FieldAlign = std::max(FieldAlign, AA->getAlignment());
409
410 // Check if we need to add padding to give the field the correct
411 // alignment.
412 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
413 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
414
415 // Padding members don't affect overall alignment
416 if (!FD->getIdentifier())
417 FieldAlign = 1;
418 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000419 if (FD->getType()->isIncompleteArrayType()) {
420 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000421 // query getTypeInfo about these, so we figure it out here.
422 // Flexible array members don't have any size, but they
423 // have to be aligned appropriately for their element type.
424 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000425 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000426 FieldAlign = Context.getTypeAlign(ATy->getElementType());
427 } else {
428 std::pair<uint64_t, unsigned> FieldInfo =
429 Context.getTypeInfo(FD->getType());
430 FieldSize = FieldInfo.first;
431 FieldAlign = FieldInfo.second;
432 }
433
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000434 // Determine the alignment of this bitfield. The packing
435 // attributes define a maximum and the alignment attribute defines
436 // a minimum. Additionally, the packing alignment must be at least
437 // a byte for non-bitfields.
438 //
439 // FIXME: What is the right behavior when the specified alignment
440 // is smaller than the specified packing?
441 if (FieldPacking)
442 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000443 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
444 FieldAlign = std::max(FieldAlign, AA->getAlignment());
445
446 // Round up the current record size to the field's alignment boundary.
447 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
448 }
449
450 // Place this field at the current location.
451 FieldOffsets[FieldNo] = FieldOffset;
452
453 // Reserve space for this field.
454 if (IsUnion) {
455 Size = std::max(Size, FieldSize);
456 } else {
457 Size = FieldOffset + FieldSize;
458 }
459
460 // Remember max struct/class alignment.
461 Alignment = std::max(Alignment, FieldAlign);
462}
463
Devang Patel44a3dde2008-06-04 21:54:36 +0000464
Chris Lattner61710852008-10-05 17:34:18 +0000465/// getASTObjcInterfaceLayout - Get or compute information about the layout of
466/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000467/// position information.
468const ASTRecordLayout &
469ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
470 // Look up this layout, if already laid out, return what we have.
471 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
472 if (Entry) return *Entry;
473
474 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
475 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000476 ASTRecordLayout *NewEntry = NULL;
477 unsigned FieldCount = D->ivar_size();
478 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
479 FieldCount++;
480 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
481 unsigned Alignment = SL.getAlignment();
482 uint64_t Size = SL.getSize();
483 NewEntry = new ASTRecordLayout(Size, Alignment);
484 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000485 // Super class is at the beginning of the layout.
486 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000487 } else {
488 NewEntry = new ASTRecordLayout();
489 NewEntry->InitializeLayout(FieldCount);
490 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000491 Entry = NewEntry;
492
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000493 unsigned StructPacking = 0;
494 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
495 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000496
497 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
498 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
499 AA->getAlignment()));
500
501 // Layout each ivar sequentially.
502 unsigned i = 0;
503 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
504 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
505 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000506 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000507 }
508
509 // Finally, round the size of the total struct up to the alignment of the
510 // struct itself.
511 NewEntry->FinalizeLayout();
512 return *NewEntry;
513}
514
Devang Patel88a981b2007-11-01 19:11:01 +0000515/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000516/// specified record (struct/union/class), which indicates its size and field
517/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000518const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000519 D = D->getDefinition(*this);
520 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000521
Chris Lattner464175b2007-07-18 17:52:12 +0000522 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000523 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000524 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000525
Devang Patel88a981b2007-11-01 19:11:01 +0000526 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
527 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
528 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000529 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000530
Devang Patel8b277042008-06-04 21:22:16 +0000531 NewEntry->InitializeLayout(D->getNumMembers());
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000532 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000533
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000534 unsigned StructPacking = 0;
535 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
536 StructPacking = PA->getAlignment();
537
Eli Friedman4bd998b2008-05-30 09:31:38 +0000538 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000539 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
540 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000541
Eli Friedman4bd998b2008-05-30 09:31:38 +0000542 // Layout each field, for now, just sequentially, respecting alignment. In
543 // the future, this will need to be tweakable by targets.
544 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
545 const FieldDecl *FD = D->getMember(i);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000546 NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000547 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000548
549 // Finally, round the size of the total struct up to the alignment of the
550 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000551 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000552 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000553}
554
Chris Lattnera7674d82007-07-13 22:13:22 +0000555//===----------------------------------------------------------------------===//
556// Type creation/memoization methods
557//===----------------------------------------------------------------------===//
558
Christopher Lambebb97e92008-02-04 02:31:56 +0000559QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000560 QualType CanT = getCanonicalType(T);
561 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000562 return T;
563
564 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
565 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000566 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000567 "Type is already address space qualified");
568
569 // Check if we've already instantiated an address space qual'd type of this
570 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000571 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000572 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000573 void *InsertPos = 0;
574 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
575 return QualType(ASQy, 0);
576
577 // If the base type isn't canonical, this won't be a canonical type either,
578 // so fill in the canonical type field.
579 QualType Canonical;
580 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000581 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000582
583 // Get the new insert position for the node we care about.
584 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000585 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000586 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000587 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000588 ASQualTypes.InsertNode(New, InsertPos);
589 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000590 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000591}
592
Chris Lattnera7674d82007-07-13 22:13:22 +0000593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594/// getComplexType - Return the uniqued reference to the type for a complex
595/// number with the specified element type.
596QualType ASTContext::getComplexType(QualType T) {
597 // Unique pointers, to guarantee there is only one pointer of a particular
598 // structure.
599 llvm::FoldingSetNodeID ID;
600 ComplexType::Profile(ID, T);
601
602 void *InsertPos = 0;
603 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
604 return QualType(CT, 0);
605
606 // If the pointee type isn't canonical, this won't be a canonical type either,
607 // so fill in the canonical type field.
608 QualType Canonical;
609 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000610 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000611
612 // Get the new insert position for the node we care about.
613 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000614 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
616 ComplexType *New = new ComplexType(T, Canonical);
617 Types.push_back(New);
618 ComplexTypes.InsertNode(New, InsertPos);
619 return QualType(New, 0);
620}
621
622
623/// getPointerType - Return the uniqued reference to the type for a pointer to
624/// the specified type.
625QualType ASTContext::getPointerType(QualType T) {
626 // Unique pointers, to guarantee there is only one pointer of a particular
627 // structure.
628 llvm::FoldingSetNodeID ID;
629 PointerType::Profile(ID, T);
630
631 void *InsertPos = 0;
632 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
633 return QualType(PT, 0);
634
635 // If the pointee type isn't canonical, this won't be a canonical type either,
636 // so fill in the canonical type field.
637 QualType Canonical;
638 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000639 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000640
641 // Get the new insert position for the node we care about.
642 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000643 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 }
645 PointerType *New = new PointerType(T, Canonical);
646 Types.push_back(New);
647 PointerTypes.InsertNode(New, InsertPos);
648 return QualType(New, 0);
649}
650
Steve Naroff5618bd42008-08-27 16:04:49 +0000651/// getBlockPointerType - Return the uniqued reference to the type for
652/// a pointer to the specified block.
653QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000654 assert(T->isFunctionType() && "block of function types only");
655 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000656 // structure.
657 llvm::FoldingSetNodeID ID;
658 BlockPointerType::Profile(ID, T);
659
660 void *InsertPos = 0;
661 if (BlockPointerType *PT =
662 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
663 return QualType(PT, 0);
664
Steve Naroff296e8d52008-08-28 19:20:44 +0000665 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000666 // type either so fill in the canonical type field.
667 QualType Canonical;
668 if (!T->isCanonical()) {
669 Canonical = getBlockPointerType(getCanonicalType(T));
670
671 // Get the new insert position for the node we care about.
672 BlockPointerType *NewIP =
673 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000674 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000675 }
676 BlockPointerType *New = new BlockPointerType(T, Canonical);
677 Types.push_back(New);
678 BlockPointerTypes.InsertNode(New, InsertPos);
679 return QualType(New, 0);
680}
681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682/// getReferenceType - Return the uniqued reference to the type for a reference
683/// to the specified type.
684QualType ASTContext::getReferenceType(QualType T) {
685 // Unique pointers, to guarantee there is only one pointer of a particular
686 // structure.
687 llvm::FoldingSetNodeID ID;
688 ReferenceType::Profile(ID, T);
689
690 void *InsertPos = 0;
691 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
692 return QualType(RT, 0);
693
694 // If the referencee type isn't canonical, this won't be a canonical type
695 // either, so fill in the canonical type field.
696 QualType Canonical;
697 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000698 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000699
700 // Get the new insert position for the node we care about.
701 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000702 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 }
704
705 ReferenceType *New = new ReferenceType(T, Canonical);
706 Types.push_back(New);
707 ReferenceTypes.InsertNode(New, InsertPos);
708 return QualType(New, 0);
709}
710
Steve Narofffb22d962007-08-30 01:06:46 +0000711/// getConstantArrayType - Return the unique reference to the type for an
712/// array of the specified element type.
713QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000714 const llvm::APInt &ArySize,
715 ArrayType::ArraySizeModifier ASM,
716 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000718 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719
720 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000721 if (ConstantArrayType *ATP =
722 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 return QualType(ATP, 0);
724
725 // If the element type isn't canonical, this won't be a canonical type either,
726 // so fill in the canonical type field.
727 QualType Canonical;
728 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000729 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000730 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000732 ConstantArrayType *NewIP =
733 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000734 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 }
736
Steve Naroffc9406122007-08-30 18:10:14 +0000737 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
738 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000739 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 Types.push_back(New);
741 return QualType(New, 0);
742}
743
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000744/// getVariableArrayType - Returns a non-unique reference to the type for a
745/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000746QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
747 ArrayType::ArraySizeModifier ASM,
748 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000749 // Since we don't unique expressions, it isn't possible to unique VLA's
750 // that have an expression provided for their size.
751
752 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
753 ASM, EltTypeQuals);
754
755 VariableArrayTypes.push_back(New);
756 Types.push_back(New);
757 return QualType(New, 0);
758}
759
760QualType ASTContext::getIncompleteArrayType(QualType EltTy,
761 ArrayType::ArraySizeModifier ASM,
762 unsigned EltTypeQuals) {
763 llvm::FoldingSetNodeID ID;
764 IncompleteArrayType::Profile(ID, EltTy);
765
766 void *InsertPos = 0;
767 if (IncompleteArrayType *ATP =
768 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
769 return QualType(ATP, 0);
770
771 // If the element type isn't canonical, this won't be a canonical type
772 // either, so fill in the canonical type field.
773 QualType Canonical;
774
775 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000776 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000777 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000778
779 // Get the new insert position for the node we care about.
780 IncompleteArrayType *NewIP =
781 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000782 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000783 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000784
785 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
786 ASM, EltTypeQuals);
787
788 IncompleteArrayTypes.InsertNode(New, InsertPos);
789 Types.push_back(New);
790 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000791}
792
Steve Naroff73322922007-07-18 18:00:27 +0000793/// getVectorType - Return the unique reference to a vector type of
794/// the specified element type and size. VectorType must be a built-in type.
795QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 BuiltinType *baseType;
797
Chris Lattnerf52ab252008-04-06 22:59:24 +0000798 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000799 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000800
801 // Check if we've already instantiated a vector of this type.
802 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000803 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 void *InsertPos = 0;
805 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
806 return QualType(VTP, 0);
807
808 // If the element type isn't canonical, this won't be a canonical type either,
809 // so fill in the canonical type field.
810 QualType Canonical;
811 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000812 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
814 // Get the new insert position for the node we care about.
815 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000816 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 }
818 VectorType *New = new VectorType(vecType, NumElts, Canonical);
819 VectorTypes.InsertNode(New, InsertPos);
820 Types.push_back(New);
821 return QualType(New, 0);
822}
823
Nate Begeman213541a2008-04-18 23:10:10 +0000824/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000825/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000826QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000827 BuiltinType *baseType;
828
Chris Lattnerf52ab252008-04-06 22:59:24 +0000829 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000830 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000831
832 // Check if we've already instantiated a vector of this type.
833 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000834 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000835 void *InsertPos = 0;
836 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
837 return QualType(VTP, 0);
838
839 // If the element type isn't canonical, this won't be a canonical type either,
840 // so fill in the canonical type field.
841 QualType Canonical;
842 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000843 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000844
845 // Get the new insert position for the node we care about.
846 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000847 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +0000848 }
Nate Begeman213541a2008-04-18 23:10:10 +0000849 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000850 VectorTypes.InsertNode(New, InsertPos);
851 Types.push_back(New);
852 return QualType(New, 0);
853}
854
Reid Spencer5f016e22007-07-11 17:01:13 +0000855/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
856///
857QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
858 // Unique functions, to guarantee there is only one function of a particular
859 // structure.
860 llvm::FoldingSetNodeID ID;
861 FunctionTypeNoProto::Profile(ID, ResultTy);
862
863 void *InsertPos = 0;
864 if (FunctionTypeNoProto *FT =
865 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
866 return QualType(FT, 0);
867
868 QualType Canonical;
869 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000870 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
872 // Get the new insert position for the node we care about.
873 FunctionTypeNoProto *NewIP =
874 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000875 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 }
877
878 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
879 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000880 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 return QualType(New, 0);
882}
883
884/// getFunctionType - Return a normal function type with a typed argument
885/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +0000886QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000887 unsigned NumArgs, bool isVariadic,
888 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 // Unique functions, to guarantee there is only one function of a particular
890 // structure.
891 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000892 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
893 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894
895 void *InsertPos = 0;
896 if (FunctionTypeProto *FTP =
897 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
898 return QualType(FTP, 0);
899
900 // Determine whether the type being created is already canonical or not.
901 bool isCanonical = ResultTy->isCanonical();
902 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
903 if (!ArgArray[i]->isCanonical())
904 isCanonical = false;
905
906 // If this type isn't canonical, get the canonical version of it.
907 QualType Canonical;
908 if (!isCanonical) {
909 llvm::SmallVector<QualType, 16> CanonicalArgs;
910 CanonicalArgs.reserve(NumArgs);
911 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000912 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000913
Chris Lattnerf52ab252008-04-06 22:59:24 +0000914 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 &CanonicalArgs[0], NumArgs,
916 isVariadic);
917
918 // Get the new insert position for the node we care about.
919 FunctionTypeProto *NewIP =
920 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000921 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 }
923
924 // FunctionTypeProto objects are not allocated with new because they have a
925 // variable size array (for parameter types) at the end of them.
926 FunctionTypeProto *FTP =
927 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000928 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000930 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 Types.push_back(FTP);
932 FunctionTypeProtos.InsertNode(FTP, InsertPos);
933 return QualType(FTP, 0);
934}
935
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000936/// getTypeDeclType - Return the unique reference to the type for the
937/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000938QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000939 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000940 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
941
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000942 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000943 return getTypedefType(Typedef);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000944 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000945 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000946
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000947 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000948 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
949 : new CXXRecordType(CXXRecord);
950 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000951 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000952 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
953 : new RecordType(Record);
954 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000955 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000956 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000957 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000958 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000959
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000960 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000961 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000962}
963
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000964/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
965/// about which RecordDecl serves as the definition of a particular
966/// struct/union/class. This will eventually be used by enums as well.
967void ASTContext::setTagDefinition(TagDecl* D) {
968 assert (D->isDefinition());
969 cast<TagType>(D->TypeForDecl)->decl = D;
970}
971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972/// getTypedefType - Return the unique reference to the type for the
973/// specified typename decl.
974QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
975 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
976
Chris Lattnerf52ab252008-04-06 22:59:24 +0000977 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000978 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 Types.push_back(Decl->TypeForDecl);
980 return QualType(Decl->TypeForDecl, 0);
981}
982
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000984/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000986 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
987
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000989 Types.push_back(Decl->TypeForDecl);
990 return QualType(Decl->TypeForDecl, 0);
991}
992
Chris Lattner88cb27a2008-04-07 04:56:42 +0000993/// CmpProtocolNames - Comparison predicate for sorting protocols
994/// alphabetically.
995static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
996 const ObjCProtocolDecl *RHS) {
997 return strcmp(LHS->getName(), RHS->getName()) < 0;
998}
999
1000static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1001 unsigned &NumProtocols) {
1002 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1003
1004 // Sort protocols, keyed by name.
1005 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1006
1007 // Remove duplicates.
1008 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1009 NumProtocols = ProtocolsEnd-Protocols;
1010}
1011
1012
Chris Lattner065f0d72008-04-07 04:44:08 +00001013/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1014/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1016 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001017 // Sort the protocol list alphabetically to canonicalize it.
1018 SortAndUniqueProtocols(Protocols, NumProtocols);
1019
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001020 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001021 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001022
1023 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 if (ObjCQualifiedInterfaceType *QT =
1025 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001026 return QualType(QT, 0);
1027
1028 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001029 ObjCQualifiedInterfaceType *QType =
1030 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001031 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001033 return QualType(QType, 0);
1034}
1035
Chris Lattner88cb27a2008-04-07 04:56:42 +00001036/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1037/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001038QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001039 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001040 // Sort the protocol list alphabetically to canonicalize it.
1041 SortAndUniqueProtocols(Protocols, NumProtocols);
1042
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001043 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001044 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001045
1046 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001048 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001049 return QualType(QT, 0);
1050
1051 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001052 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001053 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001055 return QualType(QType, 0);
1056}
1057
Steve Naroff9752f252007-08-01 18:02:17 +00001058/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1059/// TypeOfExpr AST's (since expression's are never shared). For example,
1060/// multiple declarations that refer to "typeof(x)" all contain different
1061/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1062/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001063QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001064 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001065 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1066 Types.push_back(toe);
1067 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001068}
1069
Steve Naroff9752f252007-08-01 18:02:17 +00001070/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1071/// TypeOfType AST's. The only motivation to unique these nodes would be
1072/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1073/// an issue. This doesn't effect the type checker, since it operates
1074/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001075QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001076 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001077 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1078 Types.push_back(tot);
1079 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001080}
1081
Reid Spencer5f016e22007-07-11 17:01:13 +00001082/// getTagDeclType - Return the unique reference to the type for the
1083/// specified TagDecl (struct/union/class/enum) decl.
1084QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001085 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001086 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087}
1088
1089/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1090/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1091/// needs to agree with the definition in <stddef.h>.
1092QualType ASTContext::getSizeType() const {
1093 // On Darwin, size_t is defined as a "long unsigned int".
1094 // FIXME: should derive from "Target".
1095 return UnsignedLongTy;
1096}
1097
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001098/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001099/// width of characters in wide strings, The value is target dependent and
1100/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001101QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001102 if (LangOpts.CPlusPlus)
1103 return WCharTy;
1104
Eli Friedmanfd888a52008-02-12 08:29:21 +00001105 // On Darwin, wchar_t is defined as a "int".
1106 // FIXME: should derive from "Target".
1107 return IntTy;
1108}
1109
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001110/// getSignedWCharType - Return the type of "signed wchar_t".
1111/// Used when in C++, as a GCC extension.
1112QualType ASTContext::getSignedWCharType() const {
1113 // FIXME: derive from "Target" ?
1114 return WCharTy;
1115}
1116
1117/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1118/// Used when in C++, as a GCC extension.
1119QualType ASTContext::getUnsignedWCharType() const {
1120 // FIXME: derive from "Target" ?
1121 return UnsignedIntTy;
1122}
1123
Chris Lattner8b9023b2007-07-13 03:05:23 +00001124/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1125/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1126QualType ASTContext::getPointerDiffType() const {
1127 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1128 // FIXME: should derive from "Target".
1129 return IntTy;
1130}
1131
Chris Lattnere6327742008-04-02 05:18:44 +00001132//===----------------------------------------------------------------------===//
1133// Type Operators
1134//===----------------------------------------------------------------------===//
1135
Chris Lattner77c96472008-04-06 22:41:35 +00001136/// getCanonicalType - Return the canonical (structural) type corresponding to
1137/// the specified potentially non-canonical type. The non-canonical version
1138/// of a type may have many "decorated" versions of types. Decorators can
1139/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1140/// to be free of any of these, allowing two canonical types to be compared
1141/// for exact equality with a simple pointer comparison.
1142QualType ASTContext::getCanonicalType(QualType T) {
1143 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001144
1145 // If the result has type qualifiers, make sure to canonicalize them as well.
1146 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1147 if (TypeQuals == 0) return CanType;
1148
1149 // If the type qualifiers are on an array type, get the canonical type of the
1150 // array with the qualifiers applied to the element type.
1151 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1152 if (!AT)
1153 return CanType.getQualifiedType(TypeQuals);
1154
1155 // Get the canonical version of the element with the extra qualifiers on it.
1156 // This can recursively sink qualifiers through multiple levels of arrays.
1157 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1158 NewEltTy = getCanonicalType(NewEltTy);
1159
1160 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1161 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1162 CAT->getIndexTypeQualifier());
1163 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1164 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1165 IAT->getIndexTypeQualifier());
1166
1167 // FIXME: What is the ownership of size expressions in VLAs?
1168 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1169 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1170 VAT->getSizeModifier(),
1171 VAT->getIndexTypeQualifier());
1172}
1173
1174
1175const ArrayType *ASTContext::getAsArrayType(QualType T) {
1176 // Handle the non-qualified case efficiently.
1177 if (T.getCVRQualifiers() == 0) {
1178 // Handle the common positive case fast.
1179 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1180 return AT;
1181 }
1182
1183 // Handle the common negative case fast, ignoring CVR qualifiers.
1184 QualType CType = T->getCanonicalTypeInternal();
1185
1186 // Make sure to look through type qualifiers (like ASQuals) for the negative
1187 // test.
1188 if (!isa<ArrayType>(CType) &&
1189 !isa<ArrayType>(CType.getUnqualifiedType()))
1190 return 0;
1191
1192 // Apply any CVR qualifiers from the array type to the element type. This
1193 // implements C99 6.7.3p8: "If the specification of an array type includes
1194 // any type qualifiers, the element type is so qualified, not the array type."
1195
1196 // If we get here, we either have type qualifiers on the type, or we have
1197 // sugar such as a typedef in the way. If we have type qualifiers on the type
1198 // we must propagate them down into the elemeng type.
1199 unsigned CVRQuals = T.getCVRQualifiers();
1200 unsigned AddrSpace = 0;
1201 Type *Ty = T.getTypePtr();
1202
1203 // Rip through ASQualType's and typedefs to get to a concrete type.
1204 while (1) {
1205 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1206 AddrSpace = ASQT->getAddressSpace();
1207 Ty = ASQT->getBaseType();
1208 } else {
1209 T = Ty->getDesugaredType();
1210 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1211 break;
1212 CVRQuals |= T.getCVRQualifiers();
1213 Ty = T.getTypePtr();
1214 }
1215 }
1216
1217 // If we have a simple case, just return now.
1218 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1219 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1220 return ATy;
1221
1222 // Otherwise, we have an array and we have qualifiers on it. Push the
1223 // qualifiers into the array element type and return a new array type.
1224 // Get the canonical version of the element with the extra qualifiers on it.
1225 // This can recursively sink qualifiers through multiple levels of arrays.
1226 QualType NewEltTy = ATy->getElementType();
1227 if (AddrSpace)
1228 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1229 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1230
1231 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1232 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1233 CAT->getSizeModifier(),
1234 CAT->getIndexTypeQualifier()));
1235 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1236 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1237 IAT->getSizeModifier(),
1238 IAT->getIndexTypeQualifier()));
1239
1240 // FIXME: What is the ownership of size expressions in VLAs?
1241 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1242 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1243 VAT->getSizeModifier(),
1244 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001245}
1246
1247
Chris Lattnere6327742008-04-02 05:18:44 +00001248/// getArrayDecayedType - Return the properly qualified result of decaying the
1249/// specified array type to a pointer. This operation is non-trivial when
1250/// handling typedefs etc. The canonical type of "T" must be an array type,
1251/// this returns a pointer to a properly qualified element of the array.
1252///
1253/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1254QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001255 // Get the element type with 'getAsArrayType' so that we don't lose any
1256 // typedefs in the element type of the array. This also handles propagation
1257 // of type qualifiers from the array type into the element type if present
1258 // (C99 6.7.3p8).
1259 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1260 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001261
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001262 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001263
1264 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001265 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001266}
1267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268/// getFloatingRank - Return a relative rank for floating point types.
1269/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001270static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001271 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001273
Christopher Lambebb97e92008-02-04 02:31:56 +00001274 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001275 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 case BuiltinType::Float: return FloatRank;
1277 case BuiltinType::Double: return DoubleRank;
1278 case BuiltinType::LongDouble: return LongDoubleRank;
1279 }
1280}
1281
Steve Naroff716c7302007-08-27 01:41:48 +00001282/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1283/// point or a complex type (based on typeDomain/typeSize).
1284/// 'typeDomain' is a real floating point or complex type.
1285/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001286QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1287 QualType Domain) const {
1288 FloatingRank EltRank = getFloatingRank(Size);
1289 if (Domain->isComplexType()) {
1290 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001291 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001292 case FloatRank: return FloatComplexTy;
1293 case DoubleRank: return DoubleComplexTy;
1294 case LongDoubleRank: return LongDoubleComplexTy;
1295 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 }
Chris Lattner1361b112008-04-06 23:58:54 +00001297
1298 assert(Domain->isRealFloatingType() && "Unknown domain!");
1299 switch (EltRank) {
1300 default: assert(0 && "getFloatingRank(): illegal value for rank");
1301 case FloatRank: return FloatTy;
1302 case DoubleRank: return DoubleTy;
1303 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001304 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001305}
1306
Chris Lattner7cfeb082008-04-06 23:55:33 +00001307/// getFloatingTypeOrder - Compare the rank of the two specified floating
1308/// point types, ignoring the domain of the type (i.e. 'double' ==
1309/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1310/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001311int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1312 FloatingRank LHSR = getFloatingRank(LHS);
1313 FloatingRank RHSR = getFloatingRank(RHS);
1314
1315 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001316 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001317 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001318 return 1;
1319 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001320}
1321
Chris Lattnerf52ab252008-04-06 22:59:24 +00001322/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1323/// routine will assert if passed a built-in type that isn't an integer or enum,
1324/// or if it is not canonicalized.
1325static unsigned getIntegerRank(Type *T) {
1326 assert(T->isCanonical() && "T should be canonicalized");
1327 if (isa<EnumType>(T))
1328 return 4;
1329
1330 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001331 default: assert(0 && "getIntegerRank(): not a built-in integer");
1332 case BuiltinType::Bool:
1333 return 1;
1334 case BuiltinType::Char_S:
1335 case BuiltinType::Char_U:
1336 case BuiltinType::SChar:
1337 case BuiltinType::UChar:
1338 return 2;
1339 case BuiltinType::Short:
1340 case BuiltinType::UShort:
1341 return 3;
1342 case BuiltinType::Int:
1343 case BuiltinType::UInt:
1344 return 4;
1345 case BuiltinType::Long:
1346 case BuiltinType::ULong:
1347 return 5;
1348 case BuiltinType::LongLong:
1349 case BuiltinType::ULongLong:
1350 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001351 }
1352}
1353
Chris Lattner7cfeb082008-04-06 23:55:33 +00001354/// getIntegerTypeOrder - Returns the highest ranked integer type:
1355/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1356/// LHS < RHS, return -1.
1357int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001358 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1359 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001360 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361
Chris Lattnerf52ab252008-04-06 22:59:24 +00001362 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1363 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001364
Chris Lattner7cfeb082008-04-06 23:55:33 +00001365 unsigned LHSRank = getIntegerRank(LHSC);
1366 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001367
Chris Lattner7cfeb082008-04-06 23:55:33 +00001368 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1369 if (LHSRank == RHSRank) return 0;
1370 return LHSRank > RHSRank ? 1 : -1;
1371 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001372
Chris Lattner7cfeb082008-04-06 23:55:33 +00001373 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1374 if (LHSUnsigned) {
1375 // If the unsigned [LHS] type is larger, return it.
1376 if (LHSRank >= RHSRank)
1377 return 1;
1378
1379 // If the signed type can represent all values of the unsigned type, it
1380 // wins. Because we are dealing with 2's complement and types that are
1381 // powers of two larger than each other, this is always safe.
1382 return -1;
1383 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001384
Chris Lattner7cfeb082008-04-06 23:55:33 +00001385 // If the unsigned [RHS] type is larger, return it.
1386 if (RHSRank >= LHSRank)
1387 return -1;
1388
1389 // If the signed type can represent all values of the unsigned type, it
1390 // wins. Because we are dealing with 2's complement and types that are
1391 // powers of two larger than each other, this is always safe.
1392 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001394
1395// getCFConstantStringType - Return the type used for constant CFStrings.
1396QualType ASTContext::getCFConstantStringType() {
1397 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001398 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001399 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001400 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001401 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001402
1403 // const int *isa;
1404 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001405 // int flags;
1406 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001407 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001408 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001409 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001410 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001411 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001412 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001413
Anders Carlssonf06273f2007-11-19 00:25:30 +00001414 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001415 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001416 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001417
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001418 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001419 }
1420
1421 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001422}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001423
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001424QualType ASTContext::getObjCFastEnumerationStateType()
1425{
1426 if (!ObjCFastEnumerationStateTypeDecl) {
1427 QualType FieldTypes[] = {
1428 UnsignedLongTy,
1429 getPointerType(ObjCIdType),
1430 getPointerType(UnsignedLongTy),
1431 getConstantArrayType(UnsignedLongTy,
1432 llvm::APInt(32, 5), ArrayType::Normal, 0)
1433 };
1434
1435 FieldDecl *FieldDecls[4];
1436 for (size_t i = 0; i < 4; ++i)
1437 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1438 FieldTypes[i]);
1439
1440 ObjCFastEnumerationStateTypeDecl =
1441 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001442 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001443
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001444 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001445 }
1446
1447 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1448}
1449
Anders Carlssone8c49532007-10-29 06:33:42 +00001450// This returns true if a type has been typedefed to BOOL:
1451// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001452static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001453 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001454 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001455
1456 return false;
1457}
1458
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001459/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001460/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001462 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001463
1464 // Make all integer and enum types at least as large as an int
1465 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001466 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001467 // Treat arrays as pointers, since that's how they're passed in.
1468 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001469 sz = getTypeSize(VoidPtrTy);
1470 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001471}
1472
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001474/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001475void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001476 std::string& S)
1477{
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001478 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001479 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001480 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001481 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001482 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001483 // Compute size of all parameters.
1484 // Start with computing size of a pointer in number of bytes.
1485 // FIXME: There might(should) be a better way of doing this computation!
1486 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001487 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001488 // The first two arguments (self and _cmd) are pointers; account for
1489 // their size.
1490 int ParmOffset = 2 * PtrSize;
1491 int NumOfParams = Decl->getNumParams();
1492 for (int i = 0; i < NumOfParams; i++) {
1493 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 int sz = getObjCEncodingTypeSize (PType);
1495 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001496 ParmOffset += sz;
1497 }
1498 S += llvm::utostr(ParmOffset);
1499 S += "@0:";
1500 S += llvm::utostr(PtrSize);
1501
1502 // Argument types.
1503 ParmOffset = 2 * PtrSize;
1504 for (int i = 0; i < NumOfParams; i++) {
1505 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001506 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001507 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508 getObjCEncodingForTypeQualifier(
1509 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001510 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001511 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001513 }
1514}
1515
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001516/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1517/// method declaration. If non-NULL, Container must be either an
1518/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1519/// NULL when getting encodings for protocol properties.
1520void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1521 const Decl *Container,
1522 std::string& S)
1523{
1524 // Collect information from the property implementation decl(s).
1525 bool Dynamic = false;
1526 ObjCPropertyImplDecl *SynthesizePID = 0;
1527
1528 // FIXME: Duplicated code due to poor abstraction.
1529 if (Container) {
1530 if (const ObjCCategoryImplDecl *CID =
1531 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1532 for (ObjCCategoryImplDecl::propimpl_iterator
1533 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1534 ObjCPropertyImplDecl *PID = *i;
1535 if (PID->getPropertyDecl() == PD) {
1536 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1537 Dynamic = true;
1538 } else {
1539 SynthesizePID = PID;
1540 }
1541 }
1542 }
1543 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001544 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001545 for (ObjCCategoryImplDecl::propimpl_iterator
1546 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1547 ObjCPropertyImplDecl *PID = *i;
1548 if (PID->getPropertyDecl() == PD) {
1549 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1550 Dynamic = true;
1551 } else {
1552 SynthesizePID = PID;
1553 }
1554 }
1555 }
1556 }
1557 }
1558
1559 // FIXME: This is not very efficient.
1560 S = "T";
1561
1562 // Encode result type.
1563 // FIXME: GCC uses a generating_property_type_encoding mode during
1564 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001565 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001566
1567 if (PD->isReadOnly()) {
1568 S += ",R";
1569 } else {
1570 switch (PD->getSetterKind()) {
1571 case ObjCPropertyDecl::Assign: break;
1572 case ObjCPropertyDecl::Copy: S += ",C"; break;
1573 case ObjCPropertyDecl::Retain: S += ",&"; break;
1574 }
1575 }
1576
1577 // It really isn't clear at all what this means, since properties
1578 // are "dynamic by default".
1579 if (Dynamic)
1580 S += ",D";
1581
1582 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1583 S += ",G";
1584 S += PD->getGetterName().getName();
1585 }
1586
1587 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1588 S += ",S";
1589 S += PD->getSetterName().getName();
1590 }
1591
1592 if (SynthesizePID) {
1593 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1594 S += ",V";
1595 S += OID->getName();
1596 }
1597
1598 // FIXME: OBJCGC: weak & strong
1599}
1600
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001601void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001602 bool NameFields) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001603 // We follow the behavior of gcc, expanding structures which are
1604 // directly pointed to, and expanding embedded structures. Note that
1605 // these rules are sufficient to prevent recursive encoding of the
1606 // same type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001607 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001608}
1609
1610void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1611 bool ExpandPointedToStructures,
1612 bool ExpandStructures,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001613 bool NameFields) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001614 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001615 char encoding;
1616 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001617 default: assert(0 && "Unhandled builtin type kind");
1618 case BuiltinType::Void: encoding = 'v'; break;
1619 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001620 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001621 case BuiltinType::UChar: encoding = 'C'; break;
1622 case BuiltinType::UShort: encoding = 'S'; break;
1623 case BuiltinType::UInt: encoding = 'I'; break;
1624 case BuiltinType::ULong: encoding = 'L'; break;
1625 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001626 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001627 case BuiltinType::SChar: encoding = 'c'; break;
1628 case BuiltinType::Short: encoding = 's'; break;
1629 case BuiltinType::Int: encoding = 'i'; break;
1630 case BuiltinType::Long: encoding = 'l'; break;
1631 case BuiltinType::LongLong: encoding = 'q'; break;
1632 case BuiltinType::Float: encoding = 'f'; break;
1633 case BuiltinType::Double: encoding = 'd'; break;
1634 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001635 }
1636
1637 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001638 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001640 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001641 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1642 ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001643 ExpandStructures, NameFields);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001644 }
1645 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001646 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001647 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001648 S += '@';
1649 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001650 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001651 S += '#';
1652 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001654 S += ':';
1655 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001656 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001657
1658 if (PointeeTy->isCharType()) {
1659 // char pointer types should be encoded as '*' unless it is a
1660 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001661 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001662 S += '*';
1663 return;
1664 }
1665 }
1666
1667 S += '^';
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001668 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001669 false, ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001670 NameFields);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001671 } else if (const ArrayType *AT =
1672 // Ignore type qualifiers etc.
1673 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001674 S += '[';
1675
1676 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1677 S += llvm::utostr(CAT->getSize().getZExtValue());
1678 else
1679 assert(0 && "Unhandled array type!");
1680
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001681 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001682 false, ExpandStructures, NameFields);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001683 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001684 } else if (T->getAsFunctionType()) {
1685 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001686 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001687 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001688 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001689 // Anonymous structures print as '?'
1690 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1691 S += II->getName();
1692 } else {
1693 S += '?';
1694 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001695 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001696 S += '=';
1697 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001698 FieldDecl *FD = RDecl->getMember(i);
1699 if (NameFields) {
1700 S += '"';
1701 S += FD->getName();
1702 S += '"';
1703 }
1704
1705 // Special case bit-fields.
1706 if (const Expr *E = FD->getBitWidth()) {
1707 // FIXME: Fix constness.
1708 ASTContext *Ctx = const_cast<ASTContext*>(this);
1709 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1710 // FIXME: Obj-C is losing information about the type size
1711 // here. Investigate if this is a problem.
1712 S += 'b';
1713 S += llvm::utostr(N);
1714 } else {
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001715 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001716 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001717 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001718 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001719 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001720 } else if (T->isEnumeralType()) {
1721 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001722 } else if (T->isBlockPointerType()) {
1723 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001724 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001725 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001726}
1727
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001728void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001729 std::string& S) const {
1730 if (QT & Decl::OBJC_TQ_In)
1731 S += 'n';
1732 if (QT & Decl::OBJC_TQ_Inout)
1733 S += 'N';
1734 if (QT & Decl::OBJC_TQ_Out)
1735 S += 'o';
1736 if (QT & Decl::OBJC_TQ_Bycopy)
1737 S += 'O';
1738 if (QT & Decl::OBJC_TQ_Byref)
1739 S += 'R';
1740 if (QT & Decl::OBJC_TQ_Oneway)
1741 S += 'V';
1742}
1743
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001744void ASTContext::setBuiltinVaListType(QualType T)
1745{
1746 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1747
1748 BuiltinVaListType = T;
1749}
1750
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001751void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001752{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001753 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001754
1755 // typedef struct objc_object *id;
1756 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1757 assert(ptr && "'id' incorrectly typed");
1758 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1759 assert(rec && "'id' incorrectly typed");
1760 IdStructType = rec;
1761}
1762
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001763void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001764{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001765 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001766
1767 // typedef struct objc_selector *SEL;
1768 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1769 assert(ptr && "'SEL' incorrectly typed");
1770 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1771 assert(rec && "'SEL' incorrectly typed");
1772 SelStructType = rec;
1773}
1774
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001775void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001776{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001777 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001778}
1779
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001780void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001781{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001782 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001783
1784 // typedef struct objc_class *Class;
1785 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1786 assert(ptr && "'Class' incorrectly typed");
1787 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1788 assert(rec && "'Class' incorrectly typed");
1789 ClassStructType = rec;
1790}
1791
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001792void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1793 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001794 "'NSConstantString' type already set!");
1795
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001796 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001797}
1798
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001799
1800//===----------------------------------------------------------------------===//
1801// Type Predicates.
1802//===----------------------------------------------------------------------===//
1803
1804/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1805/// to an object type. This includes "id" and "Class" (two 'special' pointers
1806/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1807/// ID type).
1808bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1809 if (Ty->isObjCQualifiedIdType())
1810 return true;
1811
Steve Naroff6ae98502008-10-21 18:24:04 +00001812 // Blocks are objects.
1813 if (Ty->isBlockPointerType())
1814 return true;
1815
1816 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001817 if (!Ty->isPointerType())
1818 return false;
1819
1820 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1821 // pointer types. This looks for the typedef specifically, not for the
1822 // underlying type.
1823 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1824 return true;
1825
1826 // If this a pointer to an interface (e.g. NSString*), it is ok.
1827 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1828}
1829
Chris Lattner6ac46a42008-04-07 06:51:04 +00001830//===----------------------------------------------------------------------===//
1831// Type Compatibility Testing
1832//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001833
Steve Naroff1c7d0672008-09-04 15:10:53 +00001834/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001835/// block types. Types must be strictly compatible here. For example,
1836/// C unfortunately doesn't produce an error for the following:
1837///
1838/// int (*emptyArgFunc)();
1839/// int (*intArgList)(int) = emptyArgFunc;
1840///
1841/// For blocks, we will produce an error for the following (similar to C++):
1842///
1843/// int (^emptyArgBlock)();
1844/// int (^intArgBlock)(int) = emptyArgBlock;
1845///
1846/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1847///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001848bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff3c1b9122008-09-09 13:47:19 +00001849 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff1c7d0672008-09-04 15:10:53 +00001850}
1851
Chris Lattner6ac46a42008-04-07 06:51:04 +00001852/// areCompatVectorTypes - Return true if the two specified vector types are
1853/// compatible.
1854static bool areCompatVectorTypes(const VectorType *LHS,
1855 const VectorType *RHS) {
1856 assert(LHS->isCanonical() && RHS->isCanonical());
1857 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00001858 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00001859}
1860
Eli Friedman3d815e72008-08-22 00:56:42 +00001861/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001862/// compatible for assignment from RHS to LHS. This handles validation of any
1863/// protocol qualifiers on the LHS or RHS.
1864///
Eli Friedman3d815e72008-08-22 00:56:42 +00001865bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1866 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001867 // Verify that the base decls are compatible: the RHS must be a subclass of
1868 // the LHS.
1869 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1870 return false;
1871
1872 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1873 // protocol qualified at all, then we are good.
1874 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1875 return true;
1876
1877 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1878 // isn't a superset.
1879 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1880 return true; // FIXME: should return false!
1881
1882 // Finally, we must have two protocol-qualified interfaces.
1883 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1884 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1885 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1886 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1887 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1888 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1889
1890 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1891 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1892 // LHS in a single parallel scan until we run out of elements in LHS.
1893 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1894 ObjCProtocolDecl *LHSProto = *LHSPI;
1895
1896 while (RHSPI != RHSPE) {
1897 ObjCProtocolDecl *RHSProto = *RHSPI++;
1898 // If the RHS has a protocol that the LHS doesn't, ignore it.
1899 if (RHSProto != LHSProto)
1900 continue;
1901
1902 // Otherwise, the RHS does have this element.
1903 ++LHSPI;
1904 if (LHSPI == LHSPE)
1905 return true; // All protocols in LHS exist in RHS.
1906
1907 LHSProto = *LHSPI;
1908 }
1909
1910 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1911 return false;
1912}
1913
Steve Naroffec0550f2007-10-15 20:41:53 +00001914/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1915/// both shall have the identically qualified version of a compatible type.
1916/// C99 6.2.7p1: Two types have compatible types if their types are the
1917/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001918bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1919 return !mergeTypes(LHS, RHS).isNull();
1920}
1921
1922QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1923 const FunctionType *lbase = lhs->getAsFunctionType();
1924 const FunctionType *rbase = rhs->getAsFunctionType();
1925 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1926 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1927 bool allLTypes = true;
1928 bool allRTypes = true;
1929
1930 // Check return type
1931 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1932 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00001933 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
1934 allLTypes = false;
1935 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
1936 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00001937
1938 if (lproto && rproto) { // two C99 style function prototypes
1939 unsigned lproto_nargs = lproto->getNumArgs();
1940 unsigned rproto_nargs = rproto->getNumArgs();
1941
1942 // Compatible functions must have the same number of arguments
1943 if (lproto_nargs != rproto_nargs)
1944 return QualType();
1945
1946 // Variadic and non-variadic functions aren't compatible
1947 if (lproto->isVariadic() != rproto->isVariadic())
1948 return QualType();
1949
1950 // Check argument compatibility
1951 llvm::SmallVector<QualType, 10> types;
1952 for (unsigned i = 0; i < lproto_nargs; i++) {
1953 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1954 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1955 QualType argtype = mergeTypes(largtype, rargtype);
1956 if (argtype.isNull()) return QualType();
1957 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00001958 if (getCanonicalType(argtype) != getCanonicalType(largtype))
1959 allLTypes = false;
1960 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
1961 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00001962 }
1963 if (allLTypes) return lhs;
1964 if (allRTypes) return rhs;
1965 return getFunctionType(retType, types.begin(), types.size(),
1966 lproto->isVariadic());
1967 }
1968
1969 if (lproto) allRTypes = false;
1970 if (rproto) allLTypes = false;
1971
1972 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1973 if (proto) {
1974 if (proto->isVariadic()) return QualType();
1975 // Check that the types are compatible with the types that
1976 // would result from default argument promotions (C99 6.7.5.3p15).
1977 // The only types actually affected are promotable integer
1978 // types and floats, which would be passed as a different
1979 // type depending on whether the prototype is visible.
1980 unsigned proto_nargs = proto->getNumArgs();
1981 for (unsigned i = 0; i < proto_nargs; ++i) {
1982 QualType argTy = proto->getArgType(i);
1983 if (argTy->isPromotableIntegerType() ||
1984 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1985 return QualType();
1986 }
1987
1988 if (allLTypes) return lhs;
1989 if (allRTypes) return rhs;
1990 return getFunctionType(retType, proto->arg_type_begin(),
1991 proto->getNumArgs(), lproto->isVariadic());
1992 }
1993
1994 if (allLTypes) return lhs;
1995 if (allRTypes) return rhs;
1996 return getFunctionTypeNoProto(retType);
1997}
1998
1999QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002000 // C++ [expr]: If an expression initially has the type "reference to T", the
2001 // type is adjusted to "T" prior to any further analysis, the expression
2002 // designates the object or function denoted by the reference, and the
2003 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002004 // FIXME: C++ shouldn't be going through here! The rules are different
2005 // enough that they should be handled separately.
2006 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002007 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002008 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002009 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002010
Eli Friedman3d815e72008-08-22 00:56:42 +00002011 QualType LHSCan = getCanonicalType(LHS),
2012 RHSCan = getCanonicalType(RHS);
2013
2014 // If two types are identical, they are compatible.
2015 if (LHSCan == RHSCan)
2016 return LHS;
2017
2018 // If the qualifiers are different, the types aren't compatible
2019 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2020 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2021 return QualType();
2022
2023 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2024 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2025
Chris Lattner1adb8832008-01-14 05:45:46 +00002026 // We want to consider the two function types to be the same for these
2027 // comparisons, just force one to the other.
2028 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2029 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002030
2031 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002032 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2033 LHSClass = Type::ConstantArray;
2034 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2035 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002036
Nate Begeman213541a2008-04-18 23:10:10 +00002037 // Canonicalize ExtVector -> Vector.
2038 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2039 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002040
Chris Lattnerb0489812008-04-07 06:38:24 +00002041 // Consider qualified interfaces and interfaces the same.
2042 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2043 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002044
Chris Lattnera36a61f2008-04-07 05:43:21 +00002045 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002046 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00002047 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00002048 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff87f3b932008-10-20 18:19:10 +00002049 if (const PointerType *PT = RHS->getAsPointerType()) {
2050 QualType pType = PT->getPointeeType();
2051 if (isObjCIdType(pType))
Eli Friedman3d815e72008-08-22 00:56:42 +00002052 return LHS;
Steve Naroff87f3b932008-10-20 18:19:10 +00002053 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2054 // Unfortunately, this API is part of Sema (which we don't have access
2055 // to. Need to refactor. The following check is insufficient, since we
2056 // need to make sure the class implements the protocol.
2057 if (pType->isObjCInterfaceType())
2058 return LHS;
2059 }
Steve Naroff97341622008-06-04 15:07:33 +00002060 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002061 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff87f3b932008-10-20 18:19:10 +00002062 if (const PointerType *PT = LHS->getAsPointerType()) {
2063 QualType pType = PT->getPointeeType();
2064 if (isObjCIdType(pType))
Eli Friedman3d815e72008-08-22 00:56:42 +00002065 return RHS;
Steve Naroff87f3b932008-10-20 18:19:10 +00002066 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2067 // Unfortunately, this API is part of Sema (which we don't have access
2068 // to. Need to refactor. The following check is insufficient, since we
2069 // need to make sure the class implements the protocol.
2070 if (pType->isObjCInterfaceType())
2071 return RHS;
2072 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002073 }
2074
Chris Lattner1adb8832008-01-14 05:45:46 +00002075 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2076 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002077 if (const EnumType* ETy = LHS->getAsEnumType()) {
2078 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2079 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002080 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002081 if (const EnumType* ETy = RHS->getAsEnumType()) {
2082 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2083 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002084 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002085
Eli Friedman3d815e72008-08-22 00:56:42 +00002086 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002087 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002088
Steve Naroff4a746782008-01-09 22:43:08 +00002089 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002090 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002091 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002092 {
2093 // Merge two pointer types, while trying to preserve typedef info
2094 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2095 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2096 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2097 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002098 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2099 return LHS;
2100 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2101 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002102 return getPointerType(ResultType);
2103 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002104 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002105 {
2106 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2107 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2108 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2109 return QualType();
2110
2111 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2112 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2113 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2114 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002115 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2116 return LHS;
2117 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2118 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002119 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2120 ArrayType::ArraySizeModifier(), 0);
2121 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2122 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002123 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2124 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002125 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2126 return LHS;
2127 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2128 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002129 if (LVAT) {
2130 // FIXME: This isn't correct! But tricky to implement because
2131 // the array's size has to be the size of LHS, but the type
2132 // has to be different.
2133 return LHS;
2134 }
2135 if (RVAT) {
2136 // FIXME: This isn't correct! But tricky to implement because
2137 // the array's size has to be the size of RHS, but the type
2138 // has to be different.
2139 return RHS;
2140 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002141 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2142 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002143 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002144 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002145 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002146 return mergeFunctionTypes(LHS, RHS);
2147 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002148 // FIXME: Why are these compatible?
2149 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2150 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2151 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002152 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002153 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002154 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002155 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002156 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2157 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002158 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002159 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002160 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2161 // for checking assignment/comparison safety
2162 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002163 default:
2164 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002165 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002166 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002167}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002168
Chris Lattner5426bf62008-04-07 07:01:58 +00002169//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002170// Integer Predicates
2171//===----------------------------------------------------------------------===//
2172unsigned ASTContext::getIntWidth(QualType T) {
2173 if (T == BoolTy)
2174 return 1;
2175 // At the moment, only bool has padding bits
2176 return (unsigned)getTypeSize(T);
2177}
2178
2179QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2180 assert(T->isSignedIntegerType() && "Unexpected type");
2181 if (const EnumType* ETy = T->getAsEnumType())
2182 T = ETy->getDecl()->getIntegerType();
2183 const BuiltinType* BTy = T->getAsBuiltinType();
2184 assert (BTy && "Unexpected signed integer type");
2185 switch (BTy->getKind()) {
2186 case BuiltinType::Char_S:
2187 case BuiltinType::SChar:
2188 return UnsignedCharTy;
2189 case BuiltinType::Short:
2190 return UnsignedShortTy;
2191 case BuiltinType::Int:
2192 return UnsignedIntTy;
2193 case BuiltinType::Long:
2194 return UnsignedLongTy;
2195 case BuiltinType::LongLong:
2196 return UnsignedLongLongTy;
2197 default:
2198 assert(0 && "Unexpected signed integer type");
2199 return QualType();
2200 }
2201}
2202
2203
2204//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002205// Serialization Support
2206//===----------------------------------------------------------------------===//
2207
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002208/// Emit - Serialize an ASTContext object to Bitcode.
2209void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002210 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002211 S.EmitRef(SourceMgr);
2212 S.EmitRef(Target);
2213 S.EmitRef(Idents);
2214 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002215
Ted Kremenekfee04522007-10-31 22:44:07 +00002216 // Emit the size of the type vector so that we can reserve that size
2217 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002218 S.EmitInt(Types.size());
2219
Ted Kremenek03ed4402007-11-13 22:02:55 +00002220 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2221 I!=E;++I)
2222 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002223
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002224 S.EmitOwnedPtr(TUDecl);
2225
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002226 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002227}
2228
Ted Kremenek0f84c002007-11-13 00:25:37 +00002229ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002230
2231 // Read the language options.
2232 LangOptions LOpts;
2233 LOpts.Read(D);
2234
Ted Kremenekfee04522007-10-31 22:44:07 +00002235 SourceManager &SM = D.ReadRef<SourceManager>();
2236 TargetInfo &t = D.ReadRef<TargetInfo>();
2237 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2238 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002239
Ted Kremenekfee04522007-10-31 22:44:07 +00002240 unsigned size_reserve = D.ReadInt();
2241
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002242 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002243
Ted Kremenek03ed4402007-11-13 22:02:55 +00002244 for (unsigned i = 0; i < size_reserve; ++i)
2245 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002246
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002247 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2248
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002249 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002250
2251 return A;
2252}