blob: 62364bab318ba7ae9d3e54f484e9289adf970fdf [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
Daniel Dunbarde300732008-08-11 04:54:23 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
31 IdentifierTable &idents, SelectorTable &sels,
32 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000033 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
34 SourceMgr(SM), LangOpts(LOpts), Target(t),
Daniel Dunbarde300732008-08-11 04:54:23 +000035 Idents(idents), Selectors(sels)
36{
37 if (size_reserve > 0) Types.reserve(size_reserve);
38 InitBuiltinTypes();
39 BuiltinInfo.InitializeBuiltins(idents, Target);
40 TUDecl = TranslationUnitDecl::Create(*this);
41}
42
Chris Lattner4b009652007-07-25 00:24:17 +000043ASTContext::~ASTContext() {
44 // Deallocate all the types.
45 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000046 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000047 Types.pop_back();
48 }
Eli Friedman65489b72008-05-27 03:08:09 +000049
50 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000051}
52
53void ASTContext::PrintStats() const {
54 fprintf(stderr, "*** AST Context Stats:\n");
55 fprintf(stderr, " %d types total.\n", (int)Types.size());
56 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
57 unsigned NumVector = 0, NumComplex = 0;
58 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
59
60 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000061 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
62 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000063 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000064
65 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
66 Type *T = Types[i];
67 if (isa<BuiltinType>(T))
68 ++NumBuiltin;
69 else if (isa<PointerType>(T))
70 ++NumPointer;
71 else if (isa<ReferenceType>(T))
72 ++NumReference;
73 else if (isa<ComplexType>(T))
74 ++NumComplex;
75 else if (isa<ArrayType>(T))
76 ++NumArray;
77 else if (isa<VectorType>(T))
78 ++NumVector;
79 else if (isa<FunctionTypeNoProto>(T))
80 ++NumFunctionNP;
81 else if (isa<FunctionTypeProto>(T))
82 ++NumFunctionP;
83 else if (isa<TypedefType>(T))
84 ++NumTypeName;
85 else if (TagType *TT = dyn_cast<TagType>(T)) {
86 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000087 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000089 case TagDecl::TK_struct: ++NumTagStruct; break;
90 case TagDecl::TK_union: ++NumTagUnion; break;
91 case TagDecl::TK_class: ++NumTagClass; break;
92 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000093 }
Ted Kremenek42730c52008-01-07 19:49:32 +000094 } else if (isa<ObjCInterfaceType>(T))
95 ++NumObjCInterfaces;
96 else if (isa<ObjCQualifiedInterfaceType>(T))
97 ++NumObjCQualifiedInterfaces;
98 else if (isa<ObjCQualifiedIdType>(T))
99 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000100 else if (isa<TypeOfType>(T))
101 ++NumTypeOfTypes;
102 else if (isa<TypeOfExpr>(T))
103 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000104 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000105 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000106 assert(0 && "Unknown type!");
107 }
108 }
109
110 fprintf(stderr, " %d builtin types\n", NumBuiltin);
111 fprintf(stderr, " %d pointer types\n", NumPointer);
112 fprintf(stderr, " %d reference types\n", NumReference);
113 fprintf(stderr, " %d complex types\n", NumComplex);
114 fprintf(stderr, " %d array types\n", NumArray);
115 fprintf(stderr, " %d vector types\n", NumVector);
116 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
117 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
118 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
119 fprintf(stderr, " %d tagged types\n", NumTagged);
120 fprintf(stderr, " %d struct types\n", NumTagStruct);
121 fprintf(stderr, " %d union types\n", NumTagUnion);
122 fprintf(stderr, " %d class types\n", NumTagClass);
123 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000124 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000125 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000126 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000127 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000128 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000129 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
130 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
131
Chris Lattner4b009652007-07-25 00:24:17 +0000132 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
133 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
134 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
135 NumFunctionP*sizeof(FunctionTypeProto)+
136 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000137 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
138 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000139}
140
141
142void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
143 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
144}
145
Chris Lattner4b009652007-07-25 00:24:17 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
148
149 // C99 6.2.5p19.
150 InitBuiltinType(VoidTy, BuiltinType::Void);
151
152 // C99 6.2.5p2.
153 InitBuiltinType(BoolTy, BuiltinType::Bool);
154 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000155 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
159 // C99 6.2.5p4.
160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
165
166 // C99 6.2.5p6.
167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
172
173 // C99 6.2.5p10.
174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000177
178 // C++ 3.9.1p5
179 InitBuiltinType(WCharTy, BuiltinType::WChar);
180
Chris Lattner4b009652007-07-25 00:24:17 +0000181 // C99 6.2.5p11.
182 FloatComplexTy = getComplexType(FloatTy);
183 DoubleComplexTy = getComplexType(DoubleTy);
184 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000185
186 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000187 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000188 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000189 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000190 ClassStructType = 0;
191
Ted Kremenek42730c52008-01-07 19:49:32 +0000192 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000193
194 // void * type
195 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000196}
197
198//===----------------------------------------------------------------------===//
199// Type Sizing and Analysis
200//===----------------------------------------------------------------------===//
201
Chris Lattner2a674dc2008-06-30 18:32:54 +0000202/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
203/// scalar floating point type.
204const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
205 const BuiltinType *BT = T->getAsBuiltinType();
206 assert(BT && "Not a floating point type!");
207 switch (BT->getKind()) {
208 default: assert(0 && "Not a floating point type!");
209 case BuiltinType::Float: return Target.getFloatFormat();
210 case BuiltinType::Double: return Target.getDoubleFormat();
211 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
212 }
213}
214
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// getTypeSize - Return the size of the specified type, in bits. This method
217/// does not work on incomplete types.
218std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000219ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000220 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000221 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000222 unsigned Align;
223 switch (T->getTypeClass()) {
224 case Type::TypeName: assert(0 && "Not a canonical type!");
225 case Type::FunctionNoProto:
226 case Type::FunctionProto:
227 default:
228 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000229 case Type::VariableArray:
230 assert(0 && "VLAs not implemented yet!");
231 case Type::ConstantArray: {
232 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
233
Chris Lattner8cd0e932008-03-05 18:54:05 +0000234 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000235 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000236 Align = EltInfo.second;
237 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000238 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000239 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000240 case Type::Vector: {
241 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000242 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000243 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000244 // FIXME: This isn't right for unusual vectors
245 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000246 break;
247 }
248
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000249 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000250 switch (cast<BuiltinType>(T)->getKind()) {
251 default: assert(0 && "Unknown builtin type!");
252 case BuiltinType::Void:
253 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000255 Width = Target.getBoolWidth();
256 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000258 case BuiltinType::Char_S:
259 case BuiltinType::Char_U:
260 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000262 Width = Target.getCharWidth();
263 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000264 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000265 case BuiltinType::WChar:
266 Width = Target.getWCharWidth();
267 Align = Target.getWCharAlign();
268 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000269 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000271 Width = Target.getShortWidth();
272 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000273 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000274 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000276 Width = Target.getIntWidth();
277 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000278 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000279 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000280 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000281 Width = Target.getLongWidth();
282 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000283 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000284 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000285 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000286 Width = Target.getLongLongWidth();
287 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000288 break;
289 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000290 Width = Target.getFloatWidth();
291 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000292 break;
293 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000294 Width = Target.getDoubleWidth();
295 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000296 break;
297 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000298 Width = Target.getLongDoubleWidth();
299 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000300 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000301 }
302 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000303 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000304 // FIXME: Pointers into different addr spaces could have different sizes and
305 // alignment requirements: getPointerInfo should take an AddrSpace.
306 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000307 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000308 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000309 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000310 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000311 case Type::Pointer: {
312 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000313 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000314 Align = Target.getPointerAlign(AS);
315 break;
316 }
Chris Lattner4b009652007-07-25 00:24:17 +0000317 case Type::Reference:
318 // "When applied to a reference or a reference type, the result is the size
319 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000320 // FIXME: This is wrong for struct layout: a reference in a struct has
321 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000322 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000323
324 case Type::Complex: {
325 // Complex types have the same alignment as their elements, but twice the
326 // size.
327 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000328 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000329 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000330 Align = EltInfo.second;
331 break;
332 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000333 case Type::ObjCInterface: {
334 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
335 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
336 Width = Layout.getSize();
337 Align = Layout.getAlignment();
338 break;
339 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000340 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000341 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
342 Width = 1;
343 Align = 1;
344 break;
345 }
346
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000347 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
348 return getTypeInfo(ET->getDecl()->getIntegerType());
349
350 RecordType *RT = cast<RecordType>(T);
351 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
352 Width = Layout.getSize();
353 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000354 break;
355 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000356 }
Chris Lattner4b009652007-07-25 00:24:17 +0000357
358 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000360}
361
Devang Patelbfe323c2008-06-04 21:22:16 +0000362/// LayoutField - Field layout.
363void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
364 bool IsUnion, bool StructIsPacked,
365 ASTContext &Context) {
366 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
367 uint64_t FieldOffset = IsUnion ? 0 : Size;
368 uint64_t FieldSize;
369 unsigned FieldAlign;
370
371 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
372 // TODO: Need to check this algorithm on other targets!
373 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000374 FieldSize =
375 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000376
377 std::pair<uint64_t, unsigned> FieldInfo =
378 Context.getTypeInfo(FD->getType());
379 uint64_t TypeSize = FieldInfo.first;
380
381 FieldAlign = FieldInfo.second;
382 if (FieldIsPacked)
383 FieldAlign = 1;
384 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
385 FieldAlign = std::max(FieldAlign, AA->getAlignment());
386
387 // Check if we need to add padding to give the field the correct
388 // alignment.
389 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
390 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
391
392 // Padding members don't affect overall alignment
393 if (!FD->getIdentifier())
394 FieldAlign = 1;
395 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000396 if (FD->getType()->isIncompleteArrayType()) {
397 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000398 // query getTypeInfo about these, so we figure it out here.
399 // Flexible array members don't have any size, but they
400 // have to be aligned appropriately for their element type.
401 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000402 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000403 FieldAlign = Context.getTypeAlign(ATy->getElementType());
404 } else {
405 std::pair<uint64_t, unsigned> FieldInfo =
406 Context.getTypeInfo(FD->getType());
407 FieldSize = FieldInfo.first;
408 FieldAlign = FieldInfo.second;
409 }
410
411 if (FieldIsPacked)
412 FieldAlign = 8;
413 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
414 FieldAlign = std::max(FieldAlign, AA->getAlignment());
415
416 // Round up the current record size to the field's alignment boundary.
417 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
418 }
419
420 // Place this field at the current location.
421 FieldOffsets[FieldNo] = FieldOffset;
422
423 // Reserve space for this field.
424 if (IsUnion) {
425 Size = std::max(Size, FieldSize);
426 } else {
427 Size = FieldOffset + FieldSize;
428 }
429
430 // Remember max struct/class alignment.
431 Alignment = std::max(Alignment, FieldAlign);
432}
433
Devang Patel4b6bf702008-06-04 21:54:36 +0000434
435/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
436/// specified Objective C, which indicates its size and ivar
437/// position information.
438const ASTRecordLayout &
439ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
440 // Look up this layout, if already laid out, return what we have.
441 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
442 if (Entry) return *Entry;
443
444 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
445 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000446 ASTRecordLayout *NewEntry = NULL;
447 unsigned FieldCount = D->ivar_size();
448 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
449 FieldCount++;
450 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
451 unsigned Alignment = SL.getAlignment();
452 uint64_t Size = SL.getSize();
453 NewEntry = new ASTRecordLayout(Size, Alignment);
454 NewEntry->InitializeLayout(FieldCount);
455 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
456 } else {
457 NewEntry = new ASTRecordLayout();
458 NewEntry->InitializeLayout(FieldCount);
459 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000460 Entry = NewEntry;
461
Devang Patel4b6bf702008-06-04 21:54:36 +0000462 bool IsPacked = D->getAttr<PackedAttr>();
463
464 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
465 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
466 AA->getAlignment()));
467
468 // Layout each ivar sequentially.
469 unsigned i = 0;
470 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
471 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
472 const ObjCIvarDecl* Ivar = (*IVI);
473 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
474 }
475
476 // Finally, round the size of the total struct up to the alignment of the
477 // struct itself.
478 NewEntry->FinalizeLayout();
479 return *NewEntry;
480}
481
Devang Patel7a78e432007-11-01 19:11:01 +0000482/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000483/// specified record (struct/union/class), which indicates its size and field
484/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000485const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000486 D = D->getDefinition(*this);
487 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000488
Chris Lattner4b009652007-07-25 00:24:17 +0000489 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000490 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000491 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000492
Devang Patel7a78e432007-11-01 19:11:01 +0000493 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
494 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
495 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000496 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000497
Devang Patelbfe323c2008-06-04 21:22:16 +0000498 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000499 bool StructIsPacked = D->getAttr<PackedAttr>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000500 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000501
Eli Friedman5949a022008-05-30 09:31:38 +0000502 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000503 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
504 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000505
Eli Friedman5949a022008-05-30 09:31:38 +0000506 // Layout each field, for now, just sequentially, respecting alignment. In
507 // the future, this will need to be tweakable by targets.
508 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
509 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000510 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000511 }
Eli Friedman5949a022008-05-30 09:31:38 +0000512
513 // Finally, round the size of the total struct up to the alignment of the
514 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000515 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000516 return *NewEntry;
517}
518
Chris Lattner4b009652007-07-25 00:24:17 +0000519//===----------------------------------------------------------------------===//
520// Type creation/memoization methods
521//===----------------------------------------------------------------------===//
522
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000523QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000524 QualType CanT = getCanonicalType(T);
525 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000526 return T;
527
528 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
529 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000530 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000531 "Type is already address space qualified");
532
533 // Check if we've already instantiated an address space qual'd type of this
534 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000535 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000536 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000537 void *InsertPos = 0;
538 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
539 return QualType(ASQy, 0);
540
541 // If the base type isn't canonical, this won't be a canonical type either,
542 // so fill in the canonical type field.
543 QualType Canonical;
544 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000545 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000546
547 // Get the new insert position for the node we care about.
548 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
549 assert(NewIP == 0 && "Shouldn't be in the map!");
550 }
Chris Lattner35fef522008-02-20 20:55:12 +0000551 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000552 ASQualTypes.InsertNode(New, InsertPos);
553 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000554 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000555}
556
Chris Lattner4b009652007-07-25 00:24:17 +0000557
558/// getComplexType - Return the uniqued reference to the type for a complex
559/// number with the specified element type.
560QualType ASTContext::getComplexType(QualType T) {
561 // Unique pointers, to guarantee there is only one pointer of a particular
562 // structure.
563 llvm::FoldingSetNodeID ID;
564 ComplexType::Profile(ID, T);
565
566 void *InsertPos = 0;
567 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
568 return QualType(CT, 0);
569
570 // If the pointee type isn't canonical, this won't be a canonical type either,
571 // so fill in the canonical type field.
572 QualType Canonical;
573 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000574 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000575
576 // Get the new insert position for the node we care about.
577 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
578 assert(NewIP == 0 && "Shouldn't be in the map!");
579 }
580 ComplexType *New = new ComplexType(T, Canonical);
581 Types.push_back(New);
582 ComplexTypes.InsertNode(New, InsertPos);
583 return QualType(New, 0);
584}
585
586
587/// getPointerType - Return the uniqued reference to the type for a pointer to
588/// the specified type.
589QualType ASTContext::getPointerType(QualType T) {
590 // Unique pointers, to guarantee there is only one pointer of a particular
591 // structure.
592 llvm::FoldingSetNodeID ID;
593 PointerType::Profile(ID, T);
594
595 void *InsertPos = 0;
596 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
597 return QualType(PT, 0);
598
599 // If the pointee type isn't canonical, this won't be a canonical type either,
600 // so fill in the canonical type field.
601 QualType Canonical;
602 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000603 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000604
605 // Get the new insert position for the node we care about.
606 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
607 assert(NewIP == 0 && "Shouldn't be in the map!");
608 }
609 PointerType *New = new PointerType(T, Canonical);
610 Types.push_back(New);
611 PointerTypes.InsertNode(New, InsertPos);
612 return QualType(New, 0);
613}
614
Steve Naroff7aa54752008-08-27 16:04:49 +0000615/// getBlockPointerType - Return the uniqued reference to the type for
616/// a pointer to the specified block.
617QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000618 assert(T->isFunctionType() && "block of function types only");
619 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000620 // structure.
621 llvm::FoldingSetNodeID ID;
622 BlockPointerType::Profile(ID, T);
623
624 void *InsertPos = 0;
625 if (BlockPointerType *PT =
626 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
627 return QualType(PT, 0);
628
Steve Narofffd5b19d2008-08-28 19:20:44 +0000629 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000630 // type either so fill in the canonical type field.
631 QualType Canonical;
632 if (!T->isCanonical()) {
633 Canonical = getBlockPointerType(getCanonicalType(T));
634
635 // Get the new insert position for the node we care about.
636 BlockPointerType *NewIP =
637 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
638 assert(NewIP == 0 && "Shouldn't be in the map!");
639 }
640 BlockPointerType *New = new BlockPointerType(T, Canonical);
641 Types.push_back(New);
642 BlockPointerTypes.InsertNode(New, InsertPos);
643 return QualType(New, 0);
644}
645
Chris Lattner4b009652007-07-25 00:24:17 +0000646/// getReferenceType - Return the uniqued reference to the type for a reference
647/// to the specified type.
648QualType ASTContext::getReferenceType(QualType T) {
649 // Unique pointers, to guarantee there is only one pointer of a particular
650 // structure.
651 llvm::FoldingSetNodeID ID;
652 ReferenceType::Profile(ID, T);
653
654 void *InsertPos = 0;
655 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
656 return QualType(RT, 0);
657
658 // If the referencee type isn't canonical, this won't be a canonical type
659 // either, so fill in the canonical type field.
660 QualType Canonical;
661 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000662 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000663
664 // Get the new insert position for the node we care about.
665 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
666 assert(NewIP == 0 && "Shouldn't be in the map!");
667 }
668
669 ReferenceType *New = new ReferenceType(T, Canonical);
670 Types.push_back(New);
671 ReferenceTypes.InsertNode(New, InsertPos);
672 return QualType(New, 0);
673}
674
Steve Naroff83c13012007-08-30 01:06:46 +0000675/// getConstantArrayType - Return the unique reference to the type for an
676/// array of the specified element type.
677QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000678 const llvm::APInt &ArySize,
679 ArrayType::ArraySizeModifier ASM,
680 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000681 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000682 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000683
684 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000685 if (ConstantArrayType *ATP =
686 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000687 return QualType(ATP, 0);
688
689 // If the element type isn't canonical, this won't be a canonical type either,
690 // so fill in the canonical type field.
691 QualType Canonical;
692 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000693 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000694 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000695 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000696 ConstantArrayType *NewIP =
697 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
698
Chris Lattner4b009652007-07-25 00:24:17 +0000699 assert(NewIP == 0 && "Shouldn't be in the map!");
700 }
701
Steve Naroff24c9b982007-08-30 18:10:14 +0000702 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
703 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000704 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000705 Types.push_back(New);
706 return QualType(New, 0);
707}
708
Steve Naroffe2579e32007-08-30 18:14:25 +0000709/// getVariableArrayType - Returns a non-unique reference to the type for a
710/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000711QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
712 ArrayType::ArraySizeModifier ASM,
713 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000714 // Since we don't unique expressions, it isn't possible to unique VLA's
715 // that have an expression provided for their size.
716
717 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
718 ASM, EltTypeQuals);
719
720 VariableArrayTypes.push_back(New);
721 Types.push_back(New);
722 return QualType(New, 0);
723}
724
725QualType ASTContext::getIncompleteArrayType(QualType EltTy,
726 ArrayType::ArraySizeModifier ASM,
727 unsigned EltTypeQuals) {
728 llvm::FoldingSetNodeID ID;
729 IncompleteArrayType::Profile(ID, EltTy);
730
731 void *InsertPos = 0;
732 if (IncompleteArrayType *ATP =
733 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
734 return QualType(ATP, 0);
735
736 // If the element type isn't canonical, this won't be a canonical type
737 // either, so fill in the canonical type field.
738 QualType Canonical;
739
740 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000741 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000742 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000743
744 // Get the new insert position for the node we care about.
745 IncompleteArrayType *NewIP =
746 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
747
748 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000749 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000750
751 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
752 ASM, EltTypeQuals);
753
754 IncompleteArrayTypes.InsertNode(New, InsertPos);
755 Types.push_back(New);
756 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000757}
758
Chris Lattner4b009652007-07-25 00:24:17 +0000759/// getVectorType - Return the unique reference to a vector type of
760/// the specified element type and size. VectorType must be a built-in type.
761QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
762 BuiltinType *baseType;
763
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000764 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000765 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
766
767 // Check if we've already instantiated a vector of this type.
768 llvm::FoldingSetNodeID ID;
769 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
770 void *InsertPos = 0;
771 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
772 return QualType(VTP, 0);
773
774 // If the element type isn't canonical, this won't be a canonical type either,
775 // so fill in the canonical type field.
776 QualType Canonical;
777 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000778 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000779
780 // Get the new insert position for the node we care about.
781 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
782 assert(NewIP == 0 && "Shouldn't be in the map!");
783 }
784 VectorType *New = new VectorType(vecType, NumElts, Canonical);
785 VectorTypes.InsertNode(New, InsertPos);
786 Types.push_back(New);
787 return QualType(New, 0);
788}
789
Nate Begemanaf6ed502008-04-18 23:10:10 +0000790/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000791/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000792QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000793 BuiltinType *baseType;
794
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000795 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000796 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000797
798 // Check if we've already instantiated a vector of this type.
799 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000800 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000801 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()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000809 Canonical = getExtVectorType(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);
813 assert(NewIP == 0 && "Shouldn't be in the map!");
814 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000815 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000816 VectorTypes.InsertNode(New, InsertPos);
817 Types.push_back(New);
818 return QualType(New, 0);
819}
820
821/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
822///
823QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
824 // Unique functions, to guarantee there is only one function of a particular
825 // structure.
826 llvm::FoldingSetNodeID ID;
827 FunctionTypeNoProto::Profile(ID, ResultTy);
828
829 void *InsertPos = 0;
830 if (FunctionTypeNoProto *FT =
831 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
832 return QualType(FT, 0);
833
834 QualType Canonical;
835 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000836 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000837
838 // Get the new insert position for the node we care about.
839 FunctionTypeNoProto *NewIP =
840 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
841 assert(NewIP == 0 && "Shouldn't be in the map!");
842 }
843
844 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
845 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000846 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000847 return QualType(New, 0);
848}
849
850/// getFunctionType - Return a normal function type with a typed argument
851/// list. isVariadic indicates whether the argument list includes '...'.
Eli Friedman36104c12008-08-22 00:59:49 +0000852QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray,
Chris Lattner4b009652007-07-25 00:24:17 +0000853 unsigned NumArgs, bool isVariadic) {
854 // Unique functions, to guarantee there is only one function of a particular
855 // structure.
856 llvm::FoldingSetNodeID ID;
857 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
858
859 void *InsertPos = 0;
860 if (FunctionTypeProto *FTP =
861 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
862 return QualType(FTP, 0);
863
864 // Determine whether the type being created is already canonical or not.
865 bool isCanonical = ResultTy->isCanonical();
866 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
867 if (!ArgArray[i]->isCanonical())
868 isCanonical = false;
869
870 // If this type isn't canonical, get the canonical version of it.
871 QualType Canonical;
872 if (!isCanonical) {
873 llvm::SmallVector<QualType, 16> CanonicalArgs;
874 CanonicalArgs.reserve(NumArgs);
875 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000876 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000877
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000878 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000879 &CanonicalArgs[0], NumArgs,
880 isVariadic);
881
882 // Get the new insert position for the node we care about.
883 FunctionTypeProto *NewIP =
884 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
885 assert(NewIP == 0 && "Shouldn't be in the map!");
886 }
887
888 // FunctionTypeProto objects are not allocated with new because they have a
889 // variable size array (for parameter types) at the end of them.
890 FunctionTypeProto *FTP =
891 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
892 NumArgs*sizeof(QualType));
893 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
894 Canonical);
895 Types.push_back(FTP);
896 FunctionTypeProtos.InsertNode(FTP, InsertPos);
897 return QualType(FTP, 0);
898}
899
Douglas Gregor1d661552008-04-13 21:07:44 +0000900/// getTypeDeclType - Return the unique reference to the type for the
901/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000902QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Douglas Gregor1d661552008-04-13 21:07:44 +0000903 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
904
905 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
906 return getTypedefType(Typedef);
907 else if (ObjCInterfaceDecl *ObjCInterface
908 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
909 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000910
Ted Kremenek46a837c2008-09-05 17:16:31 +0000911 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl)) {
912 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
913 : new CXXRecordType(CXXRecord);
914 }
915 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
916 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
917 : new RecordType(Record);
918 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000919 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000920 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000921 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000922 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000923
Ted Kremenek46a837c2008-09-05 17:16:31 +0000924 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000925 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000926}
927
Ted Kremenek46a837c2008-09-05 17:16:31 +0000928/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
929/// about which RecordDecl serves as the definition of a particular
930/// struct/union/class. This will eventually be used by enums as well.
931void ASTContext::setTagDefinition(TagDecl* D) {
932 assert (D->isDefinition());
933 cast<TagType>(D->TypeForDecl)->decl = D;
934}
935
Chris Lattner4b009652007-07-25 00:24:17 +0000936/// getTypedefType - Return the unique reference to the type for the
937/// specified typename decl.
938QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
939 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
940
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000941 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000942 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000943 Types.push_back(Decl->TypeForDecl);
944 return QualType(Decl->TypeForDecl, 0);
945}
946
Ted Kremenek42730c52008-01-07 19:49:32 +0000947/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000948/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000949QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000950 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
951
Ted Kremenek42730c52008-01-07 19:49:32 +0000952 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000953 Types.push_back(Decl->TypeForDecl);
954 return QualType(Decl->TypeForDecl, 0);
955}
956
Chris Lattnere1352302008-04-07 04:56:42 +0000957/// CmpProtocolNames - Comparison predicate for sorting protocols
958/// alphabetically.
959static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
960 const ObjCProtocolDecl *RHS) {
961 return strcmp(LHS->getName(), RHS->getName()) < 0;
962}
963
964static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
965 unsigned &NumProtocols) {
966 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
967
968 // Sort protocols, keyed by name.
969 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
970
971 // Remove duplicates.
972 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
973 NumProtocols = ProtocolsEnd-Protocols;
974}
975
976
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000977/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
978/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000979QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
980 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000981 // Sort the protocol list alphabetically to canonicalize it.
982 SortAndUniqueProtocols(Protocols, NumProtocols);
983
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000984 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000985 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000986
987 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000988 if (ObjCQualifiedInterfaceType *QT =
989 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000990 return QualType(QT, 0);
991
992 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000993 ObjCQualifiedInterfaceType *QType =
994 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000995 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000996 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000997 return QualType(QType, 0);
998}
999
Chris Lattnere1352302008-04-07 04:56:42 +00001000/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1001/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001002QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001003 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001004 // Sort the protocol list alphabetically to canonicalize it.
1005 SortAndUniqueProtocols(Protocols, NumProtocols);
1006
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001007 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001008 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001009
1010 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001011 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001012 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001013 return QualType(QT, 0);
1014
1015 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001016 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001017 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001018 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001019 return QualType(QType, 0);
1020}
1021
Steve Naroff0604dd92007-08-01 18:02:17 +00001022/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1023/// TypeOfExpr AST's (since expression's are never shared). For example,
1024/// multiple declarations that refer to "typeof(x)" all contain different
1025/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1026/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001027QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001028 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001029 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1030 Types.push_back(toe);
1031 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001032}
1033
Steve Naroff0604dd92007-08-01 18:02:17 +00001034/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1035/// TypeOfType AST's. The only motivation to unique these nodes would be
1036/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1037/// an issue. This doesn't effect the type checker, since it operates
1038/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001039QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001040 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001041 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1042 Types.push_back(tot);
1043 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001044}
1045
Chris Lattner4b009652007-07-25 00:24:17 +00001046/// getTagDeclType - Return the unique reference to the type for the
1047/// specified TagDecl (struct/union/class/enum) decl.
1048QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001049 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001050 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001051}
1052
1053/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1054/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1055/// needs to agree with the definition in <stddef.h>.
1056QualType ASTContext::getSizeType() const {
1057 // On Darwin, size_t is defined as a "long unsigned int".
1058 // FIXME: should derive from "Target".
1059 return UnsignedLongTy;
1060}
1061
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001062/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001063/// width of characters in wide strings, The value is target dependent and
1064/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001065QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001066 if (LangOpts.CPlusPlus)
1067 return WCharTy;
1068
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001069 // On Darwin, wchar_t is defined as a "int".
1070 // FIXME: should derive from "Target".
1071 return IntTy;
1072}
1073
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001074/// getSignedWCharType - Return the type of "signed wchar_t".
1075/// Used when in C++, as a GCC extension.
1076QualType ASTContext::getSignedWCharType() const {
1077 // FIXME: derive from "Target" ?
1078 return WCharTy;
1079}
1080
1081/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1082/// Used when in C++, as a GCC extension.
1083QualType ASTContext::getUnsignedWCharType() const {
1084 // FIXME: derive from "Target" ?
1085 return UnsignedIntTy;
1086}
1087
Chris Lattner4b009652007-07-25 00:24:17 +00001088/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1089/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1090QualType ASTContext::getPointerDiffType() const {
1091 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1092 // FIXME: should derive from "Target".
1093 return IntTy;
1094}
1095
Chris Lattner19eb97e2008-04-02 05:18:44 +00001096//===----------------------------------------------------------------------===//
1097// Type Operators
1098//===----------------------------------------------------------------------===//
1099
Chris Lattner3dae6f42008-04-06 22:41:35 +00001100/// getCanonicalType - Return the canonical (structural) type corresponding to
1101/// the specified potentially non-canonical type. The non-canonical version
1102/// of a type may have many "decorated" versions of types. Decorators can
1103/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1104/// to be free of any of these, allowing two canonical types to be compared
1105/// for exact equality with a simple pointer comparison.
1106QualType ASTContext::getCanonicalType(QualType T) {
1107 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001108
1109 // If the result has type qualifiers, make sure to canonicalize them as well.
1110 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1111 if (TypeQuals == 0) return CanType;
1112
1113 // If the type qualifiers are on an array type, get the canonical type of the
1114 // array with the qualifiers applied to the element type.
1115 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1116 if (!AT)
1117 return CanType.getQualifiedType(TypeQuals);
1118
1119 // Get the canonical version of the element with the extra qualifiers on it.
1120 // This can recursively sink qualifiers through multiple levels of arrays.
1121 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1122 NewEltTy = getCanonicalType(NewEltTy);
1123
1124 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1125 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1126 CAT->getIndexTypeQualifier());
1127 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1128 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1129 IAT->getIndexTypeQualifier());
1130
1131 // FIXME: What is the ownership of size expressions in VLAs?
1132 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1133 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1134 VAT->getSizeModifier(),
1135 VAT->getIndexTypeQualifier());
1136}
1137
1138
1139const ArrayType *ASTContext::getAsArrayType(QualType T) {
1140 // Handle the non-qualified case efficiently.
1141 if (T.getCVRQualifiers() == 0) {
1142 // Handle the common positive case fast.
1143 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1144 return AT;
1145 }
1146
1147 // Handle the common negative case fast, ignoring CVR qualifiers.
1148 QualType CType = T->getCanonicalTypeInternal();
1149
1150 // Make sure to look through type qualifiers (like ASQuals) for the negative
1151 // test.
1152 if (!isa<ArrayType>(CType) &&
1153 !isa<ArrayType>(CType.getUnqualifiedType()))
1154 return 0;
1155
1156 // Apply any CVR qualifiers from the array type to the element type. This
1157 // implements C99 6.7.3p8: "If the specification of an array type includes
1158 // any type qualifiers, the element type is so qualified, not the array type."
1159
1160 // If we get here, we either have type qualifiers on the type, or we have
1161 // sugar such as a typedef in the way. If we have type qualifiers on the type
1162 // we must propagate them down into the elemeng type.
1163 unsigned CVRQuals = T.getCVRQualifiers();
1164 unsigned AddrSpace = 0;
1165 Type *Ty = T.getTypePtr();
1166
1167 // Rip through ASQualType's and typedefs to get to a concrete type.
1168 while (1) {
1169 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1170 AddrSpace = ASQT->getAddressSpace();
1171 Ty = ASQT->getBaseType();
1172 } else {
1173 T = Ty->getDesugaredType();
1174 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1175 break;
1176 CVRQuals |= T.getCVRQualifiers();
1177 Ty = T.getTypePtr();
1178 }
1179 }
1180
1181 // If we have a simple case, just return now.
1182 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1183 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1184 return ATy;
1185
1186 // Otherwise, we have an array and we have qualifiers on it. Push the
1187 // qualifiers into the array element type and return a new array type.
1188 // Get the canonical version of the element with the extra qualifiers on it.
1189 // This can recursively sink qualifiers through multiple levels of arrays.
1190 QualType NewEltTy = ATy->getElementType();
1191 if (AddrSpace)
1192 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1193 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1194
1195 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1196 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1197 CAT->getSizeModifier(),
1198 CAT->getIndexTypeQualifier()));
1199 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1200 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1201 IAT->getSizeModifier(),
1202 IAT->getIndexTypeQualifier()));
1203
1204 // FIXME: What is the ownership of size expressions in VLAs?
1205 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1206 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1207 VAT->getSizeModifier(),
1208 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001209}
1210
1211
Chris Lattner19eb97e2008-04-02 05:18:44 +00001212/// getArrayDecayedType - Return the properly qualified result of decaying the
1213/// specified array type to a pointer. This operation is non-trivial when
1214/// handling typedefs etc. The canonical type of "T" must be an array type,
1215/// this returns a pointer to a properly qualified element of the array.
1216///
1217/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1218QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001219 // Get the element type with 'getAsArrayType' so that we don't lose any
1220 // typedefs in the element type of the array. This also handles propagation
1221 // of type qualifiers from the array type into the element type if present
1222 // (C99 6.7.3p8).
1223 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1224 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001225
Chris Lattnera1923f62008-08-04 07:31:14 +00001226 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001227
1228 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001229 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001230}
1231
Chris Lattner4b009652007-07-25 00:24:17 +00001232/// getFloatingRank - Return a relative rank for floating point types.
1233/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001234static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001235 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001236 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001237
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001238 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001239 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001240 case BuiltinType::Float: return FloatRank;
1241 case BuiltinType::Double: return DoubleRank;
1242 case BuiltinType::LongDouble: return LongDoubleRank;
1243 }
1244}
1245
Steve Narofffa0c4532007-08-27 01:41:48 +00001246/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1247/// point or a complex type (based on typeDomain/typeSize).
1248/// 'typeDomain' is a real floating point or complex type.
1249/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001250QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1251 QualType Domain) const {
1252 FloatingRank EltRank = getFloatingRank(Size);
1253 if (Domain->isComplexType()) {
1254 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001255 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001256 case FloatRank: return FloatComplexTy;
1257 case DoubleRank: return DoubleComplexTy;
1258 case LongDoubleRank: return LongDoubleComplexTy;
1259 }
Chris Lattner4b009652007-07-25 00:24:17 +00001260 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001261
1262 assert(Domain->isRealFloatingType() && "Unknown domain!");
1263 switch (EltRank) {
1264 default: assert(0 && "getFloatingRank(): illegal value for rank");
1265 case FloatRank: return FloatTy;
1266 case DoubleRank: return DoubleTy;
1267 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001268 }
Chris Lattner4b009652007-07-25 00:24:17 +00001269}
1270
Chris Lattner51285d82008-04-06 23:55:33 +00001271/// getFloatingTypeOrder - Compare the rank of the two specified floating
1272/// point types, ignoring the domain of the type (i.e. 'double' ==
1273/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1274/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001275int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1276 FloatingRank LHSR = getFloatingRank(LHS);
1277 FloatingRank RHSR = getFloatingRank(RHS);
1278
1279 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001280 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001281 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001282 return 1;
1283 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001284}
1285
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001286/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1287/// routine will assert if passed a built-in type that isn't an integer or enum,
1288/// or if it is not canonicalized.
1289static unsigned getIntegerRank(Type *T) {
1290 assert(T->isCanonical() && "T should be canonicalized");
1291 if (isa<EnumType>(T))
1292 return 4;
1293
1294 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001295 default: assert(0 && "getIntegerRank(): not a built-in integer");
1296 case BuiltinType::Bool:
1297 return 1;
1298 case BuiltinType::Char_S:
1299 case BuiltinType::Char_U:
1300 case BuiltinType::SChar:
1301 case BuiltinType::UChar:
1302 return 2;
1303 case BuiltinType::Short:
1304 case BuiltinType::UShort:
1305 return 3;
1306 case BuiltinType::Int:
1307 case BuiltinType::UInt:
1308 return 4;
1309 case BuiltinType::Long:
1310 case BuiltinType::ULong:
1311 return 5;
1312 case BuiltinType::LongLong:
1313 case BuiltinType::ULongLong:
1314 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001315 }
1316}
1317
Chris Lattner51285d82008-04-06 23:55:33 +00001318/// getIntegerTypeOrder - Returns the highest ranked integer type:
1319/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1320/// LHS < RHS, return -1.
1321int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001322 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1323 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001324 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001325
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001326 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1327 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001328
Chris Lattner51285d82008-04-06 23:55:33 +00001329 unsigned LHSRank = getIntegerRank(LHSC);
1330 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001331
Chris Lattner51285d82008-04-06 23:55:33 +00001332 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1333 if (LHSRank == RHSRank) return 0;
1334 return LHSRank > RHSRank ? 1 : -1;
1335 }
Chris Lattner4b009652007-07-25 00:24:17 +00001336
Chris Lattner51285d82008-04-06 23:55:33 +00001337 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1338 if (LHSUnsigned) {
1339 // If the unsigned [LHS] type is larger, return it.
1340 if (LHSRank >= RHSRank)
1341 return 1;
1342
1343 // If the signed type can represent all values of the unsigned type, it
1344 // wins. Because we are dealing with 2's complement and types that are
1345 // powers of two larger than each other, this is always safe.
1346 return -1;
1347 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001348
Chris Lattner51285d82008-04-06 23:55:33 +00001349 // If the unsigned [RHS] type is larger, return it.
1350 if (RHSRank >= LHSRank)
1351 return -1;
1352
1353 // If the signed type can represent all values of the unsigned type, it
1354 // wins. Because we are dealing with 2's complement and types that are
1355 // powers of two larger than each other, this is always safe.
1356 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001357}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001358
1359// getCFConstantStringType - Return the type used for constant CFStrings.
1360QualType ASTContext::getCFConstantStringType() {
1361 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001362 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001363 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001364 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001365 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001366
1367 // const int *isa;
1368 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001369 // int flags;
1370 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001371 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001372 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001373 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001374 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001375 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001376 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001377
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001378 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001379 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001380 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001381
Ted Kremenek46a837c2008-09-05 17:16:31 +00001382 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001383 }
1384
1385 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001386}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001387
Anders Carlssonf58cac72008-08-30 19:34:46 +00001388QualType ASTContext::getObjCFastEnumerationStateType()
1389{
1390 if (!ObjCFastEnumerationStateTypeDecl) {
1391 QualType FieldTypes[] = {
1392 UnsignedLongTy,
1393 getPointerType(ObjCIdType),
1394 getPointerType(UnsignedLongTy),
1395 getConstantArrayType(UnsignedLongTy,
1396 llvm::APInt(32, 5), ArrayType::Normal, 0)
1397 };
1398
1399 FieldDecl *FieldDecls[4];
1400 for (size_t i = 0; i < 4; ++i)
1401 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1402 FieldTypes[i]);
1403
1404 ObjCFastEnumerationStateTypeDecl =
1405 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001406 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonf58cac72008-08-30 19:34:46 +00001407
Ted Kremenek46a837c2008-09-05 17:16:31 +00001408 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001409 }
1410
1411 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1412}
1413
Anders Carlssone3f02572007-10-29 06:33:42 +00001414// This returns true if a type has been typedefed to BOOL:
1415// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001416static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001417 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001418 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001419
1420 return false;
1421}
1422
Ted Kremenek42730c52008-01-07 19:49:32 +00001423/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001424/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001425int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001426 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001427
1428 // Make all integer and enum types at least as large as an int
1429 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001430 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001431 // Treat arrays as pointers, since that's how they're passed in.
1432 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001433 sz = getTypeSize(VoidPtrTy);
1434 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001435}
1436
Ted Kremenek42730c52008-01-07 19:49:32 +00001437/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001438/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001439void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001440 std::string& S)
1441{
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001442 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001443 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001445 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001446 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001447 // Compute size of all parameters.
1448 // Start with computing size of a pointer in number of bytes.
1449 // FIXME: There might(should) be a better way of doing this computation!
1450 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001451 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001452 // The first two arguments (self and _cmd) are pointers; account for
1453 // their size.
1454 int ParmOffset = 2 * PtrSize;
1455 int NumOfParams = Decl->getNumParams();
1456 for (int i = 0; i < NumOfParams; i++) {
1457 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001458 int sz = getObjCEncodingTypeSize (PType);
1459 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001460 ParmOffset += sz;
1461 }
1462 S += llvm::utostr(ParmOffset);
1463 S += "@0:";
1464 S += llvm::utostr(PtrSize);
1465
1466 // Argument types.
1467 ParmOffset = 2 * PtrSize;
1468 for (int i = 0; i < NumOfParams; i++) {
1469 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001470 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001471 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001472 getObjCEncodingForTypeQualifier(
1473 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001474 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001475 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001476 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001477 }
1478}
1479
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001480/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1481/// method declaration. If non-NULL, Container must be either an
1482/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1483/// NULL when getting encodings for protocol properties.
1484void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1485 const Decl *Container,
1486 std::string& S)
1487{
1488 // Collect information from the property implementation decl(s).
1489 bool Dynamic = false;
1490 ObjCPropertyImplDecl *SynthesizePID = 0;
1491
1492 // FIXME: Duplicated code due to poor abstraction.
1493 if (Container) {
1494 if (const ObjCCategoryImplDecl *CID =
1495 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1496 for (ObjCCategoryImplDecl::propimpl_iterator
1497 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1498 ObjCPropertyImplDecl *PID = *i;
1499 if (PID->getPropertyDecl() == PD) {
1500 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1501 Dynamic = true;
1502 } else {
1503 SynthesizePID = PID;
1504 }
1505 }
1506 }
1507 } else {
1508 const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container);
1509 for (ObjCCategoryImplDecl::propimpl_iterator
1510 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1511 ObjCPropertyImplDecl *PID = *i;
1512 if (PID->getPropertyDecl() == PD) {
1513 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1514 Dynamic = true;
1515 } else {
1516 SynthesizePID = PID;
1517 }
1518 }
1519 }
1520 }
1521 }
1522
1523 // FIXME: This is not very efficient.
1524 S = "T";
1525
1526 // Encode result type.
1527 // FIXME: GCC uses a generating_property_type_encoding mode during
1528 // this part. Investigate.
1529 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1530
1531 if (PD->isReadOnly()) {
1532 S += ",R";
1533 } else {
1534 switch (PD->getSetterKind()) {
1535 case ObjCPropertyDecl::Assign: break;
1536 case ObjCPropertyDecl::Copy: S += ",C"; break;
1537 case ObjCPropertyDecl::Retain: S += ",&"; break;
1538 }
1539 }
1540
1541 // It really isn't clear at all what this means, since properties
1542 // are "dynamic by default".
1543 if (Dynamic)
1544 S += ",D";
1545
1546 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1547 S += ",G";
1548 S += PD->getGetterName().getName();
1549 }
1550
1551 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1552 S += ",S";
1553 S += PD->getSetterName().getName();
1554 }
1555
1556 if (SynthesizePID) {
1557 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1558 S += ",V";
1559 S += OID->getName();
1560 }
1561
1562 // FIXME: OBJCGC: weak & strong
1563}
1564
Fariborz Jahanian248db262008-01-22 22:44:46 +00001565void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnera1923f62008-08-04 07:31:14 +00001566 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001567 // FIXME: This currently doesn't encode:
1568 // @ An object (whether statically typed or typed id)
1569 // # A class object (Class)
1570 // : A method selector (SEL)
1571 // {name=type...} A structure
1572 // (name=type...) A union
1573 // bnum A bit field of num bits
1574
1575 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001576 char encoding;
1577 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001578 default: assert(0 && "Unhandled builtin type kind");
1579 case BuiltinType::Void: encoding = 'v'; break;
1580 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001581 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001582 case BuiltinType::UChar: encoding = 'C'; break;
1583 case BuiltinType::UShort: encoding = 'S'; break;
1584 case BuiltinType::UInt: encoding = 'I'; break;
1585 case BuiltinType::ULong: encoding = 'L'; break;
1586 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001587 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001588 case BuiltinType::SChar: encoding = 'c'; break;
1589 case BuiltinType::Short: encoding = 's'; break;
1590 case BuiltinType::Int: encoding = 'i'; break;
1591 case BuiltinType::Long: encoding = 'l'; break;
1592 case BuiltinType::LongLong: encoding = 'q'; break;
1593 case BuiltinType::Float: encoding = 'f'; break;
1594 case BuiltinType::Double: encoding = 'd'; break;
1595 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001596 }
1597
1598 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001599 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001600 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001601 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001602 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001603
1604 }
1605 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001606 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001607 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001608 S += '@';
1609 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001610 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001611 S += '#';
1612 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001613 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001614 S += ':';
1615 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001616 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001617
1618 if (PointeeTy->isCharType()) {
1619 // char pointer types should be encoded as '*' unless it is a
1620 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001621 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001622 S += '*';
1623 return;
1624 }
1625 }
1626
1627 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001628 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnera1923f62008-08-04 07:31:14 +00001629 } else if (const ArrayType *AT =
1630 // Ignore type qualifiers etc.
1631 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001632 S += '[';
1633
1634 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1635 S += llvm::utostr(CAT->getSize().getZExtValue());
1636 else
1637 assert(0 && "Unhandled array type!");
1638
Fariborz Jahanian248db262008-01-22 22:44:46 +00001639 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001640 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001641 } else if (T->getAsFunctionType()) {
1642 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001643 } else if (const RecordType *RTy = T->getAsRecordType()) {
1644 RecordDecl *RDecl= RTy->getDecl();
Steve Naroff03385292008-08-14 15:00:38 +00001645 // This mimics the behavior in gcc's encode_aggregate_within().
1646 // The idea is to only inline structure definitions for top level pointers
1647 // to structures and embedded structures.
1648 bool inlining = (S.size() == 1 && S[0] == '^' ||
1649 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001650 S += '{';
1651 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001652 bool found = false;
1653 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1654 if (ERType[i] == RTy) {
1655 found = true;
1656 break;
1657 }
Steve Naroff03385292008-08-14 15:00:38 +00001658 if (!found && inlining) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001659 ERType.push_back(RTy);
1660 S += '=';
1661 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1662 FieldDecl *field = RDecl->getMember(i);
1663 getObjCEncodingForType(field->getType(), S, ERType);
1664 }
1665 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1666 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001667 }
1668 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001669 } else if (T->isEnumeralType()) {
1670 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001671 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001672 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001673}
1674
Ted Kremenek42730c52008-01-07 19:49:32 +00001675void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001676 std::string& S) const {
1677 if (QT & Decl::OBJC_TQ_In)
1678 S += 'n';
1679 if (QT & Decl::OBJC_TQ_Inout)
1680 S += 'N';
1681 if (QT & Decl::OBJC_TQ_Out)
1682 S += 'o';
1683 if (QT & Decl::OBJC_TQ_Bycopy)
1684 S += 'O';
1685 if (QT & Decl::OBJC_TQ_Byref)
1686 S += 'R';
1687 if (QT & Decl::OBJC_TQ_Oneway)
1688 S += 'V';
1689}
1690
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001691void ASTContext::setBuiltinVaListType(QualType T)
1692{
1693 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1694
1695 BuiltinVaListType = T;
1696}
1697
Ted Kremenek42730c52008-01-07 19:49:32 +00001698void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001699{
Ted Kremenek42730c52008-01-07 19:49:32 +00001700 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001701
Ted Kremenek42730c52008-01-07 19:49:32 +00001702 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001703
1704 // typedef struct objc_object *id;
1705 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1706 assert(ptr && "'id' incorrectly typed");
1707 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1708 assert(rec && "'id' incorrectly typed");
1709 IdStructType = rec;
1710}
1711
Ted Kremenek42730c52008-01-07 19:49:32 +00001712void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001713{
Ted Kremenek42730c52008-01-07 19:49:32 +00001714 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001715
Ted Kremenek42730c52008-01-07 19:49:32 +00001716 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001717
1718 // typedef struct objc_selector *SEL;
1719 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1720 assert(ptr && "'SEL' incorrectly typed");
1721 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1722 assert(rec && "'SEL' incorrectly typed");
1723 SelStructType = rec;
1724}
1725
Ted Kremenek42730c52008-01-07 19:49:32 +00001726void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001727{
Ted Kremenek42730c52008-01-07 19:49:32 +00001728 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1729 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001730}
1731
Ted Kremenek42730c52008-01-07 19:49:32 +00001732void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001733{
Ted Kremenek42730c52008-01-07 19:49:32 +00001734 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001735
Ted Kremenek42730c52008-01-07 19:49:32 +00001736 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001737
1738 // typedef struct objc_class *Class;
1739 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1740 assert(ptr && "'Class' incorrectly typed");
1741 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1742 assert(rec && "'Class' incorrectly typed");
1743 ClassStructType = rec;
1744}
1745
Ted Kremenek42730c52008-01-07 19:49:32 +00001746void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1747 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001748 "'NSConstantString' type already set!");
1749
Ted Kremenek42730c52008-01-07 19:49:32 +00001750 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001751}
1752
Ted Kremenek118930e2008-07-24 23:58:27 +00001753
1754//===----------------------------------------------------------------------===//
1755// Type Predicates.
1756//===----------------------------------------------------------------------===//
1757
1758/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1759/// to an object type. This includes "id" and "Class" (two 'special' pointers
1760/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1761/// ID type).
1762bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1763 if (Ty->isObjCQualifiedIdType())
1764 return true;
1765
1766 if (!Ty->isPointerType())
1767 return false;
1768
1769 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1770 // pointer types. This looks for the typedef specifically, not for the
1771 // underlying type.
1772 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1773 return true;
1774
1775 // If this a pointer to an interface (e.g. NSString*), it is ok.
1776 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1777}
1778
Chris Lattner6ff358b2008-04-07 06:51:04 +00001779//===----------------------------------------------------------------------===//
1780// Type Compatibility Testing
1781//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001782
Steve Naroff3454b6c2008-09-04 15:10:53 +00001783/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001784/// block types. Types must be strictly compatible here. For example,
1785/// C unfortunately doesn't produce an error for the following:
1786///
1787/// int (*emptyArgFunc)();
1788/// int (*intArgList)(int) = emptyArgFunc;
1789///
1790/// For blocks, we will produce an error for the following (similar to C++):
1791///
1792/// int (^emptyArgBlock)();
1793/// int (^intArgBlock)(int) = emptyArgBlock;
1794///
1795/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1796///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001797bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
1798 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers())
1799 return false;
1800
1801 QualType lcanon = getCanonicalType(lhs);
1802 QualType rcanon = getCanonicalType(rhs);
1803
1804 // If two types are identical, they are are compatible
1805 if (lcanon == rcanon)
1806 return true;
1807 if (isa<FunctionType>(lcanon) && isa<FunctionType>(rcanon)) {
1808 const FunctionType *lbase = cast<FunctionType>(lcanon);
1809 const FunctionType *rbase = cast<FunctionType>(rcanon);
1810
1811 // First check the return types.
1812 if (!typesAreBlockCompatible(lbase->getResultType(),rbase->getResultType()))
1813 return false;
1814
1815 // Return types matched, now check the argument types.
1816 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1817 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1818
1819 if (lproto && rproto) { // two C99 style function prototypes
1820 unsigned lproto_nargs = lproto->getNumArgs();
1821 unsigned rproto_nargs = rproto->getNumArgs();
1822
1823 if (lproto_nargs != rproto_nargs)
1824 return false;
1825
1826 if (lproto->isVariadic() || rproto->isVariadic())
1827 return false;
1828
1829 // The use of ellipsis agree...now check the argument types.
1830 for (unsigned i = 0; i < lproto_nargs; i++)
1831 if (!typesAreBlockCompatible(lproto->getArgType(i),
1832 rproto->getArgType(i)))
1833 return false;
1834 return true;
1835 }
1836 return (!lproto && !rproto); // two K&R style function decls match.
1837 }
1838 return false;
1839}
1840
Chris Lattner6ff358b2008-04-07 06:51:04 +00001841/// areCompatVectorTypes - Return true if the two specified vector types are
1842/// compatible.
1843static bool areCompatVectorTypes(const VectorType *LHS,
1844 const VectorType *RHS) {
1845 assert(LHS->isCanonical() && RHS->isCanonical());
1846 return LHS->getElementType() == RHS->getElementType() &&
1847 LHS->getNumElements() == RHS->getNumElements();
1848}
1849
Eli Friedman0d9549b2008-08-22 00:56:42 +00001850/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001851/// compatible for assignment from RHS to LHS. This handles validation of any
1852/// protocol qualifiers on the LHS or RHS.
1853///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001854bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1855 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001856 // Verify that the base decls are compatible: the RHS must be a subclass of
1857 // the LHS.
1858 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1859 return false;
1860
1861 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1862 // protocol qualified at all, then we are good.
1863 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1864 return true;
1865
1866 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1867 // isn't a superset.
1868 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1869 return true; // FIXME: should return false!
1870
1871 // Finally, we must have two protocol-qualified interfaces.
1872 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1873 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1874 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1875 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1876 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1877 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1878
1879 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1880 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1881 // LHS in a single parallel scan until we run out of elements in LHS.
1882 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1883 ObjCProtocolDecl *LHSProto = *LHSPI;
1884
1885 while (RHSPI != RHSPE) {
1886 ObjCProtocolDecl *RHSProto = *RHSPI++;
1887 // If the RHS has a protocol that the LHS doesn't, ignore it.
1888 if (RHSProto != LHSProto)
1889 continue;
1890
1891 // Otherwise, the RHS does have this element.
1892 ++LHSPI;
1893 if (LHSPI == LHSPE)
1894 return true; // All protocols in LHS exist in RHS.
1895
1896 LHSProto = *LHSPI;
1897 }
1898
1899 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1900 return false;
1901}
1902
Steve Naroff85f0dc52007-10-15 20:41:53 +00001903/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1904/// both shall have the identically qualified version of a compatible type.
1905/// C99 6.2.7p1: Two types have compatible types if their types are the
1906/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001907bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1908 return !mergeTypes(LHS, RHS).isNull();
1909}
1910
1911QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1912 const FunctionType *lbase = lhs->getAsFunctionType();
1913 const FunctionType *rbase = rhs->getAsFunctionType();
1914 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1915 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1916 bool allLTypes = true;
1917 bool allRTypes = true;
1918
1919 // Check return type
1920 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1921 if (retType.isNull()) return QualType();
1922 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false;
1923 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false;
1924
1925 if (lproto && rproto) { // two C99 style function prototypes
1926 unsigned lproto_nargs = lproto->getNumArgs();
1927 unsigned rproto_nargs = rproto->getNumArgs();
1928
1929 // Compatible functions must have the same number of arguments
1930 if (lproto_nargs != rproto_nargs)
1931 return QualType();
1932
1933 // Variadic and non-variadic functions aren't compatible
1934 if (lproto->isVariadic() != rproto->isVariadic())
1935 return QualType();
1936
1937 // Check argument compatibility
1938 llvm::SmallVector<QualType, 10> types;
1939 for (unsigned i = 0; i < lproto_nargs; i++) {
1940 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1941 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1942 QualType argtype = mergeTypes(largtype, rargtype);
1943 if (argtype.isNull()) return QualType();
1944 types.push_back(argtype);
1945 if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false;
1946 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false;
1947 }
1948 if (allLTypes) return lhs;
1949 if (allRTypes) return rhs;
1950 return getFunctionType(retType, types.begin(), types.size(),
1951 lproto->isVariadic());
1952 }
1953
1954 if (lproto) allRTypes = false;
1955 if (rproto) allLTypes = false;
1956
1957 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1958 if (proto) {
1959 if (proto->isVariadic()) return QualType();
1960 // Check that the types are compatible with the types that
1961 // would result from default argument promotions (C99 6.7.5.3p15).
1962 // The only types actually affected are promotable integer
1963 // types and floats, which would be passed as a different
1964 // type depending on whether the prototype is visible.
1965 unsigned proto_nargs = proto->getNumArgs();
1966 for (unsigned i = 0; i < proto_nargs; ++i) {
1967 QualType argTy = proto->getArgType(i);
1968 if (argTy->isPromotableIntegerType() ||
1969 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1970 return QualType();
1971 }
1972
1973 if (allLTypes) return lhs;
1974 if (allRTypes) return rhs;
1975 return getFunctionType(retType, proto->arg_type_begin(),
1976 proto->getNumArgs(), lproto->isVariadic());
1977 }
1978
1979 if (allLTypes) return lhs;
1980 if (allRTypes) return rhs;
1981 return getFunctionTypeNoProto(retType);
1982}
1983
1984QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00001985 // C++ [expr]: If an expression initially has the type "reference to T", the
1986 // type is adjusted to "T" prior to any further analysis, the expression
1987 // designates the object or function denoted by the reference, and the
1988 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001989 // FIXME: C++ shouldn't be going through here! The rules are different
1990 // enough that they should be handled separately.
1991 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001992 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00001993 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001994 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00001995
Eli Friedman0d9549b2008-08-22 00:56:42 +00001996 QualType LHSCan = getCanonicalType(LHS),
1997 RHSCan = getCanonicalType(RHS);
1998
1999 // If two types are identical, they are compatible.
2000 if (LHSCan == RHSCan)
2001 return LHS;
2002
2003 // If the qualifiers are different, the types aren't compatible
2004 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2005 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2006 return QualType();
2007
2008 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2009 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2010
Chris Lattnerc38d4522008-01-14 05:45:46 +00002011 // We want to consider the two function types to be the same for these
2012 // comparisons, just force one to the other.
2013 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2014 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002015
2016 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002017 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2018 LHSClass = Type::ConstantArray;
2019 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2020 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002021
Nate Begemanaf6ed502008-04-18 23:10:10 +00002022 // Canonicalize ExtVector -> Vector.
2023 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2024 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002025
Chris Lattner7cdcb252008-04-07 06:38:24 +00002026 // Consider qualified interfaces and interfaces the same.
2027 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2028 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002029
Chris Lattnerb5709e22008-04-07 05:43:21 +00002030 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002031 if (LHSClass != RHSClass) {
Steve Naroff44549772008-06-04 15:07:33 +00002032 // ID is compatible with all qualified id types.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002033 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00002034 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002035 if (isObjCIdType(PT->getPointeeType()))
2036 return LHS;
Steve Naroff44549772008-06-04 15:07:33 +00002037 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002038 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00002039 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002040 if (isObjCIdType(PT->getPointeeType()))
2041 return RHS;
2042 }
2043
Chris Lattnerc38d4522008-01-14 05:45:46 +00002044 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2045 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002046 if (const EnumType* ETy = LHS->getAsEnumType()) {
2047 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2048 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002049 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002050 if (const EnumType* ETy = RHS->getAsEnumType()) {
2051 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2052 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002053 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002054
Eli Friedman0d9549b2008-08-22 00:56:42 +00002055 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002056 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002057
Steve Naroffc88babe2008-01-09 22:43:08 +00002058 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002059 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002060 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002061 {
2062 // Merge two pointer types, while trying to preserve typedef info
2063 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2064 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2065 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2066 if (ResultType.isNull()) return QualType();
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002067 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS;
2068 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002069 return getPointerType(ResultType);
2070 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002071 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002072 {
2073 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2074 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2075 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2076 return QualType();
2077
2078 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2079 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2080 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2081 if (ResultType.isNull()) return QualType();
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002082 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2083 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
2084 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2085 ArrayType::ArraySizeModifier(), 0);
2086 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2087 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002088 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2089 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002090 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2091 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002092 if (LVAT) {
2093 // FIXME: This isn't correct! But tricky to implement because
2094 // the array's size has to be the size of LHS, but the type
2095 // has to be different.
2096 return LHS;
2097 }
2098 if (RVAT) {
2099 // FIXME: This isn't correct! But tricky to implement because
2100 // the array's size has to be the size of RHS, but the type
2101 // has to be different.
2102 return RHS;
2103 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002104 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2105 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002106 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0);
2107 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002108 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002109 return mergeFunctionTypes(LHS, RHS);
2110 case Type::Tagged:
2111 {
2112 // FIXME: Why are these compatible?
2113 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2114 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2115 return QualType();
2116 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002117 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002118 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002119 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002120 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002121 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2122 return LHS;
Chris Lattnerc38d4522008-01-14 05:45:46 +00002123 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002124 {
2125 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2126 // for checking assignment/comparison safety
2127 return QualType();
2128 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002129 default:
2130 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002131 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002132 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002133}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002134
Chris Lattner1d78a862008-04-07 07:01:58 +00002135//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002136// Integer Predicates
2137//===----------------------------------------------------------------------===//
2138unsigned ASTContext::getIntWidth(QualType T) {
2139 if (T == BoolTy)
2140 return 1;
2141 // At the moment, only bool has padding bits
2142 return (unsigned)getTypeSize(T);
2143}
2144
2145QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2146 assert(T->isSignedIntegerType() && "Unexpected type");
2147 if (const EnumType* ETy = T->getAsEnumType())
2148 T = ETy->getDecl()->getIntegerType();
2149 const BuiltinType* BTy = T->getAsBuiltinType();
2150 assert (BTy && "Unexpected signed integer type");
2151 switch (BTy->getKind()) {
2152 case BuiltinType::Char_S:
2153 case BuiltinType::SChar:
2154 return UnsignedCharTy;
2155 case BuiltinType::Short:
2156 return UnsignedShortTy;
2157 case BuiltinType::Int:
2158 return UnsignedIntTy;
2159 case BuiltinType::Long:
2160 return UnsignedLongTy;
2161 case BuiltinType::LongLong:
2162 return UnsignedLongLongTy;
2163 default:
2164 assert(0 && "Unexpected signed integer type");
2165 return QualType();
2166 }
2167}
2168
2169
2170//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002171// Serialization Support
2172//===----------------------------------------------------------------------===//
2173
Ted Kremenek738e6c02007-10-31 17:10:13 +00002174/// Emit - Serialize an ASTContext object to Bitcode.
2175void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002176 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002177 S.EmitRef(SourceMgr);
2178 S.EmitRef(Target);
2179 S.EmitRef(Idents);
2180 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002181
Ted Kremenek68228a92007-10-31 22:44:07 +00002182 // Emit the size of the type vector so that we can reserve that size
2183 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002184 S.EmitInt(Types.size());
2185
Ted Kremenek034a78c2007-11-13 22:02:55 +00002186 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2187 I!=E;++I)
2188 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002189
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002190 S.EmitOwnedPtr(TUDecl);
2191
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002192 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002193}
2194
Ted Kremenekacba3612007-11-13 00:25:37 +00002195ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002196
2197 // Read the language options.
2198 LangOptions LOpts;
2199 LOpts.Read(D);
2200
Ted Kremenek68228a92007-10-31 22:44:07 +00002201 SourceManager &SM = D.ReadRef<SourceManager>();
2202 TargetInfo &t = D.ReadRef<TargetInfo>();
2203 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2204 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002205
Ted Kremenek68228a92007-10-31 22:44:07 +00002206 unsigned size_reserve = D.ReadInt();
2207
Ted Kremenek842126e2008-06-04 15:55:15 +00002208 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002209
Ted Kremenek034a78c2007-11-13 22:02:55 +00002210 for (unsigned i = 0; i < size_reserve; ++i)
2211 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002212
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002213 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2214
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002215 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002216
2217 return A;
2218}