blob: e7c08e2da39ba42933c98c19a6d6d5c010eaf53f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000023#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000024
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27enum FloatingRank {
28 FloatRank, DoubleRank, LongDoubleRank
29};
30
Chris Lattner61710852008-10-05 17:34:18 +000031ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
32 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000033 IdentifierTable &idents, SelectorTable &sels,
34 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
36 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000037 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000038{
39 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
41 BuiltinInfo.InitializeBuiltins(idents, Target);
42 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 Types.pop_back();
50 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000051
Nuno Lopesb74668e2008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Eli Friedmanb26153c2008-05-27 03:08:09 +000079 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
82void ASTContext::PrintStats() const {
83 fprintf(stderr, "*** AST Context Stats:\n");
84 fprintf(stderr, " %d types total.\n", (int)Types.size());
85 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000086 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redlf30208a2009-01-24 21:16:55 +000088 unsigned NumMemberPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000089
90 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
92 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000093 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000094
95 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
96 Type *T = Types[i];
97 if (isa<BuiltinType>(T))
98 ++NumBuiltin;
99 else if (isa<PointerType>(T))
100 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 else if (isa<BlockPointerType>(T))
102 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 else if (isa<ReferenceType>(T))
104 ++NumReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000105 else if (isa<MemberPointerType>(T))
106 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000107 else if (isa<ComplexType>(T))
108 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 else if (isa<ArrayType>(T))
110 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000111 else if (isa<VectorType>(T))
112 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 else if (isa<FunctionTypeNoProto>(T))
114 ++NumFunctionNP;
115 else if (isa<FunctionTypeProto>(T))
116 ++NumFunctionP;
117 else if (isa<TypedefType>(T))
118 ++NumTypeName;
119 else if (TagType *TT = dyn_cast<TagType>(T)) {
120 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000121 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000123 case TagDecl::TK_struct: ++NumTagStruct; break;
124 case TagDecl::TK_union: ++NumTagUnion; break;
125 case TagDecl::TK_class: ++NumTagClass; break;
126 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 } else if (isa<ObjCInterfaceType>(T))
129 ++NumObjCInterfaces;
130 else if (isa<ObjCQualifiedInterfaceType>(T))
131 ++NumObjCQualifiedInterfaces;
132 else if (isa<ObjCQualifiedIdType>(T))
133 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000134 else if (isa<TypeOfType>(T))
135 ++NumTypeOfTypes;
136 else if (isa<TypeOfExpr>(T))
137 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000138 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000139 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 assert(0 && "Unknown type!");
141 }
142 }
143
144 fprintf(stderr, " %d builtin types\n", NumBuiltin);
145 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000146 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000148 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000149 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000151 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
153 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
154 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
155 fprintf(stderr, " %d tagged types\n", NumTagged);
156 fprintf(stderr, " %d struct types\n", NumTagStruct);
157 fprintf(stderr, " %d union types\n", NumTagUnion);
158 fprintf(stderr, " %d class types\n", NumTagClass);
159 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000160 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000161 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000162 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000163 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000164 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000165 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
166 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
169 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000171 NumMemberPointer*sizeof(MemberPointerType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 NumFunctionP*sizeof(FunctionTypeProto)+
173 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000174 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
175 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
178
179void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff506010b2009-01-19 22:45:10 +0000180 void *Mem = Allocator.Allocate(sizeof(BuiltinType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000181 Types.push_back((R = QualType(new (Mem) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000233
Steve Naroff7e219e42007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattner464175b2007-07-18 17:52:12 +0000246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000249
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattneraf707ab2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
266unsigned ASTContext::getDeclAlign(const Decl *D) {
267 // FIXME: If attribute(align) is specified on the decl, round up to it.
268
269 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
270 QualType T = VD->getType();
271 // Incomplete or function types default to 1.
272 if (T->isIncompleteType() || T->isFunctionType())
273 return 1;
274
275 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
276 T = cast<ArrayType>(T)->getElementType();
277
278 return getTypeAlign(T);
279 }
280
281 return 1;
282}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000283
Chris Lattnera7674d82007-07-13 22:13:22 +0000284/// getTypeSize - Return the size of the specified type, in bits. This method
285/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000286std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000287ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000288 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000289 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000290 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000291 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000292 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000293 case Type::FunctionNoProto:
294 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000295 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000296 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000297 case Type::VariableArray:
298 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000299 case Type::DependentSizedArray:
300 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000301 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000302 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000303
Chris Lattner98be4942008-03-05 18:54:05 +0000304 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000305 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000306 Align = EltInfo.second;
307 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000308 }
Nate Begeman213541a2008-04-18 23:10:10 +0000309 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000310 case Type::Vector: {
311 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000312 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000313 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000314 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000315 // If the alignment is not a power of 2, round up to the next power of 2.
316 // This happens for non-power-of-2 length vectors.
317 // FIXME: this should probably be a target property.
318 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000319 break;
320 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000321
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000322 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000323 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000324 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000325 case BuiltinType::Void:
326 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000327 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000328 Width = Target.getBoolWidth();
329 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000330 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000331 case BuiltinType::Char_S:
332 case BuiltinType::Char_U:
333 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000334 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000335 Width = Target.getCharWidth();
336 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000337 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000338 case BuiltinType::WChar:
339 Width = Target.getWCharWidth();
340 Align = Target.getWCharAlign();
341 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000342 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000344 Width = Target.getShortWidth();
345 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000346 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000347 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000348 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000349 Width = Target.getIntWidth();
350 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000351 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000352 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000353 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000354 Width = Target.getLongWidth();
355 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000356 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000357 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000358 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = Target.getLongLongWidth();
360 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000361 break;
362 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = Target.getFloatWidth();
364 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 break;
366 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000367 Width = Target.getDoubleWidth();
368 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000369 break;
370 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000371 Width = Target.getLongDoubleWidth();
372 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000374 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000375 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000376 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000377 // FIXME: Pointers into different addr spaces could have different sizes and
378 // alignment requirements: getPointerInfo should take an AddrSpace.
379 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000381 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000382 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000384 case Type::BlockPointer: {
385 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
386 Width = Target.getPointerWidth(AS);
387 Align = Target.getPointerAlign(AS);
388 break;
389 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000390 case Type::Pointer: {
391 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000392 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000393 Align = Target.getPointerAlign(AS);
394 break;
395 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000396 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000397 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000403 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000404 // the GCC ABI, where pointers to data are one pointer large, pointers to
405 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000406 // other compilers too, we need to delegate this completely to TargetInfo
407 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000408 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
409 unsigned AS = Pointee.getAddressSpace();
410 Width = Target.getPointerWidth(AS);
411 if (Pointee->isFunctionType())
412 Width *= 2;
413 Align = Target.getPointerAlign(AS);
414 // GCC aligns at single pointer width.
415 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Chris Lattner71763312008-04-06 22:05:18 +0000432 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000433 const TagType *TT = cast<TagType>(T);
434
435 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000436 Width = 1;
437 Align = 1;
438 break;
439 }
440
Daniel Dunbar1d751182008-11-08 05:48:37 +0000441 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000442 return getTypeInfo(ET->getDecl()->getIntegerType());
443
Daniel Dunbar1d751182008-11-08 05:48:37 +0000444 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000445 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000448 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000449 }
Chris Lattner71763312008-04-06 22:05:18 +0000450 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000451
Chris Lattner464175b2007-07-18 17:52:12 +0000452 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000453 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000454}
455
Chris Lattner34ebde42009-01-27 18:08:34 +0000456/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
457/// type for the current target in bits. This can be different than the ABI
458/// alignment in cases where it is beneficial for performance to overalign
459/// a data type.
460unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
461 unsigned ABIAlign = getTypeAlign(T);
462
463 // Doubles should be naturally aligned if possible.
464 if (const BuiltinType *BT = dyn_cast<BuiltinType>(getCanonicalType(T)))
465 if (BT->getKind() == BuiltinType::Double)
466 return std::max(ABIAlign, 8U);
467
468 return ABIAlign;
469}
470
471
Devang Patel8b277042008-06-04 21:22:16 +0000472/// LayoutField - Field layout.
473void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000474 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000475 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000476 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000477 uint64_t FieldOffset = IsUnion ? 0 : Size;
478 uint64_t FieldSize;
479 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000480
481 // FIXME: Should this override struct packing? Probably we want to
482 // take the minimum?
483 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
484 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000485
486 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
487 // TODO: Need to check this algorithm on other targets!
488 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000489 FieldSize =
490 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000491
492 std::pair<uint64_t, unsigned> FieldInfo =
493 Context.getTypeInfo(FD->getType());
494 uint64_t TypeSize = FieldInfo.first;
495
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000496 // Determine the alignment of this bitfield. The packing
497 // attributes define a maximum and the alignment attribute defines
498 // a minimum.
499 // FIXME: What is the right behavior when the specified alignment
500 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000501 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000502 if (FieldPacking)
503 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000504 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
505 FieldAlign = std::max(FieldAlign, AA->getAlignment());
506
507 // Check if we need to add padding to give the field the correct
508 // alignment.
509 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
510 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
511
512 // Padding members don't affect overall alignment
513 if (!FD->getIdentifier())
514 FieldAlign = 1;
515 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000516 if (FD->getType()->isIncompleteArrayType()) {
517 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000518 // query getTypeInfo about these, so we figure it out here.
519 // Flexible array members don't have any size, but they
520 // have to be aligned appropriately for their element type.
521 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000522 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000523 FieldAlign = Context.getTypeAlign(ATy->getElementType());
524 } else {
525 std::pair<uint64_t, unsigned> FieldInfo =
526 Context.getTypeInfo(FD->getType());
527 FieldSize = FieldInfo.first;
528 FieldAlign = FieldInfo.second;
529 }
530
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000531 // Determine the alignment of this bitfield. The packing
532 // attributes define a maximum and the alignment attribute defines
533 // a minimum. Additionally, the packing alignment must be at least
534 // a byte for non-bitfields.
535 //
536 // FIXME: What is the right behavior when the specified alignment
537 // is smaller than the specified packing?
538 if (FieldPacking)
539 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000540 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
541 FieldAlign = std::max(FieldAlign, AA->getAlignment());
542
543 // Round up the current record size to the field's alignment boundary.
544 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
545 }
546
547 // Place this field at the current location.
548 FieldOffsets[FieldNo] = FieldOffset;
549
550 // Reserve space for this field.
551 if (IsUnion) {
552 Size = std::max(Size, FieldSize);
553 } else {
554 Size = FieldOffset + FieldSize;
555 }
556
557 // Remember max struct/class alignment.
558 Alignment = std::max(Alignment, FieldAlign);
559}
560
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000561static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
562 std::vector<FieldDecl*> &Fields) {
563 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
564 if (SuperClass)
565 CollectObjCIvars(SuperClass, Fields);
566 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
567 E = OI->ivar_end(); I != E; ++I) {
568 ObjCIvarDecl *IVDecl = (*I);
569 if (!IVDecl->isInvalidDecl())
570 Fields.push_back(cast<FieldDecl>(IVDecl));
571 }
572}
573
574/// addRecordToClass - produces record info. for the class for its
575/// ivars and all those inherited.
576///
577const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
578{
579 const RecordDecl *&RD = ASTRecordForInterface[D];
580 if (RD)
581 return RD;
582 std::vector<FieldDecl*> RecFields;
583 CollectObjCIvars(D, RecFields);
584 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
585 D->getLocation(),
586 D->getIdentifier());
587 /// FIXME! Can do collection of ivars and adding to the record while
588 /// doing it.
589 for (unsigned int i = 0; i != RecFields.size(); i++) {
590 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
591 RecFields[i]->getLocation(),
592 RecFields[i]->getIdentifier(),
593 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000594 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000595 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000596 }
597 NewRD->completeDefinition(*this);
598 RD = NewRD;
599 return RD;
600}
Devang Patel44a3dde2008-06-04 21:54:36 +0000601
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000602/// setFieldDecl - maps a field for the given Ivar reference node.
603//
604void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
605 const ObjCIvarDecl *Ivar,
606 const ObjCIvarRefExpr *MRef) {
607 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
608 lookupFieldDeclForIvar(*this, Ivar);
609 ASTFieldForIvarRef[MRef] = FD;
610}
611
Chris Lattner61710852008-10-05 17:34:18 +0000612/// getASTObjcInterfaceLayout - Get or compute information about the layout of
613/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000614/// position information.
615const ASTRecordLayout &
616ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
617 // Look up this layout, if already laid out, return what we have.
618 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
619 if (Entry) return *Entry;
620
621 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
622 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000623 ASTRecordLayout *NewEntry = NULL;
624 unsigned FieldCount = D->ivar_size();
625 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
626 FieldCount++;
627 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
628 unsigned Alignment = SL.getAlignment();
629 uint64_t Size = SL.getSize();
630 NewEntry = new ASTRecordLayout(Size, Alignment);
631 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000632 // Super class is at the beginning of the layout.
633 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000634 } else {
635 NewEntry = new ASTRecordLayout();
636 NewEntry->InitializeLayout(FieldCount);
637 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000638 Entry = NewEntry;
639
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000640 unsigned StructPacking = 0;
641 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
642 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000643
644 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
645 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
646 AA->getAlignment()));
647
648 // Layout each ivar sequentially.
649 unsigned i = 0;
650 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
651 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
652 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000653 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000654 }
655
656 // Finally, round the size of the total struct up to the alignment of the
657 // struct itself.
658 NewEntry->FinalizeLayout();
659 return *NewEntry;
660}
661
Devang Patel88a981b2007-11-01 19:11:01 +0000662/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000663/// specified record (struct/union/class), which indicates its size and field
664/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000665const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000666 D = D->getDefinition(*this);
667 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000668
Chris Lattner464175b2007-07-18 17:52:12 +0000669 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000670 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000671 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000672
Devang Patel88a981b2007-11-01 19:11:01 +0000673 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
674 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
675 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000676 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000677
Douglas Gregore267ff32008-12-11 20:41:00 +0000678 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000679 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000680 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000681
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000682 unsigned StructPacking = 0;
683 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
684 StructPacking = PA->getAlignment();
685
Eli Friedman4bd998b2008-05-30 09:31:38 +0000686 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000687 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
688 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000689
Eli Friedman4bd998b2008-05-30 09:31:38 +0000690 // Layout each field, for now, just sequentially, respecting alignment. In
691 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000692 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000693 for (RecordDecl::field_iterator Field = D->field_begin(),
694 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000695 Field != FieldEnd; (void)++Field, ++FieldIdx)
696 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000697
698 // Finally, round the size of the total struct up to the alignment of the
699 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000700 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000701 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000702}
703
Chris Lattnera7674d82007-07-13 22:13:22 +0000704//===----------------------------------------------------------------------===//
705// Type creation/memoization methods
706//===----------------------------------------------------------------------===//
707
Christopher Lambebb97e92008-02-04 02:31:56 +0000708QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000709 QualType CanT = getCanonicalType(T);
710 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000711 return T;
712
713 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
714 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000715 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000716 "Type is already address space qualified");
717
718 // Check if we've already instantiated an address space qual'd type of this
719 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000720 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000721 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000722 void *InsertPos = 0;
723 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(ASQy, 0);
725
726 // If the base type isn't canonical, this won't be a canonical type either,
727 // so fill in the canonical type field.
728 QualType Canonical;
729 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000730 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000731
732 // Get the new insert position for the node we care about.
733 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000734 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000735 }
Steve Naroff506010b2009-01-19 22:45:10 +0000736 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000737 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000738 ASQualTypes.InsertNode(New, InsertPos);
739 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000740 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000741}
742
Chris Lattnera7674d82007-07-13 22:13:22 +0000743
Reid Spencer5f016e22007-07-11 17:01:13 +0000744/// getComplexType - Return the uniqued reference to the type for a complex
745/// number with the specified element type.
746QualType ASTContext::getComplexType(QualType T) {
747 // Unique pointers, to guarantee there is only one pointer of a particular
748 // structure.
749 llvm::FoldingSetNodeID ID;
750 ComplexType::Profile(ID, T);
751
752 void *InsertPos = 0;
753 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
754 return QualType(CT, 0);
755
756 // If the pointee type isn't canonical, this won't be a canonical type either,
757 // so fill in the canonical type field.
758 QualType Canonical;
759 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000760 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000761
762 // Get the new insert position for the node we care about.
763 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000764 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 }
Steve Naroff506010b2009-01-19 22:45:10 +0000766 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000767 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 Types.push_back(New);
769 ComplexTypes.InsertNode(New, InsertPos);
770 return QualType(New, 0);
771}
772
773
774/// getPointerType - Return the uniqued reference to the type for a pointer to
775/// the specified type.
776QualType ASTContext::getPointerType(QualType T) {
777 // Unique pointers, to guarantee there is only one pointer of a particular
778 // structure.
779 llvm::FoldingSetNodeID ID;
780 PointerType::Profile(ID, T);
781
782 void *InsertPos = 0;
783 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
784 return QualType(PT, 0);
785
786 // If the pointee type isn't canonical, this won't be a canonical type either,
787 // so fill in the canonical type field.
788 QualType Canonical;
789 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000790 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000791
792 // Get the new insert position for the node we care about.
793 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000794 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 }
Steve Naroff506010b2009-01-19 22:45:10 +0000796 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000797 PointerType *New = new (Mem) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 Types.push_back(New);
799 PointerTypes.InsertNode(New, InsertPos);
800 return QualType(New, 0);
801}
802
Steve Naroff5618bd42008-08-27 16:04:49 +0000803/// getBlockPointerType - Return the uniqued reference to the type for
804/// a pointer to the specified block.
805QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000806 assert(T->isFunctionType() && "block of function types only");
807 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000808 // structure.
809 llvm::FoldingSetNodeID ID;
810 BlockPointerType::Profile(ID, T);
811
812 void *InsertPos = 0;
813 if (BlockPointerType *PT =
814 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
815 return QualType(PT, 0);
816
Steve Naroff296e8d52008-08-28 19:20:44 +0000817 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000818 // type either so fill in the canonical type field.
819 QualType Canonical;
820 if (!T->isCanonical()) {
821 Canonical = getBlockPointerType(getCanonicalType(T));
822
823 // Get the new insert position for the node we care about.
824 BlockPointerType *NewIP =
825 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000826 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000827 }
Steve Naroff506010b2009-01-19 22:45:10 +0000828 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000829 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000830 Types.push_back(New);
831 BlockPointerTypes.InsertNode(New, InsertPos);
832 return QualType(New, 0);
833}
834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835/// getReferenceType - Return the uniqued reference to the type for a reference
836/// to the specified type.
837QualType ASTContext::getReferenceType(QualType T) {
838 // Unique pointers, to guarantee there is only one pointer of a particular
839 // structure.
840 llvm::FoldingSetNodeID ID;
841 ReferenceType::Profile(ID, T);
842
843 void *InsertPos = 0;
844 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
845 return QualType(RT, 0);
846
847 // If the referencee type isn't canonical, this won't be a canonical type
848 // either, so fill in the canonical type field.
849 QualType Canonical;
850 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000851 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000852
853 // Get the new insert position for the node we care about.
854 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000855 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 }
857
Steve Naroff506010b2009-01-19 22:45:10 +0000858 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000859 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 Types.push_back(New);
861 ReferenceTypes.InsertNode(New, InsertPos);
862 return QualType(New, 0);
863}
864
Sebastian Redlf30208a2009-01-24 21:16:55 +0000865/// getMemberPointerType - Return the uniqued reference to the type for a
866/// member pointer to the specified type, in the specified class.
867QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
868{
869 // Unique pointers, to guarantee there is only one pointer of a particular
870 // structure.
871 llvm::FoldingSetNodeID ID;
872 MemberPointerType::Profile(ID, T, Cls);
873
874 void *InsertPos = 0;
875 if (MemberPointerType *PT =
876 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
877 return QualType(PT, 0);
878
879 // If the pointee or class type isn't canonical, this won't be a canonical
880 // type either, so fill in the canonical type field.
881 QualType Canonical;
882 if (!T->isCanonical()) {
883 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
884
885 // Get the new insert position for the node we care about.
886 MemberPointerType *NewIP =
887 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
888 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
889 }
890 void *Mem = Allocator.Allocate(sizeof(MemberPointerType), 8);
891 MemberPointerType *New = new (Mem) MemberPointerType(T, Cls, Canonical);
892 Types.push_back(New);
893 MemberPointerTypes.InsertNode(New, InsertPos);
894 return QualType(New, 0);
895}
896
Steve Narofffb22d962007-08-30 01:06:46 +0000897/// getConstantArrayType - Return the unique reference to the type for an
898/// array of the specified element type.
899QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000900 const llvm::APInt &ArySize,
901 ArrayType::ArraySizeModifier ASM,
902 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000904 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000905
906 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000907 if (ConstantArrayType *ATP =
908 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 return QualType(ATP, 0);
910
911 // If the element type isn't canonical, this won't be a canonical type either,
912 // so fill in the canonical type field.
913 QualType Canonical;
914 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000915 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000916 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000918 ConstantArrayType *NewIP =
919 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000920 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 }
922
Steve Naroff506010b2009-01-19 22:45:10 +0000923 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000924 ConstantArrayType *New =
925 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000926 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 Types.push_back(New);
928 return QualType(New, 0);
929}
930
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000931/// getVariableArrayType - Returns a non-unique reference to the type for a
932/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000933QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
934 ArrayType::ArraySizeModifier ASM,
935 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000936 // Since we don't unique expressions, it isn't possible to unique VLA's
937 // that have an expression provided for their size.
938
Steve Naroff506010b2009-01-19 22:45:10 +0000939 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000940 VariableArrayType *New =
941 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000942
943 VariableArrayTypes.push_back(New);
944 Types.push_back(New);
945 return QualType(New, 0);
946}
947
Douglas Gregor898574e2008-12-05 23:32:09 +0000948/// getDependentSizedArrayType - Returns a non-unique reference to
949/// the type for a dependently-sized array of the specified element
950/// type. FIXME: We will need these to be uniqued, or at least
951/// comparable, at some point.
952QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
953 ArrayType::ArraySizeModifier ASM,
954 unsigned EltTypeQuals) {
955 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
956 "Size must be type- or value-dependent!");
957
958 // Since we don't unique expressions, it isn't possible to unique
959 // dependently-sized array types.
960
Steve Naroff506010b2009-01-19 22:45:10 +0000961 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000962 DependentSizedArrayType *New =
963 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
964 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000965
966 DependentSizedArrayTypes.push_back(New);
967 Types.push_back(New);
968 return QualType(New, 0);
969}
970
Eli Friedmanc5773c42008-02-15 18:16:39 +0000971QualType ASTContext::getIncompleteArrayType(QualType EltTy,
972 ArrayType::ArraySizeModifier ASM,
973 unsigned EltTypeQuals) {
974 llvm::FoldingSetNodeID ID;
975 IncompleteArrayType::Profile(ID, EltTy);
976
977 void *InsertPos = 0;
978 if (IncompleteArrayType *ATP =
979 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
980 return QualType(ATP, 0);
981
982 // If the element type isn't canonical, this won't be a canonical type
983 // either, so fill in the canonical type field.
984 QualType Canonical;
985
986 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000987 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000988 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000989
990 // Get the new insert position for the node we care about.
991 IncompleteArrayType *NewIP =
992 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000993 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000994 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000995
Steve Naroff506010b2009-01-19 22:45:10 +0000996 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000997 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
998 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000999
1000 IncompleteArrayTypes.InsertNode(New, InsertPos);
1001 Types.push_back(New);
1002 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001003}
1004
Steve Naroff73322922007-07-18 18:00:27 +00001005/// getVectorType - Return the unique reference to a vector type of
1006/// the specified element type and size. VectorType must be a built-in type.
1007QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 BuiltinType *baseType;
1009
Chris Lattnerf52ab252008-04-06 22:59:24 +00001010 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001011 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001012
1013 // Check if we've already instantiated a vector of this type.
1014 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001015 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 void *InsertPos = 0;
1017 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1018 return QualType(VTP, 0);
1019
1020 // If the element type isn't canonical, this won't be a canonical type either,
1021 // so fill in the canonical type field.
1022 QualType Canonical;
1023 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001024 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001025
1026 // Get the new insert position for the node we care about.
1027 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001028 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 }
Steve Naroff506010b2009-01-19 22:45:10 +00001030 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001031 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 VectorTypes.InsertNode(New, InsertPos);
1033 Types.push_back(New);
1034 return QualType(New, 0);
1035}
1036
Nate Begeman213541a2008-04-18 23:10:10 +00001037/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001038/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001039QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001040 BuiltinType *baseType;
1041
Chris Lattnerf52ab252008-04-06 22:59:24 +00001042 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001043 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001044
1045 // Check if we've already instantiated a vector of this type.
1046 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001047 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001048 void *InsertPos = 0;
1049 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1050 return QualType(VTP, 0);
1051
1052 // If the element type isn't canonical, this won't be a canonical type either,
1053 // so fill in the canonical type field.
1054 QualType Canonical;
1055 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001056 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001057
1058 // Get the new insert position for the node we care about.
1059 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001060 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001061 }
Steve Naroff506010b2009-01-19 22:45:10 +00001062 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001063 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001064 VectorTypes.InsertNode(New, InsertPos);
1065 Types.push_back(New);
1066 return QualType(New, 0);
1067}
1068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1070///
1071QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1072 // Unique functions, to guarantee there is only one function of a particular
1073 // structure.
1074 llvm::FoldingSetNodeID ID;
1075 FunctionTypeNoProto::Profile(ID, ResultTy);
1076
1077 void *InsertPos = 0;
1078 if (FunctionTypeNoProto *FT =
1079 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1080 return QualType(FT, 0);
1081
1082 QualType Canonical;
1083 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001084 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001085
1086 // Get the new insert position for the node we care about.
1087 FunctionTypeNoProto *NewIP =
1088 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001089 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
1091
Douglas Gregorb17e3b02009-01-20 21:02:13 +00001092 void *Mem = Allocator.Allocate(sizeof(FunctionTypeNoProto), 8);
1093 FunctionTypeNoProto *New = new (Mem) FunctionTypeNoProto(ResultTy, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001095 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 return QualType(New, 0);
1097}
1098
1099/// getFunctionType - Return a normal function type with a typed argument
1100/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001101QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001102 unsigned NumArgs, bool isVariadic,
1103 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 // Unique functions, to guarantee there is only one function of a particular
1105 // structure.
1106 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001107 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1108 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001109
1110 void *InsertPos = 0;
1111 if (FunctionTypeProto *FTP =
1112 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1113 return QualType(FTP, 0);
1114
1115 // Determine whether the type being created is already canonical or not.
1116 bool isCanonical = ResultTy->isCanonical();
1117 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1118 if (!ArgArray[i]->isCanonical())
1119 isCanonical = false;
1120
1121 // If this type isn't canonical, get the canonical version of it.
1122 QualType Canonical;
1123 if (!isCanonical) {
1124 llvm::SmallVector<QualType, 16> CanonicalArgs;
1125 CanonicalArgs.reserve(NumArgs);
1126 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001127 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001128
Chris Lattnerf52ab252008-04-06 22:59:24 +00001129 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001131 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132
1133 // Get the new insert position for the node we care about.
1134 FunctionTypeProto *NewIP =
1135 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001136 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 }
1138
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001139 // FunctionTypeProto objects are allocated with extra bytes after them
1140 // for a variable size array (for parameter types) at the end of them.
1141 // FIXME: Can we do better than forcing a 16-byte alignment?
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 FunctionTypeProto *FTP =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001143 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1144 NumArgs*sizeof(QualType), 16);
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001146 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 Types.push_back(FTP);
1148 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1149 return QualType(FTP, 0);
1150}
1151
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001152/// getTypeDeclType - Return the unique reference to the type for the
1153/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001154QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001155 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001156 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1157
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001158 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001159 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001160 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1161 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001162 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001163 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001164
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001165 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001166 if (PrevDecl)
1167 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1168 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001169 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001170 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1171 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001172 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001173 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001174 if (PrevDecl)
1175 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1176 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001177 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001178 Decl->TypeForDecl = new (Mem) RecordType(Record);
1179 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001180 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001181 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1182 if (PrevDecl)
1183 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1184 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001185 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001186 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1187 }
1188 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001189 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001190 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001191
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001192 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001193 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001194}
1195
Reid Spencer5f016e22007-07-11 17:01:13 +00001196/// getTypedefType - Return the unique reference to the type for the
1197/// specified typename decl.
1198QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1199 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1200
Chris Lattnerf52ab252008-04-06 22:59:24 +00001201 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff506010b2009-01-19 22:45:10 +00001202 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001203 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 Types.push_back(Decl->TypeForDecl);
1205 return QualType(Decl->TypeForDecl, 0);
1206}
1207
Douglas Gregor72c3f312008-12-05 18:15:24 +00001208/// getTemplateTypeParmType - Return the unique reference to the type
1209/// for the specified template type parameter declaration.
1210QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1211 if (!Decl->TypeForDecl) {
Steve Naroff506010b2009-01-19 22:45:10 +00001212 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001213 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001214 Types.push_back(Decl->TypeForDecl);
1215 }
1216 return QualType(Decl->TypeForDecl, 0);
1217}
1218
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001219/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001220/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001222 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1223
Steve Naroff506010b2009-01-19 22:45:10 +00001224 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001225 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001226 Types.push_back(Decl->TypeForDecl);
1227 return QualType(Decl->TypeForDecl, 0);
1228}
1229
Chris Lattner88cb27a2008-04-07 04:56:42 +00001230/// CmpProtocolNames - Comparison predicate for sorting protocols
1231/// alphabetically.
1232static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1233 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001234 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001235}
1236
1237static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1238 unsigned &NumProtocols) {
1239 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1240
1241 // Sort protocols, keyed by name.
1242 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1243
1244 // Remove duplicates.
1245 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1246 NumProtocols = ProtocolsEnd-Protocols;
1247}
1248
1249
Chris Lattner065f0d72008-04-07 04:44:08 +00001250/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1251/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001252QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1253 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001254 // Sort the protocol list alphabetically to canonicalize it.
1255 SortAndUniqueProtocols(Protocols, NumProtocols);
1256
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001257 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001258 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001259
1260 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001261 if (ObjCQualifiedInterfaceType *QT =
1262 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001263 return QualType(QT, 0);
1264
1265 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001266 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001267 ObjCQualifiedInterfaceType *QType =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001268 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1269
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001270 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001271 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001272 return QualType(QType, 0);
1273}
1274
Chris Lattner88cb27a2008-04-07 04:56:42 +00001275/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1276/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001277QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001278 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001279 // Sort the protocol list alphabetically to canonicalize it.
1280 SortAndUniqueProtocols(Protocols, NumProtocols);
1281
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001282 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001284
1285 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001286 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001287 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001288 return QualType(QT, 0);
1289
1290 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001291 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001292 ObjCQualifiedIdType *QType =
1293 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001294 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001295 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001296 return QualType(QType, 0);
1297}
1298
Steve Naroff9752f252007-08-01 18:02:17 +00001299/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1300/// TypeOfExpr AST's (since expression's are never shared). For example,
1301/// multiple declarations that refer to "typeof(x)" all contain different
1302/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1303/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001304QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001305 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregorb17e3b02009-01-20 21:02:13 +00001306 void *Mem = Allocator.Allocate(sizeof(TypeOfExpr), 8);
1307 TypeOfExpr *toe = new (Mem) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001308 Types.push_back(toe);
1309 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001310}
1311
Steve Naroff9752f252007-08-01 18:02:17 +00001312/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1313/// TypeOfType AST's. The only motivation to unique these nodes would be
1314/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1315/// an issue. This doesn't effect the type checker, since it operates
1316/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001317QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001318 QualType Canonical = getCanonicalType(tofType);
Steve Naroff506010b2009-01-19 22:45:10 +00001319 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001320 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001321 Types.push_back(tot);
1322 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001323}
1324
Reid Spencer5f016e22007-07-11 17:01:13 +00001325/// getTagDeclType - Return the unique reference to the type for the
1326/// specified TagDecl (struct/union/class/enum) decl.
1327QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001328 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001329 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001330}
1331
1332/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1333/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1334/// needs to agree with the definition in <stddef.h>.
1335QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001336 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
1338
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001339/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001340/// width of characters in wide strings, The value is target dependent and
1341/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001342QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001343 if (LangOpts.CPlusPlus)
1344 return WCharTy;
1345
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001346 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1347 // wide-character type?
1348 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001349}
1350
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001351/// getSignedWCharType - Return the type of "signed wchar_t".
1352/// Used when in C++, as a GCC extension.
1353QualType ASTContext::getSignedWCharType() const {
1354 // FIXME: derive from "Target" ?
1355 return WCharTy;
1356}
1357
1358/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1359/// Used when in C++, as a GCC extension.
1360QualType ASTContext::getUnsignedWCharType() const {
1361 // FIXME: derive from "Target" ?
1362 return UnsignedIntTy;
1363}
1364
Chris Lattner8b9023b2007-07-13 03:05:23 +00001365/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1366/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1367QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001368 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001369}
1370
Chris Lattnere6327742008-04-02 05:18:44 +00001371//===----------------------------------------------------------------------===//
1372// Type Operators
1373//===----------------------------------------------------------------------===//
1374
Chris Lattner77c96472008-04-06 22:41:35 +00001375/// getCanonicalType - Return the canonical (structural) type corresponding to
1376/// the specified potentially non-canonical type. The non-canonical version
1377/// of a type may have many "decorated" versions of types. Decorators can
1378/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1379/// to be free of any of these, allowing two canonical types to be compared
1380/// for exact equality with a simple pointer comparison.
1381QualType ASTContext::getCanonicalType(QualType T) {
1382 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001383
1384 // If the result has type qualifiers, make sure to canonicalize them as well.
1385 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1386 if (TypeQuals == 0) return CanType;
1387
1388 // If the type qualifiers are on an array type, get the canonical type of the
1389 // array with the qualifiers applied to the element type.
1390 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1391 if (!AT)
1392 return CanType.getQualifiedType(TypeQuals);
1393
1394 // Get the canonical version of the element with the extra qualifiers on it.
1395 // This can recursively sink qualifiers through multiple levels of arrays.
1396 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1397 NewEltTy = getCanonicalType(NewEltTy);
1398
1399 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1400 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1401 CAT->getIndexTypeQualifier());
1402 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1403 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1404 IAT->getIndexTypeQualifier());
1405
Douglas Gregor898574e2008-12-05 23:32:09 +00001406 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1407 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1408 DSAT->getSizeModifier(),
1409 DSAT->getIndexTypeQualifier());
1410
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001411 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1412 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1413 VAT->getSizeModifier(),
1414 VAT->getIndexTypeQualifier());
1415}
1416
1417
1418const ArrayType *ASTContext::getAsArrayType(QualType T) {
1419 // Handle the non-qualified case efficiently.
1420 if (T.getCVRQualifiers() == 0) {
1421 // Handle the common positive case fast.
1422 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1423 return AT;
1424 }
1425
1426 // Handle the common negative case fast, ignoring CVR qualifiers.
1427 QualType CType = T->getCanonicalTypeInternal();
1428
1429 // Make sure to look through type qualifiers (like ASQuals) for the negative
1430 // test.
1431 if (!isa<ArrayType>(CType) &&
1432 !isa<ArrayType>(CType.getUnqualifiedType()))
1433 return 0;
1434
1435 // Apply any CVR qualifiers from the array type to the element type. This
1436 // implements C99 6.7.3p8: "If the specification of an array type includes
1437 // any type qualifiers, the element type is so qualified, not the array type."
1438
1439 // If we get here, we either have type qualifiers on the type, or we have
1440 // sugar such as a typedef in the way. If we have type qualifiers on the type
1441 // we must propagate them down into the elemeng type.
1442 unsigned CVRQuals = T.getCVRQualifiers();
1443 unsigned AddrSpace = 0;
1444 Type *Ty = T.getTypePtr();
1445
1446 // Rip through ASQualType's and typedefs to get to a concrete type.
1447 while (1) {
1448 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1449 AddrSpace = ASQT->getAddressSpace();
1450 Ty = ASQT->getBaseType();
1451 } else {
1452 T = Ty->getDesugaredType();
1453 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1454 break;
1455 CVRQuals |= T.getCVRQualifiers();
1456 Ty = T.getTypePtr();
1457 }
1458 }
1459
1460 // If we have a simple case, just return now.
1461 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1462 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1463 return ATy;
1464
1465 // Otherwise, we have an array and we have qualifiers on it. Push the
1466 // qualifiers into the array element type and return a new array type.
1467 // Get the canonical version of the element with the extra qualifiers on it.
1468 // This can recursively sink qualifiers through multiple levels of arrays.
1469 QualType NewEltTy = ATy->getElementType();
1470 if (AddrSpace)
1471 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1472 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1473
1474 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1475 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1476 CAT->getSizeModifier(),
1477 CAT->getIndexTypeQualifier()));
1478 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1479 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1480 IAT->getSizeModifier(),
1481 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001482
Douglas Gregor898574e2008-12-05 23:32:09 +00001483 if (const DependentSizedArrayType *DSAT
1484 = dyn_cast<DependentSizedArrayType>(ATy))
1485 return cast<ArrayType>(
1486 getDependentSizedArrayType(NewEltTy,
1487 DSAT->getSizeExpr(),
1488 DSAT->getSizeModifier(),
1489 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001490
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001491 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1492 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1493 VAT->getSizeModifier(),
1494 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001495}
1496
1497
Chris Lattnere6327742008-04-02 05:18:44 +00001498/// getArrayDecayedType - Return the properly qualified result of decaying the
1499/// specified array type to a pointer. This operation is non-trivial when
1500/// handling typedefs etc. The canonical type of "T" must be an array type,
1501/// this returns a pointer to a properly qualified element of the array.
1502///
1503/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1504QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001505 // Get the element type with 'getAsArrayType' so that we don't lose any
1506 // typedefs in the element type of the array. This also handles propagation
1507 // of type qualifiers from the array type into the element type if present
1508 // (C99 6.7.3p8).
1509 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1510 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001511
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001512 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001513
1514 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001515 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001516}
1517
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001518QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001519 QualType ElemTy = VAT->getElementType();
1520
1521 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1522 return getBaseElementType(VAT);
1523
1524 return ElemTy;
1525}
1526
Reid Spencer5f016e22007-07-11 17:01:13 +00001527/// getFloatingRank - Return a relative rank for floating point types.
1528/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001529static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001530 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001532
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001533 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001534 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001535 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 case BuiltinType::Float: return FloatRank;
1537 case BuiltinType::Double: return DoubleRank;
1538 case BuiltinType::LongDouble: return LongDoubleRank;
1539 }
1540}
1541
Steve Naroff716c7302007-08-27 01:41:48 +00001542/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1543/// point or a complex type (based on typeDomain/typeSize).
1544/// 'typeDomain' is a real floating point or complex type.
1545/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001546QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1547 QualType Domain) const {
1548 FloatingRank EltRank = getFloatingRank(Size);
1549 if (Domain->isComplexType()) {
1550 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001551 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001552 case FloatRank: return FloatComplexTy;
1553 case DoubleRank: return DoubleComplexTy;
1554 case LongDoubleRank: return LongDoubleComplexTy;
1555 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 }
Chris Lattner1361b112008-04-06 23:58:54 +00001557
1558 assert(Domain->isRealFloatingType() && "Unknown domain!");
1559 switch (EltRank) {
1560 default: assert(0 && "getFloatingRank(): illegal value for rank");
1561 case FloatRank: return FloatTy;
1562 case DoubleRank: return DoubleTy;
1563 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001564 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001565}
1566
Chris Lattner7cfeb082008-04-06 23:55:33 +00001567/// getFloatingTypeOrder - Compare the rank of the two specified floating
1568/// point types, ignoring the domain of the type (i.e. 'double' ==
1569/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1570/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001571int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1572 FloatingRank LHSR = getFloatingRank(LHS);
1573 FloatingRank RHSR = getFloatingRank(RHS);
1574
1575 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001576 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001577 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001578 return 1;
1579 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001580}
1581
Chris Lattnerf52ab252008-04-06 22:59:24 +00001582/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1583/// routine will assert if passed a built-in type that isn't an integer or enum,
1584/// or if it is not canonicalized.
1585static unsigned getIntegerRank(Type *T) {
1586 assert(T->isCanonical() && "T should be canonicalized");
1587 if (isa<EnumType>(T))
1588 return 4;
1589
1590 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001591 default: assert(0 && "getIntegerRank(): not a built-in integer");
1592 case BuiltinType::Bool:
1593 return 1;
1594 case BuiltinType::Char_S:
1595 case BuiltinType::Char_U:
1596 case BuiltinType::SChar:
1597 case BuiltinType::UChar:
1598 return 2;
1599 case BuiltinType::Short:
1600 case BuiltinType::UShort:
1601 return 3;
1602 case BuiltinType::Int:
1603 case BuiltinType::UInt:
1604 return 4;
1605 case BuiltinType::Long:
1606 case BuiltinType::ULong:
1607 return 5;
1608 case BuiltinType::LongLong:
1609 case BuiltinType::ULongLong:
1610 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001611 }
1612}
1613
Chris Lattner7cfeb082008-04-06 23:55:33 +00001614/// getIntegerTypeOrder - Returns the highest ranked integer type:
1615/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1616/// LHS < RHS, return -1.
1617int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001618 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1619 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001620 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001621
Chris Lattnerf52ab252008-04-06 22:59:24 +00001622 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1623 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001624
Chris Lattner7cfeb082008-04-06 23:55:33 +00001625 unsigned LHSRank = getIntegerRank(LHSC);
1626 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001627
Chris Lattner7cfeb082008-04-06 23:55:33 +00001628 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1629 if (LHSRank == RHSRank) return 0;
1630 return LHSRank > RHSRank ? 1 : -1;
1631 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001632
Chris Lattner7cfeb082008-04-06 23:55:33 +00001633 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1634 if (LHSUnsigned) {
1635 // If the unsigned [LHS] type is larger, return it.
1636 if (LHSRank >= RHSRank)
1637 return 1;
1638
1639 // If the signed type can represent all values of the unsigned type, it
1640 // wins. Because we are dealing with 2's complement and types that are
1641 // powers of two larger than each other, this is always safe.
1642 return -1;
1643 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001644
Chris Lattner7cfeb082008-04-06 23:55:33 +00001645 // If the unsigned [RHS] type is larger, return it.
1646 if (RHSRank >= LHSRank)
1647 return -1;
1648
1649 // If the signed type can represent all values of the unsigned type, it
1650 // wins. Because we are dealing with 2's complement and types that are
1651 // powers of two larger than each other, this is always safe.
1652 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001653}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001654
1655// getCFConstantStringType - Return the type used for constant CFStrings.
1656QualType ASTContext::getCFConstantStringType() {
1657 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001658 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001659 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001660 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001661 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001662
1663 // const int *isa;
1664 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001665 // int flags;
1666 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001667 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001668 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001669 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001670 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001671
Anders Carlsson71993dd2007-08-17 05:31:46 +00001672 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001673 for (unsigned i = 0; i < 4; ++i) {
1674 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1675 SourceLocation(), 0,
1676 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001677 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001678 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001679 }
1680
1681 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001682 }
1683
1684 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001685}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001686
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001687QualType ASTContext::getObjCFastEnumerationStateType()
1688{
1689 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001690 ObjCFastEnumerationStateTypeDecl =
1691 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1692 &Idents.get("__objcFastEnumerationState"));
1693
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001694 QualType FieldTypes[] = {
1695 UnsignedLongTy,
1696 getPointerType(ObjCIdType),
1697 getPointerType(UnsignedLongTy),
1698 getConstantArrayType(UnsignedLongTy,
1699 llvm::APInt(32, 5), ArrayType::Normal, 0)
1700 };
1701
Douglas Gregor44b43212008-12-11 16:49:14 +00001702 for (size_t i = 0; i < 4; ++i) {
1703 FieldDecl *Field = FieldDecl::Create(*this,
1704 ObjCFastEnumerationStateTypeDecl,
1705 SourceLocation(), 0,
1706 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001707 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001708 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001709 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001710
Douglas Gregor44b43212008-12-11 16:49:14 +00001711 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001712 }
1713
1714 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1715}
1716
Anders Carlssone8c49532007-10-29 06:33:42 +00001717// This returns true if a type has been typedefed to BOOL:
1718// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001719static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001720 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001721 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1722 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001723
1724 return false;
1725}
1726
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001727/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001728/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001729int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001730 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001731
1732 // Make all integer and enum types at least as large as an int
1733 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001734 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001735 // Treat arrays as pointers, since that's how they're passed in.
1736 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001737 sz = getTypeSize(VoidPtrTy);
1738 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001739}
1740
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001741/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001742/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001743void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001744 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001745 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001746 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001747 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001748 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001749 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001750 // Compute size of all parameters.
1751 // Start with computing size of a pointer in number of bytes.
1752 // FIXME: There might(should) be a better way of doing this computation!
1753 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001754 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001755 // The first two arguments (self and _cmd) are pointers; account for
1756 // their size.
1757 int ParmOffset = 2 * PtrSize;
1758 int NumOfParams = Decl->getNumParams();
1759 for (int i = 0; i < NumOfParams; i++) {
1760 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001761 int sz = getObjCEncodingTypeSize (PType);
1762 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001763 ParmOffset += sz;
1764 }
1765 S += llvm::utostr(ParmOffset);
1766 S += "@0:";
1767 S += llvm::utostr(PtrSize);
1768
1769 // Argument types.
1770 ParmOffset = 2 * PtrSize;
1771 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001772 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1773 QualType PType = PVDecl->getOriginalType();
1774 if (const ArrayType *AT =
1775 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1776 // Use array's original type only if it has known number of
1777 // elements.
1778 if (!dyn_cast<ConstantArrayType>(AT))
1779 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001780 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001781 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001782 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001783 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001784 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001785 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001786 }
1787}
1788
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001789/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001790/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001791/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1792/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001793/// Property attributes are stored as a comma-delimited C string. The simple
1794/// attributes readonly and bycopy are encoded as single characters. The
1795/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1796/// encoded as single characters, followed by an identifier. Property types
1797/// are also encoded as a parametrized attribute. The characters used to encode
1798/// these attributes are defined by the following enumeration:
1799/// @code
1800/// enum PropertyAttributes {
1801/// kPropertyReadOnly = 'R', // property is read-only.
1802/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1803/// kPropertyByref = '&', // property is a reference to the value last assigned
1804/// kPropertyDynamic = 'D', // property is dynamic
1805/// kPropertyGetter = 'G', // followed by getter selector name
1806/// kPropertySetter = 'S', // followed by setter selector name
1807/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1808/// kPropertyType = 't' // followed by old-style type encoding.
1809/// kPropertyWeak = 'W' // 'weak' property
1810/// kPropertyStrong = 'P' // property GC'able
1811/// kPropertyNonAtomic = 'N' // property non-atomic
1812/// };
1813/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001814void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1815 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001816 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001817 // Collect information from the property implementation decl(s).
1818 bool Dynamic = false;
1819 ObjCPropertyImplDecl *SynthesizePID = 0;
1820
1821 // FIXME: Duplicated code due to poor abstraction.
1822 if (Container) {
1823 if (const ObjCCategoryImplDecl *CID =
1824 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1825 for (ObjCCategoryImplDecl::propimpl_iterator
1826 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1827 ObjCPropertyImplDecl *PID = *i;
1828 if (PID->getPropertyDecl() == PD) {
1829 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1830 Dynamic = true;
1831 } else {
1832 SynthesizePID = PID;
1833 }
1834 }
1835 }
1836 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001837 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001838 for (ObjCCategoryImplDecl::propimpl_iterator
1839 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1840 ObjCPropertyImplDecl *PID = *i;
1841 if (PID->getPropertyDecl() == PD) {
1842 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1843 Dynamic = true;
1844 } else {
1845 SynthesizePID = PID;
1846 }
1847 }
1848 }
1849 }
1850 }
1851
1852 // FIXME: This is not very efficient.
1853 S = "T";
1854
1855 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001856 // GCC has some special rules regarding encoding of properties which
1857 // closely resembles encoding of ivars.
1858 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1859 true /* outermost type */,
1860 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001861
1862 if (PD->isReadOnly()) {
1863 S += ",R";
1864 } else {
1865 switch (PD->getSetterKind()) {
1866 case ObjCPropertyDecl::Assign: break;
1867 case ObjCPropertyDecl::Copy: S += ",C"; break;
1868 case ObjCPropertyDecl::Retain: S += ",&"; break;
1869 }
1870 }
1871
1872 // It really isn't clear at all what this means, since properties
1873 // are "dynamic by default".
1874 if (Dynamic)
1875 S += ",D";
1876
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001877 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1878 S += ",N";
1879
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001880 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1881 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001882 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001883 }
1884
1885 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1886 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001887 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001888 }
1889
1890 if (SynthesizePID) {
1891 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1892 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001893 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001894 }
1895
1896 // FIXME: OBJCGC: weak & strong
1897}
1898
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001899/// getLegacyIntegralTypeEncoding -
1900/// Another legacy compatibility encoding: 32-bit longs are encoded as
1901/// 'l' or 'L', but not always. For typedefs, we need to use
1902/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1903///
1904void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1905 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1906 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1907 if (BT->getKind() == BuiltinType::ULong)
1908 PointeeTy = UnsignedIntTy;
1909 else if (BT->getKind() == BuiltinType::Long)
1910 PointeeTy = IntTy;
1911 }
1912 }
1913}
1914
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001915void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001916 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001917 // We follow the behavior of gcc, expanding structures which are
1918 // directly pointed to, and expanding embedded structures. Note that
1919 // these rules are sufficient to prevent recursive encoding of the
1920 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001921 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1922 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001923}
1924
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001925static void EncodeBitField(const ASTContext *Context, std::string& S,
1926 FieldDecl *FD) {
1927 const Expr *E = FD->getBitWidth();
1928 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1929 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1930 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1931 S += 'b';
1932 S += llvm::utostr(N);
1933}
1934
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001935void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1936 bool ExpandPointedToStructures,
1937 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001938 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001939 bool OutermostType,
1940 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001941 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001942 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001943 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001944 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001945 else {
1946 char encoding;
1947 switch (BT->getKind()) {
1948 default: assert(0 && "Unhandled builtin type kind");
1949 case BuiltinType::Void: encoding = 'v'; break;
1950 case BuiltinType::Bool: encoding = 'B'; break;
1951 case BuiltinType::Char_U:
1952 case BuiltinType::UChar: encoding = 'C'; break;
1953 case BuiltinType::UShort: encoding = 'S'; break;
1954 case BuiltinType::UInt: encoding = 'I'; break;
1955 case BuiltinType::ULong: encoding = 'L'; break;
1956 case BuiltinType::ULongLong: encoding = 'Q'; break;
1957 case BuiltinType::Char_S:
1958 case BuiltinType::SChar: encoding = 'c'; break;
1959 case BuiltinType::Short: encoding = 's'; break;
1960 case BuiltinType::Int: encoding = 'i'; break;
1961 case BuiltinType::Long: encoding = 'l'; break;
1962 case BuiltinType::LongLong: encoding = 'q'; break;
1963 case BuiltinType::Float: encoding = 'f'; break;
1964 case BuiltinType::Double: encoding = 'd'; break;
1965 case BuiltinType::LongDouble: encoding = 'd'; break;
1966 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001967
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001968 S += encoding;
1969 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001970 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001971 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001972 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1973 ExpandPointedToStructures,
1974 ExpandStructures, FD);
1975 if (FD || EncodingProperty) {
1976 // Note that we do extended encoding of protocol qualifer list
1977 // Only when doing ivar or property encoding.
1978 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1979 S += '"';
1980 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1981 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1982 S += '<';
1983 S += Proto->getNameAsString();
1984 S += '>';
1985 }
1986 S += '"';
1987 }
1988 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001989 }
1990 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001991 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001992 bool isReadOnly = false;
1993 // For historical/compatibility reasons, the read-only qualifier of the
1994 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1995 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1996 // Also, do not emit the 'r' for anything but the outermost type!
1997 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1998 if (OutermostType && T.isConstQualified()) {
1999 isReadOnly = true;
2000 S += 'r';
2001 }
2002 }
2003 else if (OutermostType) {
2004 QualType P = PointeeTy;
2005 while (P->getAsPointerType())
2006 P = P->getAsPointerType()->getPointeeType();
2007 if (P.isConstQualified()) {
2008 isReadOnly = true;
2009 S += 'r';
2010 }
2011 }
2012 if (isReadOnly) {
2013 // Another legacy compatibility encoding. Some ObjC qualifier and type
2014 // combinations need to be rearranged.
2015 // Rewrite "in const" from "nr" to "rn"
2016 const char * s = S.c_str();
2017 int len = S.length();
2018 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2019 std::string replace = "rn";
2020 S.replace(S.end()-2, S.end(), replace);
2021 }
2022 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002023 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002024 S += '@';
2025 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002026 }
2027 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002028 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2029 // Another historical/compatibility reason.
2030 // We encode the underlying type which comes out as
2031 // {...};
2032 S += '^';
2033 getObjCEncodingForTypeImpl(PointeeTy, S,
2034 false, ExpandPointedToStructures,
2035 NULL);
2036 return;
2037 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002038 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002039 if (FD || EncodingProperty) {
2040 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2041 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002042 S += '"';
2043 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002044 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2045 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2046 S += '<';
2047 S += Proto->getNameAsString();
2048 S += '>';
2049 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002050 S += '"';
2051 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002052 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002053 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002054 S += '#';
2055 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002056 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002057 S += ':';
2058 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002059 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002060
2061 if (PointeeTy->isCharType()) {
2062 // char pointer types should be encoded as '*' unless it is a
2063 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002064 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002065 S += '*';
2066 return;
2067 }
2068 }
2069
2070 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002071 getLegacyIntegralTypeEncoding(PointeeTy);
2072
2073 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002074 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002075 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002076 } else if (const ArrayType *AT =
2077 // Ignore type qualifiers etc.
2078 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002079 S += '[';
2080
2081 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2082 S += llvm::utostr(CAT->getSize().getZExtValue());
2083 else
2084 assert(0 && "Unhandled array type!");
2085
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002086 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002087 false, ExpandStructures, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002088 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002089 } else if (T->getAsFunctionType()) {
2090 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002091 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002092 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002093 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002094 // Anonymous structures print as '?'
2095 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2096 S += II->getName();
2097 } else {
2098 S += '?';
2099 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002100 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002101 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002102 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2103 FieldEnd = RDecl->field_end();
2104 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002105 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002106 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002107 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002108 S += '"';
2109 }
2110
2111 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002112 if (Field->isBitField()) {
2113 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2114 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002115 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002116 QualType qt = Field->getType();
2117 getLegacyIntegralTypeEncoding(qt);
2118 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002119 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002120 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002121 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002122 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002123 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002124 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002125 if (FD && FD->isBitField())
2126 EncodeBitField(this, S, FD);
2127 else
2128 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002129 } else if (T->isBlockPointerType()) {
2130 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002131 } else if (T->isObjCInterfaceType()) {
2132 // @encode(class_name)
2133 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2134 S += '{';
2135 const IdentifierInfo *II = OI->getIdentifier();
2136 S += II->getName();
2137 S += '=';
2138 std::vector<FieldDecl*> RecFields;
2139 CollectObjCIvars(OI, RecFields);
2140 for (unsigned int i = 0; i != RecFields.size(); i++) {
2141 if (RecFields[i]->isBitField())
2142 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2143 RecFields[i]);
2144 else
2145 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2146 FD);
2147 }
2148 S += '}';
2149 }
2150 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002151 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002152}
2153
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002154void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002155 std::string& S) const {
2156 if (QT & Decl::OBJC_TQ_In)
2157 S += 'n';
2158 if (QT & Decl::OBJC_TQ_Inout)
2159 S += 'N';
2160 if (QT & Decl::OBJC_TQ_Out)
2161 S += 'o';
2162 if (QT & Decl::OBJC_TQ_Bycopy)
2163 S += 'O';
2164 if (QT & Decl::OBJC_TQ_Byref)
2165 S += 'R';
2166 if (QT & Decl::OBJC_TQ_Oneway)
2167 S += 'V';
2168}
2169
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002170void ASTContext::setBuiltinVaListType(QualType T)
2171{
2172 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2173
2174 BuiltinVaListType = T;
2175}
2176
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002177void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002178{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002179 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002180
2181 // typedef struct objc_object *id;
2182 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002183 // User error - caller will issue diagnostics.
2184 if (!ptr)
2185 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002186 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002187 // User error - caller will issue diagnostics.
2188 if (!rec)
2189 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002190 IdStructType = rec;
2191}
2192
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002193void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002194{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002195 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002196
2197 // typedef struct objc_selector *SEL;
2198 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002199 if (!ptr)
2200 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002201 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002202 if (!rec)
2203 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002204 SelStructType = rec;
2205}
2206
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002207void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002208{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002209 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002210}
2211
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002212void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002213{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002214 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002215
2216 // typedef struct objc_class *Class;
2217 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2218 assert(ptr && "'Class' incorrectly typed");
2219 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2220 assert(rec && "'Class' incorrectly typed");
2221 ClassStructType = rec;
2222}
2223
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002224void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2225 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002226 "'NSConstantString' type already set!");
2227
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002228 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002229}
2230
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002231/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002232/// TargetInfo, produce the corresponding type. The unsigned @p Type
2233/// is actually a value of type @c TargetInfo::IntType.
2234QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002235 switch (Type) {
2236 case TargetInfo::NoInt: return QualType();
2237 case TargetInfo::SignedShort: return ShortTy;
2238 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2239 case TargetInfo::SignedInt: return IntTy;
2240 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2241 case TargetInfo::SignedLong: return LongTy;
2242 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2243 case TargetInfo::SignedLongLong: return LongLongTy;
2244 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2245 }
2246
2247 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002248 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002249}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002250
2251//===----------------------------------------------------------------------===//
2252// Type Predicates.
2253//===----------------------------------------------------------------------===//
2254
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002255/// isObjCNSObjectType - Return true if this is an NSObject object using
2256/// NSObject attribute on a c-style pointer type.
2257/// FIXME - Make it work directly on types.
2258///
2259bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2260 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2261 if (TypedefDecl *TD = TDT->getDecl())
2262 if (TD->getAttr<ObjCNSObjectAttr>())
2263 return true;
2264 }
2265 return false;
2266}
2267
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002268/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2269/// to an object type. This includes "id" and "Class" (two 'special' pointers
2270/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2271/// ID type).
2272bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2273 if (Ty->isObjCQualifiedIdType())
2274 return true;
2275
Steve Naroff6ae98502008-10-21 18:24:04 +00002276 // Blocks are objects.
2277 if (Ty->isBlockPointerType())
2278 return true;
2279
2280 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002281 if (!Ty->isPointerType())
2282 return false;
2283
2284 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2285 // pointer types. This looks for the typedef specifically, not for the
2286 // underlying type.
2287 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2288 return true;
2289
2290 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002291 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2292 return true;
2293
2294 // If is has NSObject attribute, OK as well.
2295 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002296}
2297
Chris Lattner6ac46a42008-04-07 06:51:04 +00002298//===----------------------------------------------------------------------===//
2299// Type Compatibility Testing
2300//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002301
Steve Naroff1c7d0672008-09-04 15:10:53 +00002302/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002303/// block types. Types must be strictly compatible here. For example,
2304/// C unfortunately doesn't produce an error for the following:
2305///
2306/// int (*emptyArgFunc)();
2307/// int (*intArgList)(int) = emptyArgFunc;
2308///
2309/// For blocks, we will produce an error for the following (similar to C++):
2310///
2311/// int (^emptyArgBlock)();
2312/// int (^intArgBlock)(int) = emptyArgBlock;
2313///
2314/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2315///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002316bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002317 const FunctionType *lbase = lhs->getAsFunctionType();
2318 const FunctionType *rbase = rhs->getAsFunctionType();
2319 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2320 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2321 if (lproto && rproto)
2322 return !mergeTypes(lhs, rhs).isNull();
2323 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002324}
2325
Chris Lattner6ac46a42008-04-07 06:51:04 +00002326/// areCompatVectorTypes - Return true if the two specified vector types are
2327/// compatible.
2328static bool areCompatVectorTypes(const VectorType *LHS,
2329 const VectorType *RHS) {
2330 assert(LHS->isCanonical() && RHS->isCanonical());
2331 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002332 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002333}
2334
Eli Friedman3d815e72008-08-22 00:56:42 +00002335/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002336/// compatible for assignment from RHS to LHS. This handles validation of any
2337/// protocol qualifiers on the LHS or RHS.
2338///
Eli Friedman3d815e72008-08-22 00:56:42 +00002339bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2340 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002341 // Verify that the base decls are compatible: the RHS must be a subclass of
2342 // the LHS.
2343 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2344 return false;
2345
2346 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2347 // protocol qualified at all, then we are good.
2348 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2349 return true;
2350
2351 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2352 // isn't a superset.
2353 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2354 return true; // FIXME: should return false!
2355
2356 // Finally, we must have two protocol-qualified interfaces.
2357 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2358 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2359 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2360 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2361 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2362 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2363
2364 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2365 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2366 // LHS in a single parallel scan until we run out of elements in LHS.
2367 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2368 ObjCProtocolDecl *LHSProto = *LHSPI;
2369
2370 while (RHSPI != RHSPE) {
2371 ObjCProtocolDecl *RHSProto = *RHSPI++;
2372 // If the RHS has a protocol that the LHS doesn't, ignore it.
2373 if (RHSProto != LHSProto)
2374 continue;
2375
2376 // Otherwise, the RHS does have this element.
2377 ++LHSPI;
2378 if (LHSPI == LHSPE)
2379 return true; // All protocols in LHS exist in RHS.
2380
2381 LHSProto = *LHSPI;
2382 }
2383
2384 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2385 return false;
2386}
2387
Steve Naroffec0550f2007-10-15 20:41:53 +00002388/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2389/// both shall have the identically qualified version of a compatible type.
2390/// C99 6.2.7p1: Two types have compatible types if their types are the
2391/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002392bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2393 return !mergeTypes(LHS, RHS).isNull();
2394}
2395
2396QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2397 const FunctionType *lbase = lhs->getAsFunctionType();
2398 const FunctionType *rbase = rhs->getAsFunctionType();
2399 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2400 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2401 bool allLTypes = true;
2402 bool allRTypes = true;
2403
2404 // Check return type
2405 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2406 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002407 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2408 allLTypes = false;
2409 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2410 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002411
2412 if (lproto && rproto) { // two C99 style function prototypes
2413 unsigned lproto_nargs = lproto->getNumArgs();
2414 unsigned rproto_nargs = rproto->getNumArgs();
2415
2416 // Compatible functions must have the same number of arguments
2417 if (lproto_nargs != rproto_nargs)
2418 return QualType();
2419
2420 // Variadic and non-variadic functions aren't compatible
2421 if (lproto->isVariadic() != rproto->isVariadic())
2422 return QualType();
2423
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002424 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2425 return QualType();
2426
Eli Friedman3d815e72008-08-22 00:56:42 +00002427 // Check argument compatibility
2428 llvm::SmallVector<QualType, 10> types;
2429 for (unsigned i = 0; i < lproto_nargs; i++) {
2430 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2431 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2432 QualType argtype = mergeTypes(largtype, rargtype);
2433 if (argtype.isNull()) return QualType();
2434 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002435 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2436 allLTypes = false;
2437 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2438 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002439 }
2440 if (allLTypes) return lhs;
2441 if (allRTypes) return rhs;
2442 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002443 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002444 }
2445
2446 if (lproto) allRTypes = false;
2447 if (rproto) allLTypes = false;
2448
2449 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2450 if (proto) {
2451 if (proto->isVariadic()) return QualType();
2452 // Check that the types are compatible with the types that
2453 // would result from default argument promotions (C99 6.7.5.3p15).
2454 // The only types actually affected are promotable integer
2455 // types and floats, which would be passed as a different
2456 // type depending on whether the prototype is visible.
2457 unsigned proto_nargs = proto->getNumArgs();
2458 for (unsigned i = 0; i < proto_nargs; ++i) {
2459 QualType argTy = proto->getArgType(i);
2460 if (argTy->isPromotableIntegerType() ||
2461 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2462 return QualType();
2463 }
2464
2465 if (allLTypes) return lhs;
2466 if (allRTypes) return rhs;
2467 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002468 proto->getNumArgs(), lproto->isVariadic(),
2469 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002470 }
2471
2472 if (allLTypes) return lhs;
2473 if (allRTypes) return rhs;
2474 return getFunctionTypeNoProto(retType);
2475}
2476
2477QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002478 // C++ [expr]: If an expression initially has the type "reference to T", the
2479 // type is adjusted to "T" prior to any further analysis, the expression
2480 // designates the object or function denoted by the reference, and the
2481 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002482 // FIXME: C++ shouldn't be going through here! The rules are different
2483 // enough that they should be handled separately.
2484 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002485 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002486 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002487 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002488
Eli Friedman3d815e72008-08-22 00:56:42 +00002489 QualType LHSCan = getCanonicalType(LHS),
2490 RHSCan = getCanonicalType(RHS);
2491
2492 // If two types are identical, they are compatible.
2493 if (LHSCan == RHSCan)
2494 return LHS;
2495
2496 // If the qualifiers are different, the types aren't compatible
2497 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2498 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2499 return QualType();
2500
2501 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2502 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2503
Chris Lattner1adb8832008-01-14 05:45:46 +00002504 // We want to consider the two function types to be the same for these
2505 // comparisons, just force one to the other.
2506 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2507 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002508
2509 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002510 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2511 LHSClass = Type::ConstantArray;
2512 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2513 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002514
Nate Begeman213541a2008-04-18 23:10:10 +00002515 // Canonicalize ExtVector -> Vector.
2516 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2517 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002518
Chris Lattnerb0489812008-04-07 06:38:24 +00002519 // Consider qualified interfaces and interfaces the same.
2520 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2521 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002522
Chris Lattnera36a61f2008-04-07 05:43:21 +00002523 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002524 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002525 // ID is compatible with all qualified id types.
2526 if (LHS->isObjCQualifiedIdType()) {
2527 if (const PointerType *PT = RHS->getAsPointerType()) {
2528 QualType pType = PT->getPointeeType();
2529 if (isObjCIdType(pType))
2530 return LHS;
2531 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2532 // Unfortunately, this API is part of Sema (which we don't have access
2533 // to. Need to refactor. The following check is insufficient, since we
2534 // need to make sure the class implements the protocol.
2535 if (pType->isObjCInterfaceType())
2536 return LHS;
2537 }
2538 }
2539 if (RHS->isObjCQualifiedIdType()) {
2540 if (const PointerType *PT = LHS->getAsPointerType()) {
2541 QualType pType = PT->getPointeeType();
2542 if (isObjCIdType(pType))
2543 return RHS;
2544 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2545 // Unfortunately, this API is part of Sema (which we don't have access
2546 // to. Need to refactor. The following check is insufficient, since we
2547 // need to make sure the class implements the protocol.
2548 if (pType->isObjCInterfaceType())
2549 return RHS;
2550 }
2551 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002552 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2553 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002554 if (const EnumType* ETy = LHS->getAsEnumType()) {
2555 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2556 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002557 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002558 if (const EnumType* ETy = RHS->getAsEnumType()) {
2559 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2560 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002561 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002562
Eli Friedman3d815e72008-08-22 00:56:42 +00002563 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002564 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002565
Steve Naroff4a746782008-01-09 22:43:08 +00002566 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002567 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002568 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002569 {
2570 // Merge two pointer types, while trying to preserve typedef info
2571 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2572 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2573 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2574 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002575 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2576 return LHS;
2577 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2578 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002579 return getPointerType(ResultType);
2580 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002581 case Type::BlockPointer:
2582 {
2583 // Merge two block pointer types, while trying to preserve typedef info
2584 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2585 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2586 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2587 if (ResultType.isNull()) return QualType();
2588 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2589 return LHS;
2590 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2591 return RHS;
2592 return getBlockPointerType(ResultType);
2593 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002594 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002595 {
2596 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2597 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2598 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2599 return QualType();
2600
2601 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2602 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2603 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2604 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002605 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2606 return LHS;
2607 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2608 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002609 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2610 ArrayType::ArraySizeModifier(), 0);
2611 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2612 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002613 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2614 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002615 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2616 return LHS;
2617 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2618 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002619 if (LVAT) {
2620 // FIXME: This isn't correct! But tricky to implement because
2621 // the array's size has to be the size of LHS, but the type
2622 // has to be different.
2623 return LHS;
2624 }
2625 if (RVAT) {
2626 // FIXME: This isn't correct! But tricky to implement because
2627 // the array's size has to be the size of RHS, but the type
2628 // has to be different.
2629 return RHS;
2630 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002631 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2632 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002633 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002634 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002635 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002636 return mergeFunctionTypes(LHS, RHS);
2637 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002638 // FIXME: Why are these compatible?
2639 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2640 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2641 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002642 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002643 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002644 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002645 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002646 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2647 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002648 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002649 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002650 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2651 // for checking assignment/comparison safety
2652 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002653 case Type::ObjCQualifiedId:
2654 // Distinct qualified id's are not compatible.
2655 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002656 default:
2657 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002658 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002659 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002660}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002661
Chris Lattner5426bf62008-04-07 07:01:58 +00002662//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002663// Integer Predicates
2664//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002665
Eli Friedmanad74a752008-06-28 06:23:08 +00002666unsigned ASTContext::getIntWidth(QualType T) {
2667 if (T == BoolTy)
2668 return 1;
2669 // At the moment, only bool has padding bits
2670 return (unsigned)getTypeSize(T);
2671}
2672
2673QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2674 assert(T->isSignedIntegerType() && "Unexpected type");
2675 if (const EnumType* ETy = T->getAsEnumType())
2676 T = ETy->getDecl()->getIntegerType();
2677 const BuiltinType* BTy = T->getAsBuiltinType();
2678 assert (BTy && "Unexpected signed integer type");
2679 switch (BTy->getKind()) {
2680 case BuiltinType::Char_S:
2681 case BuiltinType::SChar:
2682 return UnsignedCharTy;
2683 case BuiltinType::Short:
2684 return UnsignedShortTy;
2685 case BuiltinType::Int:
2686 return UnsignedIntTy;
2687 case BuiltinType::Long:
2688 return UnsignedLongTy;
2689 case BuiltinType::LongLong:
2690 return UnsignedLongLongTy;
2691 default:
2692 assert(0 && "Unexpected signed integer type");
2693 return QualType();
2694 }
2695}
2696
2697
2698//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002699// Serialization Support
2700//===----------------------------------------------------------------------===//
2701
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002702/// Emit - Serialize an ASTContext object to Bitcode.
2703void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002704 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002705 S.EmitRef(SourceMgr);
2706 S.EmitRef(Target);
2707 S.EmitRef(Idents);
2708 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002709
Ted Kremenekfee04522007-10-31 22:44:07 +00002710 // Emit the size of the type vector so that we can reserve that size
2711 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002712 S.EmitInt(Types.size());
2713
Ted Kremenek03ed4402007-11-13 22:02:55 +00002714 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2715 I!=E;++I)
2716 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002717
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002718 S.EmitOwnedPtr(TUDecl);
2719
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002720 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002721}
2722
Ted Kremenek0f84c002007-11-13 00:25:37 +00002723ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002724
2725 // Read the language options.
2726 LangOptions LOpts;
2727 LOpts.Read(D);
2728
Ted Kremenekfee04522007-10-31 22:44:07 +00002729 SourceManager &SM = D.ReadRef<SourceManager>();
2730 TargetInfo &t = D.ReadRef<TargetInfo>();
2731 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2732 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002733
Ted Kremenekfee04522007-10-31 22:44:07 +00002734 unsigned size_reserve = D.ReadInt();
2735
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002736 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2737 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002738
Ted Kremenek03ed4402007-11-13 22:02:55 +00002739 for (unsigned i = 0; i < size_reserve; ++i)
2740 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002741
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002742 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2743
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002744 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002745
2746 return A;
2747}