blob: feb1bad4f95943d38f3b1ddc3abf99c57171c41a [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;
Daniel Dunbar47677342008-09-26 03:23:00 +000057 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000058 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;
Daniel Dunbar47677342008-09-26 03:23:00 +000071 else if (isa<BlockPointerType>(T))
72 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +000073 else if (isa<ReferenceType>(T))
74 ++NumReference;
75 else if (isa<ComplexType>(T))
76 ++NumComplex;
77 else if (isa<ArrayType>(T))
78 ++NumArray;
79 else if (isa<VectorType>(T))
80 ++NumVector;
81 else if (isa<FunctionTypeNoProto>(T))
82 ++NumFunctionNP;
83 else if (isa<FunctionTypeProto>(T))
84 ++NumFunctionP;
85 else if (isa<TypedefType>(T))
86 ++NumTypeName;
87 else if (TagType *TT = dyn_cast<TagType>(T)) {
88 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000089 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000090 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000091 case TagDecl::TK_struct: ++NumTagStruct; break;
92 case TagDecl::TK_union: ++NumTagUnion; break;
93 case TagDecl::TK_class: ++NumTagClass; break;
94 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000095 }
Ted Kremenek42730c52008-01-07 19:49:32 +000096 } else if (isa<ObjCInterfaceType>(T))
97 ++NumObjCInterfaces;
98 else if (isa<ObjCQualifiedInterfaceType>(T))
99 ++NumObjCQualifiedInterfaces;
100 else if (isa<ObjCQualifiedIdType>(T))
101 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000102 else if (isa<TypeOfType>(T))
103 ++NumTypeOfTypes;
104 else if (isa<TypeOfExpr>(T))
105 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000106 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000107 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000108 assert(0 && "Unknown type!");
109 }
110 }
111
112 fprintf(stderr, " %d builtin types\n", NumBuiltin);
113 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000114 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000115 fprintf(stderr, " %d reference types\n", NumReference);
116 fprintf(stderr, " %d complex types\n", NumComplex);
117 fprintf(stderr, " %d array types\n", NumArray);
118 fprintf(stderr, " %d vector types\n", NumVector);
119 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
120 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
121 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
122 fprintf(stderr, " %d tagged types\n", NumTagged);
123 fprintf(stderr, " %d struct types\n", NumTagStruct);
124 fprintf(stderr, " %d union types\n", NumTagUnion);
125 fprintf(stderr, " %d class types\n", NumTagClass);
126 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000127 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000128 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000130 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000131 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000132 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
133 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
134
Chris Lattner4b009652007-07-25 00:24:17 +0000135 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
136 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
137 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
138 NumFunctionP*sizeof(FunctionTypeProto)+
139 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000140 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
141 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000142}
143
144
145void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
146 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
147}
148
Chris Lattner4b009652007-07-25 00:24:17 +0000149void ASTContext::InitBuiltinTypes() {
150 assert(VoidTy.isNull() && "Context reinitialized?");
151
152 // C99 6.2.5p19.
153 InitBuiltinType(VoidTy, BuiltinType::Void);
154
155 // C99 6.2.5p2.
156 InitBuiltinType(BoolTy, BuiltinType::Bool);
157 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000158 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000159 InitBuiltinType(CharTy, BuiltinType::Char_S);
160 else
161 InitBuiltinType(CharTy, BuiltinType::Char_U);
162 // C99 6.2.5p4.
163 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
164 InitBuiltinType(ShortTy, BuiltinType::Short);
165 InitBuiltinType(IntTy, BuiltinType::Int);
166 InitBuiltinType(LongTy, BuiltinType::Long);
167 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
168
169 // C99 6.2.5p6.
170 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
171 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
172 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
173 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
174 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
175
176 // C99 6.2.5p10.
177 InitBuiltinType(FloatTy, BuiltinType::Float);
178 InitBuiltinType(DoubleTy, BuiltinType::Double);
179 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000180
181 // C++ 3.9.1p5
182 InitBuiltinType(WCharTy, BuiltinType::WChar);
183
Chris Lattner4b009652007-07-25 00:24:17 +0000184 // C99 6.2.5p11.
185 FloatComplexTy = getComplexType(FloatTy);
186 DoubleComplexTy = getComplexType(DoubleTy);
187 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000188
189 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000190 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000191 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000192 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000193 ClassStructType = 0;
194
Ted Kremenek42730c52008-01-07 19:49:32 +0000195 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000196
197 // void * type
198 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000199}
200
201//===----------------------------------------------------------------------===//
202// Type Sizing and Analysis
203//===----------------------------------------------------------------------===//
204
Chris Lattner2a674dc2008-06-30 18:32:54 +0000205/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
206/// scalar floating point type.
207const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
208 const BuiltinType *BT = T->getAsBuiltinType();
209 assert(BT && "Not a floating point type!");
210 switch (BT->getKind()) {
211 default: assert(0 && "Not a floating point type!");
212 case BuiltinType::Float: return Target.getFloatFormat();
213 case BuiltinType::Double: return Target.getDoubleFormat();
214 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
215 }
216}
217
218
Chris Lattner4b009652007-07-25 00:24:17 +0000219/// getTypeSize - Return the size of the specified type, in bits. This method
220/// does not work on incomplete types.
221std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000222ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000223 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000224 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 unsigned Align;
226 switch (T->getTypeClass()) {
227 case Type::TypeName: assert(0 && "Not a canonical type!");
228 case Type::FunctionNoProto:
229 case Type::FunctionProto:
230 default:
231 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000232 case Type::VariableArray:
233 assert(0 && "VLAs not implemented yet!");
234 case Type::ConstantArray: {
235 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
236
Chris Lattner8cd0e932008-03-05 18:54:05 +0000237 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000238 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000239 Align = EltInfo.second;
240 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000241 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000242 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000243 case Type::Vector: {
244 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000245 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000246 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000247 // FIXME: This isn't right for unusual vectors
248 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000249 break;
250 }
251
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000252 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000253 switch (cast<BuiltinType>(T)->getKind()) {
254 default: assert(0 && "Unknown builtin type!");
255 case BuiltinType::Void:
256 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000258 Width = Target.getBoolWidth();
259 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000261 case BuiltinType::Char_S:
262 case BuiltinType::Char_U:
263 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000264 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000265 Width = Target.getCharWidth();
266 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000267 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000268 case BuiltinType::WChar:
269 Width = Target.getWCharWidth();
270 Align = Target.getWCharAlign();
271 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000272 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000273 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000274 Width = Target.getShortWidth();
275 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000276 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000277 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000278 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000279 Width = Target.getIntWidth();
280 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000281 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000282 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000283 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000284 Width = Target.getLongWidth();
285 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000286 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000287 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000288 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 Width = Target.getLongLongWidth();
290 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000291 break;
292 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000293 Width = Target.getFloatWidth();
294 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000295 break;
296 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000297 Width = Target.getDoubleWidth();
298 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000299 break;
300 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000301 Width = Target.getLongDoubleWidth();
302 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000303 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000304 }
305 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000306 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000307 // FIXME: Pointers into different addr spaces could have different sizes and
308 // alignment requirements: getPointerInfo should take an AddrSpace.
309 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000310 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000311 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000312 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000313 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000314 case Type::BlockPointer: {
315 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
316 Width = Target.getPointerWidth(AS);
317 Align = Target.getPointerAlign(AS);
318 break;
319 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000320 case Type::Pointer: {
321 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000322 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000323 Align = Target.getPointerAlign(AS);
324 break;
325 }
Chris Lattner4b009652007-07-25 00:24:17 +0000326 case Type::Reference:
327 // "When applied to a reference or a reference type, the result is the size
328 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000329 // FIXME: This is wrong for struct layout: a reference in a struct has
330 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000331 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000332
333 case Type::Complex: {
334 // Complex types have the same alignment as their elements, but twice the
335 // size.
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000337 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000339 Align = EltInfo.second;
340 break;
341 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000342 case Type::ObjCInterface: {
343 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
344 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
345 Width = Layout.getSize();
346 Align = Layout.getAlignment();
347 break;
348 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000349 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000350 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
351 Width = 1;
352 Align = 1;
353 break;
354 }
355
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000356 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
357 return getTypeInfo(ET->getDecl()->getIntegerType());
358
359 RecordType *RT = cast<RecordType>(T);
360 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
361 Width = Layout.getSize();
362 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000363 break;
364 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000365 }
Chris Lattner4b009652007-07-25 00:24:17 +0000366
367 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000368 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000369}
370
Devang Patelbfe323c2008-06-04 21:22:16 +0000371/// LayoutField - Field layout.
372void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
373 bool IsUnion, bool StructIsPacked,
374 ASTContext &Context) {
375 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
376 uint64_t FieldOffset = IsUnion ? 0 : Size;
377 uint64_t FieldSize;
378 unsigned FieldAlign;
379
380 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
381 // TODO: Need to check this algorithm on other targets!
382 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000383 FieldSize =
384 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000385
386 std::pair<uint64_t, unsigned> FieldInfo =
387 Context.getTypeInfo(FD->getType());
388 uint64_t TypeSize = FieldInfo.first;
389
390 FieldAlign = FieldInfo.second;
391 if (FieldIsPacked)
392 FieldAlign = 1;
393 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
394 FieldAlign = std::max(FieldAlign, AA->getAlignment());
395
396 // Check if we need to add padding to give the field the correct
397 // alignment.
398 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
399 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
400
401 // Padding members don't affect overall alignment
402 if (!FD->getIdentifier())
403 FieldAlign = 1;
404 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000405 if (FD->getType()->isIncompleteArrayType()) {
406 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000407 // query getTypeInfo about these, so we figure it out here.
408 // Flexible array members don't have any size, but they
409 // have to be aligned appropriately for their element type.
410 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000411 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000412 FieldAlign = Context.getTypeAlign(ATy->getElementType());
413 } else {
414 std::pair<uint64_t, unsigned> FieldInfo =
415 Context.getTypeInfo(FD->getType());
416 FieldSize = FieldInfo.first;
417 FieldAlign = FieldInfo.second;
418 }
419
420 if (FieldIsPacked)
421 FieldAlign = 8;
422 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
423 FieldAlign = std::max(FieldAlign, AA->getAlignment());
424
425 // Round up the current record size to the field's alignment boundary.
426 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
427 }
428
429 // Place this field at the current location.
430 FieldOffsets[FieldNo] = FieldOffset;
431
432 // Reserve space for this field.
433 if (IsUnion) {
434 Size = std::max(Size, FieldSize);
435 } else {
436 Size = FieldOffset + FieldSize;
437 }
438
439 // Remember max struct/class alignment.
440 Alignment = std::max(Alignment, FieldAlign);
441}
442
Devang Patel4b6bf702008-06-04 21:54:36 +0000443
444/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
445/// specified Objective C, which indicates its size and ivar
446/// position information.
447const ASTRecordLayout &
448ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
449 // Look up this layout, if already laid out, return what we have.
450 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
451 if (Entry) return *Entry;
452
453 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
454 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000455 ASTRecordLayout *NewEntry = NULL;
456 unsigned FieldCount = D->ivar_size();
457 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
458 FieldCount++;
459 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
460 unsigned Alignment = SL.getAlignment();
461 uint64_t Size = SL.getSize();
462 NewEntry = new ASTRecordLayout(Size, Alignment);
463 NewEntry->InitializeLayout(FieldCount);
464 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
465 } else {
466 NewEntry = new ASTRecordLayout();
467 NewEntry->InitializeLayout(FieldCount);
468 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000469 Entry = NewEntry;
470
Devang Patel4b6bf702008-06-04 21:54:36 +0000471 bool IsPacked = D->getAttr<PackedAttr>();
472
473 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
474 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
475 AA->getAlignment()));
476
477 // Layout each ivar sequentially.
478 unsigned i = 0;
479 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
480 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
481 const ObjCIvarDecl* Ivar = (*IVI);
482 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
483 }
484
485 // Finally, round the size of the total struct up to the alignment of the
486 // struct itself.
487 NewEntry->FinalizeLayout();
488 return *NewEntry;
489}
490
Devang Patel7a78e432007-11-01 19:11:01 +0000491/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000492/// specified record (struct/union/class), which indicates its size and field
493/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000494const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000495 D = D->getDefinition(*this);
496 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000497
Chris Lattner4b009652007-07-25 00:24:17 +0000498 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000499 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000500 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000501
Devang Patel7a78e432007-11-01 19:11:01 +0000502 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
503 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
504 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000505 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000506
Devang Patelbfe323c2008-06-04 21:22:16 +0000507 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000508 bool StructIsPacked = D->getAttr<PackedAttr>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000509 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000510
Eli Friedman5949a022008-05-30 09:31:38 +0000511 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000512 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
513 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000514
Eli Friedman5949a022008-05-30 09:31:38 +0000515 // Layout each field, for now, just sequentially, respecting alignment. In
516 // the future, this will need to be tweakable by targets.
517 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
518 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000519 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000520 }
Eli Friedman5949a022008-05-30 09:31:38 +0000521
522 // Finally, round the size of the total struct up to the alignment of the
523 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000524 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000525 return *NewEntry;
526}
527
Chris Lattner4b009652007-07-25 00:24:17 +0000528//===----------------------------------------------------------------------===//
529// Type creation/memoization methods
530//===----------------------------------------------------------------------===//
531
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000532QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000533 QualType CanT = getCanonicalType(T);
534 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000535 return T;
536
537 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
538 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000539 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000540 "Type is already address space qualified");
541
542 // Check if we've already instantiated an address space qual'd type of this
543 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000544 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000545 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000546 void *InsertPos = 0;
547 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
548 return QualType(ASQy, 0);
549
550 // If the base type isn't canonical, this won't be a canonical type either,
551 // so fill in the canonical type field.
552 QualType Canonical;
553 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000554 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000555
556 // Get the new insert position for the node we care about.
557 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
558 assert(NewIP == 0 && "Shouldn't be in the map!");
559 }
Chris Lattner35fef522008-02-20 20:55:12 +0000560 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000561 ASQualTypes.InsertNode(New, InsertPos);
562 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000563 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000564}
565
Chris Lattner4b009652007-07-25 00:24:17 +0000566
567/// getComplexType - Return the uniqued reference to the type for a complex
568/// number with the specified element type.
569QualType ASTContext::getComplexType(QualType T) {
570 // Unique pointers, to guarantee there is only one pointer of a particular
571 // structure.
572 llvm::FoldingSetNodeID ID;
573 ComplexType::Profile(ID, T);
574
575 void *InsertPos = 0;
576 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
577 return QualType(CT, 0);
578
579 // If the pointee type isn't canonical, this won't be a canonical type either,
580 // so fill in the canonical type field.
581 QualType Canonical;
582 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000583 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000584
585 // Get the new insert position for the node we care about.
586 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
587 assert(NewIP == 0 && "Shouldn't be in the map!");
588 }
589 ComplexType *New = new ComplexType(T, Canonical);
590 Types.push_back(New);
591 ComplexTypes.InsertNode(New, InsertPos);
592 return QualType(New, 0);
593}
594
595
596/// getPointerType - Return the uniqued reference to the type for a pointer to
597/// the specified type.
598QualType ASTContext::getPointerType(QualType T) {
599 // Unique pointers, to guarantee there is only one pointer of a particular
600 // structure.
601 llvm::FoldingSetNodeID ID;
602 PointerType::Profile(ID, T);
603
604 void *InsertPos = 0;
605 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
606 return QualType(PT, 0);
607
608 // If the pointee type isn't canonical, this won't be a canonical type either,
609 // so fill in the canonical type field.
610 QualType Canonical;
611 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000612 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000613
614 // Get the new insert position for the node we care about.
615 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
616 assert(NewIP == 0 && "Shouldn't be in the map!");
617 }
618 PointerType *New = new PointerType(T, Canonical);
619 Types.push_back(New);
620 PointerTypes.InsertNode(New, InsertPos);
621 return QualType(New, 0);
622}
623
Steve Naroff7aa54752008-08-27 16:04:49 +0000624/// getBlockPointerType - Return the uniqued reference to the type for
625/// a pointer to the specified block.
626QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000627 assert(T->isFunctionType() && "block of function types only");
628 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000629 // structure.
630 llvm::FoldingSetNodeID ID;
631 BlockPointerType::Profile(ID, T);
632
633 void *InsertPos = 0;
634 if (BlockPointerType *PT =
635 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
636 return QualType(PT, 0);
637
Steve Narofffd5b19d2008-08-28 19:20:44 +0000638 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000639 // type either so fill in the canonical type field.
640 QualType Canonical;
641 if (!T->isCanonical()) {
642 Canonical = getBlockPointerType(getCanonicalType(T));
643
644 // Get the new insert position for the node we care about.
645 BlockPointerType *NewIP =
646 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
647 assert(NewIP == 0 && "Shouldn't be in the map!");
648 }
649 BlockPointerType *New = new BlockPointerType(T, Canonical);
650 Types.push_back(New);
651 BlockPointerTypes.InsertNode(New, InsertPos);
652 return QualType(New, 0);
653}
654
Chris Lattner4b009652007-07-25 00:24:17 +0000655/// getReferenceType - Return the uniqued reference to the type for a reference
656/// to the specified type.
657QualType ASTContext::getReferenceType(QualType T) {
658 // Unique pointers, to guarantee there is only one pointer of a particular
659 // structure.
660 llvm::FoldingSetNodeID ID;
661 ReferenceType::Profile(ID, T);
662
663 void *InsertPos = 0;
664 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
665 return QualType(RT, 0);
666
667 // If the referencee type isn't canonical, this won't be a canonical type
668 // either, so fill in the canonical type field.
669 QualType Canonical;
670 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000671 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000672
673 // Get the new insert position for the node we care about.
674 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
675 assert(NewIP == 0 && "Shouldn't be in the map!");
676 }
677
678 ReferenceType *New = new ReferenceType(T, Canonical);
679 Types.push_back(New);
680 ReferenceTypes.InsertNode(New, InsertPos);
681 return QualType(New, 0);
682}
683
Steve Naroff83c13012007-08-30 01:06:46 +0000684/// getConstantArrayType - Return the unique reference to the type for an
685/// array of the specified element type.
686QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000687 const llvm::APInt &ArySize,
688 ArrayType::ArraySizeModifier ASM,
689 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000690 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000691 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000692
693 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000694 if (ConstantArrayType *ATP =
695 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000696 return QualType(ATP, 0);
697
698 // If the element type isn't canonical, this won't be a canonical type either,
699 // so fill in the canonical type field.
700 QualType Canonical;
701 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000702 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000703 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000704 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000705 ConstantArrayType *NewIP =
706 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
707
Chris Lattner4b009652007-07-25 00:24:17 +0000708 assert(NewIP == 0 && "Shouldn't be in the map!");
709 }
710
Steve Naroff24c9b982007-08-30 18:10:14 +0000711 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
712 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000713 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000714 Types.push_back(New);
715 return QualType(New, 0);
716}
717
Steve Naroffe2579e32007-08-30 18:14:25 +0000718/// getVariableArrayType - Returns a non-unique reference to the type for a
719/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000720QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
721 ArrayType::ArraySizeModifier ASM,
722 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000723 // Since we don't unique expressions, it isn't possible to unique VLA's
724 // that have an expression provided for their size.
725
726 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
727 ASM, EltTypeQuals);
728
729 VariableArrayTypes.push_back(New);
730 Types.push_back(New);
731 return QualType(New, 0);
732}
733
734QualType ASTContext::getIncompleteArrayType(QualType EltTy,
735 ArrayType::ArraySizeModifier ASM,
736 unsigned EltTypeQuals) {
737 llvm::FoldingSetNodeID ID;
738 IncompleteArrayType::Profile(ID, EltTy);
739
740 void *InsertPos = 0;
741 if (IncompleteArrayType *ATP =
742 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
743 return QualType(ATP, 0);
744
745 // If the element type isn't canonical, this won't be a canonical type
746 // either, so fill in the canonical type field.
747 QualType Canonical;
748
749 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000750 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000751 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000752
753 // Get the new insert position for the node we care about.
754 IncompleteArrayType *NewIP =
755 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
756
757 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000758 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000759
760 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
761 ASM, EltTypeQuals);
762
763 IncompleteArrayTypes.InsertNode(New, InsertPos);
764 Types.push_back(New);
765 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000766}
767
Chris Lattner4b009652007-07-25 00:24:17 +0000768/// getVectorType - Return the unique reference to a vector type of
769/// the specified element type and size. VectorType must be a built-in type.
770QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
771 BuiltinType *baseType;
772
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000773 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000774 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
775
776 // Check if we've already instantiated a vector of this type.
777 llvm::FoldingSetNodeID ID;
778 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
779 void *InsertPos = 0;
780 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
781 return QualType(VTP, 0);
782
783 // If the element type isn't canonical, this won't be a canonical type either,
784 // so fill in the canonical type field.
785 QualType Canonical;
786 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000787 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000788
789 // Get the new insert position for the node we care about.
790 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
791 assert(NewIP == 0 && "Shouldn't be in the map!");
792 }
793 VectorType *New = new VectorType(vecType, NumElts, Canonical);
794 VectorTypes.InsertNode(New, InsertPos);
795 Types.push_back(New);
796 return QualType(New, 0);
797}
798
Nate Begemanaf6ed502008-04-18 23:10:10 +0000799/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000800/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000801QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000802 BuiltinType *baseType;
803
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000804 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000805 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000806
807 // Check if we've already instantiated a vector of this type.
808 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000809 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000810 void *InsertPos = 0;
811 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
812 return QualType(VTP, 0);
813
814 // If the element type isn't canonical, this won't be a canonical type either,
815 // so fill in the canonical type field.
816 QualType Canonical;
817 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000818 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000819
820 // Get the new insert position for the node we care about.
821 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
822 assert(NewIP == 0 && "Shouldn't be in the map!");
823 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000824 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000825 VectorTypes.InsertNode(New, InsertPos);
826 Types.push_back(New);
827 return QualType(New, 0);
828}
829
830/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
831///
832QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
833 // Unique functions, to guarantee there is only one function of a particular
834 // structure.
835 llvm::FoldingSetNodeID ID;
836 FunctionTypeNoProto::Profile(ID, ResultTy);
837
838 void *InsertPos = 0;
839 if (FunctionTypeNoProto *FT =
840 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
841 return QualType(FT, 0);
842
843 QualType Canonical;
844 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000845 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000846
847 // Get the new insert position for the node we care about.
848 FunctionTypeNoProto *NewIP =
849 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
850 assert(NewIP == 0 && "Shouldn't be in the map!");
851 }
852
853 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
854 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000855 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000856 return QualType(New, 0);
857}
858
859/// getFunctionType - Return a normal function type with a typed argument
860/// list. isVariadic indicates whether the argument list includes '...'.
Eli Friedman36104c12008-08-22 00:59:49 +0000861QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray,
Chris Lattner4b009652007-07-25 00:24:17 +0000862 unsigned NumArgs, bool isVariadic) {
863 // Unique functions, to guarantee there is only one function of a particular
864 // structure.
865 llvm::FoldingSetNodeID ID;
866 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
867
868 void *InsertPos = 0;
869 if (FunctionTypeProto *FTP =
870 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
871 return QualType(FTP, 0);
872
873 // Determine whether the type being created is already canonical or not.
874 bool isCanonical = ResultTy->isCanonical();
875 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
876 if (!ArgArray[i]->isCanonical())
877 isCanonical = false;
878
879 // If this type isn't canonical, get the canonical version of it.
880 QualType Canonical;
881 if (!isCanonical) {
882 llvm::SmallVector<QualType, 16> CanonicalArgs;
883 CanonicalArgs.reserve(NumArgs);
884 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000885 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000886
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000887 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000888 &CanonicalArgs[0], NumArgs,
889 isVariadic);
890
891 // Get the new insert position for the node we care about.
892 FunctionTypeProto *NewIP =
893 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
894 assert(NewIP == 0 && "Shouldn't be in the map!");
895 }
896
897 // FunctionTypeProto objects are not allocated with new because they have a
898 // variable size array (for parameter types) at the end of them.
899 FunctionTypeProto *FTP =
900 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
901 NumArgs*sizeof(QualType));
902 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
903 Canonical);
904 Types.push_back(FTP);
905 FunctionTypeProtos.InsertNode(FTP, InsertPos);
906 return QualType(FTP, 0);
907}
908
Douglas Gregor1d661552008-04-13 21:07:44 +0000909/// getTypeDeclType - Return the unique reference to the type for the
910/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000911QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Douglas Gregor1d661552008-04-13 21:07:44 +0000912 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
913
914 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
915 return getTypedefType(Typedef);
916 else if (ObjCInterfaceDecl *ObjCInterface
917 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
918 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000919
Ted Kremenek46a837c2008-09-05 17:16:31 +0000920 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl)) {
921 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
922 : new CXXRecordType(CXXRecord);
923 }
924 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
925 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
926 : new RecordType(Record);
927 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000928 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000929 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000930 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000931 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000932
Ted Kremenek46a837c2008-09-05 17:16:31 +0000933 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000934 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000935}
936
Ted Kremenek46a837c2008-09-05 17:16:31 +0000937/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
938/// about which RecordDecl serves as the definition of a particular
939/// struct/union/class. This will eventually be used by enums as well.
940void ASTContext::setTagDefinition(TagDecl* D) {
941 assert (D->isDefinition());
942 cast<TagType>(D->TypeForDecl)->decl = D;
943}
944
Chris Lattner4b009652007-07-25 00:24:17 +0000945/// getTypedefType - Return the unique reference to the type for the
946/// specified typename decl.
947QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
948 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
949
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000950 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000951 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000952 Types.push_back(Decl->TypeForDecl);
953 return QualType(Decl->TypeForDecl, 0);
954}
955
Ted Kremenek42730c52008-01-07 19:49:32 +0000956/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000957/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000958QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000959 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
960
Ted Kremenek42730c52008-01-07 19:49:32 +0000961 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000962 Types.push_back(Decl->TypeForDecl);
963 return QualType(Decl->TypeForDecl, 0);
964}
965
Chris Lattnere1352302008-04-07 04:56:42 +0000966/// CmpProtocolNames - Comparison predicate for sorting protocols
967/// alphabetically.
968static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
969 const ObjCProtocolDecl *RHS) {
970 return strcmp(LHS->getName(), RHS->getName()) < 0;
971}
972
973static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
974 unsigned &NumProtocols) {
975 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
976
977 // Sort protocols, keyed by name.
978 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
979
980 // Remove duplicates.
981 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
982 NumProtocols = ProtocolsEnd-Protocols;
983}
984
985
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000986/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
987/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000988QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
989 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000990 // Sort the protocol list alphabetically to canonicalize it.
991 SortAndUniqueProtocols(Protocols, NumProtocols);
992
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000993 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000994 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000995
996 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000997 if (ObjCQualifiedInterfaceType *QT =
998 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000999 return QualType(QT, 0);
1000
1001 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001002 ObjCQualifiedInterfaceType *QType =
1003 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001004 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001005 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001006 return QualType(QType, 0);
1007}
1008
Chris Lattnere1352302008-04-07 04:56:42 +00001009/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1010/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001011QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001012 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001013 // Sort the protocol list alphabetically to canonicalize it.
1014 SortAndUniqueProtocols(Protocols, NumProtocols);
1015
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001016 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001017 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001018
1019 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001020 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001021 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001022 return QualType(QT, 0);
1023
1024 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001025 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001026 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001027 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001028 return QualType(QType, 0);
1029}
1030
Steve Naroff0604dd92007-08-01 18:02:17 +00001031/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1032/// TypeOfExpr AST's (since expression's are never shared). For example,
1033/// multiple declarations that refer to "typeof(x)" all contain different
1034/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1035/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001036QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001037 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001038 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1039 Types.push_back(toe);
1040 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001041}
1042
Steve Naroff0604dd92007-08-01 18:02:17 +00001043/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1044/// TypeOfType AST's. The only motivation to unique these nodes would be
1045/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1046/// an issue. This doesn't effect the type checker, since it operates
1047/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001048QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001049 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001050 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1051 Types.push_back(tot);
1052 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001053}
1054
Chris Lattner4b009652007-07-25 00:24:17 +00001055/// getTagDeclType - Return the unique reference to the type for the
1056/// specified TagDecl (struct/union/class/enum) decl.
1057QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001058 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001059 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001060}
1061
1062/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1063/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1064/// needs to agree with the definition in <stddef.h>.
1065QualType ASTContext::getSizeType() const {
1066 // On Darwin, size_t is defined as a "long unsigned int".
1067 // FIXME: should derive from "Target".
1068 return UnsignedLongTy;
1069}
1070
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001071/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001072/// width of characters in wide strings, The value is target dependent and
1073/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001074QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001075 if (LangOpts.CPlusPlus)
1076 return WCharTy;
1077
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001078 // On Darwin, wchar_t is defined as a "int".
1079 // FIXME: should derive from "Target".
1080 return IntTy;
1081}
1082
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001083/// getSignedWCharType - Return the type of "signed wchar_t".
1084/// Used when in C++, as a GCC extension.
1085QualType ASTContext::getSignedWCharType() const {
1086 // FIXME: derive from "Target" ?
1087 return WCharTy;
1088}
1089
1090/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1091/// Used when in C++, as a GCC extension.
1092QualType ASTContext::getUnsignedWCharType() const {
1093 // FIXME: derive from "Target" ?
1094 return UnsignedIntTy;
1095}
1096
Chris Lattner4b009652007-07-25 00:24:17 +00001097/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1098/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1099QualType ASTContext::getPointerDiffType() const {
1100 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1101 // FIXME: should derive from "Target".
1102 return IntTy;
1103}
1104
Chris Lattner19eb97e2008-04-02 05:18:44 +00001105//===----------------------------------------------------------------------===//
1106// Type Operators
1107//===----------------------------------------------------------------------===//
1108
Chris Lattner3dae6f42008-04-06 22:41:35 +00001109/// getCanonicalType - Return the canonical (structural) type corresponding to
1110/// the specified potentially non-canonical type. The non-canonical version
1111/// of a type may have many "decorated" versions of types. Decorators can
1112/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1113/// to be free of any of these, allowing two canonical types to be compared
1114/// for exact equality with a simple pointer comparison.
1115QualType ASTContext::getCanonicalType(QualType T) {
1116 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001117
1118 // If the result has type qualifiers, make sure to canonicalize them as well.
1119 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1120 if (TypeQuals == 0) return CanType;
1121
1122 // If the type qualifiers are on an array type, get the canonical type of the
1123 // array with the qualifiers applied to the element type.
1124 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1125 if (!AT)
1126 return CanType.getQualifiedType(TypeQuals);
1127
1128 // Get the canonical version of the element with the extra qualifiers on it.
1129 // This can recursively sink qualifiers through multiple levels of arrays.
1130 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1131 NewEltTy = getCanonicalType(NewEltTy);
1132
1133 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1134 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1135 CAT->getIndexTypeQualifier());
1136 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1137 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1138 IAT->getIndexTypeQualifier());
1139
1140 // FIXME: What is the ownership of size expressions in VLAs?
1141 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1142 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1143 VAT->getSizeModifier(),
1144 VAT->getIndexTypeQualifier());
1145}
1146
1147
1148const ArrayType *ASTContext::getAsArrayType(QualType T) {
1149 // Handle the non-qualified case efficiently.
1150 if (T.getCVRQualifiers() == 0) {
1151 // Handle the common positive case fast.
1152 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1153 return AT;
1154 }
1155
1156 // Handle the common negative case fast, ignoring CVR qualifiers.
1157 QualType CType = T->getCanonicalTypeInternal();
1158
1159 // Make sure to look through type qualifiers (like ASQuals) for the negative
1160 // test.
1161 if (!isa<ArrayType>(CType) &&
1162 !isa<ArrayType>(CType.getUnqualifiedType()))
1163 return 0;
1164
1165 // Apply any CVR qualifiers from the array type to the element type. This
1166 // implements C99 6.7.3p8: "If the specification of an array type includes
1167 // any type qualifiers, the element type is so qualified, not the array type."
1168
1169 // If we get here, we either have type qualifiers on the type, or we have
1170 // sugar such as a typedef in the way. If we have type qualifiers on the type
1171 // we must propagate them down into the elemeng type.
1172 unsigned CVRQuals = T.getCVRQualifiers();
1173 unsigned AddrSpace = 0;
1174 Type *Ty = T.getTypePtr();
1175
1176 // Rip through ASQualType's and typedefs to get to a concrete type.
1177 while (1) {
1178 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1179 AddrSpace = ASQT->getAddressSpace();
1180 Ty = ASQT->getBaseType();
1181 } else {
1182 T = Ty->getDesugaredType();
1183 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1184 break;
1185 CVRQuals |= T.getCVRQualifiers();
1186 Ty = T.getTypePtr();
1187 }
1188 }
1189
1190 // If we have a simple case, just return now.
1191 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1192 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1193 return ATy;
1194
1195 // Otherwise, we have an array and we have qualifiers on it. Push the
1196 // qualifiers into the array element type and return a new array type.
1197 // Get the canonical version of the element with the extra qualifiers on it.
1198 // This can recursively sink qualifiers through multiple levels of arrays.
1199 QualType NewEltTy = ATy->getElementType();
1200 if (AddrSpace)
1201 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1202 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1203
1204 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1205 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1206 CAT->getSizeModifier(),
1207 CAT->getIndexTypeQualifier()));
1208 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1209 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1210 IAT->getSizeModifier(),
1211 IAT->getIndexTypeQualifier()));
1212
1213 // FIXME: What is the ownership of size expressions in VLAs?
1214 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1215 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1216 VAT->getSizeModifier(),
1217 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001218}
1219
1220
Chris Lattner19eb97e2008-04-02 05:18:44 +00001221/// getArrayDecayedType - Return the properly qualified result of decaying the
1222/// specified array type to a pointer. This operation is non-trivial when
1223/// handling typedefs etc. The canonical type of "T" must be an array type,
1224/// this returns a pointer to a properly qualified element of the array.
1225///
1226/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1227QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001228 // Get the element type with 'getAsArrayType' so that we don't lose any
1229 // typedefs in the element type of the array. This also handles propagation
1230 // of type qualifiers from the array type into the element type if present
1231 // (C99 6.7.3p8).
1232 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1233 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001234
Chris Lattnera1923f62008-08-04 07:31:14 +00001235 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001236
1237 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001238 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001239}
1240
Chris Lattner4b009652007-07-25 00:24:17 +00001241/// getFloatingRank - Return a relative rank for floating point types.
1242/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001243static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001244 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001245 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001246
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001247 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001248 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001249 case BuiltinType::Float: return FloatRank;
1250 case BuiltinType::Double: return DoubleRank;
1251 case BuiltinType::LongDouble: return LongDoubleRank;
1252 }
1253}
1254
Steve Narofffa0c4532007-08-27 01:41:48 +00001255/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1256/// point or a complex type (based on typeDomain/typeSize).
1257/// 'typeDomain' is a real floating point or complex type.
1258/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001259QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1260 QualType Domain) const {
1261 FloatingRank EltRank = getFloatingRank(Size);
1262 if (Domain->isComplexType()) {
1263 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001264 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001265 case FloatRank: return FloatComplexTy;
1266 case DoubleRank: return DoubleComplexTy;
1267 case LongDoubleRank: return LongDoubleComplexTy;
1268 }
Chris Lattner4b009652007-07-25 00:24:17 +00001269 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001270
1271 assert(Domain->isRealFloatingType() && "Unknown domain!");
1272 switch (EltRank) {
1273 default: assert(0 && "getFloatingRank(): illegal value for rank");
1274 case FloatRank: return FloatTy;
1275 case DoubleRank: return DoubleTy;
1276 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001277 }
Chris Lattner4b009652007-07-25 00:24:17 +00001278}
1279
Chris Lattner51285d82008-04-06 23:55:33 +00001280/// getFloatingTypeOrder - Compare the rank of the two specified floating
1281/// point types, ignoring the domain of the type (i.e. 'double' ==
1282/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1283/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001284int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1285 FloatingRank LHSR = getFloatingRank(LHS);
1286 FloatingRank RHSR = getFloatingRank(RHS);
1287
1288 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001289 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001290 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001291 return 1;
1292 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001293}
1294
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001295/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1296/// routine will assert if passed a built-in type that isn't an integer or enum,
1297/// or if it is not canonicalized.
1298static unsigned getIntegerRank(Type *T) {
1299 assert(T->isCanonical() && "T should be canonicalized");
1300 if (isa<EnumType>(T))
1301 return 4;
1302
1303 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001304 default: assert(0 && "getIntegerRank(): not a built-in integer");
1305 case BuiltinType::Bool:
1306 return 1;
1307 case BuiltinType::Char_S:
1308 case BuiltinType::Char_U:
1309 case BuiltinType::SChar:
1310 case BuiltinType::UChar:
1311 return 2;
1312 case BuiltinType::Short:
1313 case BuiltinType::UShort:
1314 return 3;
1315 case BuiltinType::Int:
1316 case BuiltinType::UInt:
1317 return 4;
1318 case BuiltinType::Long:
1319 case BuiltinType::ULong:
1320 return 5;
1321 case BuiltinType::LongLong:
1322 case BuiltinType::ULongLong:
1323 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001324 }
1325}
1326
Chris Lattner51285d82008-04-06 23:55:33 +00001327/// getIntegerTypeOrder - Returns the highest ranked integer type:
1328/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1329/// LHS < RHS, return -1.
1330int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001331 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1332 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001333 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001334
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001335 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1336 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001337
Chris Lattner51285d82008-04-06 23:55:33 +00001338 unsigned LHSRank = getIntegerRank(LHSC);
1339 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001340
Chris Lattner51285d82008-04-06 23:55:33 +00001341 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1342 if (LHSRank == RHSRank) return 0;
1343 return LHSRank > RHSRank ? 1 : -1;
1344 }
Chris Lattner4b009652007-07-25 00:24:17 +00001345
Chris Lattner51285d82008-04-06 23:55:33 +00001346 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1347 if (LHSUnsigned) {
1348 // If the unsigned [LHS] type is larger, return it.
1349 if (LHSRank >= RHSRank)
1350 return 1;
1351
1352 // If the signed type can represent all values of the unsigned type, it
1353 // wins. Because we are dealing with 2's complement and types that are
1354 // powers of two larger than each other, this is always safe.
1355 return -1;
1356 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001357
Chris Lattner51285d82008-04-06 23:55:33 +00001358 // If the unsigned [RHS] type is larger, return it.
1359 if (RHSRank >= LHSRank)
1360 return -1;
1361
1362 // If the signed type can represent all values of the unsigned type, it
1363 // wins. Because we are dealing with 2's complement and types that are
1364 // powers of two larger than each other, this is always safe.
1365 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001366}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001367
1368// getCFConstantStringType - Return the type used for constant CFStrings.
1369QualType ASTContext::getCFConstantStringType() {
1370 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001371 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001372 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001373 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001374 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001375
1376 // const int *isa;
1377 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001378 // int flags;
1379 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001380 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001381 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001382 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001383 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001384 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001385 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001386
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001387 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001388 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001389 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001390
Ted Kremenek46a837c2008-09-05 17:16:31 +00001391 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001392 }
1393
1394 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001395}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001396
Anders Carlssonf58cac72008-08-30 19:34:46 +00001397QualType ASTContext::getObjCFastEnumerationStateType()
1398{
1399 if (!ObjCFastEnumerationStateTypeDecl) {
1400 QualType FieldTypes[] = {
1401 UnsignedLongTy,
1402 getPointerType(ObjCIdType),
1403 getPointerType(UnsignedLongTy),
1404 getConstantArrayType(UnsignedLongTy,
1405 llvm::APInt(32, 5), ArrayType::Normal, 0)
1406 };
1407
1408 FieldDecl *FieldDecls[4];
1409 for (size_t i = 0; i < 4; ++i)
1410 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1411 FieldTypes[i]);
1412
1413 ObjCFastEnumerationStateTypeDecl =
1414 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001415 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonf58cac72008-08-30 19:34:46 +00001416
Ted Kremenek46a837c2008-09-05 17:16:31 +00001417 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001418 }
1419
1420 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1421}
1422
Anders Carlssone3f02572007-10-29 06:33:42 +00001423// This returns true if a type has been typedefed to BOOL:
1424// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001425static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001426 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001427 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001428
1429 return false;
1430}
1431
Ted Kremenek42730c52008-01-07 19:49:32 +00001432/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001433/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001434int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001435 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001436
1437 // Make all integer and enum types at least as large as an int
1438 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001439 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001440 // Treat arrays as pointers, since that's how they're passed in.
1441 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001442 sz = getTypeSize(VoidPtrTy);
1443 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001444}
1445
Ted Kremenek42730c52008-01-07 19:49:32 +00001446/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001447/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001448void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001449 std::string& S)
1450{
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001451 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001452 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001453 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001454 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001455 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001456 // Compute size of all parameters.
1457 // Start with computing size of a pointer in number of bytes.
1458 // FIXME: There might(should) be a better way of doing this computation!
1459 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001460 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001461 // The first two arguments (self and _cmd) are pointers; account for
1462 // their size.
1463 int ParmOffset = 2 * PtrSize;
1464 int NumOfParams = Decl->getNumParams();
1465 for (int i = 0; i < NumOfParams; i++) {
1466 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001467 int sz = getObjCEncodingTypeSize (PType);
1468 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001469 ParmOffset += sz;
1470 }
1471 S += llvm::utostr(ParmOffset);
1472 S += "@0:";
1473 S += llvm::utostr(PtrSize);
1474
1475 // Argument types.
1476 ParmOffset = 2 * PtrSize;
1477 for (int i = 0; i < NumOfParams; i++) {
1478 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001479 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001480 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001481 getObjCEncodingForTypeQualifier(
1482 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001483 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001484 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001485 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001486 }
1487}
1488
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001489/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1490/// method declaration. If non-NULL, Container must be either an
1491/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1492/// NULL when getting encodings for protocol properties.
1493void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1494 const Decl *Container,
1495 std::string& S)
1496{
1497 // Collect information from the property implementation decl(s).
1498 bool Dynamic = false;
1499 ObjCPropertyImplDecl *SynthesizePID = 0;
1500
1501 // FIXME: Duplicated code due to poor abstraction.
1502 if (Container) {
1503 if (const ObjCCategoryImplDecl *CID =
1504 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1505 for (ObjCCategoryImplDecl::propimpl_iterator
1506 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1507 ObjCPropertyImplDecl *PID = *i;
1508 if (PID->getPropertyDecl() == PD) {
1509 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1510 Dynamic = true;
1511 } else {
1512 SynthesizePID = PID;
1513 }
1514 }
1515 }
1516 } else {
1517 const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container);
1518 for (ObjCCategoryImplDecl::propimpl_iterator
1519 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1520 ObjCPropertyImplDecl *PID = *i;
1521 if (PID->getPropertyDecl() == PD) {
1522 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1523 Dynamic = true;
1524 } else {
1525 SynthesizePID = PID;
1526 }
1527 }
1528 }
1529 }
1530 }
1531
1532 // FIXME: This is not very efficient.
1533 S = "T";
1534
1535 // Encode result type.
1536 // FIXME: GCC uses a generating_property_type_encoding mode during
1537 // this part. Investigate.
1538 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1539
1540 if (PD->isReadOnly()) {
1541 S += ",R";
1542 } else {
1543 switch (PD->getSetterKind()) {
1544 case ObjCPropertyDecl::Assign: break;
1545 case ObjCPropertyDecl::Copy: S += ",C"; break;
1546 case ObjCPropertyDecl::Retain: S += ",&"; break;
1547 }
1548 }
1549
1550 // It really isn't clear at all what this means, since properties
1551 // are "dynamic by default".
1552 if (Dynamic)
1553 S += ",D";
1554
1555 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1556 S += ",G";
1557 S += PD->getGetterName().getName();
1558 }
1559
1560 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1561 S += ",S";
1562 S += PD->getSetterName().getName();
1563 }
1564
1565 if (SynthesizePID) {
1566 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1567 S += ",V";
1568 S += OID->getName();
1569 }
1570
1571 // FIXME: OBJCGC: weak & strong
1572}
1573
Fariborz Jahanian248db262008-01-22 22:44:46 +00001574void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnera1923f62008-08-04 07:31:14 +00001575 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001576 // FIXME: This currently doesn't encode:
1577 // @ An object (whether statically typed or typed id)
1578 // # A class object (Class)
1579 // : A method selector (SEL)
1580 // {name=type...} A structure
1581 // (name=type...) A union
1582 // bnum A bit field of num bits
1583
1584 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001585 char encoding;
1586 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001587 default: assert(0 && "Unhandled builtin type kind");
1588 case BuiltinType::Void: encoding = 'v'; break;
1589 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001590 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001591 case BuiltinType::UChar: encoding = 'C'; break;
1592 case BuiltinType::UShort: encoding = 'S'; break;
1593 case BuiltinType::UInt: encoding = 'I'; break;
1594 case BuiltinType::ULong: encoding = 'L'; break;
1595 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001596 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001597 case BuiltinType::SChar: encoding = 'c'; break;
1598 case BuiltinType::Short: encoding = 's'; break;
1599 case BuiltinType::Int: encoding = 'i'; break;
1600 case BuiltinType::Long: encoding = 'l'; break;
1601 case BuiltinType::LongLong: encoding = 'q'; break;
1602 case BuiltinType::Float: encoding = 'f'; break;
1603 case BuiltinType::Double: encoding = 'd'; break;
1604 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001605 }
1606
1607 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001608 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001609 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001610 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001611 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001612
1613 }
1614 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001615 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001617 S += '@';
1618 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001619 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001620 S += '#';
1621 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001622 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001623 S += ':';
1624 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001625 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001626
1627 if (PointeeTy->isCharType()) {
1628 // char pointer types should be encoded as '*' unless it is a
1629 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001630 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001631 S += '*';
1632 return;
1633 }
1634 }
1635
1636 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001637 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnera1923f62008-08-04 07:31:14 +00001638 } else if (const ArrayType *AT =
1639 // Ignore type qualifiers etc.
1640 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001641 S += '[';
1642
1643 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1644 S += llvm::utostr(CAT->getSize().getZExtValue());
1645 else
1646 assert(0 && "Unhandled array type!");
1647
Fariborz Jahanian248db262008-01-22 22:44:46 +00001648 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001649 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001650 } else if (T->getAsFunctionType()) {
1651 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001652 } else if (const RecordType *RTy = T->getAsRecordType()) {
1653 RecordDecl *RDecl= RTy->getDecl();
Steve Naroff03385292008-08-14 15:00:38 +00001654 // This mimics the behavior in gcc's encode_aggregate_within().
1655 // The idea is to only inline structure definitions for top level pointers
1656 // to structures and embedded structures.
1657 bool inlining = (S.size() == 1 && S[0] == '^' ||
1658 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001659 S += '{';
1660 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001661 bool found = false;
1662 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1663 if (ERType[i] == RTy) {
1664 found = true;
1665 break;
1666 }
Steve Naroff03385292008-08-14 15:00:38 +00001667 if (!found && inlining) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001668 ERType.push_back(RTy);
1669 S += '=';
1670 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1671 FieldDecl *field = RDecl->getMember(i);
1672 getObjCEncodingForType(field->getType(), S, ERType);
1673 }
1674 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1675 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001676 }
1677 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001678 } else if (T->isEnumeralType()) {
1679 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001680 } else if (T->isBlockPointerType()) {
1681 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001682 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001683 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001684}
1685
Ted Kremenek42730c52008-01-07 19:49:32 +00001686void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001687 std::string& S) const {
1688 if (QT & Decl::OBJC_TQ_In)
1689 S += 'n';
1690 if (QT & Decl::OBJC_TQ_Inout)
1691 S += 'N';
1692 if (QT & Decl::OBJC_TQ_Out)
1693 S += 'o';
1694 if (QT & Decl::OBJC_TQ_Bycopy)
1695 S += 'O';
1696 if (QT & Decl::OBJC_TQ_Byref)
1697 S += 'R';
1698 if (QT & Decl::OBJC_TQ_Oneway)
1699 S += 'V';
1700}
1701
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001702void ASTContext::setBuiltinVaListType(QualType T)
1703{
1704 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1705
1706 BuiltinVaListType = T;
1707}
1708
Ted Kremenek42730c52008-01-07 19:49:32 +00001709void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001710{
Ted Kremenek42730c52008-01-07 19:49:32 +00001711 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001712
1713 // typedef struct objc_object *id;
1714 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1715 assert(ptr && "'id' incorrectly typed");
1716 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1717 assert(rec && "'id' incorrectly typed");
1718 IdStructType = rec;
1719}
1720
Ted Kremenek42730c52008-01-07 19:49:32 +00001721void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001722{
Ted Kremenek42730c52008-01-07 19:49:32 +00001723 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001724
1725 // typedef struct objc_selector *SEL;
1726 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1727 assert(ptr && "'SEL' incorrectly typed");
1728 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1729 assert(rec && "'SEL' incorrectly typed");
1730 SelStructType = rec;
1731}
1732
Ted Kremenek42730c52008-01-07 19:49:32 +00001733void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001734{
Ted Kremenek42730c52008-01-07 19:49:32 +00001735 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001736}
1737
Ted Kremenek42730c52008-01-07 19:49:32 +00001738void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001739{
Ted Kremenek42730c52008-01-07 19:49:32 +00001740 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001741
1742 // typedef struct objc_class *Class;
1743 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1744 assert(ptr && "'Class' incorrectly typed");
1745 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1746 assert(rec && "'Class' incorrectly typed");
1747 ClassStructType = rec;
1748}
1749
Ted Kremenek42730c52008-01-07 19:49:32 +00001750void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1751 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001752 "'NSConstantString' type already set!");
1753
Ted Kremenek42730c52008-01-07 19:49:32 +00001754 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001755}
1756
Ted Kremenek118930e2008-07-24 23:58:27 +00001757
1758//===----------------------------------------------------------------------===//
1759// Type Predicates.
1760//===----------------------------------------------------------------------===//
1761
1762/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1763/// to an object type. This includes "id" and "Class" (two 'special' pointers
1764/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1765/// ID type).
1766bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1767 if (Ty->isObjCQualifiedIdType())
1768 return true;
1769
1770 if (!Ty->isPointerType())
1771 return false;
1772
1773 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1774 // pointer types. This looks for the typedef specifically, not for the
1775 // underlying type.
1776 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1777 return true;
1778
1779 // If this a pointer to an interface (e.g. NSString*), it is ok.
1780 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1781}
1782
Chris Lattner6ff358b2008-04-07 06:51:04 +00001783//===----------------------------------------------------------------------===//
1784// Type Compatibility Testing
1785//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001786
Steve Naroff3454b6c2008-09-04 15:10:53 +00001787/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001788/// block types. Types must be strictly compatible here. For example,
1789/// C unfortunately doesn't produce an error for the following:
1790///
1791/// int (*emptyArgFunc)();
1792/// int (*intArgList)(int) = emptyArgFunc;
1793///
1794/// For blocks, we will produce an error for the following (similar to C++):
1795///
1796/// int (^emptyArgBlock)();
1797/// int (^intArgBlock)(int) = emptyArgBlock;
1798///
1799/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1800///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001801bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Narofff5e7eff2008-09-09 13:47:19 +00001802 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff3454b6c2008-09-04 15:10:53 +00001803}
1804
Chris Lattner6ff358b2008-04-07 06:51:04 +00001805/// areCompatVectorTypes - Return true if the two specified vector types are
1806/// compatible.
1807static bool areCompatVectorTypes(const VectorType *LHS,
1808 const VectorType *RHS) {
1809 assert(LHS->isCanonical() && RHS->isCanonical());
1810 return LHS->getElementType() == RHS->getElementType() &&
1811 LHS->getNumElements() == RHS->getNumElements();
1812}
1813
Eli Friedman0d9549b2008-08-22 00:56:42 +00001814/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001815/// compatible for assignment from RHS to LHS. This handles validation of any
1816/// protocol qualifiers on the LHS or RHS.
1817///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001818bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1819 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001820 // Verify that the base decls are compatible: the RHS must be a subclass of
1821 // the LHS.
1822 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1823 return false;
1824
1825 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1826 // protocol qualified at all, then we are good.
1827 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1828 return true;
1829
1830 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1831 // isn't a superset.
1832 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1833 return true; // FIXME: should return false!
1834
1835 // Finally, we must have two protocol-qualified interfaces.
1836 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1837 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1838 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1839 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1840 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1841 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1842
1843 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1844 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1845 // LHS in a single parallel scan until we run out of elements in LHS.
1846 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1847 ObjCProtocolDecl *LHSProto = *LHSPI;
1848
1849 while (RHSPI != RHSPE) {
1850 ObjCProtocolDecl *RHSProto = *RHSPI++;
1851 // If the RHS has a protocol that the LHS doesn't, ignore it.
1852 if (RHSProto != LHSProto)
1853 continue;
1854
1855 // Otherwise, the RHS does have this element.
1856 ++LHSPI;
1857 if (LHSPI == LHSPE)
1858 return true; // All protocols in LHS exist in RHS.
1859
1860 LHSProto = *LHSPI;
1861 }
1862
1863 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1864 return false;
1865}
1866
Steve Naroff85f0dc52007-10-15 20:41:53 +00001867/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1868/// both shall have the identically qualified version of a compatible type.
1869/// C99 6.2.7p1: Two types have compatible types if their types are the
1870/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001871bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1872 return !mergeTypes(LHS, RHS).isNull();
1873}
1874
1875QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1876 const FunctionType *lbase = lhs->getAsFunctionType();
1877 const FunctionType *rbase = rhs->getAsFunctionType();
1878 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1879 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1880 bool allLTypes = true;
1881 bool allRTypes = true;
1882
1883 // Check return type
1884 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1885 if (retType.isNull()) return QualType();
1886 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false;
1887 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false;
1888
1889 if (lproto && rproto) { // two C99 style function prototypes
1890 unsigned lproto_nargs = lproto->getNumArgs();
1891 unsigned rproto_nargs = rproto->getNumArgs();
1892
1893 // Compatible functions must have the same number of arguments
1894 if (lproto_nargs != rproto_nargs)
1895 return QualType();
1896
1897 // Variadic and non-variadic functions aren't compatible
1898 if (lproto->isVariadic() != rproto->isVariadic())
1899 return QualType();
1900
1901 // Check argument compatibility
1902 llvm::SmallVector<QualType, 10> types;
1903 for (unsigned i = 0; i < lproto_nargs; i++) {
1904 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1905 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1906 QualType argtype = mergeTypes(largtype, rargtype);
1907 if (argtype.isNull()) return QualType();
1908 types.push_back(argtype);
1909 if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false;
1910 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false;
1911 }
1912 if (allLTypes) return lhs;
1913 if (allRTypes) return rhs;
1914 return getFunctionType(retType, types.begin(), types.size(),
1915 lproto->isVariadic());
1916 }
1917
1918 if (lproto) allRTypes = false;
1919 if (rproto) allLTypes = false;
1920
1921 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1922 if (proto) {
1923 if (proto->isVariadic()) return QualType();
1924 // Check that the types are compatible with the types that
1925 // would result from default argument promotions (C99 6.7.5.3p15).
1926 // The only types actually affected are promotable integer
1927 // types and floats, which would be passed as a different
1928 // type depending on whether the prototype is visible.
1929 unsigned proto_nargs = proto->getNumArgs();
1930 for (unsigned i = 0; i < proto_nargs; ++i) {
1931 QualType argTy = proto->getArgType(i);
1932 if (argTy->isPromotableIntegerType() ||
1933 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1934 return QualType();
1935 }
1936
1937 if (allLTypes) return lhs;
1938 if (allRTypes) return rhs;
1939 return getFunctionType(retType, proto->arg_type_begin(),
1940 proto->getNumArgs(), lproto->isVariadic());
1941 }
1942
1943 if (allLTypes) return lhs;
1944 if (allRTypes) return rhs;
1945 return getFunctionTypeNoProto(retType);
1946}
1947
1948QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00001949 // C++ [expr]: If an expression initially has the type "reference to T", the
1950 // type is adjusted to "T" prior to any further analysis, the expression
1951 // designates the object or function denoted by the reference, and the
1952 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001953 // FIXME: C++ shouldn't be going through here! The rules are different
1954 // enough that they should be handled separately.
1955 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001956 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00001957 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00001958 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00001959
Eli Friedman0d9549b2008-08-22 00:56:42 +00001960 QualType LHSCan = getCanonicalType(LHS),
1961 RHSCan = getCanonicalType(RHS);
1962
1963 // If two types are identical, they are compatible.
1964 if (LHSCan == RHSCan)
1965 return LHS;
1966
1967 // If the qualifiers are different, the types aren't compatible
1968 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
1969 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
1970 return QualType();
1971
1972 Type::TypeClass LHSClass = LHSCan->getTypeClass();
1973 Type::TypeClass RHSClass = RHSCan->getTypeClass();
1974
Chris Lattnerc38d4522008-01-14 05:45:46 +00001975 // We want to consider the two function types to be the same for these
1976 // comparisons, just force one to the other.
1977 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1978 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001979
1980 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001981 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1982 LHSClass = Type::ConstantArray;
1983 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1984 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001985
Nate Begemanaf6ed502008-04-18 23:10:10 +00001986 // Canonicalize ExtVector -> Vector.
1987 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1988 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001989
Chris Lattner7cdcb252008-04-07 06:38:24 +00001990 // Consider qualified interfaces and interfaces the same.
1991 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1992 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00001993
Chris Lattnerb5709e22008-04-07 05:43:21 +00001994 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001995 if (LHSClass != RHSClass) {
Steve Naroff44549772008-06-04 15:07:33 +00001996 // ID is compatible with all qualified id types.
Eli Friedman0d9549b2008-08-22 00:56:42 +00001997 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00001998 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00001999 if (isObjCIdType(PT->getPointeeType()))
2000 return LHS;
Steve Naroff44549772008-06-04 15:07:33 +00002001 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002002 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff44549772008-06-04 15:07:33 +00002003 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002004 if (isObjCIdType(PT->getPointeeType()))
2005 return RHS;
2006 }
2007
Chris Lattnerc38d4522008-01-14 05:45:46 +00002008 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2009 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002010 if (const EnumType* ETy = LHS->getAsEnumType()) {
2011 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2012 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002013 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002014 if (const EnumType* ETy = RHS->getAsEnumType()) {
2015 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2016 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002017 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002018
Eli Friedman0d9549b2008-08-22 00:56:42 +00002019 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002020 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002021
Steve Naroffc88babe2008-01-09 22:43:08 +00002022 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002023 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002024 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002025 {
2026 // Merge two pointer types, while trying to preserve typedef info
2027 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2028 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2029 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2030 if (ResultType.isNull()) return QualType();
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002031 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS;
2032 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002033 return getPointerType(ResultType);
2034 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002035 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002036 {
2037 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2038 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2039 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2040 return QualType();
2041
2042 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2043 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2044 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2045 if (ResultType.isNull()) return QualType();
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002046 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2047 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
2048 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2049 ArrayType::ArraySizeModifier(), 0);
2050 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2051 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002052 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2053 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002054 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2055 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002056 if (LVAT) {
2057 // FIXME: This isn't correct! But tricky to implement because
2058 // the array's size has to be the size of LHS, but the type
2059 // has to be different.
2060 return LHS;
2061 }
2062 if (RVAT) {
2063 // FIXME: This isn't correct! But tricky to implement because
2064 // the array's size has to be the size of RHS, but the type
2065 // has to be different.
2066 return RHS;
2067 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002068 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2069 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002070 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0);
2071 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002072 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002073 return mergeFunctionTypes(LHS, RHS);
2074 case Type::Tagged:
2075 {
2076 // FIXME: Why are these compatible?
2077 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2078 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2079 return QualType();
2080 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002081 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002082 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002083 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002084 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002085 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2086 return LHS;
Chris Lattnerc38d4522008-01-14 05:45:46 +00002087 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002088 {
2089 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2090 // for checking assignment/comparison safety
2091 return QualType();
2092 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002093 default:
2094 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002095 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002096 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002097}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002098
Chris Lattner1d78a862008-04-07 07:01:58 +00002099//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002100// Integer Predicates
2101//===----------------------------------------------------------------------===//
2102unsigned ASTContext::getIntWidth(QualType T) {
2103 if (T == BoolTy)
2104 return 1;
2105 // At the moment, only bool has padding bits
2106 return (unsigned)getTypeSize(T);
2107}
2108
2109QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2110 assert(T->isSignedIntegerType() && "Unexpected type");
2111 if (const EnumType* ETy = T->getAsEnumType())
2112 T = ETy->getDecl()->getIntegerType();
2113 const BuiltinType* BTy = T->getAsBuiltinType();
2114 assert (BTy && "Unexpected signed integer type");
2115 switch (BTy->getKind()) {
2116 case BuiltinType::Char_S:
2117 case BuiltinType::SChar:
2118 return UnsignedCharTy;
2119 case BuiltinType::Short:
2120 return UnsignedShortTy;
2121 case BuiltinType::Int:
2122 return UnsignedIntTy;
2123 case BuiltinType::Long:
2124 return UnsignedLongTy;
2125 case BuiltinType::LongLong:
2126 return UnsignedLongLongTy;
2127 default:
2128 assert(0 && "Unexpected signed integer type");
2129 return QualType();
2130 }
2131}
2132
2133
2134//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002135// Serialization Support
2136//===----------------------------------------------------------------------===//
2137
Ted Kremenek738e6c02007-10-31 17:10:13 +00002138/// Emit - Serialize an ASTContext object to Bitcode.
2139void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002140 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002141 S.EmitRef(SourceMgr);
2142 S.EmitRef(Target);
2143 S.EmitRef(Idents);
2144 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002145
Ted Kremenek68228a92007-10-31 22:44:07 +00002146 // Emit the size of the type vector so that we can reserve that size
2147 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002148 S.EmitInt(Types.size());
2149
Ted Kremenek034a78c2007-11-13 22:02:55 +00002150 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2151 I!=E;++I)
2152 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002153
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002154 S.EmitOwnedPtr(TUDecl);
2155
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002156 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002157}
2158
Ted Kremenekacba3612007-11-13 00:25:37 +00002159ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002160
2161 // Read the language options.
2162 LangOptions LOpts;
2163 LOpts.Read(D);
2164
Ted Kremenek68228a92007-10-31 22:44:07 +00002165 SourceManager &SM = D.ReadRef<SourceManager>();
2166 TargetInfo &t = D.ReadRef<TargetInfo>();
2167 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2168 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002169
Ted Kremenek68228a92007-10-31 22:44:07 +00002170 unsigned size_reserve = D.ReadInt();
2171
Ted Kremenek842126e2008-06-04 15:55:15 +00002172 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002173
Ted Kremenek034a78c2007-11-13 22:02:55 +00002174 for (unsigned i = 0; i < size_reserve; ++i)
2175 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002176
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002177 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2178
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002179 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002180
2181 return A;
2182}