blob: deb80900bb374b19f54a28973a1475ddcab69a5d [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
Chris Lattner4b009652007-07-25 00:24:17 +0000185 // C99 6.2.5p11.
186 FloatComplexTy = getComplexType(FloatTy);
187 DoubleComplexTy = getComplexType(DoubleTy);
188 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000189
190 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000191 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000192 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000193 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000194 ClassStructType = 0;
195
Ted Kremenek42730c52008-01-07 19:49:32 +0000196 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000197
198 // void * type
199 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000200}
201
202//===----------------------------------------------------------------------===//
203// Type Sizing and Analysis
204//===----------------------------------------------------------------------===//
205
Chris Lattner2a674dc2008-06-30 18:32:54 +0000206/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
207/// scalar floating point type.
208const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
209 const BuiltinType *BT = T->getAsBuiltinType();
210 assert(BT && "Not a floating point type!");
211 switch (BT->getKind()) {
212 default: assert(0 && "Not a floating point type!");
213 case BuiltinType::Float: return Target.getFloatFormat();
214 case BuiltinType::Double: return Target.getDoubleFormat();
215 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
216 }
217}
218
219
Chris Lattner4b009652007-07-25 00:24:17 +0000220/// getTypeSize - Return the size of the specified type, in bits. This method
221/// does not work on incomplete types.
222std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000223ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000224 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000225 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000226 unsigned Align;
227 switch (T->getTypeClass()) {
228 case Type::TypeName: assert(0 && "Not a canonical type!");
229 case Type::FunctionNoProto:
230 case Type::FunctionProto:
231 default:
232 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000233 case Type::VariableArray:
234 assert(0 && "VLAs not implemented yet!");
235 case Type::ConstantArray: {
236 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
237
Chris Lattner8cd0e932008-03-05 18:54:05 +0000238 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000239 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000240 Align = EltInfo.second;
241 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000242 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000243 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000244 case Type::Vector: {
245 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000246 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000247 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000248 // FIXME: This isn't right for unusual vectors
249 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000250 break;
251 }
252
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000253 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000254 switch (cast<BuiltinType>(T)->getKind()) {
255 default: assert(0 && "Unknown builtin type!");
256 case BuiltinType::Void:
257 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000258 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000259 Width = Target.getBoolWidth();
260 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000262 case BuiltinType::Char_S:
263 case BuiltinType::Char_U:
264 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000266 Width = Target.getCharWidth();
267 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000268 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000269 case BuiltinType::WChar:
270 Width = Target.getWCharWidth();
271 Align = Target.getWCharAlign();
272 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000273 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000274 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000275 Width = Target.getShortWidth();
276 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000277 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000278 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000279 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000280 Width = Target.getIntWidth();
281 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000282 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000283 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000284 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000285 Width = Target.getLongWidth();
286 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000287 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000288 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000289 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000290 Width = Target.getLongLongWidth();
291 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000292 break;
293 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000294 Width = Target.getFloatWidth();
295 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000296 break;
297 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000298 Width = Target.getDoubleWidth();
299 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000300 break;
301 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000302 Width = Target.getLongDoubleWidth();
303 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000304 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000305 }
306 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000307 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000308 // FIXME: Pointers into different addr spaces could have different sizes and
309 // alignment requirements: getPointerInfo should take an AddrSpace.
310 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000311 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000312 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000313 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000314 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000315 case Type::BlockPointer: {
316 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
317 Width = Target.getPointerWidth(AS);
318 Align = Target.getPointerAlign(AS);
319 break;
320 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000321 case Type::Pointer: {
322 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000323 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000324 Align = Target.getPointerAlign(AS);
325 break;
326 }
Chris Lattner4b009652007-07-25 00:24:17 +0000327 case Type::Reference:
328 // "When applied to a reference or a reference type, the result is the size
329 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000330 // FIXME: This is wrong for struct layout: a reference in a struct has
331 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000332 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000333
334 case Type::Complex: {
335 // Complex types have the same alignment as their elements, but twice the
336 // size.
337 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000338 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000339 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000340 Align = EltInfo.second;
341 break;
342 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000343 case Type::ObjCInterface: {
344 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
345 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
346 Width = Layout.getSize();
347 Align = Layout.getAlignment();
348 break;
349 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000350 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000351 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
352 Width = 1;
353 Align = 1;
354 break;
355 }
356
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000357 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
358 return getTypeInfo(ET->getDecl()->getIntegerType());
359
360 RecordType *RT = cast<RecordType>(T);
361 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
362 Width = Layout.getSize();
363 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000364 break;
365 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000366 }
Chris Lattner4b009652007-07-25 00:24:17 +0000367
368 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000369 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000370}
371
Devang Patelbfe323c2008-06-04 21:22:16 +0000372/// LayoutField - Field layout.
373void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000374 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000375 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000376 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000377 uint64_t FieldOffset = IsUnion ? 0 : Size;
378 uint64_t FieldSize;
379 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000380
381 // FIXME: Should this override struct packing? Probably we want to
382 // take the minimum?
383 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
384 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000385
386 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
387 // TODO: Need to check this algorithm on other targets!
388 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000389 FieldSize =
390 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000391
392 std::pair<uint64_t, unsigned> FieldInfo =
393 Context.getTypeInfo(FD->getType());
394 uint64_t TypeSize = FieldInfo.first;
395
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000396 // Determine the alignment of this bitfield. The packing
397 // attributes define a maximum and the alignment attribute defines
398 // a minimum.
399 // FIXME: What is the right behavior when the specified alignment
400 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000401 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000402 if (FieldPacking)
403 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000404 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
405 FieldAlign = std::max(FieldAlign, AA->getAlignment());
406
407 // Check if we need to add padding to give the field the correct
408 // alignment.
409 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
410 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
411
412 // Padding members don't affect overall alignment
413 if (!FD->getIdentifier())
414 FieldAlign = 1;
415 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000416 if (FD->getType()->isIncompleteArrayType()) {
417 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000418 // query getTypeInfo about these, so we figure it out here.
419 // Flexible array members don't have any size, but they
420 // have to be aligned appropriately for their element type.
421 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000422 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000423 FieldAlign = Context.getTypeAlign(ATy->getElementType());
424 } else {
425 std::pair<uint64_t, unsigned> FieldInfo =
426 Context.getTypeInfo(FD->getType());
427 FieldSize = FieldInfo.first;
428 FieldAlign = FieldInfo.second;
429 }
430
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000431 // Determine the alignment of this bitfield. The packing
432 // attributes define a maximum and the alignment attribute defines
433 // a minimum. Additionally, the packing alignment must be at least
434 // a byte for non-bitfields.
435 //
436 // FIXME: What is the right behavior when the specified alignment
437 // is smaller than the specified packing?
438 if (FieldPacking)
439 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000440 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
441 FieldAlign = std::max(FieldAlign, AA->getAlignment());
442
443 // Round up the current record size to the field's alignment boundary.
444 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
445 }
446
447 // Place this field at the current location.
448 FieldOffsets[FieldNo] = FieldOffset;
449
450 // Reserve space for this field.
451 if (IsUnion) {
452 Size = std::max(Size, FieldSize);
453 } else {
454 Size = FieldOffset + FieldSize;
455 }
456
457 // Remember max struct/class alignment.
458 Alignment = std::max(Alignment, FieldAlign);
459}
460
Devang Patel4b6bf702008-06-04 21:54:36 +0000461
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000462/// getASTObjcInterfaceLayout - Get or compute information about the layout of
463/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000464/// position information.
465const ASTRecordLayout &
466ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
467 // Look up this layout, if already laid out, return what we have.
468 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
469 if (Entry) return *Entry;
470
471 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
472 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000473 ASTRecordLayout *NewEntry = NULL;
474 unsigned FieldCount = D->ivar_size();
475 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
476 FieldCount++;
477 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
478 unsigned Alignment = SL.getAlignment();
479 uint64_t Size = SL.getSize();
480 NewEntry = new ASTRecordLayout(Size, Alignment);
481 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000482 // Super class is at the beginning of the layout.
483 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000484 } else {
485 NewEntry = new ASTRecordLayout();
486 NewEntry->InitializeLayout(FieldCount);
487 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000488 Entry = NewEntry;
489
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000490 unsigned StructPacking = 0;
491 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
492 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000493
494 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
495 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
496 AA->getAlignment()));
497
498 // Layout each ivar sequentially.
499 unsigned i = 0;
500 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
501 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
502 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000503 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000504 }
505
506 // Finally, round the size of the total struct up to the alignment of the
507 // struct itself.
508 NewEntry->FinalizeLayout();
509 return *NewEntry;
510}
511
Devang Patel7a78e432007-11-01 19:11:01 +0000512/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000513/// specified record (struct/union/class), which indicates its size and field
514/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000515const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000516 D = D->getDefinition(*this);
517 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000518
Chris Lattner4b009652007-07-25 00:24:17 +0000519 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000520 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000521 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000522
Devang Patel7a78e432007-11-01 19:11:01 +0000523 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
524 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
525 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000526 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000527
Devang Patelbfe323c2008-06-04 21:22:16 +0000528 NewEntry->InitializeLayout(D->getNumMembers());
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000529 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000530
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000531 unsigned StructPacking = 0;
532 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
533 StructPacking = PA->getAlignment();
534
Eli Friedman5949a022008-05-30 09:31:38 +0000535 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000536 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
537 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000538
Eli Friedman5949a022008-05-30 09:31:38 +0000539 // Layout each field, for now, just sequentially, respecting alignment. In
540 // the future, this will need to be tweakable by targets.
541 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
542 const FieldDecl *FD = D->getMember(i);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000543 NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000544 }
Eli Friedman5949a022008-05-30 09:31:38 +0000545
546 // Finally, round the size of the total struct up to the alignment of the
547 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000548 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000549 return *NewEntry;
550}
551
Chris Lattner4b009652007-07-25 00:24:17 +0000552//===----------------------------------------------------------------------===//
553// Type creation/memoization methods
554//===----------------------------------------------------------------------===//
555
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000556QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000557 QualType CanT = getCanonicalType(T);
558 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000559 return T;
560
561 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
562 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000563 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000564 "Type is already address space qualified");
565
566 // Check if we've already instantiated an address space qual'd type of this
567 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000568 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000569 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000570 void *InsertPos = 0;
571 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
572 return QualType(ASQy, 0);
573
574 // If the base type isn't canonical, this won't be a canonical type either,
575 // so fill in the canonical type field.
576 QualType Canonical;
577 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000578 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000579
580 // Get the new insert position for the node we care about.
581 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000582 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000583 }
Chris Lattner35fef522008-02-20 20:55:12 +0000584 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000585 ASQualTypes.InsertNode(New, InsertPos);
586 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000587 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000588}
589
Chris Lattner4b009652007-07-25 00:24:17 +0000590
591/// getComplexType - Return the uniqued reference to the type for a complex
592/// number with the specified element type.
593QualType ASTContext::getComplexType(QualType T) {
594 // Unique pointers, to guarantee there is only one pointer of a particular
595 // structure.
596 llvm::FoldingSetNodeID ID;
597 ComplexType::Profile(ID, T);
598
599 void *InsertPos = 0;
600 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
601 return QualType(CT, 0);
602
603 // If the pointee type isn't canonical, this won't be a canonical type either,
604 // so fill in the canonical type field.
605 QualType Canonical;
606 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000607 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000608
609 // Get the new insert position for the node we care about.
610 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000611 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000612 }
613 ComplexType *New = new ComplexType(T, Canonical);
614 Types.push_back(New);
615 ComplexTypes.InsertNode(New, InsertPos);
616 return QualType(New, 0);
617}
618
619
620/// getPointerType - Return the uniqued reference to the type for a pointer to
621/// the specified type.
622QualType ASTContext::getPointerType(QualType T) {
623 // Unique pointers, to guarantee there is only one pointer of a particular
624 // structure.
625 llvm::FoldingSetNodeID ID;
626 PointerType::Profile(ID, T);
627
628 void *InsertPos = 0;
629 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
630 return QualType(PT, 0);
631
632 // If the pointee type isn't canonical, this won't be a canonical type either,
633 // so fill in the canonical type field.
634 QualType Canonical;
635 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000636 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000637
638 // Get the new insert position for the node we care about.
639 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000640 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000641 }
642 PointerType *New = new PointerType(T, Canonical);
643 Types.push_back(New);
644 PointerTypes.InsertNode(New, InsertPos);
645 return QualType(New, 0);
646}
647
Steve Naroff7aa54752008-08-27 16:04:49 +0000648/// getBlockPointerType - Return the uniqued reference to the type for
649/// a pointer to the specified block.
650QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000651 assert(T->isFunctionType() && "block of function types only");
652 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000653 // structure.
654 llvm::FoldingSetNodeID ID;
655 BlockPointerType::Profile(ID, T);
656
657 void *InsertPos = 0;
658 if (BlockPointerType *PT =
659 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
660 return QualType(PT, 0);
661
Steve Narofffd5b19d2008-08-28 19:20:44 +0000662 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000663 // type either so fill in the canonical type field.
664 QualType Canonical;
665 if (!T->isCanonical()) {
666 Canonical = getBlockPointerType(getCanonicalType(T));
667
668 // Get the new insert position for the node we care about.
669 BlockPointerType *NewIP =
670 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000671 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000672 }
673 BlockPointerType *New = new BlockPointerType(T, Canonical);
674 Types.push_back(New);
675 BlockPointerTypes.InsertNode(New, InsertPos);
676 return QualType(New, 0);
677}
678
Chris Lattner4b009652007-07-25 00:24:17 +0000679/// getReferenceType - Return the uniqued reference to the type for a reference
680/// to the specified type.
681QualType ASTContext::getReferenceType(QualType T) {
682 // Unique pointers, to guarantee there is only one pointer of a particular
683 // structure.
684 llvm::FoldingSetNodeID ID;
685 ReferenceType::Profile(ID, T);
686
687 void *InsertPos = 0;
688 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
689 return QualType(RT, 0);
690
691 // If the referencee type isn't canonical, this won't be a canonical type
692 // either, so fill in the canonical type field.
693 QualType Canonical;
694 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000695 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000696
697 // Get the new insert position for the node we care about.
698 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000699 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000700 }
701
702 ReferenceType *New = new ReferenceType(T, Canonical);
703 Types.push_back(New);
704 ReferenceTypes.InsertNode(New, InsertPos);
705 return QualType(New, 0);
706}
707
Steve Naroff83c13012007-08-30 01:06:46 +0000708/// getConstantArrayType - Return the unique reference to the type for an
709/// array of the specified element type.
710QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000711 const llvm::APInt &ArySize,
712 ArrayType::ArraySizeModifier ASM,
713 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000714 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000715 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000716
717 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000718 if (ConstantArrayType *ATP =
719 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000720 return QualType(ATP, 0);
721
722 // If the element type isn't canonical, this won't be a canonical type either,
723 // so fill in the canonical type field.
724 QualType Canonical;
725 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000726 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000727 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000728 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000729 ConstantArrayType *NewIP =
730 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000731 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000732 }
733
Steve Naroff24c9b982007-08-30 18:10:14 +0000734 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
735 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000736 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000737 Types.push_back(New);
738 return QualType(New, 0);
739}
740
Steve Naroffe2579e32007-08-30 18:14:25 +0000741/// getVariableArrayType - Returns a non-unique reference to the type for a
742/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000743QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
744 ArrayType::ArraySizeModifier ASM,
745 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000746 // Since we don't unique expressions, it isn't possible to unique VLA's
747 // that have an expression provided for their size.
748
749 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
750 ASM, EltTypeQuals);
751
752 VariableArrayTypes.push_back(New);
753 Types.push_back(New);
754 return QualType(New, 0);
755}
756
757QualType ASTContext::getIncompleteArrayType(QualType EltTy,
758 ArrayType::ArraySizeModifier ASM,
759 unsigned EltTypeQuals) {
760 llvm::FoldingSetNodeID ID;
761 IncompleteArrayType::Profile(ID, EltTy);
762
763 void *InsertPos = 0;
764 if (IncompleteArrayType *ATP =
765 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
766 return QualType(ATP, 0);
767
768 // If the element type isn't canonical, this won't be a canonical type
769 // either, so fill in the canonical type field.
770 QualType Canonical;
771
772 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000773 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000774 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000775
776 // Get the new insert position for the node we care about.
777 IncompleteArrayType *NewIP =
778 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000779 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000780 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000781
782 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
783 ASM, EltTypeQuals);
784
785 IncompleteArrayTypes.InsertNode(New, InsertPos);
786 Types.push_back(New);
787 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000788}
789
Chris Lattner4b009652007-07-25 00:24:17 +0000790/// getVectorType - Return the unique reference to a vector type of
791/// the specified element type and size. VectorType must be a built-in type.
792QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
793 BuiltinType *baseType;
794
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000795 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000796 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
797
798 // Check if we've already instantiated a vector of this type.
799 llvm::FoldingSetNodeID ID;
800 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
801 void *InsertPos = 0;
802 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
803 return QualType(VTP, 0);
804
805 // If the element type isn't canonical, this won't be a canonical type either,
806 // so fill in the canonical type field.
807 QualType Canonical;
808 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000809 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000810
811 // Get the new insert position for the node we care about.
812 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000813 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000814 }
815 VectorType *New = new VectorType(vecType, NumElts, Canonical);
816 VectorTypes.InsertNode(New, InsertPos);
817 Types.push_back(New);
818 return QualType(New, 0);
819}
820
Nate Begemanaf6ed502008-04-18 23:10:10 +0000821/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000822/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000823QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000824 BuiltinType *baseType;
825
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000826 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000827 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000828
829 // Check if we've already instantiated a vector of this type.
830 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000831 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000832 void *InsertPos = 0;
833 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
834 return QualType(VTP, 0);
835
836 // If the element type isn't canonical, this won't be a canonical type either,
837 // so fill in the canonical type field.
838 QualType Canonical;
839 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000840 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000841
842 // Get the new insert position for the node we care about.
843 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000844 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000845 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000846 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000847 VectorTypes.InsertNode(New, InsertPos);
848 Types.push_back(New);
849 return QualType(New, 0);
850}
851
852/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
853///
854QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
855 // Unique functions, to guarantee there is only one function of a particular
856 // structure.
857 llvm::FoldingSetNodeID ID;
858 FunctionTypeNoProto::Profile(ID, ResultTy);
859
860 void *InsertPos = 0;
861 if (FunctionTypeNoProto *FT =
862 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
863 return QualType(FT, 0);
864
865 QualType Canonical;
866 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000867 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000868
869 // Get the new insert position for the node we care about.
870 FunctionTypeNoProto *NewIP =
871 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000872 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000873 }
874
875 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
876 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000877 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000878 return QualType(New, 0);
879}
880
881/// getFunctionType - Return a normal function type with a typed argument
882/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000883QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Chris Lattner4b009652007-07-25 00:24:17 +0000884 unsigned NumArgs, bool isVariadic) {
885 // Unique functions, to guarantee there is only one function of a particular
886 // structure.
887 llvm::FoldingSetNodeID ID;
888 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
889
890 void *InsertPos = 0;
891 if (FunctionTypeProto *FTP =
892 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
893 return QualType(FTP, 0);
894
895 // Determine whether the type being created is already canonical or not.
896 bool isCanonical = ResultTy->isCanonical();
897 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
898 if (!ArgArray[i]->isCanonical())
899 isCanonical = false;
900
901 // If this type isn't canonical, get the canonical version of it.
902 QualType Canonical;
903 if (!isCanonical) {
904 llvm::SmallVector<QualType, 16> CanonicalArgs;
905 CanonicalArgs.reserve(NumArgs);
906 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000907 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000908
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000909 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000910 &CanonicalArgs[0], NumArgs,
911 isVariadic);
912
913 // Get the new insert position for the node we care about.
914 FunctionTypeProto *NewIP =
915 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000916 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000917 }
918
919 // FunctionTypeProto objects are not allocated with new because they have a
920 // variable size array (for parameter types) at the end of them.
921 FunctionTypeProto *FTP =
922 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
923 NumArgs*sizeof(QualType));
924 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
925 Canonical);
926 Types.push_back(FTP);
927 FunctionTypeProtos.InsertNode(FTP, InsertPos);
928 return QualType(FTP, 0);
929}
930
Douglas Gregor1d661552008-04-13 21:07:44 +0000931/// getTypeDeclType - Return the unique reference to the type for the
932/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000933QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000934 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +0000935 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
936
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000937 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000938 return getTypedefType(Typedef);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000939 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000940 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000941
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000942 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000943 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
944 : new CXXRecordType(CXXRecord);
945 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000946 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000947 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
948 : new RecordType(Record);
949 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000950 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000951 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000952 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000953 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000954
Ted Kremenek46a837c2008-09-05 17:16:31 +0000955 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000956 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000957}
958
Ted Kremenek46a837c2008-09-05 17:16:31 +0000959/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
960/// about which RecordDecl serves as the definition of a particular
961/// struct/union/class. This will eventually be used by enums as well.
962void ASTContext::setTagDefinition(TagDecl* D) {
963 assert (D->isDefinition());
964 cast<TagType>(D->TypeForDecl)->decl = D;
965}
966
Chris Lattner4b009652007-07-25 00:24:17 +0000967/// getTypedefType - Return the unique reference to the type for the
968/// specified typename decl.
969QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
970 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
971
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000972 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000973 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000974 Types.push_back(Decl->TypeForDecl);
975 return QualType(Decl->TypeForDecl, 0);
976}
977
Ted Kremenek42730c52008-01-07 19:49:32 +0000978/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000979/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000980QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000981 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
982
Ted Kremenek42730c52008-01-07 19:49:32 +0000983 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000984 Types.push_back(Decl->TypeForDecl);
985 return QualType(Decl->TypeForDecl, 0);
986}
987
Chris Lattnere1352302008-04-07 04:56:42 +0000988/// CmpProtocolNames - Comparison predicate for sorting protocols
989/// alphabetically.
990static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
991 const ObjCProtocolDecl *RHS) {
992 return strcmp(LHS->getName(), RHS->getName()) < 0;
993}
994
995static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
996 unsigned &NumProtocols) {
997 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
998
999 // Sort protocols, keyed by name.
1000 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1001
1002 // Remove duplicates.
1003 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1004 NumProtocols = ProtocolsEnd-Protocols;
1005}
1006
1007
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001008/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1009/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001010QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1011 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001012 // Sort the protocol list alphabetically to canonicalize it.
1013 SortAndUniqueProtocols(Protocols, NumProtocols);
1014
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001015 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001016 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001017
1018 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 if (ObjCQualifiedInterfaceType *QT =
1020 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001021 return QualType(QT, 0);
1022
1023 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001024 ObjCQualifiedInterfaceType *QType =
1025 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001026 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001027 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001028 return QualType(QType, 0);
1029}
1030
Chris Lattnere1352302008-04-07 04:56:42 +00001031/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1032/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001033QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001034 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001035 // Sort the protocol list alphabetically to canonicalize it.
1036 SortAndUniqueProtocols(Protocols, NumProtocols);
1037
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001038 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001040
1041 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001042 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001043 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001044 return QualType(QT, 0);
1045
1046 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001047 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001048 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001050 return QualType(QType, 0);
1051}
1052
Steve Naroff0604dd92007-08-01 18:02:17 +00001053/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1054/// TypeOfExpr AST's (since expression's are never shared). For example,
1055/// multiple declarations that refer to "typeof(x)" all contain different
1056/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1057/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001058QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001059 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001060 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1061 Types.push_back(toe);
1062 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001063}
1064
Steve Naroff0604dd92007-08-01 18:02:17 +00001065/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1066/// TypeOfType AST's. The only motivation to unique these nodes would be
1067/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1068/// an issue. This doesn't effect the type checker, since it operates
1069/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001070QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001071 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001072 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1073 Types.push_back(tot);
1074 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001075}
1076
Chris Lattner4b009652007-07-25 00:24:17 +00001077/// getTagDeclType - Return the unique reference to the type for the
1078/// specified TagDecl (struct/union/class/enum) decl.
1079QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001080 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001081 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001082}
1083
1084/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1085/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1086/// needs to agree with the definition in <stddef.h>.
1087QualType ASTContext::getSizeType() const {
1088 // On Darwin, size_t is defined as a "long unsigned int".
1089 // FIXME: should derive from "Target".
1090 return UnsignedLongTy;
1091}
1092
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001093/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001094/// width of characters in wide strings, The value is target dependent and
1095/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001096QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001097 if (LangOpts.CPlusPlus)
1098 return WCharTy;
1099
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001100 // On Darwin, wchar_t is defined as a "int".
1101 // FIXME: should derive from "Target".
1102 return IntTy;
1103}
1104
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001105/// getSignedWCharType - Return the type of "signed wchar_t".
1106/// Used when in C++, as a GCC extension.
1107QualType ASTContext::getSignedWCharType() const {
1108 // FIXME: derive from "Target" ?
1109 return WCharTy;
1110}
1111
1112/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1113/// Used when in C++, as a GCC extension.
1114QualType ASTContext::getUnsignedWCharType() const {
1115 // FIXME: derive from "Target" ?
1116 return UnsignedIntTy;
1117}
1118
Chris Lattner4b009652007-07-25 00:24:17 +00001119/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1120/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1121QualType ASTContext::getPointerDiffType() const {
1122 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1123 // FIXME: should derive from "Target".
1124 return IntTy;
1125}
1126
Chris Lattner19eb97e2008-04-02 05:18:44 +00001127//===----------------------------------------------------------------------===//
1128// Type Operators
1129//===----------------------------------------------------------------------===//
1130
Chris Lattner3dae6f42008-04-06 22:41:35 +00001131/// getCanonicalType - Return the canonical (structural) type corresponding to
1132/// the specified potentially non-canonical type. The non-canonical version
1133/// of a type may have many "decorated" versions of types. Decorators can
1134/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1135/// to be free of any of these, allowing two canonical types to be compared
1136/// for exact equality with a simple pointer comparison.
1137QualType ASTContext::getCanonicalType(QualType T) {
1138 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001139
1140 // If the result has type qualifiers, make sure to canonicalize them as well.
1141 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1142 if (TypeQuals == 0) return CanType;
1143
1144 // If the type qualifiers are on an array type, get the canonical type of the
1145 // array with the qualifiers applied to the element type.
1146 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1147 if (!AT)
1148 return CanType.getQualifiedType(TypeQuals);
1149
1150 // Get the canonical version of the element with the extra qualifiers on it.
1151 // This can recursively sink qualifiers through multiple levels of arrays.
1152 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1153 NewEltTy = getCanonicalType(NewEltTy);
1154
1155 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1156 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1157 CAT->getIndexTypeQualifier());
1158 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1159 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1160 IAT->getIndexTypeQualifier());
1161
1162 // FIXME: What is the ownership of size expressions in VLAs?
1163 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1164 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1165 VAT->getSizeModifier(),
1166 VAT->getIndexTypeQualifier());
1167}
1168
1169
1170const ArrayType *ASTContext::getAsArrayType(QualType T) {
1171 // Handle the non-qualified case efficiently.
1172 if (T.getCVRQualifiers() == 0) {
1173 // Handle the common positive case fast.
1174 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1175 return AT;
1176 }
1177
1178 // Handle the common negative case fast, ignoring CVR qualifiers.
1179 QualType CType = T->getCanonicalTypeInternal();
1180
1181 // Make sure to look through type qualifiers (like ASQuals) for the negative
1182 // test.
1183 if (!isa<ArrayType>(CType) &&
1184 !isa<ArrayType>(CType.getUnqualifiedType()))
1185 return 0;
1186
1187 // Apply any CVR qualifiers from the array type to the element type. This
1188 // implements C99 6.7.3p8: "If the specification of an array type includes
1189 // any type qualifiers, the element type is so qualified, not the array type."
1190
1191 // If we get here, we either have type qualifiers on the type, or we have
1192 // sugar such as a typedef in the way. If we have type qualifiers on the type
1193 // we must propagate them down into the elemeng type.
1194 unsigned CVRQuals = T.getCVRQualifiers();
1195 unsigned AddrSpace = 0;
1196 Type *Ty = T.getTypePtr();
1197
1198 // Rip through ASQualType's and typedefs to get to a concrete type.
1199 while (1) {
1200 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1201 AddrSpace = ASQT->getAddressSpace();
1202 Ty = ASQT->getBaseType();
1203 } else {
1204 T = Ty->getDesugaredType();
1205 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1206 break;
1207 CVRQuals |= T.getCVRQualifiers();
1208 Ty = T.getTypePtr();
1209 }
1210 }
1211
1212 // If we have a simple case, just return now.
1213 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1214 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1215 return ATy;
1216
1217 // Otherwise, we have an array and we have qualifiers on it. Push the
1218 // qualifiers into the array element type and return a new array type.
1219 // Get the canonical version of the element with the extra qualifiers on it.
1220 // This can recursively sink qualifiers through multiple levels of arrays.
1221 QualType NewEltTy = ATy->getElementType();
1222 if (AddrSpace)
1223 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1224 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1225
1226 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1227 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1228 CAT->getSizeModifier(),
1229 CAT->getIndexTypeQualifier()));
1230 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1231 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1232 IAT->getSizeModifier(),
1233 IAT->getIndexTypeQualifier()));
1234
1235 // FIXME: What is the ownership of size expressions in VLAs?
1236 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1237 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1238 VAT->getSizeModifier(),
1239 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001240}
1241
1242
Chris Lattner19eb97e2008-04-02 05:18:44 +00001243/// getArrayDecayedType - Return the properly qualified result of decaying the
1244/// specified array type to a pointer. This operation is non-trivial when
1245/// handling typedefs etc. The canonical type of "T" must be an array type,
1246/// this returns a pointer to a properly qualified element of the array.
1247///
1248/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1249QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001250 // Get the element type with 'getAsArrayType' so that we don't lose any
1251 // typedefs in the element type of the array. This also handles propagation
1252 // of type qualifiers from the array type into the element type if present
1253 // (C99 6.7.3p8).
1254 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1255 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001256
Chris Lattnera1923f62008-08-04 07:31:14 +00001257 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001258
1259 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001260 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001261}
1262
Chris Lattner4b009652007-07-25 00:24:17 +00001263/// getFloatingRank - Return a relative rank for floating point types.
1264/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001265static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001266 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001267 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001268
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001269 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001270 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001271 case BuiltinType::Float: return FloatRank;
1272 case BuiltinType::Double: return DoubleRank;
1273 case BuiltinType::LongDouble: return LongDoubleRank;
1274 }
1275}
1276
Steve Narofffa0c4532007-08-27 01:41:48 +00001277/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1278/// point or a complex type (based on typeDomain/typeSize).
1279/// 'typeDomain' is a real floating point or complex type.
1280/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001281QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1282 QualType Domain) const {
1283 FloatingRank EltRank = getFloatingRank(Size);
1284 if (Domain->isComplexType()) {
1285 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001286 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001287 case FloatRank: return FloatComplexTy;
1288 case DoubleRank: return DoubleComplexTy;
1289 case LongDoubleRank: return LongDoubleComplexTy;
1290 }
Chris Lattner4b009652007-07-25 00:24:17 +00001291 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001292
1293 assert(Domain->isRealFloatingType() && "Unknown domain!");
1294 switch (EltRank) {
1295 default: assert(0 && "getFloatingRank(): illegal value for rank");
1296 case FloatRank: return FloatTy;
1297 case DoubleRank: return DoubleTy;
1298 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001299 }
Chris Lattner4b009652007-07-25 00:24:17 +00001300}
1301
Chris Lattner51285d82008-04-06 23:55:33 +00001302/// getFloatingTypeOrder - Compare the rank of the two specified floating
1303/// point types, ignoring the domain of the type (i.e. 'double' ==
1304/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1305/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001306int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1307 FloatingRank LHSR = getFloatingRank(LHS);
1308 FloatingRank RHSR = getFloatingRank(RHS);
1309
1310 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001311 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001312 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001313 return 1;
1314 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001315}
1316
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001317/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1318/// routine will assert if passed a built-in type that isn't an integer or enum,
1319/// or if it is not canonicalized.
1320static unsigned getIntegerRank(Type *T) {
1321 assert(T->isCanonical() && "T should be canonicalized");
1322 if (isa<EnumType>(T))
1323 return 4;
1324
1325 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001326 default: assert(0 && "getIntegerRank(): not a built-in integer");
1327 case BuiltinType::Bool:
1328 return 1;
1329 case BuiltinType::Char_S:
1330 case BuiltinType::Char_U:
1331 case BuiltinType::SChar:
1332 case BuiltinType::UChar:
1333 return 2;
1334 case BuiltinType::Short:
1335 case BuiltinType::UShort:
1336 return 3;
1337 case BuiltinType::Int:
1338 case BuiltinType::UInt:
1339 return 4;
1340 case BuiltinType::Long:
1341 case BuiltinType::ULong:
1342 return 5;
1343 case BuiltinType::LongLong:
1344 case BuiltinType::ULongLong:
1345 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001346 }
1347}
1348
Chris Lattner51285d82008-04-06 23:55:33 +00001349/// getIntegerTypeOrder - Returns the highest ranked integer type:
1350/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1351/// LHS < RHS, return -1.
1352int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001353 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1354 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001355 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001356
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001357 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1358 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001359
Chris Lattner51285d82008-04-06 23:55:33 +00001360 unsigned LHSRank = getIntegerRank(LHSC);
1361 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001362
Chris Lattner51285d82008-04-06 23:55:33 +00001363 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1364 if (LHSRank == RHSRank) return 0;
1365 return LHSRank > RHSRank ? 1 : -1;
1366 }
Chris Lattner4b009652007-07-25 00:24:17 +00001367
Chris Lattner51285d82008-04-06 23:55:33 +00001368 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1369 if (LHSUnsigned) {
1370 // If the unsigned [LHS] type is larger, return it.
1371 if (LHSRank >= RHSRank)
1372 return 1;
1373
1374 // If the signed type can represent all values of the unsigned type, it
1375 // wins. Because we are dealing with 2's complement and types that are
1376 // powers of two larger than each other, this is always safe.
1377 return -1;
1378 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001379
Chris Lattner51285d82008-04-06 23:55:33 +00001380 // If the unsigned [RHS] type is larger, return it.
1381 if (RHSRank >= LHSRank)
1382 return -1;
1383
1384 // If the signed type can represent all values of the unsigned type, it
1385 // wins. Because we are dealing with 2's complement and types that are
1386 // powers of two larger than each other, this is always safe.
1387 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001388}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001389
1390// getCFConstantStringType - Return the type used for constant CFStrings.
1391QualType ASTContext::getCFConstantStringType() {
1392 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001393 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001394 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001395 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001396 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001397
1398 // const int *isa;
1399 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001400 // int flags;
1401 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001402 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001403 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001404 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001405 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001406 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001407 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001408
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001409 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001410 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001411 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001412
Ted Kremenek46a837c2008-09-05 17:16:31 +00001413 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001414 }
1415
1416 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001417}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001418
Anders Carlssonf58cac72008-08-30 19:34:46 +00001419QualType ASTContext::getObjCFastEnumerationStateType()
1420{
1421 if (!ObjCFastEnumerationStateTypeDecl) {
1422 QualType FieldTypes[] = {
1423 UnsignedLongTy,
1424 getPointerType(ObjCIdType),
1425 getPointerType(UnsignedLongTy),
1426 getConstantArrayType(UnsignedLongTy,
1427 llvm::APInt(32, 5), ArrayType::Normal, 0)
1428 };
1429
1430 FieldDecl *FieldDecls[4];
1431 for (size_t i = 0; i < 4; ++i)
1432 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1433 FieldTypes[i]);
1434
1435 ObjCFastEnumerationStateTypeDecl =
1436 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001437 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonf58cac72008-08-30 19:34:46 +00001438
Ted Kremenek46a837c2008-09-05 17:16:31 +00001439 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001440 }
1441
1442 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1443}
1444
Anders Carlssone3f02572007-10-29 06:33:42 +00001445// This returns true if a type has been typedefed to BOOL:
1446// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001447static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001448 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001449 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001450
1451 return false;
1452}
1453
Ted Kremenek42730c52008-01-07 19:49:32 +00001454/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001455/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001456int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001457 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001458
1459 // Make all integer and enum types at least as large as an int
1460 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001461 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001462 // Treat arrays as pointers, since that's how they're passed in.
1463 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001464 sz = getTypeSize(VoidPtrTy);
1465 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001466}
1467
Ted Kremenek42730c52008-01-07 19:49:32 +00001468/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001469/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001470void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001471 std::string& S)
1472{
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001473 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001474 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001475 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001476 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001477 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001478 // Compute size of all parameters.
1479 // Start with computing size of a pointer in number of bytes.
1480 // FIXME: There might(should) be a better way of doing this computation!
1481 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001482 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001483 // The first two arguments (self and _cmd) are pointers; account for
1484 // their size.
1485 int ParmOffset = 2 * PtrSize;
1486 int NumOfParams = Decl->getNumParams();
1487 for (int i = 0; i < NumOfParams; i++) {
1488 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001489 int sz = getObjCEncodingTypeSize (PType);
1490 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001491 ParmOffset += sz;
1492 }
1493 S += llvm::utostr(ParmOffset);
1494 S += "@0:";
1495 S += llvm::utostr(PtrSize);
1496
1497 // Argument types.
1498 ParmOffset = 2 * PtrSize;
1499 for (int i = 0; i < NumOfParams; i++) {
1500 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001501 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001502 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001503 getObjCEncodingForTypeQualifier(
1504 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001505 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001506 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001507 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001508 }
1509}
1510
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001511/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1512/// method declaration. If non-NULL, Container must be either an
1513/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1514/// NULL when getting encodings for protocol properties.
1515void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1516 const Decl *Container,
1517 std::string& S)
1518{
1519 // Collect information from the property implementation decl(s).
1520 bool Dynamic = false;
1521 ObjCPropertyImplDecl *SynthesizePID = 0;
1522
1523 // FIXME: Duplicated code due to poor abstraction.
1524 if (Container) {
1525 if (const ObjCCategoryImplDecl *CID =
1526 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1527 for (ObjCCategoryImplDecl::propimpl_iterator
1528 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1529 ObjCPropertyImplDecl *PID = *i;
1530 if (PID->getPropertyDecl() == PD) {
1531 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1532 Dynamic = true;
1533 } else {
1534 SynthesizePID = PID;
1535 }
1536 }
1537 }
1538 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001539 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001540 for (ObjCCategoryImplDecl::propimpl_iterator
1541 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1542 ObjCPropertyImplDecl *PID = *i;
1543 if (PID->getPropertyDecl() == PD) {
1544 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1545 Dynamic = true;
1546 } else {
1547 SynthesizePID = PID;
1548 }
1549 }
1550 }
1551 }
1552 }
1553
1554 // FIXME: This is not very efficient.
1555 S = "T";
1556
1557 // Encode result type.
1558 // FIXME: GCC uses a generating_property_type_encoding mode during
1559 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001560 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001561
1562 if (PD->isReadOnly()) {
1563 S += ",R";
1564 } else {
1565 switch (PD->getSetterKind()) {
1566 case ObjCPropertyDecl::Assign: break;
1567 case ObjCPropertyDecl::Copy: S += ",C"; break;
1568 case ObjCPropertyDecl::Retain: S += ",&"; break;
1569 }
1570 }
1571
1572 // It really isn't clear at all what this means, since properties
1573 // are "dynamic by default".
1574 if (Dynamic)
1575 S += ",D";
1576
1577 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1578 S += ",G";
1579 S += PD->getGetterName().getName();
1580 }
1581
1582 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1583 S += ",S";
1584 S += PD->getSetterName().getName();
1585 }
1586
1587 if (SynthesizePID) {
1588 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1589 S += ",V";
1590 S += OID->getName();
1591 }
1592
1593 // FIXME: OBJCGC: weak & strong
1594}
1595
Fariborz Jahanian248db262008-01-22 22:44:46 +00001596void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001597 bool NameFields) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001598 // We follow the behavior of gcc, expanding structures which are
1599 // directly pointed to, and expanding embedded structures. Note that
1600 // these rules are sufficient to prevent recursive encoding of the
1601 // same type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001602 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001603}
1604
1605void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1606 bool ExpandPointedToStructures,
1607 bool ExpandStructures,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001608 bool NameFields) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001609 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001610 char encoding;
1611 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001612 default: assert(0 && "Unhandled builtin type kind");
1613 case BuiltinType::Void: encoding = 'v'; break;
1614 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001615 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001616 case BuiltinType::UChar: encoding = 'C'; break;
1617 case BuiltinType::UShort: encoding = 'S'; break;
1618 case BuiltinType::UInt: encoding = 'I'; break;
1619 case BuiltinType::ULong: encoding = 'L'; break;
1620 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001621 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001622 case BuiltinType::SChar: encoding = 'c'; break;
1623 case BuiltinType::Short: encoding = 's'; break;
1624 case BuiltinType::Int: encoding = 'i'; break;
1625 case BuiltinType::Long: encoding = 'l'; break;
1626 case BuiltinType::LongLong: encoding = 'q'; break;
1627 case BuiltinType::Float: encoding = 'f'; break;
1628 case BuiltinType::Double: encoding = 'd'; break;
1629 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001630 }
1631
1632 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001633 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001634 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001635 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001636 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1637 ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001638 ExpandStructures, NameFields);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001639 }
1640 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001641 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001642 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001643 S += '@';
1644 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001645 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001646 S += '#';
1647 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001648 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001649 S += ':';
1650 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001651 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001652
1653 if (PointeeTy->isCharType()) {
1654 // char pointer types should be encoded as '*' unless it is a
1655 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001656 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001657 S += '*';
1658 return;
1659 }
1660 }
1661
1662 S += '^';
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001663 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001664 false, ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001665 NameFields);
Chris Lattnera1923f62008-08-04 07:31:14 +00001666 } else if (const ArrayType *AT =
1667 // Ignore type qualifiers etc.
1668 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001669 S += '[';
1670
1671 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1672 S += llvm::utostr(CAT->getSize().getZExtValue());
1673 else
1674 assert(0 && "Unhandled array type!");
1675
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001676 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001677 false, ExpandStructures, NameFields);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001678 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001679 } else if (T->getAsFunctionType()) {
1680 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001681 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001682 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001683 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001684 // Anonymous structures print as '?'
1685 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1686 S += II->getName();
1687 } else {
1688 S += '?';
1689 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001690 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001691 S += '=';
1692 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001693 FieldDecl *FD = RDecl->getMember(i);
1694 if (NameFields) {
1695 S += '"';
1696 S += FD->getName();
1697 S += '"';
1698 }
1699
1700 // Special case bit-fields.
1701 if (const Expr *E = FD->getBitWidth()) {
1702 // FIXME: Fix constness.
1703 ASTContext *Ctx = const_cast<ASTContext*>(this);
1704 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1705 // FIXME: Obj-C is losing information about the type size
1706 // here. Investigate if this is a problem.
1707 S += 'b';
1708 S += llvm::utostr(N);
1709 } else {
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001710 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001711 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001712 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001713 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001714 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001715 } else if (T->isEnumeralType()) {
1716 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001717 } else if (T->isBlockPointerType()) {
1718 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001719 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001720 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001721}
1722
Ted Kremenek42730c52008-01-07 19:49:32 +00001723void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001724 std::string& S) const {
1725 if (QT & Decl::OBJC_TQ_In)
1726 S += 'n';
1727 if (QT & Decl::OBJC_TQ_Inout)
1728 S += 'N';
1729 if (QT & Decl::OBJC_TQ_Out)
1730 S += 'o';
1731 if (QT & Decl::OBJC_TQ_Bycopy)
1732 S += 'O';
1733 if (QT & Decl::OBJC_TQ_Byref)
1734 S += 'R';
1735 if (QT & Decl::OBJC_TQ_Oneway)
1736 S += 'V';
1737}
1738
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001739void ASTContext::setBuiltinVaListType(QualType T)
1740{
1741 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1742
1743 BuiltinVaListType = T;
1744}
1745
Ted Kremenek42730c52008-01-07 19:49:32 +00001746void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001747{
Ted Kremenek42730c52008-01-07 19:49:32 +00001748 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001749
1750 // typedef struct objc_object *id;
1751 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1752 assert(ptr && "'id' incorrectly typed");
1753 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1754 assert(rec && "'id' incorrectly typed");
1755 IdStructType = rec;
1756}
1757
Ted Kremenek42730c52008-01-07 19:49:32 +00001758void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001759{
Ted Kremenek42730c52008-01-07 19:49:32 +00001760 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001761
1762 // typedef struct objc_selector *SEL;
1763 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1764 assert(ptr && "'SEL' incorrectly typed");
1765 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1766 assert(rec && "'SEL' incorrectly typed");
1767 SelStructType = rec;
1768}
1769
Ted Kremenek42730c52008-01-07 19:49:32 +00001770void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001771{
Ted Kremenek42730c52008-01-07 19:49:32 +00001772 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001773}
1774
Ted Kremenek42730c52008-01-07 19:49:32 +00001775void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001776{
Ted Kremenek42730c52008-01-07 19:49:32 +00001777 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001778
1779 // typedef struct objc_class *Class;
1780 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1781 assert(ptr && "'Class' incorrectly typed");
1782 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1783 assert(rec && "'Class' incorrectly typed");
1784 ClassStructType = rec;
1785}
1786
Ted Kremenek42730c52008-01-07 19:49:32 +00001787void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1788 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001789 "'NSConstantString' type already set!");
1790
Ted Kremenek42730c52008-01-07 19:49:32 +00001791 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001792}
1793
Ted Kremenek118930e2008-07-24 23:58:27 +00001794
1795//===----------------------------------------------------------------------===//
1796// Type Predicates.
1797//===----------------------------------------------------------------------===//
1798
1799/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1800/// to an object type. This includes "id" and "Class" (two 'special' pointers
1801/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1802/// ID type).
1803bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1804 if (Ty->isObjCQualifiedIdType())
1805 return true;
1806
1807 if (!Ty->isPointerType())
1808 return false;
1809
1810 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1811 // pointer types. This looks for the typedef specifically, not for the
1812 // underlying type.
1813 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1814 return true;
1815
1816 // If this a pointer to an interface (e.g. NSString*), it is ok.
1817 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1818}
1819
Chris Lattner6ff358b2008-04-07 06:51:04 +00001820//===----------------------------------------------------------------------===//
1821// Type Compatibility Testing
1822//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001823
Steve Naroff3454b6c2008-09-04 15:10:53 +00001824/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001825/// block types. Types must be strictly compatible here. For example,
1826/// C unfortunately doesn't produce an error for the following:
1827///
1828/// int (*emptyArgFunc)();
1829/// int (*intArgList)(int) = emptyArgFunc;
1830///
1831/// For blocks, we will produce an error for the following (similar to C++):
1832///
1833/// int (^emptyArgBlock)();
1834/// int (^intArgBlock)(int) = emptyArgBlock;
1835///
1836/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1837///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001838bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Narofff5e7eff2008-09-09 13:47:19 +00001839 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001840}
1841
Chris Lattner6ff358b2008-04-07 06:51:04 +00001842/// areCompatVectorTypes - Return true if the two specified vector types are
1843/// compatible.
1844static bool areCompatVectorTypes(const VectorType *LHS,
1845 const VectorType *RHS) {
1846 assert(LHS->isCanonical() && RHS->isCanonical());
1847 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001848 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00001849}
1850
Eli Friedman0d9549b2008-08-22 00:56:42 +00001851/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001852/// compatible for assignment from RHS to LHS. This handles validation of any
1853/// protocol qualifiers on the LHS or RHS.
1854///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001855bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1856 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001857 // Verify that the base decls are compatible: the RHS must be a subclass of
1858 // the LHS.
1859 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1860 return false;
1861
1862 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1863 // protocol qualified at all, then we are good.
1864 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1865 return true;
1866
1867 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1868 // isn't a superset.
1869 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1870 return true; // FIXME: should return false!
1871
1872 // Finally, we must have two protocol-qualified interfaces.
1873 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1874 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1875 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1876 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1877 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1878 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1879
1880 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1881 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1882 // LHS in a single parallel scan until we run out of elements in LHS.
1883 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1884 ObjCProtocolDecl *LHSProto = *LHSPI;
1885
1886 while (RHSPI != RHSPE) {
1887 ObjCProtocolDecl *RHSProto = *RHSPI++;
1888 // If the RHS has a protocol that the LHS doesn't, ignore it.
1889 if (RHSProto != LHSProto)
1890 continue;
1891
1892 // Otherwise, the RHS does have this element.
1893 ++LHSPI;
1894 if (LHSPI == LHSPE)
1895 return true; // All protocols in LHS exist in RHS.
1896
1897 LHSProto = *LHSPI;
1898 }
1899
1900 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1901 return false;
1902}
1903
Steve Naroff85f0dc52007-10-15 20:41:53 +00001904/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1905/// both shall have the identically qualified version of a compatible type.
1906/// C99 6.2.7p1: Two types have compatible types if their types are the
1907/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001908bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1909 return !mergeTypes(LHS, RHS).isNull();
1910}
1911
1912QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1913 const FunctionType *lbase = lhs->getAsFunctionType();
1914 const FunctionType *rbase = rhs->getAsFunctionType();
1915 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1916 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1917 bool allLTypes = true;
1918 bool allRTypes = true;
1919
1920 // Check return type
1921 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1922 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001923 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
1924 allLTypes = false;
1925 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
1926 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001927
1928 if (lproto && rproto) { // two C99 style function prototypes
1929 unsigned lproto_nargs = lproto->getNumArgs();
1930 unsigned rproto_nargs = rproto->getNumArgs();
1931
1932 // Compatible functions must have the same number of arguments
1933 if (lproto_nargs != rproto_nargs)
1934 return QualType();
1935
1936 // Variadic and non-variadic functions aren't compatible
1937 if (lproto->isVariadic() != rproto->isVariadic())
1938 return QualType();
1939
1940 // Check argument compatibility
1941 llvm::SmallVector<QualType, 10> types;
1942 for (unsigned i = 0; i < lproto_nargs; i++) {
1943 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1944 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1945 QualType argtype = mergeTypes(largtype, rargtype);
1946 if (argtype.isNull()) return QualType();
1947 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001948 if (getCanonicalType(argtype) != getCanonicalType(largtype))
1949 allLTypes = false;
1950 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
1951 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001952 }
1953 if (allLTypes) return lhs;
1954 if (allRTypes) return rhs;
1955 return getFunctionType(retType, types.begin(), types.size(),
1956 lproto->isVariadic());
1957 }
1958
1959 if (lproto) allRTypes = false;
1960 if (rproto) allLTypes = false;
1961
1962 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1963 if (proto) {
1964 if (proto->isVariadic()) return QualType();
1965 // Check that the types are compatible with the types that
1966 // would result from default argument promotions (C99 6.7.5.3p15).
1967 // The only types actually affected are promotable integer
1968 // types and floats, which would be passed as a different
1969 // type depending on whether the prototype is visible.
1970 unsigned proto_nargs = proto->getNumArgs();
1971 for (unsigned i = 0; i < proto_nargs; ++i) {
1972 QualType argTy = proto->getArgType(i);
1973 if (argTy->isPromotableIntegerType() ||
1974 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1975 return QualType();
1976 }
1977
1978 if (allLTypes) return lhs;
1979 if (allRTypes) return rhs;
1980 return getFunctionType(retType, proto->arg_type_begin(),
1981 proto->getNumArgs(), lproto->isVariadic());
1982 }
1983
1984 if (allLTypes) return lhs;
1985 if (allRTypes) return rhs;
1986 return getFunctionTypeNoProto(retType);
1987}
1988
1989QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00001990 // C++ [expr]: If an expression initially has the type "reference to T", the
1991 // type is adjusted to "T" prior to any further analysis, the expression
1992 // designates the object or function denoted by the reference, and the
1993 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001994 // FIXME: C++ shouldn't be going through here! The rules are different
1995 // enough that they should be handled separately.
1996 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001997 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00001998 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001999 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002000
Eli Friedman0d9549b2008-08-22 00:56:42 +00002001 QualType LHSCan = getCanonicalType(LHS),
2002 RHSCan = getCanonicalType(RHS);
2003
2004 // If two types are identical, they are compatible.
2005 if (LHSCan == RHSCan)
2006 return LHS;
2007
2008 // If the qualifiers are different, the types aren't compatible
2009 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2010 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2011 return QualType();
2012
2013 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2014 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2015
Chris Lattnerc38d4522008-01-14 05:45:46 +00002016 // We want to consider the two function types to be the same for these
2017 // comparisons, just force one to the other.
2018 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2019 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002020
2021 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002022 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2023 LHSClass = Type::ConstantArray;
2024 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2025 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002026
Nate Begemanaf6ed502008-04-18 23:10:10 +00002027 // Canonicalize ExtVector -> Vector.
2028 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2029 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002030
Chris Lattner7cdcb252008-04-07 06:38:24 +00002031 // Consider qualified interfaces and interfaces the same.
2032 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2033 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002034
Chris Lattnerb5709e22008-04-07 05:43:21 +00002035 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002036 if (LHSClass != RHSClass) {
Steve Naroff44549772008-06-04 15:07:33 +00002037 // ID is compatible with all qualified id types.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002038 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00002039 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002040 if (isObjCIdType(PT->getPointeeType()))
2041 return LHS;
Steve Naroff44549772008-06-04 15:07:33 +00002042 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002043 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00002044 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002045 if (isObjCIdType(PT->getPointeeType()))
2046 return RHS;
2047 }
2048
Chris Lattnerc38d4522008-01-14 05:45:46 +00002049 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2050 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002051 if (const EnumType* ETy = LHS->getAsEnumType()) {
2052 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2053 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002054 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002055 if (const EnumType* ETy = RHS->getAsEnumType()) {
2056 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2057 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002058 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002059
Eli Friedman0d9549b2008-08-22 00:56:42 +00002060 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002061 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002062
Steve Naroffc88babe2008-01-09 22:43:08 +00002063 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002064 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002065 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002066 {
2067 // Merge two pointer types, while trying to preserve typedef info
2068 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2069 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2070 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2071 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002072 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2073 return LHS;
2074 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2075 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002076 return getPointerType(ResultType);
2077 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002078 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002079 {
2080 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2081 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2082 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2083 return QualType();
2084
2085 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2086 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2087 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2088 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002089 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2090 return LHS;
2091 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2092 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002093 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2094 ArrayType::ArraySizeModifier(), 0);
2095 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2096 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002097 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2098 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002099 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2100 return LHS;
2101 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2102 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002103 if (LVAT) {
2104 // FIXME: This isn't correct! But tricky to implement because
2105 // the array's size has to be the size of LHS, but the type
2106 // has to be different.
2107 return LHS;
2108 }
2109 if (RVAT) {
2110 // FIXME: This isn't correct! But tricky to implement because
2111 // the array's size has to be the size of RHS, but the type
2112 // has to be different.
2113 return RHS;
2114 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002115 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2116 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002117 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002118 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002119 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002120 return mergeFunctionTypes(LHS, RHS);
2121 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002122 // FIXME: Why are these compatible?
2123 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2124 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2125 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002126 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002127 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002128 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002129 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002130 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2131 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002132 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002133 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002134 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2135 // for checking assignment/comparison safety
2136 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002137 default:
2138 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002139 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002140 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002141}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002142
Chris Lattner1d78a862008-04-07 07:01:58 +00002143//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002144// Integer Predicates
2145//===----------------------------------------------------------------------===//
2146unsigned ASTContext::getIntWidth(QualType T) {
2147 if (T == BoolTy)
2148 return 1;
2149 // At the moment, only bool has padding bits
2150 return (unsigned)getTypeSize(T);
2151}
2152
2153QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2154 assert(T->isSignedIntegerType() && "Unexpected type");
2155 if (const EnumType* ETy = T->getAsEnumType())
2156 T = ETy->getDecl()->getIntegerType();
2157 const BuiltinType* BTy = T->getAsBuiltinType();
2158 assert (BTy && "Unexpected signed integer type");
2159 switch (BTy->getKind()) {
2160 case BuiltinType::Char_S:
2161 case BuiltinType::SChar:
2162 return UnsignedCharTy;
2163 case BuiltinType::Short:
2164 return UnsignedShortTy;
2165 case BuiltinType::Int:
2166 return UnsignedIntTy;
2167 case BuiltinType::Long:
2168 return UnsignedLongTy;
2169 case BuiltinType::LongLong:
2170 return UnsignedLongLongTy;
2171 default:
2172 assert(0 && "Unexpected signed integer type");
2173 return QualType();
2174 }
2175}
2176
2177
2178//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002179// Serialization Support
2180//===----------------------------------------------------------------------===//
2181
Ted Kremenek738e6c02007-10-31 17:10:13 +00002182/// Emit - Serialize an ASTContext object to Bitcode.
2183void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002184 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002185 S.EmitRef(SourceMgr);
2186 S.EmitRef(Target);
2187 S.EmitRef(Idents);
2188 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002189
Ted Kremenek68228a92007-10-31 22:44:07 +00002190 // Emit the size of the type vector so that we can reserve that size
2191 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002192 S.EmitInt(Types.size());
2193
Ted Kremenek034a78c2007-11-13 22:02:55 +00002194 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2195 I!=E;++I)
2196 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002197
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002198 S.EmitOwnedPtr(TUDecl);
2199
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002200 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002201}
2202
Ted Kremenekacba3612007-11-13 00:25:37 +00002203ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002204
2205 // Read the language options.
2206 LangOptions LOpts;
2207 LOpts.Read(D);
2208
Ted Kremenek68228a92007-10-31 22:44:07 +00002209 SourceManager &SM = D.ReadRef<SourceManager>();
2210 TargetInfo &t = D.ReadRef<TargetInfo>();
2211 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2212 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002213
Ted Kremenek68228a92007-10-31 22:44:07 +00002214 unsigned size_reserve = D.ReadInt();
2215
Ted Kremenek842126e2008-06-04 15:55:15 +00002216 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002217
Ted Kremenek034a78c2007-11-13 22:02:55 +00002218 for (unsigned i = 0; i < size_reserve; ++i)
2219 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002220
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002221 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2222
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002223 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002224
2225 return A;
2226}