blob: 75c58880486916051e21f3276d16a7a41ae4ce5e [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,
Chris Lattner4b009652007-07-25 00:24:17 +0000887 unsigned NumArgs, bool isVariadic) {
888 // Unique functions, to guarantee there is only one function of a particular
889 // structure.
890 llvm::FoldingSetNodeID ID;
891 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
892
893 void *InsertPos = 0;
894 if (FunctionTypeProto *FTP =
895 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
896 return QualType(FTP, 0);
897
898 // Determine whether the type being created is already canonical or not.
899 bool isCanonical = ResultTy->isCanonical();
900 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
901 if (!ArgArray[i]->isCanonical())
902 isCanonical = false;
903
904 // If this type isn't canonical, get the canonical version of it.
905 QualType Canonical;
906 if (!isCanonical) {
907 llvm::SmallVector<QualType, 16> CanonicalArgs;
908 CanonicalArgs.reserve(NumArgs);
909 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000910 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000911
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000912 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000913 &CanonicalArgs[0], NumArgs,
914 isVariadic);
915
916 // Get the new insert position for the node we care about.
917 FunctionTypeProto *NewIP =
918 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000919 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000920 }
921
922 // FunctionTypeProto objects are not allocated with new because they have a
923 // variable size array (for parameter types) at the end of them.
924 FunctionTypeProto *FTP =
925 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
926 NumArgs*sizeof(QualType));
927 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
928 Canonical);
929 Types.push_back(FTP);
930 FunctionTypeProtos.InsertNode(FTP, InsertPos);
931 return QualType(FTP, 0);
932}
933
Douglas Gregor1d661552008-04-13 21:07:44 +0000934/// getTypeDeclType - Return the unique reference to the type for the
935/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000936QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000937 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +0000938 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
939
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000940 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000941 return getTypedefType(Typedef);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000942 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000943 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000944
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000945 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000946 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
947 : new CXXRecordType(CXXRecord);
948 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000949 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000950 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
951 : new RecordType(Record);
952 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000953 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000954 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000955 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000956 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000957
Ted Kremenek46a837c2008-09-05 17:16:31 +0000958 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000959 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000960}
961
Ted Kremenek46a837c2008-09-05 17:16:31 +0000962/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
963/// about which RecordDecl serves as the definition of a particular
964/// struct/union/class. This will eventually be used by enums as well.
965void ASTContext::setTagDefinition(TagDecl* D) {
966 assert (D->isDefinition());
967 cast<TagType>(D->TypeForDecl)->decl = D;
968}
969
Chris Lattner4b009652007-07-25 00:24:17 +0000970/// getTypedefType - Return the unique reference to the type for the
971/// specified typename decl.
972QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
973 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
974
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000975 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000976 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000977 Types.push_back(Decl->TypeForDecl);
978 return QualType(Decl->TypeForDecl, 0);
979}
980
Ted Kremenek42730c52008-01-07 19:49:32 +0000981/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000982/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000983QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000984 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
985
Ted Kremenek42730c52008-01-07 19:49:32 +0000986 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000987 Types.push_back(Decl->TypeForDecl);
988 return QualType(Decl->TypeForDecl, 0);
989}
990
Chris Lattnere1352302008-04-07 04:56:42 +0000991/// CmpProtocolNames - Comparison predicate for sorting protocols
992/// alphabetically.
993static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
994 const ObjCProtocolDecl *RHS) {
995 return strcmp(LHS->getName(), RHS->getName()) < 0;
996}
997
998static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
999 unsigned &NumProtocols) {
1000 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1001
1002 // Sort protocols, keyed by name.
1003 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1004
1005 // Remove duplicates.
1006 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1007 NumProtocols = ProtocolsEnd-Protocols;
1008}
1009
1010
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001011/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1012/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001013QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1014 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001015 // Sort the protocol list alphabetically to canonicalize it.
1016 SortAndUniqueProtocols(Protocols, NumProtocols);
1017
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001018 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001019 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001020
1021 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001022 if (ObjCQualifiedInterfaceType *QT =
1023 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001024 return QualType(QT, 0);
1025
1026 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001027 ObjCQualifiedInterfaceType *QType =
1028 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001029 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001030 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001031 return QualType(QType, 0);
1032}
1033
Chris Lattnere1352302008-04-07 04:56:42 +00001034/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1035/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001036QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001037 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001038 // Sort the protocol list alphabetically to canonicalize it.
1039 SortAndUniqueProtocols(Protocols, NumProtocols);
1040
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001041 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001043
1044 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001046 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001047 return QualType(QT, 0);
1048
1049 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001050 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001051 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001052 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001053 return QualType(QType, 0);
1054}
1055
Steve Naroff0604dd92007-08-01 18:02:17 +00001056/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1057/// TypeOfExpr AST's (since expression's are never shared). For example,
1058/// multiple declarations that refer to "typeof(x)" all contain different
1059/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1060/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001061QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001062 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001063 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1064 Types.push_back(toe);
1065 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001066}
1067
Steve Naroff0604dd92007-08-01 18:02:17 +00001068/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1069/// TypeOfType AST's. The only motivation to unique these nodes would be
1070/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1071/// an issue. This doesn't effect the type checker, since it operates
1072/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001073QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001074 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001075 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1076 Types.push_back(tot);
1077 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001078}
1079
Chris Lattner4b009652007-07-25 00:24:17 +00001080/// getTagDeclType - Return the unique reference to the type for the
1081/// specified TagDecl (struct/union/class/enum) decl.
1082QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001083 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001084 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001085}
1086
1087/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1088/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1089/// needs to agree with the definition in <stddef.h>.
1090QualType ASTContext::getSizeType() const {
1091 // On Darwin, size_t is defined as a "long unsigned int".
1092 // FIXME: should derive from "Target".
1093 return UnsignedLongTy;
1094}
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
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001103 // On Darwin, wchar_t is defined as a "int".
1104 // FIXME: should derive from "Target".
1105 return IntTy;
1106}
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 {
1125 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1126 // FIXME: should derive from "Target".
1127 return IntTy;
1128}
1129
Chris Lattner19eb97e2008-04-02 05:18:44 +00001130//===----------------------------------------------------------------------===//
1131// Type Operators
1132//===----------------------------------------------------------------------===//
1133
Chris Lattner3dae6f42008-04-06 22:41:35 +00001134/// getCanonicalType - Return the canonical (structural) type corresponding to
1135/// the specified potentially non-canonical type. The non-canonical version
1136/// of a type may have many "decorated" versions of types. Decorators can
1137/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1138/// to be free of any of these, allowing two canonical types to be compared
1139/// for exact equality with a simple pointer comparison.
1140QualType ASTContext::getCanonicalType(QualType T) {
1141 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001142
1143 // If the result has type qualifiers, make sure to canonicalize them as well.
1144 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1145 if (TypeQuals == 0) return CanType;
1146
1147 // If the type qualifiers are on an array type, get the canonical type of the
1148 // array with the qualifiers applied to the element type.
1149 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1150 if (!AT)
1151 return CanType.getQualifiedType(TypeQuals);
1152
1153 // Get the canonical version of the element with the extra qualifiers on it.
1154 // This can recursively sink qualifiers through multiple levels of arrays.
1155 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1156 NewEltTy = getCanonicalType(NewEltTy);
1157
1158 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1159 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1160 CAT->getIndexTypeQualifier());
1161 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1162 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1163 IAT->getIndexTypeQualifier());
1164
1165 // FIXME: What is the ownership of size expressions in VLAs?
1166 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1167 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1168 VAT->getSizeModifier(),
1169 VAT->getIndexTypeQualifier());
1170}
1171
1172
1173const ArrayType *ASTContext::getAsArrayType(QualType T) {
1174 // Handle the non-qualified case efficiently.
1175 if (T.getCVRQualifiers() == 0) {
1176 // Handle the common positive case fast.
1177 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1178 return AT;
1179 }
1180
1181 // Handle the common negative case fast, ignoring CVR qualifiers.
1182 QualType CType = T->getCanonicalTypeInternal();
1183
1184 // Make sure to look through type qualifiers (like ASQuals) for the negative
1185 // test.
1186 if (!isa<ArrayType>(CType) &&
1187 !isa<ArrayType>(CType.getUnqualifiedType()))
1188 return 0;
1189
1190 // Apply any CVR qualifiers from the array type to the element type. This
1191 // implements C99 6.7.3p8: "If the specification of an array type includes
1192 // any type qualifiers, the element type is so qualified, not the array type."
1193
1194 // If we get here, we either have type qualifiers on the type, or we have
1195 // sugar such as a typedef in the way. If we have type qualifiers on the type
1196 // we must propagate them down into the elemeng type.
1197 unsigned CVRQuals = T.getCVRQualifiers();
1198 unsigned AddrSpace = 0;
1199 Type *Ty = T.getTypePtr();
1200
1201 // Rip through ASQualType's and typedefs to get to a concrete type.
1202 while (1) {
1203 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1204 AddrSpace = ASQT->getAddressSpace();
1205 Ty = ASQT->getBaseType();
1206 } else {
1207 T = Ty->getDesugaredType();
1208 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1209 break;
1210 CVRQuals |= T.getCVRQualifiers();
1211 Ty = T.getTypePtr();
1212 }
1213 }
1214
1215 // If we have a simple case, just return now.
1216 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1217 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1218 return ATy;
1219
1220 // Otherwise, we have an array and we have qualifiers on it. Push the
1221 // qualifiers into the array element type and return a new array type.
1222 // Get the canonical version of the element with the extra qualifiers on it.
1223 // This can recursively sink qualifiers through multiple levels of arrays.
1224 QualType NewEltTy = ATy->getElementType();
1225 if (AddrSpace)
1226 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1227 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1228
1229 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1230 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1231 CAT->getSizeModifier(),
1232 CAT->getIndexTypeQualifier()));
1233 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1234 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1235 IAT->getSizeModifier(),
1236 IAT->getIndexTypeQualifier()));
1237
1238 // FIXME: What is the ownership of size expressions in VLAs?
1239 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1240 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1241 VAT->getSizeModifier(),
1242 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001243}
1244
1245
Chris Lattner19eb97e2008-04-02 05:18:44 +00001246/// getArrayDecayedType - Return the properly qualified result of decaying the
1247/// specified array type to a pointer. This operation is non-trivial when
1248/// handling typedefs etc. The canonical type of "T" must be an array type,
1249/// this returns a pointer to a properly qualified element of the array.
1250///
1251/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1252QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001253 // Get the element type with 'getAsArrayType' so that we don't lose any
1254 // typedefs in the element type of the array. This also handles propagation
1255 // of type qualifiers from the array type into the element type if present
1256 // (C99 6.7.3p8).
1257 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1258 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001259
Chris Lattnera1923f62008-08-04 07:31:14 +00001260 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001261
1262 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001263 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001264}
1265
Chris Lattner4b009652007-07-25 00:24:17 +00001266/// getFloatingRank - Return a relative rank for floating point types.
1267/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001268static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001269 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001270 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001271
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001272 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001273 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001274 case BuiltinType::Float: return FloatRank;
1275 case BuiltinType::Double: return DoubleRank;
1276 case BuiltinType::LongDouble: return LongDoubleRank;
1277 }
1278}
1279
Steve Narofffa0c4532007-08-27 01:41:48 +00001280/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1281/// point or a complex type (based on typeDomain/typeSize).
1282/// 'typeDomain' is a real floating point or complex type.
1283/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001284QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1285 QualType Domain) const {
1286 FloatingRank EltRank = getFloatingRank(Size);
1287 if (Domain->isComplexType()) {
1288 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001289 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001290 case FloatRank: return FloatComplexTy;
1291 case DoubleRank: return DoubleComplexTy;
1292 case LongDoubleRank: return LongDoubleComplexTy;
1293 }
Chris Lattner4b009652007-07-25 00:24:17 +00001294 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001295
1296 assert(Domain->isRealFloatingType() && "Unknown domain!");
1297 switch (EltRank) {
1298 default: assert(0 && "getFloatingRank(): illegal value for rank");
1299 case FloatRank: return FloatTy;
1300 case DoubleRank: return DoubleTy;
1301 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001302 }
Chris Lattner4b009652007-07-25 00:24:17 +00001303}
1304
Chris Lattner51285d82008-04-06 23:55:33 +00001305/// getFloatingTypeOrder - Compare the rank of the two specified floating
1306/// point types, ignoring the domain of the type (i.e. 'double' ==
1307/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1308/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001309int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1310 FloatingRank LHSR = getFloatingRank(LHS);
1311 FloatingRank RHSR = getFloatingRank(RHS);
1312
1313 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001314 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001315 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001316 return 1;
1317 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001318}
1319
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001320/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1321/// routine will assert if passed a built-in type that isn't an integer or enum,
1322/// or if it is not canonicalized.
1323static unsigned getIntegerRank(Type *T) {
1324 assert(T->isCanonical() && "T should be canonicalized");
1325 if (isa<EnumType>(T))
1326 return 4;
1327
1328 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001329 default: assert(0 && "getIntegerRank(): not a built-in integer");
1330 case BuiltinType::Bool:
1331 return 1;
1332 case BuiltinType::Char_S:
1333 case BuiltinType::Char_U:
1334 case BuiltinType::SChar:
1335 case BuiltinType::UChar:
1336 return 2;
1337 case BuiltinType::Short:
1338 case BuiltinType::UShort:
1339 return 3;
1340 case BuiltinType::Int:
1341 case BuiltinType::UInt:
1342 return 4;
1343 case BuiltinType::Long:
1344 case BuiltinType::ULong:
1345 return 5;
1346 case BuiltinType::LongLong:
1347 case BuiltinType::ULongLong:
1348 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001349 }
1350}
1351
Chris Lattner51285d82008-04-06 23:55:33 +00001352/// getIntegerTypeOrder - Returns the highest ranked integer type:
1353/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1354/// LHS < RHS, return -1.
1355int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001356 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1357 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001358 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001359
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001360 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1361 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001362
Chris Lattner51285d82008-04-06 23:55:33 +00001363 unsigned LHSRank = getIntegerRank(LHSC);
1364 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001365
Chris Lattner51285d82008-04-06 23:55:33 +00001366 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1367 if (LHSRank == RHSRank) return 0;
1368 return LHSRank > RHSRank ? 1 : -1;
1369 }
Chris Lattner4b009652007-07-25 00:24:17 +00001370
Chris Lattner51285d82008-04-06 23:55:33 +00001371 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1372 if (LHSUnsigned) {
1373 // If the unsigned [LHS] type is larger, return it.
1374 if (LHSRank >= RHSRank)
1375 return 1;
1376
1377 // If the signed type can represent all values of the unsigned type, it
1378 // wins. Because we are dealing with 2's complement and types that are
1379 // powers of two larger than each other, this is always safe.
1380 return -1;
1381 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001382
Chris Lattner51285d82008-04-06 23:55:33 +00001383 // If the unsigned [RHS] type is larger, return it.
1384 if (RHSRank >= LHSRank)
1385 return -1;
1386
1387 // If the signed type can represent all values of the unsigned type, it
1388 // wins. Because we are dealing with 2's complement and types that are
1389 // powers of two larger than each other, this is always safe.
1390 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001391}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001392
1393// getCFConstantStringType - Return the type used for constant CFStrings.
1394QualType ASTContext::getCFConstantStringType() {
1395 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001396 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001397 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001398 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001399 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001400
1401 // const int *isa;
1402 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001403 // int flags;
1404 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001405 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001406 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001407 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001408 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001409 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001410 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001411
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001412 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001413 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001414 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001415
Ted Kremenek46a837c2008-09-05 17:16:31 +00001416 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001417 }
1418
1419 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001420}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001421
Anders Carlssonf58cac72008-08-30 19:34:46 +00001422QualType ASTContext::getObjCFastEnumerationStateType()
1423{
1424 if (!ObjCFastEnumerationStateTypeDecl) {
1425 QualType FieldTypes[] = {
1426 UnsignedLongTy,
1427 getPointerType(ObjCIdType),
1428 getPointerType(UnsignedLongTy),
1429 getConstantArrayType(UnsignedLongTy,
1430 llvm::APInt(32, 5), ArrayType::Normal, 0)
1431 };
1432
1433 FieldDecl *FieldDecls[4];
1434 for (size_t i = 0; i < 4; ++i)
1435 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1436 FieldTypes[i]);
1437
1438 ObjCFastEnumerationStateTypeDecl =
1439 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001440 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonf58cac72008-08-30 19:34:46 +00001441
Ted Kremenek46a837c2008-09-05 17:16:31 +00001442 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001443 }
1444
1445 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1446}
1447
Anders Carlssone3f02572007-10-29 06:33:42 +00001448// This returns true if a type has been typedefed to BOOL:
1449// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001450static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001451 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001452 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001453
1454 return false;
1455}
1456
Ted Kremenek42730c52008-01-07 19:49:32 +00001457/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001458/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001459int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001460 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001461
1462 // Make all integer and enum types at least as large as an int
1463 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001464 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001465 // Treat arrays as pointers, since that's how they're passed in.
1466 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001467 sz = getTypeSize(VoidPtrTy);
1468 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001469}
1470
Ted Kremenek42730c52008-01-07 19:49:32 +00001471/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001472/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001473void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001474 std::string& S)
1475{
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001476 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001477 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001479 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001480 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001481 // Compute size of all parameters.
1482 // Start with computing size of a pointer in number of bytes.
1483 // FIXME: There might(should) be a better way of doing this computation!
1484 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001485 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001486 // The first two arguments (self and _cmd) are pointers; account for
1487 // their size.
1488 int ParmOffset = 2 * PtrSize;
1489 int NumOfParams = Decl->getNumParams();
1490 for (int i = 0; i < NumOfParams; i++) {
1491 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001492 int sz = getObjCEncodingTypeSize (PType);
1493 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001494 ParmOffset += sz;
1495 }
1496 S += llvm::utostr(ParmOffset);
1497 S += "@0:";
1498 S += llvm::utostr(PtrSize);
1499
1500 // Argument types.
1501 ParmOffset = 2 * PtrSize;
1502 for (int i = 0; i < NumOfParams; i++) {
1503 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001504 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001505 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001506 getObjCEncodingForTypeQualifier(
1507 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001508 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001509 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001510 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001511 }
1512}
1513
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001514/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1515/// method declaration. If non-NULL, Container must be either an
1516/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1517/// NULL when getting encodings for protocol properties.
1518void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1519 const Decl *Container,
1520 std::string& S)
1521{
1522 // Collect information from the property implementation decl(s).
1523 bool Dynamic = false;
1524 ObjCPropertyImplDecl *SynthesizePID = 0;
1525
1526 // FIXME: Duplicated code due to poor abstraction.
1527 if (Container) {
1528 if (const ObjCCategoryImplDecl *CID =
1529 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1530 for (ObjCCategoryImplDecl::propimpl_iterator
1531 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1532 ObjCPropertyImplDecl *PID = *i;
1533 if (PID->getPropertyDecl() == PD) {
1534 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1535 Dynamic = true;
1536 } else {
1537 SynthesizePID = PID;
1538 }
1539 }
1540 }
1541 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001542 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001543 for (ObjCCategoryImplDecl::propimpl_iterator
1544 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1545 ObjCPropertyImplDecl *PID = *i;
1546 if (PID->getPropertyDecl() == PD) {
1547 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1548 Dynamic = true;
1549 } else {
1550 SynthesizePID = PID;
1551 }
1552 }
1553 }
1554 }
1555 }
1556
1557 // FIXME: This is not very efficient.
1558 S = "T";
1559
1560 // Encode result type.
1561 // FIXME: GCC uses a generating_property_type_encoding mode during
1562 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001563 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001564
1565 if (PD->isReadOnly()) {
1566 S += ",R";
1567 } else {
1568 switch (PD->getSetterKind()) {
1569 case ObjCPropertyDecl::Assign: break;
1570 case ObjCPropertyDecl::Copy: S += ",C"; break;
1571 case ObjCPropertyDecl::Retain: S += ",&"; break;
1572 }
1573 }
1574
1575 // It really isn't clear at all what this means, since properties
1576 // are "dynamic by default".
1577 if (Dynamic)
1578 S += ",D";
1579
1580 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1581 S += ",G";
1582 S += PD->getGetterName().getName();
1583 }
1584
1585 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1586 S += ",S";
1587 S += PD->getSetterName().getName();
1588 }
1589
1590 if (SynthesizePID) {
1591 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1592 S += ",V";
1593 S += OID->getName();
1594 }
1595
1596 // FIXME: OBJCGC: weak & strong
1597}
1598
Fariborz Jahanian248db262008-01-22 22:44:46 +00001599void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001600 bool NameFields) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001601 // We follow the behavior of gcc, expanding structures which are
1602 // directly pointed to, and expanding embedded structures. Note that
1603 // these rules are sufficient to prevent recursive encoding of the
1604 // same type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001605 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001606}
1607
1608void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1609 bool ExpandPointedToStructures,
1610 bool ExpandStructures,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001611 bool NameFields) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001612 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001613 char encoding;
1614 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001615 default: assert(0 && "Unhandled builtin type kind");
1616 case BuiltinType::Void: encoding = 'v'; break;
1617 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001618 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001619 case BuiltinType::UChar: encoding = 'C'; break;
1620 case BuiltinType::UShort: encoding = 'S'; break;
1621 case BuiltinType::UInt: encoding = 'I'; break;
1622 case BuiltinType::ULong: encoding = 'L'; break;
1623 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001624 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001625 case BuiltinType::SChar: encoding = 'c'; break;
1626 case BuiltinType::Short: encoding = 's'; break;
1627 case BuiltinType::Int: encoding = 'i'; break;
1628 case BuiltinType::Long: encoding = 'l'; break;
1629 case BuiltinType::LongLong: encoding = 'q'; break;
1630 case BuiltinType::Float: encoding = 'f'; break;
1631 case BuiltinType::Double: encoding = 'd'; break;
1632 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001633 }
1634
1635 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001636 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001637 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001638 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001639 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1640 ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001641 ExpandStructures, NameFields);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001642 }
1643 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001644 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001645 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001646 S += '@';
1647 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001648 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001649 S += '#';
1650 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001651 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001652 S += ':';
1653 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001654 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001655
1656 if (PointeeTy->isCharType()) {
1657 // char pointer types should be encoded as '*' unless it is a
1658 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001659 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001660 S += '*';
1661 return;
1662 }
1663 }
1664
1665 S += '^';
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001666 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001667 false, ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001668 NameFields);
Chris Lattnera1923f62008-08-04 07:31:14 +00001669 } else if (const ArrayType *AT =
1670 // Ignore type qualifiers etc.
1671 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001672 S += '[';
1673
1674 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1675 S += llvm::utostr(CAT->getSize().getZExtValue());
1676 else
1677 assert(0 && "Unhandled array type!");
1678
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001679 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001680 false, ExpandStructures, NameFields);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001681 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001682 } else if (T->getAsFunctionType()) {
1683 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001684 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001685 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001686 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001687 // Anonymous structures print as '?'
1688 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1689 S += II->getName();
1690 } else {
1691 S += '?';
1692 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001693 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001694 S += '=';
1695 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001696 FieldDecl *FD = RDecl->getMember(i);
1697 if (NameFields) {
1698 S += '"';
1699 S += FD->getName();
1700 S += '"';
1701 }
1702
1703 // Special case bit-fields.
1704 if (const Expr *E = FD->getBitWidth()) {
1705 // FIXME: Fix constness.
1706 ASTContext *Ctx = const_cast<ASTContext*>(this);
1707 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1708 // FIXME: Obj-C is losing information about the type size
1709 // here. Investigate if this is a problem.
1710 S += 'b';
1711 S += llvm::utostr(N);
1712 } else {
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001713 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001714 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001715 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001716 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001717 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001718 } else if (T->isEnumeralType()) {
1719 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001720 } else if (T->isBlockPointerType()) {
1721 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001722 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001723 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001724}
1725
Ted Kremenek42730c52008-01-07 19:49:32 +00001726void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001727 std::string& S) const {
1728 if (QT & Decl::OBJC_TQ_In)
1729 S += 'n';
1730 if (QT & Decl::OBJC_TQ_Inout)
1731 S += 'N';
1732 if (QT & Decl::OBJC_TQ_Out)
1733 S += 'o';
1734 if (QT & Decl::OBJC_TQ_Bycopy)
1735 S += 'O';
1736 if (QT & Decl::OBJC_TQ_Byref)
1737 S += 'R';
1738 if (QT & Decl::OBJC_TQ_Oneway)
1739 S += 'V';
1740}
1741
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001742void ASTContext::setBuiltinVaListType(QualType T)
1743{
1744 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1745
1746 BuiltinVaListType = T;
1747}
1748
Ted Kremenek42730c52008-01-07 19:49:32 +00001749void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001750{
Ted Kremenek42730c52008-01-07 19:49:32 +00001751 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001752
1753 // typedef struct objc_object *id;
1754 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1755 assert(ptr && "'id' incorrectly typed");
1756 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1757 assert(rec && "'id' incorrectly typed");
1758 IdStructType = rec;
1759}
1760
Ted Kremenek42730c52008-01-07 19:49:32 +00001761void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001762{
Ted Kremenek42730c52008-01-07 19:49:32 +00001763 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001764
1765 // typedef struct objc_selector *SEL;
1766 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1767 assert(ptr && "'SEL' incorrectly typed");
1768 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1769 assert(rec && "'SEL' incorrectly typed");
1770 SelStructType = rec;
1771}
1772
Ted Kremenek42730c52008-01-07 19:49:32 +00001773void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001774{
Ted Kremenek42730c52008-01-07 19:49:32 +00001775 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001776}
1777
Ted Kremenek42730c52008-01-07 19:49:32 +00001778void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001779{
Ted Kremenek42730c52008-01-07 19:49:32 +00001780 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001781
1782 // typedef struct objc_class *Class;
1783 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1784 assert(ptr && "'Class' incorrectly typed");
1785 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1786 assert(rec && "'Class' incorrectly typed");
1787 ClassStructType = rec;
1788}
1789
Ted Kremenek42730c52008-01-07 19:49:32 +00001790void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1791 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001792 "'NSConstantString' type already set!");
1793
Ted Kremenek42730c52008-01-07 19:49:32 +00001794 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001795}
1796
Ted Kremenek118930e2008-07-24 23:58:27 +00001797
1798//===----------------------------------------------------------------------===//
1799// Type Predicates.
1800//===----------------------------------------------------------------------===//
1801
1802/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1803/// to an object type. This includes "id" and "Class" (two 'special' pointers
1804/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1805/// ID type).
1806bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1807 if (Ty->isObjCQualifiedIdType())
1808 return true;
1809
Steve Naroffd9e00802008-10-21 18:24:04 +00001810 // Blocks are objects.
1811 if (Ty->isBlockPointerType())
1812 return true;
1813
1814 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00001815 if (!Ty->isPointerType())
1816 return false;
1817
1818 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1819 // pointer types. This looks for the typedef specifically, not for the
1820 // underlying type.
1821 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1822 return true;
1823
1824 // If this a pointer to an interface (e.g. NSString*), it is ok.
1825 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1826}
1827
Chris Lattner6ff358b2008-04-07 06:51:04 +00001828//===----------------------------------------------------------------------===//
1829// Type Compatibility Testing
1830//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001831
Steve Naroff3454b6c2008-09-04 15:10:53 +00001832/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001833/// block types. Types must be strictly compatible here. For example,
1834/// C unfortunately doesn't produce an error for the following:
1835///
1836/// int (*emptyArgFunc)();
1837/// int (*intArgList)(int) = emptyArgFunc;
1838///
1839/// For blocks, we will produce an error for the following (similar to C++):
1840///
1841/// int (^emptyArgBlock)();
1842/// int (^intArgBlock)(int) = emptyArgBlock;
1843///
1844/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1845///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001846bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Narofff5e7eff2008-09-09 13:47:19 +00001847 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001848}
1849
Chris Lattner6ff358b2008-04-07 06:51:04 +00001850/// areCompatVectorTypes - Return true if the two specified vector types are
1851/// compatible.
1852static bool areCompatVectorTypes(const VectorType *LHS,
1853 const VectorType *RHS) {
1854 assert(LHS->isCanonical() && RHS->isCanonical());
1855 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001856 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00001857}
1858
Eli Friedman0d9549b2008-08-22 00:56:42 +00001859/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001860/// compatible for assignment from RHS to LHS. This handles validation of any
1861/// protocol qualifiers on the LHS or RHS.
1862///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001863bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1864 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001865 // Verify that the base decls are compatible: the RHS must be a subclass of
1866 // the LHS.
1867 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1868 return false;
1869
1870 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1871 // protocol qualified at all, then we are good.
1872 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1873 return true;
1874
1875 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1876 // isn't a superset.
1877 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1878 return true; // FIXME: should return false!
1879
1880 // Finally, we must have two protocol-qualified interfaces.
1881 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1882 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1883 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1884 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1885 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1886 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1887
1888 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1889 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1890 // LHS in a single parallel scan until we run out of elements in LHS.
1891 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1892 ObjCProtocolDecl *LHSProto = *LHSPI;
1893
1894 while (RHSPI != RHSPE) {
1895 ObjCProtocolDecl *RHSProto = *RHSPI++;
1896 // If the RHS has a protocol that the LHS doesn't, ignore it.
1897 if (RHSProto != LHSProto)
1898 continue;
1899
1900 // Otherwise, the RHS does have this element.
1901 ++LHSPI;
1902 if (LHSPI == LHSPE)
1903 return true; // All protocols in LHS exist in RHS.
1904
1905 LHSProto = *LHSPI;
1906 }
1907
1908 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1909 return false;
1910}
1911
Steve Naroff85f0dc52007-10-15 20:41:53 +00001912/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1913/// both shall have the identically qualified version of a compatible type.
1914/// C99 6.2.7p1: Two types have compatible types if their types are the
1915/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001916bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1917 return !mergeTypes(LHS, RHS).isNull();
1918}
1919
1920QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1921 const FunctionType *lbase = lhs->getAsFunctionType();
1922 const FunctionType *rbase = rhs->getAsFunctionType();
1923 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1924 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1925 bool allLTypes = true;
1926 bool allRTypes = true;
1927
1928 // Check return type
1929 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1930 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001931 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
1932 allLTypes = false;
1933 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
1934 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001935
1936 if (lproto && rproto) { // two C99 style function prototypes
1937 unsigned lproto_nargs = lproto->getNumArgs();
1938 unsigned rproto_nargs = rproto->getNumArgs();
1939
1940 // Compatible functions must have the same number of arguments
1941 if (lproto_nargs != rproto_nargs)
1942 return QualType();
1943
1944 // Variadic and non-variadic functions aren't compatible
1945 if (lproto->isVariadic() != rproto->isVariadic())
1946 return QualType();
1947
1948 // Check argument compatibility
1949 llvm::SmallVector<QualType, 10> types;
1950 for (unsigned i = 0; i < lproto_nargs; i++) {
1951 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1952 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1953 QualType argtype = mergeTypes(largtype, rargtype);
1954 if (argtype.isNull()) return QualType();
1955 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001956 if (getCanonicalType(argtype) != getCanonicalType(largtype))
1957 allLTypes = false;
1958 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
1959 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001960 }
1961 if (allLTypes) return lhs;
1962 if (allRTypes) return rhs;
1963 return getFunctionType(retType, types.begin(), types.size(),
1964 lproto->isVariadic());
1965 }
1966
1967 if (lproto) allRTypes = false;
1968 if (rproto) allLTypes = false;
1969
1970 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1971 if (proto) {
1972 if (proto->isVariadic()) return QualType();
1973 // Check that the types are compatible with the types that
1974 // would result from default argument promotions (C99 6.7.5.3p15).
1975 // The only types actually affected are promotable integer
1976 // types and floats, which would be passed as a different
1977 // type depending on whether the prototype is visible.
1978 unsigned proto_nargs = proto->getNumArgs();
1979 for (unsigned i = 0; i < proto_nargs; ++i) {
1980 QualType argTy = proto->getArgType(i);
1981 if (argTy->isPromotableIntegerType() ||
1982 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1983 return QualType();
1984 }
1985
1986 if (allLTypes) return lhs;
1987 if (allRTypes) return rhs;
1988 return getFunctionType(retType, proto->arg_type_begin(),
1989 proto->getNumArgs(), lproto->isVariadic());
1990 }
1991
1992 if (allLTypes) return lhs;
1993 if (allRTypes) return rhs;
1994 return getFunctionTypeNoProto(retType);
1995}
1996
1997QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00001998 // C++ [expr]: If an expression initially has the type "reference to T", the
1999 // type is adjusted to "T" prior to any further analysis, the expression
2000 // designates the object or function denoted by the reference, and the
2001 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002002 // FIXME: C++ shouldn't be going through here! The rules are different
2003 // enough that they should be handled separately.
2004 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002005 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002006 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002007 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002008
Eli Friedman0d9549b2008-08-22 00:56:42 +00002009 QualType LHSCan = getCanonicalType(LHS),
2010 RHSCan = getCanonicalType(RHS);
2011
2012 // If two types are identical, they are compatible.
2013 if (LHSCan == RHSCan)
2014 return LHS;
2015
2016 // If the qualifiers are different, the types aren't compatible
2017 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2018 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2019 return QualType();
2020
2021 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2022 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2023
Chris Lattnerc38d4522008-01-14 05:45:46 +00002024 // We want to consider the two function types to be the same for these
2025 // comparisons, just force one to the other.
2026 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2027 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002028
2029 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002030 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2031 LHSClass = Type::ConstantArray;
2032 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2033 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002034
Nate Begemanaf6ed502008-04-18 23:10:10 +00002035 // Canonicalize ExtVector -> Vector.
2036 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2037 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002038
Chris Lattner7cdcb252008-04-07 06:38:24 +00002039 // Consider qualified interfaces and interfaces the same.
2040 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2041 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002042
Chris Lattnerb5709e22008-04-07 05:43:21 +00002043 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002044 if (LHSClass != RHSClass) {
Steve Naroff44549772008-06-04 15:07:33 +00002045 // ID is compatible with all qualified id types.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002046 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002047 if (const PointerType *PT = RHS->getAsPointerType()) {
2048 QualType pType = PT->getPointeeType();
2049 if (isObjCIdType(pType))
Eli Friedman0d9549b2008-08-22 00:56:42 +00002050 return LHS;
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002051 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2052 // Unfortunately, this API is part of Sema (which we don't have access
2053 // to. Need to refactor. The following check is insufficient, since we
2054 // need to make sure the class implements the protocol.
2055 if (pType->isObjCInterfaceType())
2056 return LHS;
2057 }
Steve Naroff44549772008-06-04 15:07:33 +00002058 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002059 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002060 if (const PointerType *PT = LHS->getAsPointerType()) {
2061 QualType pType = PT->getPointeeType();
2062 if (isObjCIdType(pType))
Eli Friedman0d9549b2008-08-22 00:56:42 +00002063 return RHS;
Steve Naroff3b2ceea2008-10-20 18:19:10 +00002064 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2065 // Unfortunately, this API is part of Sema (which we don't have access
2066 // to. Need to refactor. The following check is insufficient, since we
2067 // need to make sure the class implements the protocol.
2068 if (pType->isObjCInterfaceType())
2069 return RHS;
2070 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002071 }
2072
Chris Lattnerc38d4522008-01-14 05:45:46 +00002073 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2074 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002075 if (const EnumType* ETy = LHS->getAsEnumType()) {
2076 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2077 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002078 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002079 if (const EnumType* ETy = RHS->getAsEnumType()) {
2080 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2081 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002082 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002083
Eli Friedman0d9549b2008-08-22 00:56:42 +00002084 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002085 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002086
Steve Naroffc88babe2008-01-09 22:43:08 +00002087 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002088 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002089 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002090 {
2091 // Merge two pointer types, while trying to preserve typedef info
2092 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2093 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2094 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2095 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002096 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2097 return LHS;
2098 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2099 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002100 return getPointerType(ResultType);
2101 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002102 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002103 {
2104 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2105 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2106 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2107 return QualType();
2108
2109 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2110 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2111 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2112 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002113 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2114 return LHS;
2115 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2116 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002117 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2118 ArrayType::ArraySizeModifier(), 0);
2119 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2120 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002121 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2122 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002123 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2124 return LHS;
2125 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2126 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002127 if (LVAT) {
2128 // FIXME: This isn't correct! But tricky to implement because
2129 // the array's size has to be the size of LHS, but the type
2130 // has to be different.
2131 return LHS;
2132 }
2133 if (RVAT) {
2134 // FIXME: This isn't correct! But tricky to implement because
2135 // the array's size has to be the size of RHS, but the type
2136 // has to be different.
2137 return RHS;
2138 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002139 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2140 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002141 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002142 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002143 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002144 return mergeFunctionTypes(LHS, RHS);
2145 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002146 // FIXME: Why are these compatible?
2147 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2148 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2149 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002150 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002151 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002152 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002153 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002154 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2155 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002156 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002157 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002158 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2159 // for checking assignment/comparison safety
2160 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002161 default:
2162 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002163 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002164 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002165}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002166
Chris Lattner1d78a862008-04-07 07:01:58 +00002167//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002168// Integer Predicates
2169//===----------------------------------------------------------------------===//
2170unsigned ASTContext::getIntWidth(QualType T) {
2171 if (T == BoolTy)
2172 return 1;
2173 // At the moment, only bool has padding bits
2174 return (unsigned)getTypeSize(T);
2175}
2176
2177QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2178 assert(T->isSignedIntegerType() && "Unexpected type");
2179 if (const EnumType* ETy = T->getAsEnumType())
2180 T = ETy->getDecl()->getIntegerType();
2181 const BuiltinType* BTy = T->getAsBuiltinType();
2182 assert (BTy && "Unexpected signed integer type");
2183 switch (BTy->getKind()) {
2184 case BuiltinType::Char_S:
2185 case BuiltinType::SChar:
2186 return UnsignedCharTy;
2187 case BuiltinType::Short:
2188 return UnsignedShortTy;
2189 case BuiltinType::Int:
2190 return UnsignedIntTy;
2191 case BuiltinType::Long:
2192 return UnsignedLongTy;
2193 case BuiltinType::LongLong:
2194 return UnsignedLongLongTy;
2195 default:
2196 assert(0 && "Unexpected signed integer type");
2197 return QualType();
2198 }
2199}
2200
2201
2202//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002203// Serialization Support
2204//===----------------------------------------------------------------------===//
2205
Ted Kremenek738e6c02007-10-31 17:10:13 +00002206/// Emit - Serialize an ASTContext object to Bitcode.
2207void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002208 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002209 S.EmitRef(SourceMgr);
2210 S.EmitRef(Target);
2211 S.EmitRef(Idents);
2212 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002213
Ted Kremenek68228a92007-10-31 22:44:07 +00002214 // Emit the size of the type vector so that we can reserve that size
2215 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002216 S.EmitInt(Types.size());
2217
Ted Kremenek034a78c2007-11-13 22:02:55 +00002218 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2219 I!=E;++I)
2220 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002221
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002222 S.EmitOwnedPtr(TUDecl);
2223
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002224 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002225}
2226
Ted Kremenekacba3612007-11-13 00:25:37 +00002227ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002228
2229 // Read the language options.
2230 LangOptions LOpts;
2231 LOpts.Read(D);
2232
Ted Kremenek68228a92007-10-31 22:44:07 +00002233 SourceManager &SM = D.ReadRef<SourceManager>();
2234 TargetInfo &t = D.ReadRef<TargetInfo>();
2235 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2236 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002237
Ted Kremenek68228a92007-10-31 22:44:07 +00002238 unsigned size_reserve = D.ReadInt();
2239
Ted Kremenek842126e2008-06-04 15:55:15 +00002240 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002241
Ted Kremenek034a78c2007-11-13 22:02:55 +00002242 for (unsigned i = 0; i < size_reserve; ++i)
2243 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002244
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002245 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2246
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002247 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002248
2249 return A;
2250}