blob: f3243c25ae71e2c2438b06248f56acf15a827785 [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
263
Chris Lattnera7674d82007-07-13 22:13:22 +0000264/// getTypeSize - Return the size of the specified type, in bits. This method
265/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000266std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000267ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000268 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000269 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000270 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000271 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000272 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000273 case Type::FunctionNoProto:
274 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000275 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000276 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000277 case Type::VariableArray:
278 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000279 case Type::DependentSizedArray:
280 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000281 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000282 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000283
Chris Lattner98be4942008-03-05 18:54:05 +0000284 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000285 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000286 Align = EltInfo.second;
287 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000288 }
Nate Begeman213541a2008-04-18 23:10:10 +0000289 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000290 case Type::Vector: {
291 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000292 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000293 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000294 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000295 // If the alignment is not a power of 2, round up to the next power of 2.
296 // This happens for non-power-of-2 length vectors.
297 // FIXME: this should probably be a target property.
298 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000299 break;
300 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000301
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000302 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000303 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000304 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000305 case BuiltinType::Void:
306 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000307 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000308 Width = Target.getBoolWidth();
309 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000310 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000311 case BuiltinType::Char_S:
312 case BuiltinType::Char_U:
313 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000314 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000315 Width = Target.getCharWidth();
316 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000317 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000318 case BuiltinType::WChar:
319 Width = Target.getWCharWidth();
320 Align = Target.getWCharAlign();
321 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000322 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000323 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000324 Width = Target.getShortWidth();
325 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000326 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000327 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000329 Width = Target.getIntWidth();
330 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000331 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000332 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000334 Width = Target.getLongWidth();
335 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000337 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000338 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000339 Width = Target.getLongLongWidth();
340 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000341 break;
342 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000343 Width = Target.getFloatWidth();
344 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000345 break;
346 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000347 Width = Target.getDoubleWidth();
348 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000349 break;
350 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000351 Width = Target.getLongDoubleWidth();
352 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000353 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000354 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000355 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000356 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000357 // FIXME: Pointers into different addr spaces could have different sizes and
358 // alignment requirements: getPointerInfo should take an AddrSpace.
359 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000361 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000362 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000363 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000364 case Type::BlockPointer: {
365 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
366 Width = Target.getPointerWidth(AS);
367 Align = Target.getPointerAlign(AS);
368 break;
369 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000370 case Type::Pointer: {
371 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000372 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000373 Align = Target.getPointerAlign(AS);
374 break;
375 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000376 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000377 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000378 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000379 // FIXME: This is wrong for struct layout: a reference in a struct has
380 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000381 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000382 case Type::MemberPointer: {
383 // Note that this is not only platform- but also ABI-dependent. We follow
384 // the GCC ABI, where pointers to data are one pointer large, pointers to
385 // functions two pointers. But if we want to support ABI compatibility with
386 // other compilers too, we need to delegate this completely to TargetInfo.
387 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
388 unsigned AS = Pointee.getAddressSpace();
389 Width = Target.getPointerWidth(AS);
390 if (Pointee->isFunctionType())
391 Width *= 2;
392 Align = Target.getPointerAlign(AS);
393 // GCC aligns at single pointer width.
394 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000395 case Type::Complex: {
396 // Complex types have the same alignment as their elements, but twice the
397 // size.
398 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000399 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000400 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000401 Align = EltInfo.second;
402 break;
403 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000404 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000405 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000406 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
407 Width = Layout.getSize();
408 Align = Layout.getAlignment();
409 break;
410 }
Chris Lattner71763312008-04-06 22:05:18 +0000411 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000412 const TagType *TT = cast<TagType>(T);
413
414 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000415 Width = 1;
416 Align = 1;
417 break;
418 }
419
Daniel Dunbar1d751182008-11-08 05:48:37 +0000420 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000421 return getTypeInfo(ET->getDecl()->getIntegerType());
422
Daniel Dunbar1d751182008-11-08 05:48:37 +0000423 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000424 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
425 Width = Layout.getSize();
426 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000427 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000428 }
Chris Lattner71763312008-04-06 22:05:18 +0000429 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000430
Chris Lattner464175b2007-07-18 17:52:12 +0000431 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000432 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000433}
434
Devang Patel8b277042008-06-04 21:22:16 +0000435/// LayoutField - Field layout.
436void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000437 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000438 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000439 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000440 uint64_t FieldOffset = IsUnion ? 0 : Size;
441 uint64_t FieldSize;
442 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000443
444 // FIXME: Should this override struct packing? Probably we want to
445 // take the minimum?
446 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
447 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000448
449 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
450 // TODO: Need to check this algorithm on other targets!
451 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000452 FieldSize =
453 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000454
455 std::pair<uint64_t, unsigned> FieldInfo =
456 Context.getTypeInfo(FD->getType());
457 uint64_t TypeSize = FieldInfo.first;
458
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000459 // Determine the alignment of this bitfield. The packing
460 // attributes define a maximum and the alignment attribute defines
461 // a minimum.
462 // FIXME: What is the right behavior when the specified alignment
463 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000464 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000465 if (FieldPacking)
466 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000467 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
468 FieldAlign = std::max(FieldAlign, AA->getAlignment());
469
470 // Check if we need to add padding to give the field the correct
471 // alignment.
472 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
473 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
474
475 // Padding members don't affect overall alignment
476 if (!FD->getIdentifier())
477 FieldAlign = 1;
478 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000479 if (FD->getType()->isIncompleteArrayType()) {
480 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000481 // query getTypeInfo about these, so we figure it out here.
482 // Flexible array members don't have any size, but they
483 // have to be aligned appropriately for their element type.
484 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000485 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000486 FieldAlign = Context.getTypeAlign(ATy->getElementType());
487 } else {
488 std::pair<uint64_t, unsigned> FieldInfo =
489 Context.getTypeInfo(FD->getType());
490 FieldSize = FieldInfo.first;
491 FieldAlign = FieldInfo.second;
492 }
493
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000494 // Determine the alignment of this bitfield. The packing
495 // attributes define a maximum and the alignment attribute defines
496 // a minimum. Additionally, the packing alignment must be at least
497 // a byte for non-bitfields.
498 //
499 // FIXME: What is the right behavior when the specified alignment
500 // is smaller than the specified packing?
501 if (FieldPacking)
502 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000503 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
504 FieldAlign = std::max(FieldAlign, AA->getAlignment());
505
506 // Round up the current record size to the field's alignment boundary.
507 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
508 }
509
510 // Place this field at the current location.
511 FieldOffsets[FieldNo] = FieldOffset;
512
513 // Reserve space for this field.
514 if (IsUnion) {
515 Size = std::max(Size, FieldSize);
516 } else {
517 Size = FieldOffset + FieldSize;
518 }
519
520 // Remember max struct/class alignment.
521 Alignment = std::max(Alignment, FieldAlign);
522}
523
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000524static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
525 std::vector<FieldDecl*> &Fields) {
526 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
527 if (SuperClass)
528 CollectObjCIvars(SuperClass, Fields);
529 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
530 E = OI->ivar_end(); I != E; ++I) {
531 ObjCIvarDecl *IVDecl = (*I);
532 if (!IVDecl->isInvalidDecl())
533 Fields.push_back(cast<FieldDecl>(IVDecl));
534 }
535}
536
537/// addRecordToClass - produces record info. for the class for its
538/// ivars and all those inherited.
539///
540const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
541{
542 const RecordDecl *&RD = ASTRecordForInterface[D];
543 if (RD)
544 return RD;
545 std::vector<FieldDecl*> RecFields;
546 CollectObjCIvars(D, RecFields);
547 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
548 D->getLocation(),
549 D->getIdentifier());
550 /// FIXME! Can do collection of ivars and adding to the record while
551 /// doing it.
552 for (unsigned int i = 0; i != RecFields.size(); i++) {
553 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
554 RecFields[i]->getLocation(),
555 RecFields[i]->getIdentifier(),
556 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000557 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000558 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000559 }
560 NewRD->completeDefinition(*this);
561 RD = NewRD;
562 return RD;
563}
Devang Patel44a3dde2008-06-04 21:54:36 +0000564
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000565/// setFieldDecl - maps a field for the given Ivar reference node.
566//
567void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
568 const ObjCIvarDecl *Ivar,
569 const ObjCIvarRefExpr *MRef) {
570 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
571 lookupFieldDeclForIvar(*this, Ivar);
572 ASTFieldForIvarRef[MRef] = FD;
573}
574
Chris Lattner61710852008-10-05 17:34:18 +0000575/// getASTObjcInterfaceLayout - Get or compute information about the layout of
576/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000577/// position information.
578const ASTRecordLayout &
579ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
580 // Look up this layout, if already laid out, return what we have.
581 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
582 if (Entry) return *Entry;
583
584 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
585 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000586 ASTRecordLayout *NewEntry = NULL;
587 unsigned FieldCount = D->ivar_size();
588 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
589 FieldCount++;
590 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
591 unsigned Alignment = SL.getAlignment();
592 uint64_t Size = SL.getSize();
593 NewEntry = new ASTRecordLayout(Size, Alignment);
594 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000595 // Super class is at the beginning of the layout.
596 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000597 } else {
598 NewEntry = new ASTRecordLayout();
599 NewEntry->InitializeLayout(FieldCount);
600 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000601 Entry = NewEntry;
602
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000603 unsigned StructPacking = 0;
604 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
605 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000606
607 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
608 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
609 AA->getAlignment()));
610
611 // Layout each ivar sequentially.
612 unsigned i = 0;
613 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
614 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
615 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000616 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000617 }
618
619 // Finally, round the size of the total struct up to the alignment of the
620 // struct itself.
621 NewEntry->FinalizeLayout();
622 return *NewEntry;
623}
624
Devang Patel88a981b2007-11-01 19:11:01 +0000625/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000626/// specified record (struct/union/class), which indicates its size and field
627/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000628const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000629 D = D->getDefinition(*this);
630 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000631
Chris Lattner464175b2007-07-18 17:52:12 +0000632 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000633 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000634 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000635
Devang Patel88a981b2007-11-01 19:11:01 +0000636 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
637 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
638 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000639 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000640
Douglas Gregore267ff32008-12-11 20:41:00 +0000641 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000642 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000643 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000644
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000645 unsigned StructPacking = 0;
646 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
647 StructPacking = PA->getAlignment();
648
Eli Friedman4bd998b2008-05-30 09:31:38 +0000649 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000650 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
651 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000652
Eli Friedman4bd998b2008-05-30 09:31:38 +0000653 // Layout each field, for now, just sequentially, respecting alignment. In
654 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000655 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000656 for (RecordDecl::field_iterator Field = D->field_begin(),
657 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000658 Field != FieldEnd; (void)++Field, ++FieldIdx)
659 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000660
661 // Finally, round the size of the total struct up to the alignment of the
662 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000663 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000664 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000665}
666
Chris Lattnera7674d82007-07-13 22:13:22 +0000667//===----------------------------------------------------------------------===//
668// Type creation/memoization methods
669//===----------------------------------------------------------------------===//
670
Christopher Lambebb97e92008-02-04 02:31:56 +0000671QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000672 QualType CanT = getCanonicalType(T);
673 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000674 return T;
675
676 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
677 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000678 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000679 "Type is already address space qualified");
680
681 // Check if we've already instantiated an address space qual'd type of this
682 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000683 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000684 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000685 void *InsertPos = 0;
686 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
687 return QualType(ASQy, 0);
688
689 // If the base type isn't canonical, this won't be a canonical type either,
690 // so fill in the canonical type field.
691 QualType Canonical;
692 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000693 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000694
695 // Get the new insert position for the node we care about.
696 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000697 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000698 }
Steve Naroff506010b2009-01-19 22:45:10 +0000699 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000700 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000701 ASQualTypes.InsertNode(New, InsertPos);
702 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000703 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000704}
705
Chris Lattnera7674d82007-07-13 22:13:22 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707/// getComplexType - Return the uniqued reference to the type for a complex
708/// number with the specified element type.
709QualType ASTContext::getComplexType(QualType T) {
710 // Unique pointers, to guarantee there is only one pointer of a particular
711 // structure.
712 llvm::FoldingSetNodeID ID;
713 ComplexType::Profile(ID, T);
714
715 void *InsertPos = 0;
716 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
717 return QualType(CT, 0);
718
719 // If the pointee type isn't canonical, this won't be a canonical type either,
720 // so fill in the canonical type field.
721 QualType Canonical;
722 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000723 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000724
725 // Get the new insert position for the node we care about.
726 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000727 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
Steve Naroff506010b2009-01-19 22:45:10 +0000729 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000730 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 Types.push_back(New);
732 ComplexTypes.InsertNode(New, InsertPos);
733 return QualType(New, 0);
734}
735
736
737/// getPointerType - Return the uniqued reference to the type for a pointer to
738/// the specified type.
739QualType ASTContext::getPointerType(QualType T) {
740 // Unique pointers, to guarantee there is only one pointer of a particular
741 // structure.
742 llvm::FoldingSetNodeID ID;
743 PointerType::Profile(ID, T);
744
745 void *InsertPos = 0;
746 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
747 return QualType(PT, 0);
748
749 // If the pointee type isn't canonical, this won't be a canonical type either,
750 // so fill in the canonical type field.
751 QualType Canonical;
752 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000753 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000754
755 // Get the new insert position for the node we care about.
756 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000757 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 }
Steve Naroff506010b2009-01-19 22:45:10 +0000759 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000760 PointerType *New = new (Mem) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 Types.push_back(New);
762 PointerTypes.InsertNode(New, InsertPos);
763 return QualType(New, 0);
764}
765
Steve Naroff5618bd42008-08-27 16:04:49 +0000766/// getBlockPointerType - Return the uniqued reference to the type for
767/// a pointer to the specified block.
768QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000769 assert(T->isFunctionType() && "block of function types only");
770 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000771 // structure.
772 llvm::FoldingSetNodeID ID;
773 BlockPointerType::Profile(ID, T);
774
775 void *InsertPos = 0;
776 if (BlockPointerType *PT =
777 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
778 return QualType(PT, 0);
779
Steve Naroff296e8d52008-08-28 19:20:44 +0000780 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000781 // type either so fill in the canonical type field.
782 QualType Canonical;
783 if (!T->isCanonical()) {
784 Canonical = getBlockPointerType(getCanonicalType(T));
785
786 // Get the new insert position for the node we care about.
787 BlockPointerType *NewIP =
788 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000789 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000790 }
Steve Naroff506010b2009-01-19 22:45:10 +0000791 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000792 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000793 Types.push_back(New);
794 BlockPointerTypes.InsertNode(New, InsertPos);
795 return QualType(New, 0);
796}
797
Reid Spencer5f016e22007-07-11 17:01:13 +0000798/// getReferenceType - Return the uniqued reference to the type for a reference
799/// to the specified type.
800QualType ASTContext::getReferenceType(QualType T) {
801 // Unique pointers, to guarantee there is only one pointer of a particular
802 // structure.
803 llvm::FoldingSetNodeID ID;
804 ReferenceType::Profile(ID, T);
805
806 void *InsertPos = 0;
807 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
808 return QualType(RT, 0);
809
810 // If the referencee type isn't canonical, this won't be a canonical type
811 // either, so fill in the canonical type field.
812 QualType Canonical;
813 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000814 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000815
816 // Get the new insert position for the node we care about.
817 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000818 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 }
820
Steve Naroff506010b2009-01-19 22:45:10 +0000821 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000822 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 Types.push_back(New);
824 ReferenceTypes.InsertNode(New, InsertPos);
825 return QualType(New, 0);
826}
827
Sebastian Redlf30208a2009-01-24 21:16:55 +0000828/// getMemberPointerType - Return the uniqued reference to the type for a
829/// member pointer to the specified type, in the specified class.
830QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
831{
832 // Unique pointers, to guarantee there is only one pointer of a particular
833 // structure.
834 llvm::FoldingSetNodeID ID;
835 MemberPointerType::Profile(ID, T, Cls);
836
837 void *InsertPos = 0;
838 if (MemberPointerType *PT =
839 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
840 return QualType(PT, 0);
841
842 // If the pointee or class type isn't canonical, this won't be a canonical
843 // type either, so fill in the canonical type field.
844 QualType Canonical;
845 if (!T->isCanonical()) {
846 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
847
848 // Get the new insert position for the node we care about.
849 MemberPointerType *NewIP =
850 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
851 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
852 }
853 void *Mem = Allocator.Allocate(sizeof(MemberPointerType), 8);
854 MemberPointerType *New = new (Mem) MemberPointerType(T, Cls, Canonical);
855 Types.push_back(New);
856 MemberPointerTypes.InsertNode(New, InsertPos);
857 return QualType(New, 0);
858}
859
Steve Narofffb22d962007-08-30 01:06:46 +0000860/// getConstantArrayType - Return the unique reference to the type for an
861/// array of the specified element type.
862QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000863 const llvm::APInt &ArySize,
864 ArrayType::ArraySizeModifier ASM,
865 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000867 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868
869 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000870 if (ConstantArrayType *ATP =
871 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 return QualType(ATP, 0);
873
874 // If the element type isn't canonical, this won't be a canonical type either,
875 // so fill in the canonical type field.
876 QualType Canonical;
877 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000878 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000879 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000881 ConstantArrayType *NewIP =
882 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000883 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
885
Steve Naroff506010b2009-01-19 22:45:10 +0000886 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000887 ConstantArrayType *New =
888 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000889 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 Types.push_back(New);
891 return QualType(New, 0);
892}
893
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000894/// getVariableArrayType - Returns a non-unique reference to the type for a
895/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000896QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
897 ArrayType::ArraySizeModifier ASM,
898 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000899 // Since we don't unique expressions, it isn't possible to unique VLA's
900 // that have an expression provided for their size.
901
Steve Naroff506010b2009-01-19 22:45:10 +0000902 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000903 VariableArrayType *New =
904 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000905
906 VariableArrayTypes.push_back(New);
907 Types.push_back(New);
908 return QualType(New, 0);
909}
910
Douglas Gregor898574e2008-12-05 23:32:09 +0000911/// getDependentSizedArrayType - Returns a non-unique reference to
912/// the type for a dependently-sized array of the specified element
913/// type. FIXME: We will need these to be uniqued, or at least
914/// comparable, at some point.
915QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
916 ArrayType::ArraySizeModifier ASM,
917 unsigned EltTypeQuals) {
918 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
919 "Size must be type- or value-dependent!");
920
921 // Since we don't unique expressions, it isn't possible to unique
922 // dependently-sized array types.
923
Steve Naroff506010b2009-01-19 22:45:10 +0000924 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000925 DependentSizedArrayType *New =
926 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
927 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000928
929 DependentSizedArrayTypes.push_back(New);
930 Types.push_back(New);
931 return QualType(New, 0);
932}
933
Eli Friedmanc5773c42008-02-15 18:16:39 +0000934QualType ASTContext::getIncompleteArrayType(QualType EltTy,
935 ArrayType::ArraySizeModifier ASM,
936 unsigned EltTypeQuals) {
937 llvm::FoldingSetNodeID ID;
938 IncompleteArrayType::Profile(ID, EltTy);
939
940 void *InsertPos = 0;
941 if (IncompleteArrayType *ATP =
942 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
943 return QualType(ATP, 0);
944
945 // If the element type isn't canonical, this won't be a canonical type
946 // either, so fill in the canonical type field.
947 QualType Canonical;
948
949 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000950 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000951 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000952
953 // Get the new insert position for the node we care about.
954 IncompleteArrayType *NewIP =
955 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000957 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000958
Steve Naroff506010b2009-01-19 22:45:10 +0000959 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000960 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
961 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000962
963 IncompleteArrayTypes.InsertNode(New, InsertPos);
964 Types.push_back(New);
965 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000966}
967
Steve Naroff73322922007-07-18 18:00:27 +0000968/// getVectorType - Return the unique reference to a vector type of
969/// the specified element type and size. VectorType must be a built-in type.
970QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 BuiltinType *baseType;
972
Chris Lattnerf52ab252008-04-06 22:59:24 +0000973 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000974 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000975
976 // Check if we've already instantiated a vector of this type.
977 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000978 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 void *InsertPos = 0;
980 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
981 return QualType(VTP, 0);
982
983 // If the element type isn't canonical, this won't be a canonical type either,
984 // so fill in the canonical type field.
985 QualType Canonical;
986 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000987 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000988
989 // Get the new insert position for the node we care about.
990 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000991 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 }
Steve Naroff506010b2009-01-19 22:45:10 +0000993 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000994 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 VectorTypes.InsertNode(New, InsertPos);
996 Types.push_back(New);
997 return QualType(New, 0);
998}
999
Nate Begeman213541a2008-04-18 23:10:10 +00001000/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001001/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001002QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001003 BuiltinType *baseType;
1004
Chris Lattnerf52ab252008-04-06 22:59:24 +00001005 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001006 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001007
1008 // Check if we've already instantiated a vector of this type.
1009 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001010 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001011 void *InsertPos = 0;
1012 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1013 return QualType(VTP, 0);
1014
1015 // If the element type isn't canonical, this won't be a canonical type either,
1016 // so fill in the canonical type field.
1017 QualType Canonical;
1018 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001019 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001020
1021 // Get the new insert position for the node we care about.
1022 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001023 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001024 }
Steve Naroff506010b2009-01-19 22:45:10 +00001025 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001026 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001027 VectorTypes.InsertNode(New, InsertPos);
1028 Types.push_back(New);
1029 return QualType(New, 0);
1030}
1031
Reid Spencer5f016e22007-07-11 17:01:13 +00001032/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1033///
1034QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1035 // Unique functions, to guarantee there is only one function of a particular
1036 // structure.
1037 llvm::FoldingSetNodeID ID;
1038 FunctionTypeNoProto::Profile(ID, ResultTy);
1039
1040 void *InsertPos = 0;
1041 if (FunctionTypeNoProto *FT =
1042 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1043 return QualType(FT, 0);
1044
1045 QualType Canonical;
1046 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001047 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001048
1049 // Get the new insert position for the node we care about.
1050 FunctionTypeNoProto *NewIP =
1051 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001052 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 }
1054
Douglas Gregorb17e3b02009-01-20 21:02:13 +00001055 void *Mem = Allocator.Allocate(sizeof(FunctionTypeNoProto), 8);
1056 FunctionTypeNoProto *New = new (Mem) FunctionTypeNoProto(ResultTy, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001058 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 return QualType(New, 0);
1060}
1061
1062/// getFunctionType - Return a normal function type with a typed argument
1063/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001064QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001065 unsigned NumArgs, bool isVariadic,
1066 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 // Unique functions, to guarantee there is only one function of a particular
1068 // structure.
1069 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001070 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1071 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072
1073 void *InsertPos = 0;
1074 if (FunctionTypeProto *FTP =
1075 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1076 return QualType(FTP, 0);
1077
1078 // Determine whether the type being created is already canonical or not.
1079 bool isCanonical = ResultTy->isCanonical();
1080 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1081 if (!ArgArray[i]->isCanonical())
1082 isCanonical = false;
1083
1084 // If this type isn't canonical, get the canonical version of it.
1085 QualType Canonical;
1086 if (!isCanonical) {
1087 llvm::SmallVector<QualType, 16> CanonicalArgs;
1088 CanonicalArgs.reserve(NumArgs);
1089 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001090 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001091
Chris Lattnerf52ab252008-04-06 22:59:24 +00001092 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001094 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001095
1096 // Get the new insert position for the node we care about.
1097 FunctionTypeProto *NewIP =
1098 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001099 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
1101
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001102 // FunctionTypeProto objects are allocated with extra bytes after them
1103 // for a variable size array (for parameter types) at the end of them.
1104 // FIXME: Can we do better than forcing a 16-byte alignment?
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 FunctionTypeProto *FTP =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001106 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1107 NumArgs*sizeof(QualType), 16);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001109 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 Types.push_back(FTP);
1111 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1112 return QualType(FTP, 0);
1113}
1114
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001115/// getTypeDeclType - Return the unique reference to the type for the
1116/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001117QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001118 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001119 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1120
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001121 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001122 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001123 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1124 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001125 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001126 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001127
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001128 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001129 if (PrevDecl)
1130 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1131 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001132 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001133 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1134 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001135 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001136 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001137 if (PrevDecl)
1138 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1139 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001140 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001141 Decl->TypeForDecl = new (Mem) RecordType(Record);
1142 }
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001143 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001144 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1145 if (PrevDecl)
1146 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1147 else {
Steve Naroff506010b2009-01-19 22:45:10 +00001148 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001149 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1150 }
1151 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001152 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001153 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001154
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001155 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001156 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001157}
1158
Reid Spencer5f016e22007-07-11 17:01:13 +00001159/// getTypedefType - Return the unique reference to the type for the
1160/// specified typename decl.
1161QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1162 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1163
Chris Lattnerf52ab252008-04-06 22:59:24 +00001164 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroff506010b2009-01-19 22:45:10 +00001165 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001166 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 Types.push_back(Decl->TypeForDecl);
1168 return QualType(Decl->TypeForDecl, 0);
1169}
1170
Douglas Gregor72c3f312008-12-05 18:15:24 +00001171/// getTemplateTypeParmType - Return the unique reference to the type
1172/// for the specified template type parameter declaration.
1173QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1174 if (!Decl->TypeForDecl) {
Steve Naroff506010b2009-01-19 22:45:10 +00001175 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001176 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001177 Types.push_back(Decl->TypeForDecl);
1178 }
1179 return QualType(Decl->TypeForDecl, 0);
1180}
1181
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001182/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001183/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001184QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001185 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1186
Steve Naroff506010b2009-01-19 22:45:10 +00001187 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001188 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001189 Types.push_back(Decl->TypeForDecl);
1190 return QualType(Decl->TypeForDecl, 0);
1191}
1192
Chris Lattner88cb27a2008-04-07 04:56:42 +00001193/// CmpProtocolNames - Comparison predicate for sorting protocols
1194/// alphabetically.
1195static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1196 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001197 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001198}
1199
1200static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1201 unsigned &NumProtocols) {
1202 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1203
1204 // Sort protocols, keyed by name.
1205 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1206
1207 // Remove duplicates.
1208 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1209 NumProtocols = ProtocolsEnd-Protocols;
1210}
1211
1212
Chris Lattner065f0d72008-04-07 04:44:08 +00001213/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1214/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001215QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1216 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001217 // Sort the protocol list alphabetically to canonicalize it.
1218 SortAndUniqueProtocols(Protocols, NumProtocols);
1219
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001220 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001221 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001222
1223 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001224 if (ObjCQualifiedInterfaceType *QT =
1225 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001226 return QualType(QT, 0);
1227
1228 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001229 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001230 ObjCQualifiedInterfaceType *QType =
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001231 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1232
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001233 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001235 return QualType(QType, 0);
1236}
1237
Chris Lattner88cb27a2008-04-07 04:56:42 +00001238/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1239/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001240QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001241 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001242 // Sort the protocol list alphabetically to canonicalize it.
1243 SortAndUniqueProtocols(Protocols, NumProtocols);
1244
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001245 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001246 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001247
1248 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001249 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001250 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001251 return QualType(QT, 0);
1252
1253 // No Match;
Steve Naroff506010b2009-01-19 22:45:10 +00001254 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001255 ObjCQualifiedIdType *QType =
1256 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001257 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001258 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001259 return QualType(QType, 0);
1260}
1261
Steve Naroff9752f252007-08-01 18:02:17 +00001262/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1263/// TypeOfExpr AST's (since expression's are never shared). For example,
1264/// multiple declarations that refer to "typeof(x)" all contain different
1265/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1266/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001267QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001268 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregorb17e3b02009-01-20 21:02:13 +00001269 void *Mem = Allocator.Allocate(sizeof(TypeOfExpr), 8);
1270 TypeOfExpr *toe = new (Mem) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001271 Types.push_back(toe);
1272 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001273}
1274
Steve Naroff9752f252007-08-01 18:02:17 +00001275/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1276/// TypeOfType AST's. The only motivation to unique these nodes would be
1277/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1278/// an issue. This doesn't effect the type checker, since it operates
1279/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001280QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001281 QualType Canonical = getCanonicalType(tofType);
Steve Naroff506010b2009-01-19 22:45:10 +00001282 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001283 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001284 Types.push_back(tot);
1285 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001286}
1287
Reid Spencer5f016e22007-07-11 17:01:13 +00001288/// getTagDeclType - Return the unique reference to the type for the
1289/// specified TagDecl (struct/union/class/enum) decl.
1290QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001291 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001292 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001293}
1294
1295/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1296/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1297/// needs to agree with the definition in <stddef.h>.
1298QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001299 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001300}
1301
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001302/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001303/// width of characters in wide strings, The value is target dependent and
1304/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001305QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001306 if (LangOpts.CPlusPlus)
1307 return WCharTy;
1308
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001309 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1310 // wide-character type?
1311 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001312}
1313
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001314/// getSignedWCharType - Return the type of "signed wchar_t".
1315/// Used when in C++, as a GCC extension.
1316QualType ASTContext::getSignedWCharType() const {
1317 // FIXME: derive from "Target" ?
1318 return WCharTy;
1319}
1320
1321/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1322/// Used when in C++, as a GCC extension.
1323QualType ASTContext::getUnsignedWCharType() const {
1324 // FIXME: derive from "Target" ?
1325 return UnsignedIntTy;
1326}
1327
Chris Lattner8b9023b2007-07-13 03:05:23 +00001328/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1329/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1330QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001331 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001332}
1333
Chris Lattnere6327742008-04-02 05:18:44 +00001334//===----------------------------------------------------------------------===//
1335// Type Operators
1336//===----------------------------------------------------------------------===//
1337
Chris Lattner77c96472008-04-06 22:41:35 +00001338/// getCanonicalType - Return the canonical (structural) type corresponding to
1339/// the specified potentially non-canonical type. The non-canonical version
1340/// of a type may have many "decorated" versions of types. Decorators can
1341/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1342/// to be free of any of these, allowing two canonical types to be compared
1343/// for exact equality with a simple pointer comparison.
1344QualType ASTContext::getCanonicalType(QualType T) {
1345 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001346
1347 // If the result has type qualifiers, make sure to canonicalize them as well.
1348 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1349 if (TypeQuals == 0) return CanType;
1350
1351 // If the type qualifiers are on an array type, get the canonical type of the
1352 // array with the qualifiers applied to the element type.
1353 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1354 if (!AT)
1355 return CanType.getQualifiedType(TypeQuals);
1356
1357 // Get the canonical version of the element with the extra qualifiers on it.
1358 // This can recursively sink qualifiers through multiple levels of arrays.
1359 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1360 NewEltTy = getCanonicalType(NewEltTy);
1361
1362 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1363 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1364 CAT->getIndexTypeQualifier());
1365 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1366 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1367 IAT->getIndexTypeQualifier());
1368
Douglas Gregor898574e2008-12-05 23:32:09 +00001369 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1370 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1371 DSAT->getSizeModifier(),
1372 DSAT->getIndexTypeQualifier());
1373
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001374 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1375 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1376 VAT->getSizeModifier(),
1377 VAT->getIndexTypeQualifier());
1378}
1379
1380
1381const ArrayType *ASTContext::getAsArrayType(QualType T) {
1382 // Handle the non-qualified case efficiently.
1383 if (T.getCVRQualifiers() == 0) {
1384 // Handle the common positive case fast.
1385 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1386 return AT;
1387 }
1388
1389 // Handle the common negative case fast, ignoring CVR qualifiers.
1390 QualType CType = T->getCanonicalTypeInternal();
1391
1392 // Make sure to look through type qualifiers (like ASQuals) for the negative
1393 // test.
1394 if (!isa<ArrayType>(CType) &&
1395 !isa<ArrayType>(CType.getUnqualifiedType()))
1396 return 0;
1397
1398 // Apply any CVR qualifiers from the array type to the element type. This
1399 // implements C99 6.7.3p8: "If the specification of an array type includes
1400 // any type qualifiers, the element type is so qualified, not the array type."
1401
1402 // If we get here, we either have type qualifiers on the type, or we have
1403 // sugar such as a typedef in the way. If we have type qualifiers on the type
1404 // we must propagate them down into the elemeng type.
1405 unsigned CVRQuals = T.getCVRQualifiers();
1406 unsigned AddrSpace = 0;
1407 Type *Ty = T.getTypePtr();
1408
1409 // Rip through ASQualType's and typedefs to get to a concrete type.
1410 while (1) {
1411 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1412 AddrSpace = ASQT->getAddressSpace();
1413 Ty = ASQT->getBaseType();
1414 } else {
1415 T = Ty->getDesugaredType();
1416 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1417 break;
1418 CVRQuals |= T.getCVRQualifiers();
1419 Ty = T.getTypePtr();
1420 }
1421 }
1422
1423 // If we have a simple case, just return now.
1424 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1425 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1426 return ATy;
1427
1428 // Otherwise, we have an array and we have qualifiers on it. Push the
1429 // qualifiers into the array element type and return a new array type.
1430 // Get the canonical version of the element with the extra qualifiers on it.
1431 // This can recursively sink qualifiers through multiple levels of arrays.
1432 QualType NewEltTy = ATy->getElementType();
1433 if (AddrSpace)
1434 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1435 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1436
1437 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1438 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1439 CAT->getSizeModifier(),
1440 CAT->getIndexTypeQualifier()));
1441 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1442 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1443 IAT->getSizeModifier(),
1444 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001445
Douglas Gregor898574e2008-12-05 23:32:09 +00001446 if (const DependentSizedArrayType *DSAT
1447 = dyn_cast<DependentSizedArrayType>(ATy))
1448 return cast<ArrayType>(
1449 getDependentSizedArrayType(NewEltTy,
1450 DSAT->getSizeExpr(),
1451 DSAT->getSizeModifier(),
1452 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001453
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001454 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1455 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1456 VAT->getSizeModifier(),
1457 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001458}
1459
1460
Chris Lattnere6327742008-04-02 05:18:44 +00001461/// getArrayDecayedType - Return the properly qualified result of decaying the
1462/// specified array type to a pointer. This operation is non-trivial when
1463/// handling typedefs etc. The canonical type of "T" must be an array type,
1464/// this returns a pointer to a properly qualified element of the array.
1465///
1466/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1467QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001468 // Get the element type with 'getAsArrayType' so that we don't lose any
1469 // typedefs in the element type of the array. This also handles propagation
1470 // of type qualifiers from the array type into the element type if present
1471 // (C99 6.7.3p8).
1472 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1473 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001474
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001475 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001476
1477 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001478 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001479}
1480
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001481QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001482 QualType ElemTy = VAT->getElementType();
1483
1484 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1485 return getBaseElementType(VAT);
1486
1487 return ElemTy;
1488}
1489
Reid Spencer5f016e22007-07-11 17:01:13 +00001490/// getFloatingRank - Return a relative rank for floating point types.
1491/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001492static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001493 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001495
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001496 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001497 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001498 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 case BuiltinType::Float: return FloatRank;
1500 case BuiltinType::Double: return DoubleRank;
1501 case BuiltinType::LongDouble: return LongDoubleRank;
1502 }
1503}
1504
Steve Naroff716c7302007-08-27 01:41:48 +00001505/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1506/// point or a complex type (based on typeDomain/typeSize).
1507/// 'typeDomain' is a real floating point or complex type.
1508/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001509QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1510 QualType Domain) const {
1511 FloatingRank EltRank = getFloatingRank(Size);
1512 if (Domain->isComplexType()) {
1513 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001514 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001515 case FloatRank: return FloatComplexTy;
1516 case DoubleRank: return DoubleComplexTy;
1517 case LongDoubleRank: return LongDoubleComplexTy;
1518 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 }
Chris Lattner1361b112008-04-06 23:58:54 +00001520
1521 assert(Domain->isRealFloatingType() && "Unknown domain!");
1522 switch (EltRank) {
1523 default: assert(0 && "getFloatingRank(): illegal value for rank");
1524 case FloatRank: return FloatTy;
1525 case DoubleRank: return DoubleTy;
1526 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001527 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001528}
1529
Chris Lattner7cfeb082008-04-06 23:55:33 +00001530/// getFloatingTypeOrder - Compare the rank of the two specified floating
1531/// point types, ignoring the domain of the type (i.e. 'double' ==
1532/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1533/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001534int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1535 FloatingRank LHSR = getFloatingRank(LHS);
1536 FloatingRank RHSR = getFloatingRank(RHS);
1537
1538 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001539 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001540 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001541 return 1;
1542 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001543}
1544
Chris Lattnerf52ab252008-04-06 22:59:24 +00001545/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1546/// routine will assert if passed a built-in type that isn't an integer or enum,
1547/// or if it is not canonicalized.
1548static unsigned getIntegerRank(Type *T) {
1549 assert(T->isCanonical() && "T should be canonicalized");
1550 if (isa<EnumType>(T))
1551 return 4;
1552
1553 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001554 default: assert(0 && "getIntegerRank(): not a built-in integer");
1555 case BuiltinType::Bool:
1556 return 1;
1557 case BuiltinType::Char_S:
1558 case BuiltinType::Char_U:
1559 case BuiltinType::SChar:
1560 case BuiltinType::UChar:
1561 return 2;
1562 case BuiltinType::Short:
1563 case BuiltinType::UShort:
1564 return 3;
1565 case BuiltinType::Int:
1566 case BuiltinType::UInt:
1567 return 4;
1568 case BuiltinType::Long:
1569 case BuiltinType::ULong:
1570 return 5;
1571 case BuiltinType::LongLong:
1572 case BuiltinType::ULongLong:
1573 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001574 }
1575}
1576
Chris Lattner7cfeb082008-04-06 23:55:33 +00001577/// getIntegerTypeOrder - Returns the highest ranked integer type:
1578/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1579/// LHS < RHS, return -1.
1580int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001581 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1582 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001583 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001584
Chris Lattnerf52ab252008-04-06 22:59:24 +00001585 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1586 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001587
Chris Lattner7cfeb082008-04-06 23:55:33 +00001588 unsigned LHSRank = getIntegerRank(LHSC);
1589 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001590
Chris Lattner7cfeb082008-04-06 23:55:33 +00001591 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1592 if (LHSRank == RHSRank) return 0;
1593 return LHSRank > RHSRank ? 1 : -1;
1594 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001595
Chris Lattner7cfeb082008-04-06 23:55:33 +00001596 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1597 if (LHSUnsigned) {
1598 // If the unsigned [LHS] type is larger, return it.
1599 if (LHSRank >= RHSRank)
1600 return 1;
1601
1602 // If the signed type can represent all values of the unsigned type, it
1603 // wins. Because we are dealing with 2's complement and types that are
1604 // powers of two larger than each other, this is always safe.
1605 return -1;
1606 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001607
Chris Lattner7cfeb082008-04-06 23:55:33 +00001608 // If the unsigned [RHS] type is larger, return it.
1609 if (RHSRank >= LHSRank)
1610 return -1;
1611
1612 // If the signed type can represent all values of the unsigned type, it
1613 // wins. Because we are dealing with 2's complement and types that are
1614 // powers of two larger than each other, this is always safe.
1615 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001616}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001617
1618// getCFConstantStringType - Return the type used for constant CFStrings.
1619QualType ASTContext::getCFConstantStringType() {
1620 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001621 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001622 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001623 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001624 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001625
1626 // const int *isa;
1627 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001628 // int flags;
1629 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001630 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001631 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001632 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001633 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001634
Anders Carlsson71993dd2007-08-17 05:31:46 +00001635 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001636 for (unsigned i = 0; i < 4; ++i) {
1637 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1638 SourceLocation(), 0,
1639 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001640 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001641 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001642 }
1643
1644 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001645 }
1646
1647 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001648}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001649
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001650QualType ASTContext::getObjCFastEnumerationStateType()
1651{
1652 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001653 ObjCFastEnumerationStateTypeDecl =
1654 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1655 &Idents.get("__objcFastEnumerationState"));
1656
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001657 QualType FieldTypes[] = {
1658 UnsignedLongTy,
1659 getPointerType(ObjCIdType),
1660 getPointerType(UnsignedLongTy),
1661 getConstantArrayType(UnsignedLongTy,
1662 llvm::APInt(32, 5), ArrayType::Normal, 0)
1663 };
1664
Douglas Gregor44b43212008-12-11 16:49:14 +00001665 for (size_t i = 0; i < 4; ++i) {
1666 FieldDecl *Field = FieldDecl::Create(*this,
1667 ObjCFastEnumerationStateTypeDecl,
1668 SourceLocation(), 0,
1669 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001670 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001671 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001672 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001673
Douglas Gregor44b43212008-12-11 16:49:14 +00001674 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001675 }
1676
1677 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1678}
1679
Anders Carlssone8c49532007-10-29 06:33:42 +00001680// This returns true if a type has been typedefed to BOOL:
1681// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001682static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001683 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001684 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1685 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001686
1687 return false;
1688}
1689
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001690/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001691/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001692int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001693 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001694
1695 // Make all integer and enum types at least as large as an int
1696 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001697 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001698 // Treat arrays as pointers, since that's how they're passed in.
1699 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001700 sz = getTypeSize(VoidPtrTy);
1701 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001702}
1703
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001704/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001705/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001706void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001707 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001708 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001709 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001710 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001711 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001712 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001713 // Compute size of all parameters.
1714 // Start with computing size of a pointer in number of bytes.
1715 // FIXME: There might(should) be a better way of doing this computation!
1716 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001717 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001718 // The first two arguments (self and _cmd) are pointers; account for
1719 // their size.
1720 int ParmOffset = 2 * PtrSize;
1721 int NumOfParams = Decl->getNumParams();
1722 for (int i = 0; i < NumOfParams; i++) {
1723 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001724 int sz = getObjCEncodingTypeSize (PType);
1725 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001726 ParmOffset += sz;
1727 }
1728 S += llvm::utostr(ParmOffset);
1729 S += "@0:";
1730 S += llvm::utostr(PtrSize);
1731
1732 // Argument types.
1733 ParmOffset = 2 * PtrSize;
1734 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001735 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1736 QualType PType = PVDecl->getOriginalType();
1737 if (const ArrayType *AT =
1738 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1739 // Use array's original type only if it has known number of
1740 // elements.
1741 if (!dyn_cast<ConstantArrayType>(AT))
1742 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001743 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001744 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001745 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001746 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001747 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001748 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001749 }
1750}
1751
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001752/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001753/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001754/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1755/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001756/// Property attributes are stored as a comma-delimited C string. The simple
1757/// attributes readonly and bycopy are encoded as single characters. The
1758/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1759/// encoded as single characters, followed by an identifier. Property types
1760/// are also encoded as a parametrized attribute. The characters used to encode
1761/// these attributes are defined by the following enumeration:
1762/// @code
1763/// enum PropertyAttributes {
1764/// kPropertyReadOnly = 'R', // property is read-only.
1765/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1766/// kPropertyByref = '&', // property is a reference to the value last assigned
1767/// kPropertyDynamic = 'D', // property is dynamic
1768/// kPropertyGetter = 'G', // followed by getter selector name
1769/// kPropertySetter = 'S', // followed by setter selector name
1770/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1771/// kPropertyType = 't' // followed by old-style type encoding.
1772/// kPropertyWeak = 'W' // 'weak' property
1773/// kPropertyStrong = 'P' // property GC'able
1774/// kPropertyNonAtomic = 'N' // property non-atomic
1775/// };
1776/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001777void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1778 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001779 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001780 // Collect information from the property implementation decl(s).
1781 bool Dynamic = false;
1782 ObjCPropertyImplDecl *SynthesizePID = 0;
1783
1784 // FIXME: Duplicated code due to poor abstraction.
1785 if (Container) {
1786 if (const ObjCCategoryImplDecl *CID =
1787 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1788 for (ObjCCategoryImplDecl::propimpl_iterator
1789 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1790 ObjCPropertyImplDecl *PID = *i;
1791 if (PID->getPropertyDecl() == PD) {
1792 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1793 Dynamic = true;
1794 } else {
1795 SynthesizePID = PID;
1796 }
1797 }
1798 }
1799 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001800 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001801 for (ObjCCategoryImplDecl::propimpl_iterator
1802 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1803 ObjCPropertyImplDecl *PID = *i;
1804 if (PID->getPropertyDecl() == PD) {
1805 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1806 Dynamic = true;
1807 } else {
1808 SynthesizePID = PID;
1809 }
1810 }
1811 }
1812 }
1813 }
1814
1815 // FIXME: This is not very efficient.
1816 S = "T";
1817
1818 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001819 // GCC has some special rules regarding encoding of properties which
1820 // closely resembles encoding of ivars.
1821 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1822 true /* outermost type */,
1823 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001824
1825 if (PD->isReadOnly()) {
1826 S += ",R";
1827 } else {
1828 switch (PD->getSetterKind()) {
1829 case ObjCPropertyDecl::Assign: break;
1830 case ObjCPropertyDecl::Copy: S += ",C"; break;
1831 case ObjCPropertyDecl::Retain: S += ",&"; break;
1832 }
1833 }
1834
1835 // It really isn't clear at all what this means, since properties
1836 // are "dynamic by default".
1837 if (Dynamic)
1838 S += ",D";
1839
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001840 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1841 S += ",N";
1842
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001843 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1844 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001845 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001846 }
1847
1848 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1849 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001850 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001851 }
1852
1853 if (SynthesizePID) {
1854 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1855 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001856 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001857 }
1858
1859 // FIXME: OBJCGC: weak & strong
1860}
1861
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001862/// getLegacyIntegralTypeEncoding -
1863/// Another legacy compatibility encoding: 32-bit longs are encoded as
1864/// 'l' or 'L', but not always. For typedefs, we need to use
1865/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1866///
1867void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1868 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1869 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1870 if (BT->getKind() == BuiltinType::ULong)
1871 PointeeTy = UnsignedIntTy;
1872 else if (BT->getKind() == BuiltinType::Long)
1873 PointeeTy = IntTy;
1874 }
1875 }
1876}
1877
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001878void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001879 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001880 // We follow the behavior of gcc, expanding structures which are
1881 // directly pointed to, and expanding embedded structures. Note that
1882 // these rules are sufficient to prevent recursive encoding of the
1883 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001884 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1885 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001886}
1887
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001888static void EncodeBitField(const ASTContext *Context, std::string& S,
1889 FieldDecl *FD) {
1890 const Expr *E = FD->getBitWidth();
1891 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1892 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1893 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1894 S += 'b';
1895 S += llvm::utostr(N);
1896}
1897
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001898void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1899 bool ExpandPointedToStructures,
1900 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00001901 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001902 bool OutermostType,
1903 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001904 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001905 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00001906 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001907 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001908 else {
1909 char encoding;
1910 switch (BT->getKind()) {
1911 default: assert(0 && "Unhandled builtin type kind");
1912 case BuiltinType::Void: encoding = 'v'; break;
1913 case BuiltinType::Bool: encoding = 'B'; break;
1914 case BuiltinType::Char_U:
1915 case BuiltinType::UChar: encoding = 'C'; break;
1916 case BuiltinType::UShort: encoding = 'S'; break;
1917 case BuiltinType::UInt: encoding = 'I'; break;
1918 case BuiltinType::ULong: encoding = 'L'; break;
1919 case BuiltinType::ULongLong: encoding = 'Q'; break;
1920 case BuiltinType::Char_S:
1921 case BuiltinType::SChar: encoding = 'c'; break;
1922 case BuiltinType::Short: encoding = 's'; break;
1923 case BuiltinType::Int: encoding = 'i'; break;
1924 case BuiltinType::Long: encoding = 'l'; break;
1925 case BuiltinType::LongLong: encoding = 'q'; break;
1926 case BuiltinType::Float: encoding = 'f'; break;
1927 case BuiltinType::Double: encoding = 'd'; break;
1928 case BuiltinType::LongDouble: encoding = 'd'; break;
1929 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001930
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00001931 S += encoding;
1932 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001933 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001934 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001935 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1936 ExpandPointedToStructures,
1937 ExpandStructures, FD);
1938 if (FD || EncodingProperty) {
1939 // Note that we do extended encoding of protocol qualifer list
1940 // Only when doing ivar or property encoding.
1941 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1942 S += '"';
1943 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1944 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1945 S += '<';
1946 S += Proto->getNameAsString();
1947 S += '>';
1948 }
1949 S += '"';
1950 }
1951 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001952 }
1953 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001954 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001955 bool isReadOnly = false;
1956 // For historical/compatibility reasons, the read-only qualifier of the
1957 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1958 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1959 // Also, do not emit the 'r' for anything but the outermost type!
1960 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1961 if (OutermostType && T.isConstQualified()) {
1962 isReadOnly = true;
1963 S += 'r';
1964 }
1965 }
1966 else if (OutermostType) {
1967 QualType P = PointeeTy;
1968 while (P->getAsPointerType())
1969 P = P->getAsPointerType()->getPointeeType();
1970 if (P.isConstQualified()) {
1971 isReadOnly = true;
1972 S += 'r';
1973 }
1974 }
1975 if (isReadOnly) {
1976 // Another legacy compatibility encoding. Some ObjC qualifier and type
1977 // combinations need to be rearranged.
1978 // Rewrite "in const" from "nr" to "rn"
1979 const char * s = S.c_str();
1980 int len = S.length();
1981 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
1982 std::string replace = "rn";
1983 S.replace(S.end()-2, S.end(), replace);
1984 }
1985 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001986 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001987 S += '@';
1988 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00001989 }
1990 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00001991 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1992 // Another historical/compatibility reason.
1993 // We encode the underlying type which comes out as
1994 // {...};
1995 S += '^';
1996 getObjCEncodingForTypeImpl(PointeeTy, S,
1997 false, ExpandPointedToStructures,
1998 NULL);
1999 return;
2000 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002001 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002002 if (FD || EncodingProperty) {
2003 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
2004 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002005 S += '"';
2006 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002007 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2008 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2009 S += '<';
2010 S += Proto->getNameAsString();
2011 S += '>';
2012 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002013 S += '"';
2014 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002015 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002016 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002017 S += '#';
2018 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002019 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002020 S += ':';
2021 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002022 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002023
2024 if (PointeeTy->isCharType()) {
2025 // char pointer types should be encoded as '*' unless it is a
2026 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002027 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002028 S += '*';
2029 return;
2030 }
2031 }
2032
2033 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002034 getLegacyIntegralTypeEncoding(PointeeTy);
2035
2036 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002037 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002038 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002039 } else if (const ArrayType *AT =
2040 // Ignore type qualifiers etc.
2041 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002042 S += '[';
2043
2044 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2045 S += llvm::utostr(CAT->getSize().getZExtValue());
2046 else
2047 assert(0 && "Unhandled array type!");
2048
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002049 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002050 false, ExpandStructures, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002051 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002052 } else if (T->getAsFunctionType()) {
2053 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002054 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002055 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002056 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002057 // Anonymous structures print as '?'
2058 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2059 S += II->getName();
2060 } else {
2061 S += '?';
2062 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002063 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002064 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002065 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2066 FieldEnd = RDecl->field_end();
2067 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002068 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002069 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002070 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002071 S += '"';
2072 }
2073
2074 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002075 if (Field->isBitField()) {
2076 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2077 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002078 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002079 QualType qt = Field->getType();
2080 getLegacyIntegralTypeEncoding(qt);
2081 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002082 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002083 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002084 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002085 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002086 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002087 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002088 if (FD && FD->isBitField())
2089 EncodeBitField(this, S, FD);
2090 else
2091 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002092 } else if (T->isBlockPointerType()) {
2093 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002094 } else if (T->isObjCInterfaceType()) {
2095 // @encode(class_name)
2096 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2097 S += '{';
2098 const IdentifierInfo *II = OI->getIdentifier();
2099 S += II->getName();
2100 S += '=';
2101 std::vector<FieldDecl*> RecFields;
2102 CollectObjCIvars(OI, RecFields);
2103 for (unsigned int i = 0; i != RecFields.size(); i++) {
2104 if (RecFields[i]->isBitField())
2105 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2106 RecFields[i]);
2107 else
2108 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2109 FD);
2110 }
2111 S += '}';
2112 }
2113 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002114 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002115}
2116
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002117void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002118 std::string& S) const {
2119 if (QT & Decl::OBJC_TQ_In)
2120 S += 'n';
2121 if (QT & Decl::OBJC_TQ_Inout)
2122 S += 'N';
2123 if (QT & Decl::OBJC_TQ_Out)
2124 S += 'o';
2125 if (QT & Decl::OBJC_TQ_Bycopy)
2126 S += 'O';
2127 if (QT & Decl::OBJC_TQ_Byref)
2128 S += 'R';
2129 if (QT & Decl::OBJC_TQ_Oneway)
2130 S += 'V';
2131}
2132
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002133void ASTContext::setBuiltinVaListType(QualType T)
2134{
2135 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2136
2137 BuiltinVaListType = T;
2138}
2139
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002140void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002141{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002142 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002143
2144 // typedef struct objc_object *id;
2145 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002146 // User error - caller will issue diagnostics.
2147 if (!ptr)
2148 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002149 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002150 // User error - caller will issue diagnostics.
2151 if (!rec)
2152 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002153 IdStructType = rec;
2154}
2155
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002156void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002157{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002158 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002159
2160 // typedef struct objc_selector *SEL;
2161 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002162 if (!ptr)
2163 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002164 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002165 if (!rec)
2166 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002167 SelStructType = rec;
2168}
2169
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002170void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002171{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002172 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002173}
2174
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002175void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002176{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002177 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002178
2179 // typedef struct objc_class *Class;
2180 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2181 assert(ptr && "'Class' incorrectly typed");
2182 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2183 assert(rec && "'Class' incorrectly typed");
2184 ClassStructType = rec;
2185}
2186
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002187void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2188 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002189 "'NSConstantString' type already set!");
2190
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002191 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002192}
2193
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002194/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002195/// TargetInfo, produce the corresponding type. The unsigned @p Type
2196/// is actually a value of type @c TargetInfo::IntType.
2197QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002198 switch (Type) {
2199 case TargetInfo::NoInt: return QualType();
2200 case TargetInfo::SignedShort: return ShortTy;
2201 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2202 case TargetInfo::SignedInt: return IntTy;
2203 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2204 case TargetInfo::SignedLong: return LongTy;
2205 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2206 case TargetInfo::SignedLongLong: return LongLongTy;
2207 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2208 }
2209
2210 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002211 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002212}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002213
2214//===----------------------------------------------------------------------===//
2215// Type Predicates.
2216//===----------------------------------------------------------------------===//
2217
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002218/// isObjCNSObjectType - Return true if this is an NSObject object using
2219/// NSObject attribute on a c-style pointer type.
2220/// FIXME - Make it work directly on types.
2221///
2222bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2223 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2224 if (TypedefDecl *TD = TDT->getDecl())
2225 if (TD->getAttr<ObjCNSObjectAttr>())
2226 return true;
2227 }
2228 return false;
2229}
2230
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002231/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2232/// to an object type. This includes "id" and "Class" (two 'special' pointers
2233/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2234/// ID type).
2235bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2236 if (Ty->isObjCQualifiedIdType())
2237 return true;
2238
Steve Naroff6ae98502008-10-21 18:24:04 +00002239 // Blocks are objects.
2240 if (Ty->isBlockPointerType())
2241 return true;
2242
2243 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002244 if (!Ty->isPointerType())
2245 return false;
2246
2247 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2248 // pointer types. This looks for the typedef specifically, not for the
2249 // underlying type.
2250 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2251 return true;
2252
2253 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002254 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2255 return true;
2256
2257 // If is has NSObject attribute, OK as well.
2258 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002259}
2260
Chris Lattner6ac46a42008-04-07 06:51:04 +00002261//===----------------------------------------------------------------------===//
2262// Type Compatibility Testing
2263//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002264
Steve Naroff1c7d0672008-09-04 15:10:53 +00002265/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002266/// block types. Types must be strictly compatible here. For example,
2267/// C unfortunately doesn't produce an error for the following:
2268///
2269/// int (*emptyArgFunc)();
2270/// int (*intArgList)(int) = emptyArgFunc;
2271///
2272/// For blocks, we will produce an error for the following (similar to C++):
2273///
2274/// int (^emptyArgBlock)();
2275/// int (^intArgBlock)(int) = emptyArgBlock;
2276///
2277/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2278///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002279bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002280 const FunctionType *lbase = lhs->getAsFunctionType();
2281 const FunctionType *rbase = rhs->getAsFunctionType();
2282 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2283 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2284 if (lproto && rproto)
2285 return !mergeTypes(lhs, rhs).isNull();
2286 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002287}
2288
Chris Lattner6ac46a42008-04-07 06:51:04 +00002289/// areCompatVectorTypes - Return true if the two specified vector types are
2290/// compatible.
2291static bool areCompatVectorTypes(const VectorType *LHS,
2292 const VectorType *RHS) {
2293 assert(LHS->isCanonical() && RHS->isCanonical());
2294 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002295 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002296}
2297
Eli Friedman3d815e72008-08-22 00:56:42 +00002298/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002299/// compatible for assignment from RHS to LHS. This handles validation of any
2300/// protocol qualifiers on the LHS or RHS.
2301///
Eli Friedman3d815e72008-08-22 00:56:42 +00002302bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2303 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002304 // Verify that the base decls are compatible: the RHS must be a subclass of
2305 // the LHS.
2306 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2307 return false;
2308
2309 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2310 // protocol qualified at all, then we are good.
2311 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2312 return true;
2313
2314 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2315 // isn't a superset.
2316 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2317 return true; // FIXME: should return false!
2318
2319 // Finally, we must have two protocol-qualified interfaces.
2320 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2321 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2322 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2323 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2324 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2325 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2326
2327 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2328 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2329 // LHS in a single parallel scan until we run out of elements in LHS.
2330 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2331 ObjCProtocolDecl *LHSProto = *LHSPI;
2332
2333 while (RHSPI != RHSPE) {
2334 ObjCProtocolDecl *RHSProto = *RHSPI++;
2335 // If the RHS has a protocol that the LHS doesn't, ignore it.
2336 if (RHSProto != LHSProto)
2337 continue;
2338
2339 // Otherwise, the RHS does have this element.
2340 ++LHSPI;
2341 if (LHSPI == LHSPE)
2342 return true; // All protocols in LHS exist in RHS.
2343
2344 LHSProto = *LHSPI;
2345 }
2346
2347 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2348 return false;
2349}
2350
Steve Naroffec0550f2007-10-15 20:41:53 +00002351/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2352/// both shall have the identically qualified version of a compatible type.
2353/// C99 6.2.7p1: Two types have compatible types if their types are the
2354/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002355bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2356 return !mergeTypes(LHS, RHS).isNull();
2357}
2358
2359QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2360 const FunctionType *lbase = lhs->getAsFunctionType();
2361 const FunctionType *rbase = rhs->getAsFunctionType();
2362 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2363 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2364 bool allLTypes = true;
2365 bool allRTypes = true;
2366
2367 // Check return type
2368 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2369 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002370 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2371 allLTypes = false;
2372 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2373 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002374
2375 if (lproto && rproto) { // two C99 style function prototypes
2376 unsigned lproto_nargs = lproto->getNumArgs();
2377 unsigned rproto_nargs = rproto->getNumArgs();
2378
2379 // Compatible functions must have the same number of arguments
2380 if (lproto_nargs != rproto_nargs)
2381 return QualType();
2382
2383 // Variadic and non-variadic functions aren't compatible
2384 if (lproto->isVariadic() != rproto->isVariadic())
2385 return QualType();
2386
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002387 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2388 return QualType();
2389
Eli Friedman3d815e72008-08-22 00:56:42 +00002390 // Check argument compatibility
2391 llvm::SmallVector<QualType, 10> types;
2392 for (unsigned i = 0; i < lproto_nargs; i++) {
2393 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2394 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2395 QualType argtype = mergeTypes(largtype, rargtype);
2396 if (argtype.isNull()) return QualType();
2397 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002398 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2399 allLTypes = false;
2400 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2401 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002402 }
2403 if (allLTypes) return lhs;
2404 if (allRTypes) return rhs;
2405 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002406 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002407 }
2408
2409 if (lproto) allRTypes = false;
2410 if (rproto) allLTypes = false;
2411
2412 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2413 if (proto) {
2414 if (proto->isVariadic()) return QualType();
2415 // Check that the types are compatible with the types that
2416 // would result from default argument promotions (C99 6.7.5.3p15).
2417 // The only types actually affected are promotable integer
2418 // types and floats, which would be passed as a different
2419 // type depending on whether the prototype is visible.
2420 unsigned proto_nargs = proto->getNumArgs();
2421 for (unsigned i = 0; i < proto_nargs; ++i) {
2422 QualType argTy = proto->getArgType(i);
2423 if (argTy->isPromotableIntegerType() ||
2424 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2425 return QualType();
2426 }
2427
2428 if (allLTypes) return lhs;
2429 if (allRTypes) return rhs;
2430 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002431 proto->getNumArgs(), lproto->isVariadic(),
2432 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002433 }
2434
2435 if (allLTypes) return lhs;
2436 if (allRTypes) return rhs;
2437 return getFunctionTypeNoProto(retType);
2438}
2439
2440QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002441 // C++ [expr]: If an expression initially has the type "reference to T", the
2442 // type is adjusted to "T" prior to any further analysis, the expression
2443 // designates the object or function denoted by the reference, and the
2444 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002445 // FIXME: C++ shouldn't be going through here! The rules are different
2446 // enough that they should be handled separately.
2447 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002448 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002449 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002450 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002451
Eli Friedman3d815e72008-08-22 00:56:42 +00002452 QualType LHSCan = getCanonicalType(LHS),
2453 RHSCan = getCanonicalType(RHS);
2454
2455 // If two types are identical, they are compatible.
2456 if (LHSCan == RHSCan)
2457 return LHS;
2458
2459 // If the qualifiers are different, the types aren't compatible
2460 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2461 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2462 return QualType();
2463
2464 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2465 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2466
Chris Lattner1adb8832008-01-14 05:45:46 +00002467 // We want to consider the two function types to be the same for these
2468 // comparisons, just force one to the other.
2469 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2470 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002471
2472 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002473 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2474 LHSClass = Type::ConstantArray;
2475 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2476 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002477
Nate Begeman213541a2008-04-18 23:10:10 +00002478 // Canonicalize ExtVector -> Vector.
2479 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2480 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002481
Chris Lattnerb0489812008-04-07 06:38:24 +00002482 // Consider qualified interfaces and interfaces the same.
2483 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2484 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002485
Chris Lattnera36a61f2008-04-07 05:43:21 +00002486 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002487 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002488 // ID is compatible with all qualified id types.
2489 if (LHS->isObjCQualifiedIdType()) {
2490 if (const PointerType *PT = RHS->getAsPointerType()) {
2491 QualType pType = PT->getPointeeType();
2492 if (isObjCIdType(pType))
2493 return LHS;
2494 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2495 // Unfortunately, this API is part of Sema (which we don't have access
2496 // to. Need to refactor. The following check is insufficient, since we
2497 // need to make sure the class implements the protocol.
2498 if (pType->isObjCInterfaceType())
2499 return LHS;
2500 }
2501 }
2502 if (RHS->isObjCQualifiedIdType()) {
2503 if (const PointerType *PT = LHS->getAsPointerType()) {
2504 QualType pType = PT->getPointeeType();
2505 if (isObjCIdType(pType))
2506 return RHS;
2507 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2508 // Unfortunately, this API is part of Sema (which we don't have access
2509 // to. Need to refactor. The following check is insufficient, since we
2510 // need to make sure the class implements the protocol.
2511 if (pType->isObjCInterfaceType())
2512 return RHS;
2513 }
2514 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002515 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2516 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002517 if (const EnumType* ETy = LHS->getAsEnumType()) {
2518 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2519 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002520 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002521 if (const EnumType* ETy = RHS->getAsEnumType()) {
2522 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2523 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002524 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002525
Eli Friedman3d815e72008-08-22 00:56:42 +00002526 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002527 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002528
Steve Naroff4a746782008-01-09 22:43:08 +00002529 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002530 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002531 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002532 {
2533 // Merge two pointer types, while trying to preserve typedef info
2534 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2535 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2536 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2537 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002538 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2539 return LHS;
2540 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2541 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002542 return getPointerType(ResultType);
2543 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002544 case Type::BlockPointer:
2545 {
2546 // Merge two block pointer types, while trying to preserve typedef info
2547 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2548 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2549 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2550 if (ResultType.isNull()) return QualType();
2551 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2552 return LHS;
2553 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2554 return RHS;
2555 return getBlockPointerType(ResultType);
2556 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002557 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002558 {
2559 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2560 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2561 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2562 return QualType();
2563
2564 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2565 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2566 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2567 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002568 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2569 return LHS;
2570 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2571 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002572 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2573 ArrayType::ArraySizeModifier(), 0);
2574 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2575 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002576 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2577 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002578 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2579 return LHS;
2580 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2581 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002582 if (LVAT) {
2583 // FIXME: This isn't correct! But tricky to implement because
2584 // the array's size has to be the size of LHS, but the type
2585 // has to be different.
2586 return LHS;
2587 }
2588 if (RVAT) {
2589 // FIXME: This isn't correct! But tricky to implement because
2590 // the array's size has to be the size of RHS, but the type
2591 // has to be different.
2592 return RHS;
2593 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002594 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2595 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002596 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002597 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002598 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002599 return mergeFunctionTypes(LHS, RHS);
2600 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002601 // FIXME: Why are these compatible?
2602 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2603 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2604 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002605 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002606 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002607 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002608 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002609 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2610 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002611 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002612 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002613 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2614 // for checking assignment/comparison safety
2615 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002616 case Type::ObjCQualifiedId:
2617 // Distinct qualified id's are not compatible.
2618 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002619 default:
2620 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002621 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002622 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002623}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002624
Chris Lattner5426bf62008-04-07 07:01:58 +00002625//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002626// Integer Predicates
2627//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002628
Eli Friedmanad74a752008-06-28 06:23:08 +00002629unsigned ASTContext::getIntWidth(QualType T) {
2630 if (T == BoolTy)
2631 return 1;
2632 // At the moment, only bool has padding bits
2633 return (unsigned)getTypeSize(T);
2634}
2635
2636QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2637 assert(T->isSignedIntegerType() && "Unexpected type");
2638 if (const EnumType* ETy = T->getAsEnumType())
2639 T = ETy->getDecl()->getIntegerType();
2640 const BuiltinType* BTy = T->getAsBuiltinType();
2641 assert (BTy && "Unexpected signed integer type");
2642 switch (BTy->getKind()) {
2643 case BuiltinType::Char_S:
2644 case BuiltinType::SChar:
2645 return UnsignedCharTy;
2646 case BuiltinType::Short:
2647 return UnsignedShortTy;
2648 case BuiltinType::Int:
2649 return UnsignedIntTy;
2650 case BuiltinType::Long:
2651 return UnsignedLongTy;
2652 case BuiltinType::LongLong:
2653 return UnsignedLongLongTy;
2654 default:
2655 assert(0 && "Unexpected signed integer type");
2656 return QualType();
2657 }
2658}
2659
2660
2661//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002662// Serialization Support
2663//===----------------------------------------------------------------------===//
2664
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002665/// Emit - Serialize an ASTContext object to Bitcode.
2666void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002667 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002668 S.EmitRef(SourceMgr);
2669 S.EmitRef(Target);
2670 S.EmitRef(Idents);
2671 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002672
Ted Kremenekfee04522007-10-31 22:44:07 +00002673 // Emit the size of the type vector so that we can reserve that size
2674 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002675 S.EmitInt(Types.size());
2676
Ted Kremenek03ed4402007-11-13 22:02:55 +00002677 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2678 I!=E;++I)
2679 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002680
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002681 S.EmitOwnedPtr(TUDecl);
2682
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002683 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002684}
2685
Ted Kremenek0f84c002007-11-13 00:25:37 +00002686ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002687
2688 // Read the language options.
2689 LangOptions LOpts;
2690 LOpts.Read(D);
2691
Ted Kremenekfee04522007-10-31 22:44:07 +00002692 SourceManager &SM = D.ReadRef<SourceManager>();
2693 TargetInfo &t = D.ReadRef<TargetInfo>();
2694 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2695 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002696
Ted Kremenekfee04522007-10-31 22:44:07 +00002697 unsigned size_reserve = D.ReadInt();
2698
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002699 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2700 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002701
Ted Kremenek03ed4402007-11-13 22:02:55 +00002702 for (unsigned i = 0; i < size_reserve; ++i)
2703 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002704
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002705 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2706
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002707 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002708
2709 return A;
2710}