blob: 0e250281a024304fd2556c49750392c21beb2570 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Chris Lattner2fda0ed2008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Daniel Dunbarde300732008-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
Chris Lattner4b009652007-07-25 00:24:17 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000048 Types.pop_back();
49 }
Eli Friedman65489b72008-05-27 03:08:09 +000050
51 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +000058 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000059 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
60
61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000062 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
63 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000064 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +000072 else if (isa<BlockPointerType>(T))
73 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +000074 else if (isa<ReferenceType>(T))
75 ++NumReference;
76 else if (isa<ComplexType>(T))
77 ++NumComplex;
78 else if (isa<ArrayType>(T))
79 ++NumArray;
80 else if (isa<VectorType>(T))
81 ++NumVector;
82 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;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000090 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000091 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-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;
Chris Lattner4b009652007-07-25 00:24:17 +000096 }
Ted Kremenek42730c52008-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 Naroffe0430632008-05-21 15:59:22 +0000103 else if (isa<TypeOfType>(T))
104 ++NumTypeOfTypes;
105 else if (isa<TypeOfExpr>(T))
106 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000107 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000108 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000115 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000116 fprintf(stderr, " %d reference types\n", NumReference);
117 fprintf(stderr, " %d complex types\n", NumComplex);
118 fprintf(stderr, " %d array types\n", NumArray);
119 fprintf(stderr, " %d vector types\n", NumVector);
120 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 Kremenek42730c52008-01-07 19:49:32 +0000128 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000129 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000130 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000131 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000132 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000133 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
134 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
135
Chris Lattner4b009652007-07-25 00:24:17 +0000136 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
137 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
138 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
139 NumFunctionP*sizeof(FunctionTypeProto)+
140 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000141 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
142 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000143}
144
145
146void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
147 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
148}
149
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000159 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +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);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000181
182 // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184
Douglas Gregord2baafd2008-10-21 16:13:35 +0000185 // Placeholder type for functions.
186 InitBuiltinType(OverloadTy, BuiltinType::Overload);
187
Chris Lattner4b009652007-07-25 00:24:17 +0000188 // C99 6.2.5p11.
189 FloatComplexTy = getComplexType(FloatTy);
190 DoubleComplexTy = getComplexType(DoubleTy);
191 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000192
Steve Naroff9d12c902007-10-15 14:41:52 +0000193 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000195 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000197 ClassStructType = 0;
198
Ted Kremenek42730c52008-01-07 19:49:32 +0000199 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000200
201 // void * type
202 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000203}
204
205//===----------------------------------------------------------------------===//
206// Type Sizing and Analysis
207//===----------------------------------------------------------------------===//
208
Chris Lattner2a674dc2008-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 Lattner4b009652007-07-25 00:24:17 +0000223/// getTypeSize - Return the size of the specified type, in bits. This method
224/// does not work on incomplete types.
225std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000226ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000227 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000228 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000229 unsigned Align;
230 switch (T->getTypeClass()) {
231 case Type::TypeName: assert(0 && "Not a canonical type!");
232 case Type::FunctionNoProto:
233 case Type::FunctionProto:
234 default:
235 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-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 Lattner8cd0e932008-03-05 18:54:05 +0000241 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000242 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000243 Align = EltInfo.second;
244 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000245 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000246 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000247 case Type::Vector: {
248 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000249 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000250 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000251 // FIXME: This isn't right for unusual vectors
252 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000253 break;
254 }
255
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000256 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000257 switch (cast<BuiltinType>(T)->getKind()) {
258 default: assert(0 && "Unknown builtin type!");
259 case BuiltinType::Void:
260 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000262 Width = Target.getBoolWidth();
263 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000264 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000265 case BuiltinType::Char_S:
266 case BuiltinType::Char_U:
267 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000268 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000269 Width = Target.getCharWidth();
270 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000271 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000272 case BuiltinType::WChar:
273 Width = Target.getWCharWidth();
274 Align = Target.getWCharAlign();
275 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000276 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000277 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000278 Width = Target.getShortWidth();
279 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000280 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000281 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000282 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000283 Width = Target.getIntWidth();
284 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000285 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000286 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000287 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000288 Width = Target.getLongWidth();
289 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000290 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000291 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000292 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000293 Width = Target.getLongLongWidth();
294 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000295 break;
296 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000297 Width = Target.getFloatWidth();
298 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000299 break;
300 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000301 Width = Target.getDoubleWidth();
302 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000303 break;
304 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000305 Width = Target.getLongDoubleWidth();
306 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000307 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000308 }
309 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000310 case Type::ASQual:
Chris Lattner8cd0e932008-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 Kremenek42730c52008-01-07 19:49:32 +0000314 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000315 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000316 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000317 break;
Steve Naroff62f09f52008-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 Lattner461a6c52008-03-08 08:34:58 +0000324 case Type::Pointer: {
325 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000326 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000327 Align = Target.getPointerAlign(AS);
328 break;
329 }
Chris Lattner4b009652007-07-25 00:24:17 +0000330 case Type::Reference:
331 // "When applied to a reference or a reference type, the result is the size
332 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000333 // FIXME: This is wrong for struct layout: a reference in a struct has
334 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000335 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000341 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000342 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000343 Align = EltInfo.second;
344 break;
345 }
Devang Patel4b6bf702008-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 Lattner2bf1d6c2008-04-06 22:05:18 +0000353 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000354 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
355 Width = 1;
356 Align = 1;
357 break;
358 }
359
Chris Lattner2bf1d6c2008-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 Lattner4b009652007-07-25 00:24:17 +0000367 break;
368 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000369 }
Chris Lattner4b009652007-07-25 00:24:17 +0000370
371 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000372 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000373}
374
Devang Patelbfe323c2008-06-04 21:22:16 +0000375/// LayoutField - Field layout.
376void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000377 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000378 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000379 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000380 uint64_t FieldOffset = IsUnion ? 0 : Size;
381 uint64_t FieldSize;
382 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar7cbcbf42008-08-13 23:47:13 +0000392 FieldSize =
393 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-06-04 21:22:16 +0000404 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000405 if (FieldPacking)
406 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-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 Lattnerfd799692008-08-09 21:35:13 +0000419 if (FD->getType()->isIncompleteArrayType()) {
420 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-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 Lattnera1923f62008-08-04 07:31:14 +0000425 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-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 Patel4b6bf702008-06-04 21:54:36 +0000464
Chris Lattner2fda0ed2008-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 Patel4b6bf702008-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 Patel8682d882008-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 Lattner2fda0ed2008-10-05 17:34:18 +0000485 // Super class is at the beginning of the layout.
486 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000487 } else {
488 NewEntry = new ASTRecordLayout();
489 NewEntry->InitializeLayout(FieldCount);
490 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000491 Entry = NewEntry;
492
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000493 unsigned StructPacking = 0;
494 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
495 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-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 Dunbar2cb762f2008-10-16 02:34:03 +0000506 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-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 Patel7a78e432007-11-01 19:11:01 +0000515/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000516/// specified record (struct/union/class), which indicates its size and field
517/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000518const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000519 D = D->getDefinition(*this);
520 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000521
Chris Lattner4b009652007-07-25 00:24:17 +0000522 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000523 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000524 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000525
Devang Patel7a78e432007-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 Lattner4b009652007-07-25 00:24:17 +0000529 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000530
Devang Patelbfe323c2008-06-04 21:22:16 +0000531 NewEntry->InitializeLayout(D->getNumMembers());
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000532 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000533
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000534 unsigned StructPacking = 0;
535 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
536 StructPacking = PA->getAlignment();
537
Eli Friedman5949a022008-05-30 09:31:38 +0000538 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000539 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
540 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000541
Eli Friedman5949a022008-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 Dunbar2cb762f2008-10-16 02:34:03 +0000546 NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000547 }
Eli Friedman5949a022008-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 Patelbfe323c2008-06-04 21:22:16 +0000551 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000552 return *NewEntry;
553}
554
Chris Lattner4b009652007-07-25 00:24:17 +0000555//===----------------------------------------------------------------------===//
556// Type creation/memoization methods
557//===----------------------------------------------------------------------===//
558
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000559QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000560 QualType CanT = getCanonicalType(T);
561 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-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 Lattnerc1b68db2008-04-06 22:59:24 +0000566 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-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 Lamb2a72bb32008-02-04 02:31:56 +0000571 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000572 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-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 Lattnerc1b68db2008-04-06 22:59:24 +0000581 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-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 Lattner578a37e2008-10-12 00:26:57 +0000585 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000586 }
Chris Lattner35fef522008-02-20 20:55:12 +0000587 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000588 ASQualTypes.InsertNode(New, InsertPos);
589 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000590 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000591}
592
Chris Lattner4b009652007-07-25 00:24:17 +0000593
594/// 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 Lattnerc1b68db2008-04-06 22:59:24 +0000610 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000611
612 // Get the new insert position for the node we care about.
613 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000614 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000639 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000640
641 // Get the new insert position for the node we care about.
642 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000643 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff7aa54752008-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 Narofffd5b19d2008-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 Naroff7aa54752008-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 Narofffd5b19d2008-08-28 19:20:44 +0000665 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-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 Lattner578a37e2008-10-12 00:26:57 +0000674 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000698 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000699
700 // Get the new insert position for the node we care about.
701 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000702 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff83c13012007-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 Naroff24c9b982007-08-30 18:10:14 +0000714 const llvm::APInt &ArySize,
715 ArrayType::ArraySizeModifier ASM,
716 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000717 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000718 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000719
720 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000721 if (ConstantArrayType *ATP =
722 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000729 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000730 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000731 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000732 ConstantArrayType *NewIP =
733 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000734 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000735 }
736
Steve Naroff24c9b982007-08-30 18:10:14 +0000737 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
738 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000739 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000740 Types.push_back(New);
741 return QualType(New, 0);
742}
743
Steve Naroffe2579e32007-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 Naroff24c9b982007-08-30 18:10:14 +0000746QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
747 ArrayType::ArraySizeModifier ASM,
748 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-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 Lattnerc1b68db2008-04-06 22:59:24 +0000776 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000777 ASM, EltTypeQuals);
Eli Friedman8ff07782008-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 Lattner578a37e2008-10-12 00:26:57 +0000782 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000783 }
Eli Friedman8ff07782008-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 Naroff83c13012007-08-30 01:06:46 +0000791}
792
Chris Lattner4b009652007-07-25 00:24:17 +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) {
796 BuiltinType *baseType;
797
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000798 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000799 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
800
801 // Check if we've already instantiated a vector of this type.
802 llvm::FoldingSetNodeID ID;
803 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
804 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 Lattnerc1b68db2008-04-06 22:59:24 +0000812 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000813
814 // Get the new insert position for the node we care about.
815 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000816 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +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 Begemanaf6ed502008-04-18 23:10:10 +0000824/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000825/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000826QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000827 BuiltinType *baseType;
828
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000829 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000830 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000831
832 // Check if we've already instantiated a vector of this type.
833 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000834 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +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 Begemanaf6ed502008-04-18 23:10:10 +0000843 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000844
845 // Get the new insert position for the node we care about.
846 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000847 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000848 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000849 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000850 VectorTypes.InsertNode(New, InsertPos);
851 Types.push_back(New);
852 return QualType(New, 0);
853}
854
855/// 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 Lattnerc1b68db2008-04-06 22:59:24 +0000870 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000871
872 // Get the new insert position for the node we care about.
873 FunctionTypeNoProto *NewIP =
874 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000875 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000876 }
877
878 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
879 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000880 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner2fda0ed2008-10-05 17:34:18 +0000886QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000887 unsigned NumArgs, bool isVariadic,
888 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000889 // Unique functions, to guarantee there is only one function of a particular
890 // structure.
891 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000892 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
893 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000912 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000913
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000914 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000915 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000916 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000917
918 // Get the new insert position for the node we care about.
919 FunctionTypeProto *NewIP =
920 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000921 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +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) +
928 NumArgs*sizeof(QualType));
929 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000930 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000931 Types.push_back(FTP);
932 FunctionTypeProtos.InsertNode(FTP, InsertPos);
933 return QualType(FTP, 0);
934}
935
Douglas Gregor1d661552008-04-13 21:07:44 +0000936/// getTypeDeclType - Return the unique reference to the type for the
937/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000938QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000939 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +0000940 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
941
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000942 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000943 return getTypedefType(Typedef);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000944 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000945 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000946
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000947 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000948 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
949 : new CXXRecordType(CXXRecord);
950 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000951 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000952 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
953 : new RecordType(Record);
954 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000955 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000956 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000957 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000958 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000959
Ted Kremenek46a837c2008-09-05 17:16:31 +0000960 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000961 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000962}
963
Ted Kremenek46a837c2008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000977 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000978 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000979 Types.push_back(Decl->TypeForDecl);
980 return QualType(Decl->TypeForDecl, 0);
981}
982
Ted Kremenek42730c52008-01-07 19:49:32 +0000983/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000984/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000985QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000986 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
987
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000989 Types.push_back(Decl->TypeForDecl);
990 return QualType(Decl->TypeForDecl, 0);
991}
992
Chris Lattnere1352302008-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 Lattnerb0c6a1f2008-04-07 04:44:08 +00001013/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1014/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001015QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1016 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001017 // Sort the protocol list alphabetically to canonicalize it.
1018 SortAndUniqueProtocols(Protocols, NumProtocols);
1019
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001020 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001021 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001022
1023 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001024 if (ObjCQualifiedInterfaceType *QT =
1025 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001026 return QualType(QT, 0);
1027
1028 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 ObjCQualifiedInterfaceType *QType =
1030 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001031 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001032 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001033 return QualType(QType, 0);
1034}
1035
Chris Lattnere1352302008-04-07 04:56:42 +00001036/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1037/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001038QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001039 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001040 // Sort the protocol list alphabetically to canonicalize it.
1041 SortAndUniqueProtocols(Protocols, NumProtocols);
1042
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001043 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001044 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001045
1046 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001047 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001048 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001049 return QualType(QT, 0);
1050
1051 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001052 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001053 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001054 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001055 return QualType(QType, 0);
1056}
1057
Steve Naroff0604dd92007-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 Naroff11b649c2007-08-01 17:20:42 +00001063QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001064 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001065 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1066 Types.push_back(toe);
1067 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001068}
1069
Steve Naroff0604dd92007-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 Naroff7cbb1462007-07-31 12:34:36 +00001075QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001076 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001077 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1078 Types.push_back(tot);
1079 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001080}
1081
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenekae8fa032007-11-26 21:16:01 +00001085 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001086 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +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 {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001093 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001094}
1095
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001096/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001097/// width of characters in wide strings, The value is target dependent and
1098/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001099QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001100 if (LangOpts.CPlusPlus)
1101 return WCharTy;
1102
Douglas Gregorc6507e42008-11-03 14:12:49 +00001103 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1104 // wide-character type?
1105 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001106}
1107
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001108/// getSignedWCharType - Return the type of "signed wchar_t".
1109/// Used when in C++, as a GCC extension.
1110QualType ASTContext::getSignedWCharType() const {
1111 // FIXME: derive from "Target" ?
1112 return WCharTy;
1113}
1114
1115/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1116/// Used when in C++, as a GCC extension.
1117QualType ASTContext::getUnsignedWCharType() const {
1118 // FIXME: derive from "Target" ?
1119 return UnsignedIntTy;
1120}
1121
Chris Lattner4b009652007-07-25 00:24:17 +00001122/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1123/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1124QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001125 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001126}
1127
Chris Lattner19eb97e2008-04-02 05:18:44 +00001128//===----------------------------------------------------------------------===//
1129// Type Operators
1130//===----------------------------------------------------------------------===//
1131
Chris Lattner3dae6f42008-04-06 22:41:35 +00001132/// getCanonicalType - Return the canonical (structural) type corresponding to
1133/// the specified potentially non-canonical type. The non-canonical version
1134/// of a type may have many "decorated" versions of types. Decorators can
1135/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1136/// to be free of any of these, allowing two canonical types to be compared
1137/// for exact equality with a simple pointer comparison.
1138QualType ASTContext::getCanonicalType(QualType T) {
1139 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001140
1141 // If the result has type qualifiers, make sure to canonicalize them as well.
1142 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1143 if (TypeQuals == 0) return CanType;
1144
1145 // If the type qualifiers are on an array type, get the canonical type of the
1146 // array with the qualifiers applied to the element type.
1147 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1148 if (!AT)
1149 return CanType.getQualifiedType(TypeQuals);
1150
1151 // Get the canonical version of the element with the extra qualifiers on it.
1152 // This can recursively sink qualifiers through multiple levels of arrays.
1153 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1154 NewEltTy = getCanonicalType(NewEltTy);
1155
1156 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1157 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1158 CAT->getIndexTypeQualifier());
1159 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1160 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1161 IAT->getIndexTypeQualifier());
1162
1163 // FIXME: What is the ownership of size expressions in VLAs?
1164 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1165 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1166 VAT->getSizeModifier(),
1167 VAT->getIndexTypeQualifier());
1168}
1169
1170
1171const ArrayType *ASTContext::getAsArrayType(QualType T) {
1172 // Handle the non-qualified case efficiently.
1173 if (T.getCVRQualifiers() == 0) {
1174 // Handle the common positive case fast.
1175 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1176 return AT;
1177 }
1178
1179 // Handle the common negative case fast, ignoring CVR qualifiers.
1180 QualType CType = T->getCanonicalTypeInternal();
1181
1182 // Make sure to look through type qualifiers (like ASQuals) for the negative
1183 // test.
1184 if (!isa<ArrayType>(CType) &&
1185 !isa<ArrayType>(CType.getUnqualifiedType()))
1186 return 0;
1187
1188 // Apply any CVR qualifiers from the array type to the element type. This
1189 // implements C99 6.7.3p8: "If the specification of an array type includes
1190 // any type qualifiers, the element type is so qualified, not the array type."
1191
1192 // If we get here, we either have type qualifiers on the type, or we have
1193 // sugar such as a typedef in the way. If we have type qualifiers on the type
1194 // we must propagate them down into the elemeng type.
1195 unsigned CVRQuals = T.getCVRQualifiers();
1196 unsigned AddrSpace = 0;
1197 Type *Ty = T.getTypePtr();
1198
1199 // Rip through ASQualType's and typedefs to get to a concrete type.
1200 while (1) {
1201 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1202 AddrSpace = ASQT->getAddressSpace();
1203 Ty = ASQT->getBaseType();
1204 } else {
1205 T = Ty->getDesugaredType();
1206 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1207 break;
1208 CVRQuals |= T.getCVRQualifiers();
1209 Ty = T.getTypePtr();
1210 }
1211 }
1212
1213 // If we have a simple case, just return now.
1214 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1215 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1216 return ATy;
1217
1218 // Otherwise, we have an array and we have qualifiers on it. Push the
1219 // qualifiers into the array element type and return a new array type.
1220 // Get the canonical version of the element with the extra qualifiers on it.
1221 // This can recursively sink qualifiers through multiple levels of arrays.
1222 QualType NewEltTy = ATy->getElementType();
1223 if (AddrSpace)
1224 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1225 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1226
1227 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1228 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1229 CAT->getSizeModifier(),
1230 CAT->getIndexTypeQualifier()));
1231 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1232 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1233 IAT->getSizeModifier(),
1234 IAT->getIndexTypeQualifier()));
1235
1236 // FIXME: What is the ownership of size expressions in VLAs?
1237 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1238 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1239 VAT->getSizeModifier(),
1240 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001241}
1242
1243
Chris Lattner19eb97e2008-04-02 05:18:44 +00001244/// getArrayDecayedType - Return the properly qualified result of decaying the
1245/// specified array type to a pointer. This operation is non-trivial when
1246/// handling typedefs etc. The canonical type of "T" must be an array type,
1247/// this returns a pointer to a properly qualified element of the array.
1248///
1249/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1250QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001251 // Get the element type with 'getAsArrayType' so that we don't lose any
1252 // typedefs in the element type of the array. This also handles propagation
1253 // of type qualifiers from the array type into the element type if present
1254 // (C99 6.7.3p8).
1255 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1256 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001257
Chris Lattnera1923f62008-08-04 07:31:14 +00001258 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001259
1260 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001261 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001262}
1263
Chris Lattner4b009652007-07-25 00:24:17 +00001264/// getFloatingRank - Return a relative rank for floating point types.
1265/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001266static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001267 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001268 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001269
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001270 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001271 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001272 case BuiltinType::Float: return FloatRank;
1273 case BuiltinType::Double: return DoubleRank;
1274 case BuiltinType::LongDouble: return LongDoubleRank;
1275 }
1276}
1277
Steve Narofffa0c4532007-08-27 01:41:48 +00001278/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1279/// point or a complex type (based on typeDomain/typeSize).
1280/// 'typeDomain' is a real floating point or complex type.
1281/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001282QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1283 QualType Domain) const {
1284 FloatingRank EltRank = getFloatingRank(Size);
1285 if (Domain->isComplexType()) {
1286 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001287 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001288 case FloatRank: return FloatComplexTy;
1289 case DoubleRank: return DoubleComplexTy;
1290 case LongDoubleRank: return LongDoubleComplexTy;
1291 }
Chris Lattner4b009652007-07-25 00:24:17 +00001292 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001293
1294 assert(Domain->isRealFloatingType() && "Unknown domain!");
1295 switch (EltRank) {
1296 default: assert(0 && "getFloatingRank(): illegal value for rank");
1297 case FloatRank: return FloatTy;
1298 case DoubleRank: return DoubleTy;
1299 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001300 }
Chris Lattner4b009652007-07-25 00:24:17 +00001301}
1302
Chris Lattner51285d82008-04-06 23:55:33 +00001303/// getFloatingTypeOrder - Compare the rank of the two specified floating
1304/// point types, ignoring the domain of the type (i.e. 'double' ==
1305/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1306/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001307int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1308 FloatingRank LHSR = getFloatingRank(LHS);
1309 FloatingRank RHSR = getFloatingRank(RHS);
1310
1311 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001312 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001313 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001314 return 1;
1315 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001316}
1317
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001318/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1319/// routine will assert if passed a built-in type that isn't an integer or enum,
1320/// or if it is not canonicalized.
1321static unsigned getIntegerRank(Type *T) {
1322 assert(T->isCanonical() && "T should be canonicalized");
1323 if (isa<EnumType>(T))
1324 return 4;
1325
1326 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001327 default: assert(0 && "getIntegerRank(): not a built-in integer");
1328 case BuiltinType::Bool:
1329 return 1;
1330 case BuiltinType::Char_S:
1331 case BuiltinType::Char_U:
1332 case BuiltinType::SChar:
1333 case BuiltinType::UChar:
1334 return 2;
1335 case BuiltinType::Short:
1336 case BuiltinType::UShort:
1337 return 3;
1338 case BuiltinType::Int:
1339 case BuiltinType::UInt:
1340 return 4;
1341 case BuiltinType::Long:
1342 case BuiltinType::ULong:
1343 return 5;
1344 case BuiltinType::LongLong:
1345 case BuiltinType::ULongLong:
1346 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001347 }
1348}
1349
Chris Lattner51285d82008-04-06 23:55:33 +00001350/// getIntegerTypeOrder - Returns the highest ranked integer type:
1351/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1352/// LHS < RHS, return -1.
1353int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001354 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1355 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001356 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001357
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001358 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1359 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001360
Chris Lattner51285d82008-04-06 23:55:33 +00001361 unsigned LHSRank = getIntegerRank(LHSC);
1362 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001363
Chris Lattner51285d82008-04-06 23:55:33 +00001364 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1365 if (LHSRank == RHSRank) return 0;
1366 return LHSRank > RHSRank ? 1 : -1;
1367 }
Chris Lattner4b009652007-07-25 00:24:17 +00001368
Chris Lattner51285d82008-04-06 23:55:33 +00001369 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1370 if (LHSUnsigned) {
1371 // If the unsigned [LHS] type is larger, return it.
1372 if (LHSRank >= RHSRank)
1373 return 1;
1374
1375 // If the signed type can represent all values of the unsigned type, it
1376 // wins. Because we are dealing with 2's complement and types that are
1377 // powers of two larger than each other, this is always safe.
1378 return -1;
1379 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001380
Chris Lattner51285d82008-04-06 23:55:33 +00001381 // If the unsigned [RHS] type is larger, return it.
1382 if (RHSRank >= LHSRank)
1383 return -1;
1384
1385 // If the signed type can represent all values of the unsigned type, it
1386 // wins. Because we are dealing with 2's complement and types that are
1387 // powers of two larger than each other, this is always safe.
1388 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001389}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001390
1391// getCFConstantStringType - Return the type used for constant CFStrings.
1392QualType ASTContext::getCFConstantStringType() {
1393 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001394 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001395 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001396 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001397 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001398
1399 // const int *isa;
1400 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001401 // int flags;
1402 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001403 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001404 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001405 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001406 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001407 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001408 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001409
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001410 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001411 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001412 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001413
Ted Kremenek46a837c2008-09-05 17:16:31 +00001414 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001415 }
1416
1417 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001418}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001419
Anders Carlssonf58cac72008-08-30 19:34:46 +00001420QualType ASTContext::getObjCFastEnumerationStateType()
1421{
1422 if (!ObjCFastEnumerationStateTypeDecl) {
1423 QualType FieldTypes[] = {
1424 UnsignedLongTy,
1425 getPointerType(ObjCIdType),
1426 getPointerType(UnsignedLongTy),
1427 getConstantArrayType(UnsignedLongTy,
1428 llvm::APInt(32, 5), ArrayType::Normal, 0)
1429 };
1430
1431 FieldDecl *FieldDecls[4];
1432 for (size_t i = 0; i < 4; ++i)
1433 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1434 FieldTypes[i]);
1435
1436 ObjCFastEnumerationStateTypeDecl =
1437 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001438 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonf58cac72008-08-30 19:34:46 +00001439
Ted Kremenek46a837c2008-09-05 17:16:31 +00001440 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001441 }
1442
1443 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1444}
1445
Anders Carlssone3f02572007-10-29 06:33:42 +00001446// This returns true if a type has been typedefed to BOOL:
1447// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001448static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001449 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001450 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001451
1452 return false;
1453}
1454
Ted Kremenek42730c52008-01-07 19:49:32 +00001455/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001456/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001457int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001458 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001459
1460 // Make all integer and enum types at least as large as an int
1461 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001462 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001463 // Treat arrays as pointers, since that's how they're passed in.
1464 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001465 sz = getTypeSize(VoidPtrTy);
1466 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001467}
1468
Ted Kremenek42730c52008-01-07 19:49:32 +00001469/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001470/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001471void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001472 std::string& S)
1473{
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001474 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001475 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001476 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001477 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001478 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001479 // Compute size of all parameters.
1480 // Start with computing size of a pointer in number of bytes.
1481 // FIXME: There might(should) be a better way of doing this computation!
1482 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001483 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001484 // The first two arguments (self and _cmd) are pointers; account for
1485 // their size.
1486 int ParmOffset = 2 * PtrSize;
1487 int NumOfParams = Decl->getNumParams();
1488 for (int i = 0; i < NumOfParams; i++) {
1489 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001490 int sz = getObjCEncodingTypeSize (PType);
1491 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001492 ParmOffset += sz;
1493 }
1494 S += llvm::utostr(ParmOffset);
1495 S += "@0:";
1496 S += llvm::utostr(PtrSize);
1497
1498 // Argument types.
1499 ParmOffset = 2 * PtrSize;
1500 for (int i = 0; i < NumOfParams; i++) {
1501 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001502 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001503 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001504 getObjCEncodingForTypeQualifier(
1505 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001506 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001507 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001508 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001509 }
1510}
1511
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001512/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1513/// method declaration. If non-NULL, Container must be either an
1514/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1515/// NULL when getting encodings for protocol properties.
1516void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1517 const Decl *Container,
1518 std::string& S)
1519{
1520 // Collect information from the property implementation decl(s).
1521 bool Dynamic = false;
1522 ObjCPropertyImplDecl *SynthesizePID = 0;
1523
1524 // FIXME: Duplicated code due to poor abstraction.
1525 if (Container) {
1526 if (const ObjCCategoryImplDecl *CID =
1527 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1528 for (ObjCCategoryImplDecl::propimpl_iterator
1529 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1530 ObjCPropertyImplDecl *PID = *i;
1531 if (PID->getPropertyDecl() == PD) {
1532 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1533 Dynamic = true;
1534 } else {
1535 SynthesizePID = PID;
1536 }
1537 }
1538 }
1539 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001540 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001541 for (ObjCCategoryImplDecl::propimpl_iterator
1542 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1543 ObjCPropertyImplDecl *PID = *i;
1544 if (PID->getPropertyDecl() == PD) {
1545 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1546 Dynamic = true;
1547 } else {
1548 SynthesizePID = PID;
1549 }
1550 }
1551 }
1552 }
1553 }
1554
1555 // FIXME: This is not very efficient.
1556 S = "T";
1557
1558 // Encode result type.
1559 // FIXME: GCC uses a generating_property_type_encoding mode during
1560 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001561 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001562
1563 if (PD->isReadOnly()) {
1564 S += ",R";
1565 } else {
1566 switch (PD->getSetterKind()) {
1567 case ObjCPropertyDecl::Assign: break;
1568 case ObjCPropertyDecl::Copy: S += ",C"; break;
1569 case ObjCPropertyDecl::Retain: S += ",&"; break;
1570 }
1571 }
1572
1573 // It really isn't clear at all what this means, since properties
1574 // are "dynamic by default".
1575 if (Dynamic)
1576 S += ",D";
1577
1578 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1579 S += ",G";
1580 S += PD->getGetterName().getName();
1581 }
1582
1583 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1584 S += ",S";
1585 S += PD->getSetterName().getName();
1586 }
1587
1588 if (SynthesizePID) {
1589 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1590 S += ",V";
1591 S += OID->getName();
1592 }
1593
1594 // FIXME: OBJCGC: weak & strong
1595}
1596
Fariborz Jahanian248db262008-01-22 22:44:46 +00001597void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001598 bool NameFields) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001599 // We follow the behavior of gcc, expanding structures which are
1600 // directly pointed to, and expanding embedded structures. Note that
1601 // these rules are sufficient to prevent recursive encoding of the
1602 // same type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001603 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001604}
1605
1606void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1607 bool ExpandPointedToStructures,
1608 bool ExpandStructures,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001609 bool NameFields) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001610 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001611 char encoding;
1612 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001613 default: assert(0 && "Unhandled builtin type kind");
1614 case BuiltinType::Void: encoding = 'v'; break;
1615 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001616 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001617 case BuiltinType::UChar: encoding = 'C'; break;
1618 case BuiltinType::UShort: encoding = 'S'; break;
1619 case BuiltinType::UInt: encoding = 'I'; break;
1620 case BuiltinType::ULong: encoding = 'L'; break;
1621 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001622 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001623 case BuiltinType::SChar: encoding = 'c'; break;
1624 case BuiltinType::Short: encoding = 's'; break;
1625 case BuiltinType::Int: encoding = 'i'; break;
1626 case BuiltinType::Long: encoding = 'l'; break;
1627 case BuiltinType::LongLong: encoding = 'q'; break;
1628 case BuiltinType::Float: encoding = 'f'; break;
1629 case BuiltinType::Double: encoding = 'd'; break;
1630 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001631 }
1632
1633 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001634 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001635 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001636 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001637 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1638 ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001639 ExpandStructures, NameFields);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001640 }
1641 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001642 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001643 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001644 S += '@';
1645 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001646 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001647 S += '#';
1648 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001649 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001650 S += ':';
1651 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001652 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001653
1654 if (PointeeTy->isCharType()) {
1655 // char pointer types should be encoded as '*' unless it is a
1656 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001657 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001658 S += '*';
1659 return;
1660 }
1661 }
1662
1663 S += '^';
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001664 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001665 false, ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001666 NameFields);
Chris Lattnera1923f62008-08-04 07:31:14 +00001667 } else if (const ArrayType *AT =
1668 // Ignore type qualifiers etc.
1669 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001670 S += '[';
1671
1672 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1673 S += llvm::utostr(CAT->getSize().getZExtValue());
1674 else
1675 assert(0 && "Unhandled array type!");
1676
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001677 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001678 false, ExpandStructures, NameFields);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001679 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001680 } else if (T->getAsFunctionType()) {
1681 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001682 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001683 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001684 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001685 // Anonymous structures print as '?'
1686 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1687 S += II->getName();
1688 } else {
1689 S += '?';
1690 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001691 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001692 S += '=';
1693 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001694 FieldDecl *FD = RDecl->getMember(i);
1695 if (NameFields) {
1696 S += '"';
1697 S += FD->getName();
1698 S += '"';
1699 }
1700
1701 // Special case bit-fields.
1702 if (const Expr *E = FD->getBitWidth()) {
1703 // FIXME: Fix constness.
1704 ASTContext *Ctx = const_cast<ASTContext*>(this);
1705 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1706 // FIXME: Obj-C is losing information about the type size
1707 // here. Investigate if this is a problem.
1708 S += 'b';
1709 S += llvm::utostr(N);
1710 } else {
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001711 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001712 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001713 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001714 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001715 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001716 } else if (T->isEnumeralType()) {
1717 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001718 } else if (T->isBlockPointerType()) {
1719 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001720 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001721 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001722}
1723
Ted Kremenek42730c52008-01-07 19:49:32 +00001724void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001725 std::string& S) const {
1726 if (QT & Decl::OBJC_TQ_In)
1727 S += 'n';
1728 if (QT & Decl::OBJC_TQ_Inout)
1729 S += 'N';
1730 if (QT & Decl::OBJC_TQ_Out)
1731 S += 'o';
1732 if (QT & Decl::OBJC_TQ_Bycopy)
1733 S += 'O';
1734 if (QT & Decl::OBJC_TQ_Byref)
1735 S += 'R';
1736 if (QT & Decl::OBJC_TQ_Oneway)
1737 S += 'V';
1738}
1739
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001740void ASTContext::setBuiltinVaListType(QualType T)
1741{
1742 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1743
1744 BuiltinVaListType = T;
1745}
1746
Ted Kremenek42730c52008-01-07 19:49:32 +00001747void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001748{
Ted Kremenek42730c52008-01-07 19:49:32 +00001749 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001750
1751 // typedef struct objc_object *id;
1752 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1753 assert(ptr && "'id' incorrectly typed");
1754 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1755 assert(rec && "'id' incorrectly typed");
1756 IdStructType = rec;
1757}
1758
Ted Kremenek42730c52008-01-07 19:49:32 +00001759void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001760{
Ted Kremenek42730c52008-01-07 19:49:32 +00001761 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001762
1763 // typedef struct objc_selector *SEL;
1764 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1765 assert(ptr && "'SEL' incorrectly typed");
1766 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1767 assert(rec && "'SEL' incorrectly typed");
1768 SelStructType = rec;
1769}
1770
Ted Kremenek42730c52008-01-07 19:49:32 +00001771void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001772{
Ted Kremenek42730c52008-01-07 19:49:32 +00001773 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001774}
1775
Ted Kremenek42730c52008-01-07 19:49:32 +00001776void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001777{
Ted Kremenek42730c52008-01-07 19:49:32 +00001778 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001779
1780 // typedef struct objc_class *Class;
1781 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1782 assert(ptr && "'Class' incorrectly typed");
1783 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1784 assert(rec && "'Class' incorrectly typed");
1785 ClassStructType = rec;
1786}
1787
Ted Kremenek42730c52008-01-07 19:49:32 +00001788void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1789 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001790 "'NSConstantString' type already set!");
1791
Ted Kremenek42730c52008-01-07 19:49:32 +00001792 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001793}
1794
Douglas Gregorc6507e42008-11-03 14:12:49 +00001795/// getFromTargetType - Given one of the integer types provided by
1796/// TargetInfo, produce the corresponding type.
1797QualType ASTContext::getFromTargetType(TargetInfo::IntType Type) const {
1798 switch (Type) {
1799 case TargetInfo::NoInt: return QualType();
1800 case TargetInfo::SignedShort: return ShortTy;
1801 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1802 case TargetInfo::SignedInt: return IntTy;
1803 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1804 case TargetInfo::SignedLong: return LongTy;
1805 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1806 case TargetInfo::SignedLongLong: return LongLongTy;
1807 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1808 }
1809
1810 assert(false && "Unhandled TargetInfo::IntType value");
1811}
Ted Kremenek118930e2008-07-24 23:58:27 +00001812
1813//===----------------------------------------------------------------------===//
1814// Type Predicates.
1815//===----------------------------------------------------------------------===//
1816
1817/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1818/// to an object type. This includes "id" and "Class" (two 'special' pointers
1819/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1820/// ID type).
1821bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1822 if (Ty->isObjCQualifiedIdType())
1823 return true;
1824
Steve Naroffd9e00802008-10-21 18:24:04 +00001825 // Blocks are objects.
1826 if (Ty->isBlockPointerType())
1827 return true;
1828
1829 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00001830 if (!Ty->isPointerType())
1831 return false;
1832
1833 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1834 // pointer types. This looks for the typedef specifically, not for the
1835 // underlying type.
1836 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1837 return true;
1838
1839 // If this a pointer to an interface (e.g. NSString*), it is ok.
1840 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1841}
1842
Chris Lattner6ff358b2008-04-07 06:51:04 +00001843//===----------------------------------------------------------------------===//
1844// Type Compatibility Testing
1845//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001846
Steve Naroff3454b6c2008-09-04 15:10:53 +00001847/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001848/// block types. Types must be strictly compatible here. For example,
1849/// C unfortunately doesn't produce an error for the following:
1850///
1851/// int (*emptyArgFunc)();
1852/// int (*intArgList)(int) = emptyArgFunc;
1853///
1854/// For blocks, we will produce an error for the following (similar to C++):
1855///
1856/// int (^emptyArgBlock)();
1857/// int (^intArgBlock)(int) = emptyArgBlock;
1858///
1859/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1860///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001861bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Narofff5e7eff2008-09-09 13:47:19 +00001862 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001863}
1864
Chris Lattner6ff358b2008-04-07 06:51:04 +00001865/// areCompatVectorTypes - Return true if the two specified vector types are
1866/// compatible.
1867static bool areCompatVectorTypes(const VectorType *LHS,
1868 const VectorType *RHS) {
1869 assert(LHS->isCanonical() && RHS->isCanonical());
1870 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001871 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00001872}
1873
Eli Friedman0d9549b2008-08-22 00:56:42 +00001874/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001875/// compatible for assignment from RHS to LHS. This handles validation of any
1876/// protocol qualifiers on the LHS or RHS.
1877///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001878bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1879 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001880 // Verify that the base decls are compatible: the RHS must be a subclass of
1881 // the LHS.
1882 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1883 return false;
1884
1885 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1886 // protocol qualified at all, then we are good.
1887 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1888 return true;
1889
1890 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1891 // isn't a superset.
1892 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1893 return true; // FIXME: should return false!
1894
1895 // Finally, we must have two protocol-qualified interfaces.
1896 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1897 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1898 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1899 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1900 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1901 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1902
1903 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1904 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1905 // LHS in a single parallel scan until we run out of elements in LHS.
1906 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1907 ObjCProtocolDecl *LHSProto = *LHSPI;
1908
1909 while (RHSPI != RHSPE) {
1910 ObjCProtocolDecl *RHSProto = *RHSPI++;
1911 // If the RHS has a protocol that the LHS doesn't, ignore it.
1912 if (RHSProto != LHSProto)
1913 continue;
1914
1915 // Otherwise, the RHS does have this element.
1916 ++LHSPI;
1917 if (LHSPI == LHSPE)
1918 return true; // All protocols in LHS exist in RHS.
1919
1920 LHSProto = *LHSPI;
1921 }
1922
1923 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1924 return false;
1925}
1926
Steve Naroff85f0dc52007-10-15 20:41:53 +00001927/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1928/// both shall have the identically qualified version of a compatible type.
1929/// C99 6.2.7p1: Two types have compatible types if their types are the
1930/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001931bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1932 return !mergeTypes(LHS, RHS).isNull();
1933}
1934
1935QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1936 const FunctionType *lbase = lhs->getAsFunctionType();
1937 const FunctionType *rbase = rhs->getAsFunctionType();
1938 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1939 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1940 bool allLTypes = true;
1941 bool allRTypes = true;
1942
1943 // Check return type
1944 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1945 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001946 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
1947 allLTypes = false;
1948 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
1949 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001950
1951 if (lproto && rproto) { // two C99 style function prototypes
1952 unsigned lproto_nargs = lproto->getNumArgs();
1953 unsigned rproto_nargs = rproto->getNumArgs();
1954
1955 // Compatible functions must have the same number of arguments
1956 if (lproto_nargs != rproto_nargs)
1957 return QualType();
1958
1959 // Variadic and non-variadic functions aren't compatible
1960 if (lproto->isVariadic() != rproto->isVariadic())
1961 return QualType();
1962
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001963 if (lproto->getTypeQuals() != rproto->getTypeQuals())
1964 return QualType();
1965
Eli Friedman0d9549b2008-08-22 00:56:42 +00001966 // Check argument compatibility
1967 llvm::SmallVector<QualType, 10> types;
1968 for (unsigned i = 0; i < lproto_nargs; i++) {
1969 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1970 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1971 QualType argtype = mergeTypes(largtype, rargtype);
1972 if (argtype.isNull()) return QualType();
1973 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001974 if (getCanonicalType(argtype) != getCanonicalType(largtype))
1975 allLTypes = false;
1976 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
1977 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001978 }
1979 if (allLTypes) return lhs;
1980 if (allRTypes) return rhs;
1981 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001982 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00001983 }
1984
1985 if (lproto) allRTypes = false;
1986 if (rproto) allLTypes = false;
1987
1988 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1989 if (proto) {
1990 if (proto->isVariadic()) return QualType();
1991 // Check that the types are compatible with the types that
1992 // would result from default argument promotions (C99 6.7.5.3p15).
1993 // The only types actually affected are promotable integer
1994 // types and floats, which would be passed as a different
1995 // type depending on whether the prototype is visible.
1996 unsigned proto_nargs = proto->getNumArgs();
1997 for (unsigned i = 0; i < proto_nargs; ++i) {
1998 QualType argTy = proto->getArgType(i);
1999 if (argTy->isPromotableIntegerType() ||
2000 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2001 return QualType();
2002 }
2003
2004 if (allLTypes) return lhs;
2005 if (allRTypes) return rhs;
2006 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002007 proto->getNumArgs(), lproto->isVariadic(),
2008 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002009 }
2010
2011 if (allLTypes) return lhs;
2012 if (allRTypes) return rhs;
2013 return getFunctionTypeNoProto(retType);
2014}
2015
2016QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002017 // C++ [expr]: If an expression initially has the type "reference to T", the
2018 // type is adjusted to "T" prior to any further analysis, the expression
2019 // designates the object or function denoted by the reference, and the
2020 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002021 // FIXME: C++ shouldn't be going through here! The rules are different
2022 // enough that they should be handled separately.
2023 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002024 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002025 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002026 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002027
Eli Friedman0d9549b2008-08-22 00:56:42 +00002028 QualType LHSCan = getCanonicalType(LHS),
2029 RHSCan = getCanonicalType(RHS);
2030
2031 // If two types are identical, they are compatible.
2032 if (LHSCan == RHSCan)
2033 return LHS;
2034
2035 // If the qualifiers are different, the types aren't compatible
2036 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2037 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2038 return QualType();
2039
2040 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2041 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2042
Chris Lattnerc38d4522008-01-14 05:45:46 +00002043 // We want to consider the two function types to be the same for these
2044 // comparisons, just force one to the other.
2045 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2046 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002047
2048 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002049 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2050 LHSClass = Type::ConstantArray;
2051 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2052 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002053
Nate Begemanaf6ed502008-04-18 23:10:10 +00002054 // Canonicalize ExtVector -> Vector.
2055 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2056 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002057
Chris Lattner7cdcb252008-04-07 06:38:24 +00002058 // Consider qualified interfaces and interfaces the same.
2059 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2060 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002061
Chris Lattnerb5709e22008-04-07 05:43:21 +00002062 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002063 if (LHSClass != RHSClass) {
Steve Naroff44549772008-06-04 15:07:33 +00002064 // ID is compatible with all qualified id types.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002065 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002066 if (const PointerType *PT = RHS->getAsPointerType()) {
2067 QualType pType = PT->getPointeeType();
2068 if (isObjCIdType(pType))
Eli Friedman0d9549b2008-08-22 00:56:42 +00002069 return LHS;
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002070 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2071 // Unfortunately, this API is part of Sema (which we don't have access
2072 // to. Need to refactor. The following check is insufficient, since we
2073 // need to make sure the class implements the protocol.
2074 if (pType->isObjCInterfaceType())
2075 return LHS;
2076 }
Steve Naroff44549772008-06-04 15:07:33 +00002077 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002078 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002079 if (const PointerType *PT = LHS->getAsPointerType()) {
2080 QualType pType = PT->getPointeeType();
2081 if (isObjCIdType(pType))
Eli Friedman0d9549b2008-08-22 00:56:42 +00002082 return RHS;
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002083 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2084 // Unfortunately, this API is part of Sema (which we don't have access
2085 // to. Need to refactor. The following check is insufficient, since we
2086 // need to make sure the class implements the protocol.
2087 if (pType->isObjCInterfaceType())
2088 return RHS;
2089 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002090 }
2091
Chris Lattnerc38d4522008-01-14 05:45:46 +00002092 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2093 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002094 if (const EnumType* ETy = LHS->getAsEnumType()) {
2095 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2096 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002097 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002098 if (const EnumType* ETy = RHS->getAsEnumType()) {
2099 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2100 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002101 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002102
Eli Friedman0d9549b2008-08-22 00:56:42 +00002103 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002104 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002105
Steve Naroffc88babe2008-01-09 22:43:08 +00002106 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002107 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002108 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002109 {
2110 // Merge two pointer types, while trying to preserve typedef info
2111 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2112 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2113 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2114 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002115 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2116 return LHS;
2117 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2118 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002119 return getPointerType(ResultType);
2120 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002121 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002122 {
2123 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2124 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2125 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2126 return QualType();
2127
2128 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2129 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2130 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2131 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002132 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2133 return LHS;
2134 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2135 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002136 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2137 ArrayType::ArraySizeModifier(), 0);
2138 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2139 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002140 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2141 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002142 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2143 return LHS;
2144 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2145 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002146 if (LVAT) {
2147 // FIXME: This isn't correct! But tricky to implement because
2148 // the array's size has to be the size of LHS, but the type
2149 // has to be different.
2150 return LHS;
2151 }
2152 if (RVAT) {
2153 // FIXME: This isn't correct! But tricky to implement because
2154 // the array's size has to be the size of RHS, but the type
2155 // has to be different.
2156 return RHS;
2157 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002158 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2159 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002160 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002161 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002162 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002163 return mergeFunctionTypes(LHS, RHS);
2164 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002165 // FIXME: Why are these compatible?
2166 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2167 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2168 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002169 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002170 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002171 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002172 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002173 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2174 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002175 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002176 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002177 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2178 // for checking assignment/comparison safety
2179 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002180 default:
2181 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002182 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002183 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002184}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002185
Chris Lattner1d78a862008-04-07 07:01:58 +00002186//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002187// Integer Predicates
2188//===----------------------------------------------------------------------===//
2189unsigned ASTContext::getIntWidth(QualType T) {
2190 if (T == BoolTy)
2191 return 1;
2192 // At the moment, only bool has padding bits
2193 return (unsigned)getTypeSize(T);
2194}
2195
2196QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2197 assert(T->isSignedIntegerType() && "Unexpected type");
2198 if (const EnumType* ETy = T->getAsEnumType())
2199 T = ETy->getDecl()->getIntegerType();
2200 const BuiltinType* BTy = T->getAsBuiltinType();
2201 assert (BTy && "Unexpected signed integer type");
2202 switch (BTy->getKind()) {
2203 case BuiltinType::Char_S:
2204 case BuiltinType::SChar:
2205 return UnsignedCharTy;
2206 case BuiltinType::Short:
2207 return UnsignedShortTy;
2208 case BuiltinType::Int:
2209 return UnsignedIntTy;
2210 case BuiltinType::Long:
2211 return UnsignedLongTy;
2212 case BuiltinType::LongLong:
2213 return UnsignedLongLongTy;
2214 default:
2215 assert(0 && "Unexpected signed integer type");
2216 return QualType();
2217 }
2218}
2219
2220
2221//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002222// Serialization Support
2223//===----------------------------------------------------------------------===//
2224
Ted Kremenek738e6c02007-10-31 17:10:13 +00002225/// Emit - Serialize an ASTContext object to Bitcode.
2226void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002227 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002228 S.EmitRef(SourceMgr);
2229 S.EmitRef(Target);
2230 S.EmitRef(Idents);
2231 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002232
Ted Kremenek68228a92007-10-31 22:44:07 +00002233 // Emit the size of the type vector so that we can reserve that size
2234 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002235 S.EmitInt(Types.size());
2236
Ted Kremenek034a78c2007-11-13 22:02:55 +00002237 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2238 I!=E;++I)
2239 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002240
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002241 S.EmitOwnedPtr(TUDecl);
2242
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002243 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002244}
2245
Ted Kremenekacba3612007-11-13 00:25:37 +00002246ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002247
2248 // Read the language options.
2249 LangOptions LOpts;
2250 LOpts.Read(D);
2251
Ted Kremenek68228a92007-10-31 22:44:07 +00002252 SourceManager &SM = D.ReadRef<SourceManager>();
2253 TargetInfo &t = D.ReadRef<TargetInfo>();
2254 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2255 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002256
Ted Kremenek68228a92007-10-31 22:44:07 +00002257 unsigned size_reserve = D.ReadInt();
2258
Ted Kremenek842126e2008-06-04 15:55:15 +00002259 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002260
Ted Kremenek034a78c2007-11-13 22:02:55 +00002261 for (unsigned i = 0; i < size_reserve; ++i)
2262 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002263
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002264 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2265
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002266 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002267
2268 return A;
2269}