blob: 281b2cd5de21683ecadfc6ed544538e829dbd872 [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"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Chris Lattner61710852008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000036 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000037{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 Types.pop_back();
49 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000050
Nuno Lopesb74668e2008-12-17 22:30:25 +000051 {
52 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
53 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
54 while (I != E) {
55 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
56 delete R;
57 }
58 }
59
60 {
61 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
62 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
63 while (I != E) {
64 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
65 delete R;
66 }
67 }
68
69 {
70 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
71 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
72 while (I != E) {
73 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
74 R->Destroy(*this);
75 }
76 }
77
Eli Friedmanb26153c2008-05-27 03:08:09 +000078 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
80
81void ASTContext::PrintStats() const {
82 fprintf(stderr, "*** AST Context Stats:\n");
83 fprintf(stderr, " %d types total.\n", (int)Types.size());
84 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000085 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
87
88 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000089 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
90 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000091 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000092
93 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
94 Type *T = Types[i];
95 if (isa<BuiltinType>(T))
96 ++NumBuiltin;
97 else if (isa<PointerType>(T))
98 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000099 else if (isa<BlockPointerType>(T))
100 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 else if (isa<ReferenceType>(T))
102 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000103 else if (isa<ComplexType>(T))
104 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 else if (isa<ArrayType>(T))
106 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000107 else if (isa<VectorType>(T))
108 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 else if (isa<FunctionTypeNoProto>(T))
110 ++NumFunctionNP;
111 else if (isa<FunctionTypeProto>(T))
112 ++NumFunctionP;
113 else if (isa<TypedefType>(T))
114 ++NumTypeName;
115 else if (TagType *TT = dyn_cast<TagType>(T)) {
116 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000117 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000119 case TagDecl::TK_struct: ++NumTagStruct; break;
120 case TagDecl::TK_union: ++NumTagUnion; break;
121 case TagDecl::TK_class: ++NumTagClass; break;
122 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000124 } else if (isa<ObjCInterfaceType>(T))
125 ++NumObjCInterfaces;
126 else if (isa<ObjCQualifiedInterfaceType>(T))
127 ++NumObjCQualifiedInterfaces;
128 else if (isa<ObjCQualifiedIdType>(T))
129 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000130 else if (isa<TypeOfType>(T))
131 ++NumTypeOfTypes;
132 else if (isa<TypeOfExpr>(T))
133 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000134 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000135 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 assert(0 && "Unknown type!");
137 }
138 }
139
140 fprintf(stderr, " %d builtin types\n", NumBuiltin);
141 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000142 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000144 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000146 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
148 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
149 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
150 fprintf(stderr, " %d tagged types\n", NumTagged);
151 fprintf(stderr, " %d struct types\n", NumTagStruct);
152 fprintf(stderr, " %d union types\n", NumTagUnion);
153 fprintf(stderr, " %d class types\n", NumTagClass);
154 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000155 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000156 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000157 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000158 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000159 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000160 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
161 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
164 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000165 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 NumFunctionP*sizeof(FunctionTypeProto)+
167 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000168 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
169 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
172
173void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
174 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
175}
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177void ASTContext::InitBuiltinTypes() {
178 assert(VoidTy.isNull() && "Context reinitialized?");
179
180 // C99 6.2.5p19.
181 InitBuiltinType(VoidTy, BuiltinType::Void);
182
183 // C99 6.2.5p2.
184 InitBuiltinType(BoolTy, BuiltinType::Bool);
185 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000186 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 InitBuiltinType(CharTy, BuiltinType::Char_S);
188 else
189 InitBuiltinType(CharTy, BuiltinType::Char_U);
190 // C99 6.2.5p4.
191 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
192 InitBuiltinType(ShortTy, BuiltinType::Short);
193 InitBuiltinType(IntTy, BuiltinType::Int);
194 InitBuiltinType(LongTy, BuiltinType::Long);
195 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
196
197 // C99 6.2.5p6.
198 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
199 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
200 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
201 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
202 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
203
204 // C99 6.2.5p10.
205 InitBuiltinType(FloatTy, BuiltinType::Float);
206 InitBuiltinType(DoubleTy, BuiltinType::Double);
207 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000208
209 // C++ 3.9.1p5
210 InitBuiltinType(WCharTy, BuiltinType::WChar);
211
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000212 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000213 InitBuiltinType(OverloadTy, BuiltinType::Overload);
214
215 // Placeholder type for type-dependent expressions whose type is
216 // completely unknown. No code should ever check a type against
217 // DependentTy and users should never see it; however, it is here to
218 // help diagnose failures to properly check for type-dependent
219 // expressions.
220 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 // C99 6.2.5p11.
223 FloatComplexTy = getComplexType(FloatTy);
224 DoubleComplexTy = getComplexType(DoubleTy);
225 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000226
Steve Naroff7e219e42007-10-15 14:41:52 +0000227 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000228 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000229 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000230 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000231 ClassStructType = 0;
232
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000233 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000234
235 // void * type
236 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000237}
238
Chris Lattner464175b2007-07-18 17:52:12 +0000239//===----------------------------------------------------------------------===//
240// Type Sizing and Analysis
241//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000242
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000243/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
244/// scalar floating point type.
245const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
246 const BuiltinType *BT = T->getAsBuiltinType();
247 assert(BT && "Not a floating point type!");
248 switch (BT->getKind()) {
249 default: assert(0 && "Not a floating point type!");
250 case BuiltinType::Float: return Target.getFloatFormat();
251 case BuiltinType::Double: return Target.getDoubleFormat();
252 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
253 }
254}
255
256
Chris Lattnera7674d82007-07-13 22:13:22 +0000257/// getTypeSize - Return the size of the specified type, in bits. This method
258/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000259std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000260ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000261 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000262 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000263 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000264 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000265 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000266 case Type::FunctionNoProto:
267 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000268 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000269 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000270 case Type::VariableArray:
271 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000272 case Type::DependentSizedArray:
273 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000274 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000275 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000276
Chris Lattner98be4942008-03-05 18:54:05 +0000277 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000278 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000279 Align = EltInfo.second;
280 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000281 }
Nate Begeman213541a2008-04-18 23:10:10 +0000282 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000283 case Type::Vector: {
284 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000285 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000286 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000287 // FIXME: This isn't right for unusual vectors
288 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000289 break;
290 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000291
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000292 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000293 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000294 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000295 case BuiltinType::Void:
296 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000297 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000298 Width = Target.getBoolWidth();
299 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000300 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000301 case BuiltinType::Char_S:
302 case BuiltinType::Char_U:
303 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000304 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000305 Width = Target.getCharWidth();
306 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000307 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000308 case BuiltinType::WChar:
309 Width = Target.getWCharWidth();
310 Align = Target.getWCharAlign();
311 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000312 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000313 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000314 Width = Target.getShortWidth();
315 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000316 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000317 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000318 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000319 Width = Target.getIntWidth();
320 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000321 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000322 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000323 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000324 Width = Target.getLongWidth();
325 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000326 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000327 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000329 Width = Target.getLongLongWidth();
330 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000331 break;
332 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000333 Width = Target.getFloatWidth();
334 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000335 break;
336 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000337 Width = Target.getDoubleWidth();
338 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000339 break;
340 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000341 Width = Target.getLongDoubleWidth();
342 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000344 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000345 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000346 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000347 // FIXME: Pointers into different addr spaces could have different sizes and
348 // alignment requirements: getPointerInfo should take an AddrSpace.
349 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000351 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000352 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000353 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000354 case Type::BlockPointer: {
355 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
356 Width = Target.getPointerWidth(AS);
357 Align = Target.getPointerAlign(AS);
358 break;
359 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000360 case Type::Pointer: {
361 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000362 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000363 Align = Target.getPointerAlign(AS);
364 break;
365 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000366 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000367 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000368 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000369 // FIXME: This is wrong for struct layout: a reference in a struct has
370 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000371 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000372
373 case Type::Complex: {
374 // Complex types have the same alignment as their elements, but twice the
375 // size.
376 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000377 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000378 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000379 Align = EltInfo.second;
380 break;
381 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000382 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000383 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000384 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
385 Width = Layout.getSize();
386 Align = Layout.getAlignment();
387 break;
388 }
Chris Lattner71763312008-04-06 22:05:18 +0000389 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000390 const TagType *TT = cast<TagType>(T);
391
392 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000393 Width = 1;
394 Align = 1;
395 break;
396 }
397
Daniel Dunbar1d751182008-11-08 05:48:37 +0000398 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000399 return getTypeInfo(ET->getDecl()->getIntegerType());
400
Daniel Dunbar1d751182008-11-08 05:48:37 +0000401 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000402 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
403 Width = Layout.getSize();
404 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000405 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000406 }
Chris Lattner71763312008-04-06 22:05:18 +0000407 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000408
Chris Lattner464175b2007-07-18 17:52:12 +0000409 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000410 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000411}
412
Devang Patel8b277042008-06-04 21:22:16 +0000413/// LayoutField - Field layout.
414void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000415 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000416 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000417 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000418 uint64_t FieldOffset = IsUnion ? 0 : Size;
419 uint64_t FieldSize;
420 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000421
422 // FIXME: Should this override struct packing? Probably we want to
423 // take the minimum?
424 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
425 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000426
427 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
428 // TODO: Need to check this algorithm on other targets!
429 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000430 FieldSize =
431 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000432
433 std::pair<uint64_t, unsigned> FieldInfo =
434 Context.getTypeInfo(FD->getType());
435 uint64_t TypeSize = FieldInfo.first;
436
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000437 // Determine the alignment of this bitfield. The packing
438 // attributes define a maximum and the alignment attribute defines
439 // a minimum.
440 // FIXME: What is the right behavior when the specified alignment
441 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000442 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000443 if (FieldPacking)
444 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000445 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
446 FieldAlign = std::max(FieldAlign, AA->getAlignment());
447
448 // Check if we need to add padding to give the field the correct
449 // alignment.
450 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
451 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
452
453 // Padding members don't affect overall alignment
454 if (!FD->getIdentifier())
455 FieldAlign = 1;
456 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000457 if (FD->getType()->isIncompleteArrayType()) {
458 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000459 // query getTypeInfo about these, so we figure it out here.
460 // Flexible array members don't have any size, but they
461 // have to be aligned appropriately for their element type.
462 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000463 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000464 FieldAlign = Context.getTypeAlign(ATy->getElementType());
465 } else {
466 std::pair<uint64_t, unsigned> FieldInfo =
467 Context.getTypeInfo(FD->getType());
468 FieldSize = FieldInfo.first;
469 FieldAlign = FieldInfo.second;
470 }
471
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000472 // Determine the alignment of this bitfield. The packing
473 // attributes define a maximum and the alignment attribute defines
474 // a minimum. Additionally, the packing alignment must be at least
475 // a byte for non-bitfields.
476 //
477 // FIXME: What is the right behavior when the specified alignment
478 // is smaller than the specified packing?
479 if (FieldPacking)
480 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000481 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
482 FieldAlign = std::max(FieldAlign, AA->getAlignment());
483
484 // Round up the current record size to the field's alignment boundary.
485 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
486 }
487
488 // Place this field at the current location.
489 FieldOffsets[FieldNo] = FieldOffset;
490
491 // Reserve space for this field.
492 if (IsUnion) {
493 Size = std::max(Size, FieldSize);
494 } else {
495 Size = FieldOffset + FieldSize;
496 }
497
498 // Remember max struct/class alignment.
499 Alignment = std::max(Alignment, FieldAlign);
500}
501
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000502static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
503 std::vector<FieldDecl*> &Fields) {
504 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
505 if (SuperClass)
506 CollectObjCIvars(SuperClass, Fields);
507 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
508 E = OI->ivar_end(); I != E; ++I) {
509 ObjCIvarDecl *IVDecl = (*I);
510 if (!IVDecl->isInvalidDecl())
511 Fields.push_back(cast<FieldDecl>(IVDecl));
512 }
513}
514
515/// addRecordToClass - produces record info. for the class for its
516/// ivars and all those inherited.
517///
518const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
519{
520 const RecordDecl *&RD = ASTRecordForInterface[D];
521 if (RD)
522 return RD;
523 std::vector<FieldDecl*> RecFields;
524 CollectObjCIvars(D, RecFields);
525 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
526 D->getLocation(),
527 D->getIdentifier());
528 /// FIXME! Can do collection of ivars and adding to the record while
529 /// doing it.
530 for (unsigned int i = 0; i != RecFields.size(); i++) {
531 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
532 RecFields[i]->getLocation(),
533 RecFields[i]->getIdentifier(),
534 RecFields[i]->getType(),
535 RecFields[i]->getBitWidth(), false, 0);
536 NewRD->addDecl(*this, Field);
537 }
538 NewRD->completeDefinition(*this);
539 RD = NewRD;
540 return RD;
541}
Devang Patel44a3dde2008-06-04 21:54:36 +0000542
Chris Lattner61710852008-10-05 17:34:18 +0000543/// getASTObjcInterfaceLayout - Get or compute information about the layout of
544/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000545/// position information.
546const ASTRecordLayout &
547ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
548 // Look up this layout, if already laid out, return what we have.
549 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
550 if (Entry) return *Entry;
551
552 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
553 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000554 ASTRecordLayout *NewEntry = NULL;
555 unsigned FieldCount = D->ivar_size();
556 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
557 FieldCount++;
558 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
559 unsigned Alignment = SL.getAlignment();
560 uint64_t Size = SL.getSize();
561 NewEntry = new ASTRecordLayout(Size, Alignment);
562 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000563 // Super class is at the beginning of the layout.
564 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000565 } else {
566 NewEntry = new ASTRecordLayout();
567 NewEntry->InitializeLayout(FieldCount);
568 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000569 Entry = NewEntry;
570
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000571 unsigned StructPacking = 0;
572 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
573 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000574
575 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
576 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
577 AA->getAlignment()));
578
579 // Layout each ivar sequentially.
580 unsigned i = 0;
581 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
582 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
583 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000584 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000585 }
586
587 // Finally, round the size of the total struct up to the alignment of the
588 // struct itself.
589 NewEntry->FinalizeLayout();
590 return *NewEntry;
591}
592
Devang Patel88a981b2007-11-01 19:11:01 +0000593/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000594/// specified record (struct/union/class), which indicates its size and field
595/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000596const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000597 D = D->getDefinition(*this);
598 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000599
Chris Lattner464175b2007-07-18 17:52:12 +0000600 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000601 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000602 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000603
Devang Patel88a981b2007-11-01 19:11:01 +0000604 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
605 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
606 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000607 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000608
Douglas Gregore267ff32008-12-11 20:41:00 +0000609 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000611 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000612
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000613 unsigned StructPacking = 0;
614 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
615 StructPacking = PA->getAlignment();
616
Eli Friedman4bd998b2008-05-30 09:31:38 +0000617 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000618 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
619 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000620
Eli Friedman4bd998b2008-05-30 09:31:38 +0000621 // Layout each field, for now, just sequentially, respecting alignment. In
622 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000623 unsigned FieldIdx = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000624 for (RecordDecl::field_const_iterator Field = D->field_begin(),
625 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000626 Field != FieldEnd; (void)++Field, ++FieldIdx)
627 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000628
629 // Finally, round the size of the total struct up to the alignment of the
630 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000631 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000632 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000633}
634
Chris Lattnera7674d82007-07-13 22:13:22 +0000635//===----------------------------------------------------------------------===//
636// Type creation/memoization methods
637//===----------------------------------------------------------------------===//
638
Christopher Lambebb97e92008-02-04 02:31:56 +0000639QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000640 QualType CanT = getCanonicalType(T);
641 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000642 return T;
643
644 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
645 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000646 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000647 "Type is already address space qualified");
648
649 // Check if we've already instantiated an address space qual'd type of this
650 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000651 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000652 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000653 void *InsertPos = 0;
654 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
655 return QualType(ASQy, 0);
656
657 // If the base type isn't canonical, this won't be a canonical type either,
658 // so fill in the canonical type field.
659 QualType Canonical;
660 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000661 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000662
663 // Get the new insert position for the node we care about.
664 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000665 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000666 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000667 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000668 ASQualTypes.InsertNode(New, InsertPos);
669 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000670 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000671}
672
Chris Lattnera7674d82007-07-13 22:13:22 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// getComplexType - Return the uniqued reference to the type for a complex
675/// number with the specified element type.
676QualType ASTContext::getComplexType(QualType T) {
677 // Unique pointers, to guarantee there is only one pointer of a particular
678 // structure.
679 llvm::FoldingSetNodeID ID;
680 ComplexType::Profile(ID, T);
681
682 void *InsertPos = 0;
683 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
684 return QualType(CT, 0);
685
686 // If the pointee type isn't canonical, this won't be a canonical type either,
687 // so fill in the canonical type field.
688 QualType Canonical;
689 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000690 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
692 // Get the new insert position for the node we care about.
693 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000694 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 }
696 ComplexType *New = new ComplexType(T, Canonical);
697 Types.push_back(New);
698 ComplexTypes.InsertNode(New, InsertPos);
699 return QualType(New, 0);
700}
701
702
703/// getPointerType - Return the uniqued reference to the type for a pointer to
704/// the specified type.
705QualType ASTContext::getPointerType(QualType T) {
706 // Unique pointers, to guarantee there is only one pointer of a particular
707 // structure.
708 llvm::FoldingSetNodeID ID;
709 PointerType::Profile(ID, T);
710
711 void *InsertPos = 0;
712 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
713 return QualType(PT, 0);
714
715 // If the pointee type isn't canonical, this won't be a canonical type either,
716 // so fill in the canonical type field.
717 QualType Canonical;
718 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000719 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000720
721 // Get the new insert position for the node we care about.
722 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000723 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 }
725 PointerType *New = new PointerType(T, Canonical);
726 Types.push_back(New);
727 PointerTypes.InsertNode(New, InsertPos);
728 return QualType(New, 0);
729}
730
Steve Naroff5618bd42008-08-27 16:04:49 +0000731/// getBlockPointerType - Return the uniqued reference to the type for
732/// a pointer to the specified block.
733QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000734 assert(T->isFunctionType() && "block of function types only");
735 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000736 // structure.
737 llvm::FoldingSetNodeID ID;
738 BlockPointerType::Profile(ID, T);
739
740 void *InsertPos = 0;
741 if (BlockPointerType *PT =
742 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
743 return QualType(PT, 0);
744
Steve Naroff296e8d52008-08-28 19:20:44 +0000745 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000746 // type either so fill in the canonical type field.
747 QualType Canonical;
748 if (!T->isCanonical()) {
749 Canonical = getBlockPointerType(getCanonicalType(T));
750
751 // Get the new insert position for the node we care about.
752 BlockPointerType *NewIP =
753 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000754 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000755 }
756 BlockPointerType *New = new BlockPointerType(T, Canonical);
757 Types.push_back(New);
758 BlockPointerTypes.InsertNode(New, InsertPos);
759 return QualType(New, 0);
760}
761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762/// getReferenceType - Return the uniqued reference to the type for a reference
763/// to the specified type.
764QualType ASTContext::getReferenceType(QualType T) {
765 // Unique pointers, to guarantee there is only one pointer of a particular
766 // structure.
767 llvm::FoldingSetNodeID ID;
768 ReferenceType::Profile(ID, T);
769
770 void *InsertPos = 0;
771 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
772 return QualType(RT, 0);
773
774 // If the referencee type isn't canonical, this won't be a canonical type
775 // either, so fill in the canonical type field.
776 QualType Canonical;
777 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000778 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
780 // Get the new insert position for the node we care about.
781 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000782 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 }
784
785 ReferenceType *New = new ReferenceType(T, Canonical);
786 Types.push_back(New);
787 ReferenceTypes.InsertNode(New, InsertPos);
788 return QualType(New, 0);
789}
790
Steve Narofffb22d962007-08-30 01:06:46 +0000791/// getConstantArrayType - Return the unique reference to the type for an
792/// array of the specified element type.
793QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000794 const llvm::APInt &ArySize,
795 ArrayType::ArraySizeModifier ASM,
796 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000798 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
800 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000801 if (ConstantArrayType *ATP =
802 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 return QualType(ATP, 0);
804
805 // If the element type isn't canonical, this won't be a canonical type either,
806 // so fill in the canonical type field.
807 QualType Canonical;
808 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000809 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000810 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000812 ConstantArrayType *NewIP =
813 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000814 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 }
816
Steve Naroffc9406122007-08-30 18:10:14 +0000817 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
818 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000819 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 Types.push_back(New);
821 return QualType(New, 0);
822}
823
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000824/// getVariableArrayType - Returns a non-unique reference to the type for a
825/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000826QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
827 ArrayType::ArraySizeModifier ASM,
828 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000829 // Since we don't unique expressions, it isn't possible to unique VLA's
830 // that have an expression provided for their size.
831
832 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
833 ASM, EltTypeQuals);
834
835 VariableArrayTypes.push_back(New);
836 Types.push_back(New);
837 return QualType(New, 0);
838}
839
Douglas Gregor898574e2008-12-05 23:32:09 +0000840/// getDependentSizedArrayType - Returns a non-unique reference to
841/// the type for a dependently-sized array of the specified element
842/// type. FIXME: We will need these to be uniqued, or at least
843/// comparable, at some point.
844QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
845 ArrayType::ArraySizeModifier ASM,
846 unsigned EltTypeQuals) {
847 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
848 "Size must be type- or value-dependent!");
849
850 // Since we don't unique expressions, it isn't possible to unique
851 // dependently-sized array types.
852
853 DependentSizedArrayType *New
854 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
855 ASM, EltTypeQuals);
856
857 DependentSizedArrayTypes.push_back(New);
858 Types.push_back(New);
859 return QualType(New, 0);
860}
861
Eli Friedmanc5773c42008-02-15 18:16:39 +0000862QualType ASTContext::getIncompleteArrayType(QualType EltTy,
863 ArrayType::ArraySizeModifier ASM,
864 unsigned EltTypeQuals) {
865 llvm::FoldingSetNodeID ID;
866 IncompleteArrayType::Profile(ID, EltTy);
867
868 void *InsertPos = 0;
869 if (IncompleteArrayType *ATP =
870 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
871 return QualType(ATP, 0);
872
873 // If the element type isn't canonical, this won't be a canonical type
874 // either, so fill in the canonical type field.
875 QualType Canonical;
876
877 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000878 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000879 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000880
881 // Get the new insert position for the node we care about.
882 IncompleteArrayType *NewIP =
883 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000884 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000885 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000886
887 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
888 ASM, EltTypeQuals);
889
890 IncompleteArrayTypes.InsertNode(New, InsertPos);
891 Types.push_back(New);
892 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000893}
894
Steve Naroff73322922007-07-18 18:00:27 +0000895/// getVectorType - Return the unique reference to a vector type of
896/// the specified element type and size. VectorType must be a built-in type.
897QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 BuiltinType *baseType;
899
Chris Lattnerf52ab252008-04-06 22:59:24 +0000900 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000901 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000902
903 // Check if we've already instantiated a vector of this type.
904 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000905 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 void *InsertPos = 0;
907 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
908 return QualType(VTP, 0);
909
910 // If the element type isn't canonical, this won't be a canonical type either,
911 // so fill in the canonical type field.
912 QualType Canonical;
913 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000914 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000915
916 // Get the new insert position for the node we care about.
917 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000918 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 }
920 VectorType *New = new VectorType(vecType, NumElts, Canonical);
921 VectorTypes.InsertNode(New, InsertPos);
922 Types.push_back(New);
923 return QualType(New, 0);
924}
925
Nate Begeman213541a2008-04-18 23:10:10 +0000926/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000927/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000928QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000929 BuiltinType *baseType;
930
Chris Lattnerf52ab252008-04-06 22:59:24 +0000931 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000932 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000933
934 // Check if we've already instantiated a vector of this type.
935 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000936 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000937 void *InsertPos = 0;
938 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
939 return QualType(VTP, 0);
940
941 // If the element type isn't canonical, this won't be a canonical type either,
942 // so fill in the canonical type field.
943 QualType Canonical;
944 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000945 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000946
947 // Get the new insert position for the node we care about.
948 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000949 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +0000950 }
Nate Begeman213541a2008-04-18 23:10:10 +0000951 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000952 VectorTypes.InsertNode(New, InsertPos);
953 Types.push_back(New);
954 return QualType(New, 0);
955}
956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
958///
959QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
960 // Unique functions, to guarantee there is only one function of a particular
961 // structure.
962 llvm::FoldingSetNodeID ID;
963 FunctionTypeNoProto::Profile(ID, ResultTy);
964
965 void *InsertPos = 0;
966 if (FunctionTypeNoProto *FT =
967 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
968 return QualType(FT, 0);
969
970 QualType Canonical;
971 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000972 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000973
974 // Get the new insert position for the node we care about.
975 FunctionTypeNoProto *NewIP =
976 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000977 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 }
979
980 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
981 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000982 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 return QualType(New, 0);
984}
985
986/// getFunctionType - Return a normal function type with a typed argument
987/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +0000988QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000989 unsigned NumArgs, bool isVariadic,
990 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 // Unique functions, to guarantee there is only one function of a particular
992 // structure.
993 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000994 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
995 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996
997 void *InsertPos = 0;
998 if (FunctionTypeProto *FTP =
999 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1000 return QualType(FTP, 0);
1001
1002 // Determine whether the type being created is already canonical or not.
1003 bool isCanonical = ResultTy->isCanonical();
1004 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1005 if (!ArgArray[i]->isCanonical())
1006 isCanonical = false;
1007
1008 // If this type isn't canonical, get the canonical version of it.
1009 QualType Canonical;
1010 if (!isCanonical) {
1011 llvm::SmallVector<QualType, 16> CanonicalArgs;
1012 CanonicalArgs.reserve(NumArgs);
1013 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001014 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001015
Chris Lattnerf52ab252008-04-06 22:59:24 +00001016 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001018 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001019
1020 // Get the new insert position for the node we care about.
1021 FunctionTypeProto *NewIP =
1022 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001023 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 }
1025
1026 // FunctionTypeProto objects are not allocated with new because they have a
1027 // variable size array (for parameter types) at the end of them.
1028 FunctionTypeProto *FTP =
1029 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +00001030 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001032 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 Types.push_back(FTP);
1034 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1035 return QualType(FTP, 0);
1036}
1037
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001038/// getTypeDeclType - Return the unique reference to the type for the
1039/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001040QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001041 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001042 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1043
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001044 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001045 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001046 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1047 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001048 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001049 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001050
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001051 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001052 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1053 : new CXXRecordType(CXXRecord);
1054 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001055 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001056 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1057 : new RecordType(Record);
1058 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001059 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor7df7b6b2008-12-15 16:32:14 +00001060 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1061 : new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001062 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001063 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001064
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001065 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001066 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001067}
1068
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001069void ASTContext::setTagDefinition(TagDecl* D) {
1070 assert (D->isDefinition());
Douglas Gregor7df7b6b2008-12-15 16:32:14 +00001071 cast<TagType>(D->TypeForDecl)->decl = D;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001072}
1073
Reid Spencer5f016e22007-07-11 17:01:13 +00001074/// getTypedefType - Return the unique reference to the type for the
1075/// specified typename decl.
1076QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1077 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1078
Chris Lattnerf52ab252008-04-06 22:59:24 +00001079 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001080 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 Types.push_back(Decl->TypeForDecl);
1082 return QualType(Decl->TypeForDecl, 0);
1083}
1084
Douglas Gregor72c3f312008-12-05 18:15:24 +00001085/// getTemplateTypeParmType - Return the unique reference to the type
1086/// for the specified template type parameter declaration.
1087QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1088 if (!Decl->TypeForDecl) {
1089 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1090 Types.push_back(Decl->TypeForDecl);
1091 }
1092 return QualType(Decl->TypeForDecl, 0);
1093}
1094
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001095/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001096/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001097QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001098 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1099
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001101 Types.push_back(Decl->TypeForDecl);
1102 return QualType(Decl->TypeForDecl, 0);
1103}
1104
Chris Lattner88cb27a2008-04-07 04:56:42 +00001105/// CmpProtocolNames - Comparison predicate for sorting protocols
1106/// alphabetically.
1107static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1108 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001109 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001110}
1111
1112static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1113 unsigned &NumProtocols) {
1114 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1115
1116 // Sort protocols, keyed by name.
1117 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1118
1119 // Remove duplicates.
1120 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1121 NumProtocols = ProtocolsEnd-Protocols;
1122}
1123
1124
Chris Lattner065f0d72008-04-07 04:44:08 +00001125/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1126/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001127QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1128 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001129 // Sort the protocol list alphabetically to canonicalize it.
1130 SortAndUniqueProtocols(Protocols, NumProtocols);
1131
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001132 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001133 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001134
1135 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001136 if (ObjCQualifiedInterfaceType *QT =
1137 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001138 return QualType(QT, 0);
1139
1140 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001141 ObjCQualifiedInterfaceType *QType =
1142 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001143 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001144 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001145 return QualType(QType, 0);
1146}
1147
Chris Lattner88cb27a2008-04-07 04:56:42 +00001148/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1149/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001150QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001151 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001152 // Sort the protocol list alphabetically to canonicalize it.
1153 SortAndUniqueProtocols(Protocols, NumProtocols);
1154
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001155 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001156 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001157
1158 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001159 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001160 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001161 return QualType(QT, 0);
1162
1163 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001164 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001165 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001166 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001167 return QualType(QType, 0);
1168}
1169
Steve Naroff9752f252007-08-01 18:02:17 +00001170/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1171/// TypeOfExpr AST's (since expression's are never shared). For example,
1172/// multiple declarations that refer to "typeof(x)" all contain different
1173/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1174/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001175QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001176 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001177 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1178 Types.push_back(toe);
1179 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001180}
1181
Steve Naroff9752f252007-08-01 18:02:17 +00001182/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1183/// TypeOfType AST's. The only motivation to unique these nodes would be
1184/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1185/// an issue. This doesn't effect the type checker, since it operates
1186/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001187QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001188 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001189 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1190 Types.push_back(tot);
1191 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001192}
1193
Reid Spencer5f016e22007-07-11 17:01:13 +00001194/// getTagDeclType - Return the unique reference to the type for the
1195/// specified TagDecl (struct/union/class/enum) decl.
1196QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001197 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001198 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001199}
1200
1201/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1202/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1203/// needs to agree with the definition in <stddef.h>.
1204QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001205 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001206}
1207
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001208/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001209/// width of characters in wide strings, The value is target dependent and
1210/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001211QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001212 if (LangOpts.CPlusPlus)
1213 return WCharTy;
1214
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001215 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1216 // wide-character type?
1217 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001218}
1219
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001220/// getSignedWCharType - Return the type of "signed wchar_t".
1221/// Used when in C++, as a GCC extension.
1222QualType ASTContext::getSignedWCharType() const {
1223 // FIXME: derive from "Target" ?
1224 return WCharTy;
1225}
1226
1227/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1228/// Used when in C++, as a GCC extension.
1229QualType ASTContext::getUnsignedWCharType() const {
1230 // FIXME: derive from "Target" ?
1231 return UnsignedIntTy;
1232}
1233
Chris Lattner8b9023b2007-07-13 03:05:23 +00001234/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1235/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1236QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001237 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001238}
1239
Chris Lattnere6327742008-04-02 05:18:44 +00001240//===----------------------------------------------------------------------===//
1241// Type Operators
1242//===----------------------------------------------------------------------===//
1243
Chris Lattner77c96472008-04-06 22:41:35 +00001244/// getCanonicalType - Return the canonical (structural) type corresponding to
1245/// the specified potentially non-canonical type. The non-canonical version
1246/// of a type may have many "decorated" versions of types. Decorators can
1247/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1248/// to be free of any of these, allowing two canonical types to be compared
1249/// for exact equality with a simple pointer comparison.
1250QualType ASTContext::getCanonicalType(QualType T) {
1251 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001252
1253 // If the result has type qualifiers, make sure to canonicalize them as well.
1254 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1255 if (TypeQuals == 0) return CanType;
1256
1257 // If the type qualifiers are on an array type, get the canonical type of the
1258 // array with the qualifiers applied to the element type.
1259 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1260 if (!AT)
1261 return CanType.getQualifiedType(TypeQuals);
1262
1263 // Get the canonical version of the element with the extra qualifiers on it.
1264 // This can recursively sink qualifiers through multiple levels of arrays.
1265 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1266 NewEltTy = getCanonicalType(NewEltTy);
1267
1268 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1269 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1270 CAT->getIndexTypeQualifier());
1271 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1272 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1273 IAT->getIndexTypeQualifier());
1274
Douglas Gregor898574e2008-12-05 23:32:09 +00001275 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1276 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1277 DSAT->getSizeModifier(),
1278 DSAT->getIndexTypeQualifier());
1279
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001280 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1281 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1282 VAT->getSizeModifier(),
1283 VAT->getIndexTypeQualifier());
1284}
1285
1286
1287const ArrayType *ASTContext::getAsArrayType(QualType T) {
1288 // Handle the non-qualified case efficiently.
1289 if (T.getCVRQualifiers() == 0) {
1290 // Handle the common positive case fast.
1291 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1292 return AT;
1293 }
1294
1295 // Handle the common negative case fast, ignoring CVR qualifiers.
1296 QualType CType = T->getCanonicalTypeInternal();
1297
1298 // Make sure to look through type qualifiers (like ASQuals) for the negative
1299 // test.
1300 if (!isa<ArrayType>(CType) &&
1301 !isa<ArrayType>(CType.getUnqualifiedType()))
1302 return 0;
1303
1304 // Apply any CVR qualifiers from the array type to the element type. This
1305 // implements C99 6.7.3p8: "If the specification of an array type includes
1306 // any type qualifiers, the element type is so qualified, not the array type."
1307
1308 // If we get here, we either have type qualifiers on the type, or we have
1309 // sugar such as a typedef in the way. If we have type qualifiers on the type
1310 // we must propagate them down into the elemeng type.
1311 unsigned CVRQuals = T.getCVRQualifiers();
1312 unsigned AddrSpace = 0;
1313 Type *Ty = T.getTypePtr();
1314
1315 // Rip through ASQualType's and typedefs to get to a concrete type.
1316 while (1) {
1317 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1318 AddrSpace = ASQT->getAddressSpace();
1319 Ty = ASQT->getBaseType();
1320 } else {
1321 T = Ty->getDesugaredType();
1322 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1323 break;
1324 CVRQuals |= T.getCVRQualifiers();
1325 Ty = T.getTypePtr();
1326 }
1327 }
1328
1329 // If we have a simple case, just return now.
1330 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1331 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1332 return ATy;
1333
1334 // Otherwise, we have an array and we have qualifiers on it. Push the
1335 // qualifiers into the array element type and return a new array type.
1336 // Get the canonical version of the element with the extra qualifiers on it.
1337 // This can recursively sink qualifiers through multiple levels of arrays.
1338 QualType NewEltTy = ATy->getElementType();
1339 if (AddrSpace)
1340 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1341 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1342
1343 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1344 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1345 CAT->getSizeModifier(),
1346 CAT->getIndexTypeQualifier()));
1347 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1348 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1349 IAT->getSizeModifier(),
1350 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001351
Douglas Gregor898574e2008-12-05 23:32:09 +00001352 if (const DependentSizedArrayType *DSAT
1353 = dyn_cast<DependentSizedArrayType>(ATy))
1354 return cast<ArrayType>(
1355 getDependentSizedArrayType(NewEltTy,
1356 DSAT->getSizeExpr(),
1357 DSAT->getSizeModifier(),
1358 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001359
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001360 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1361 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1362 VAT->getSizeModifier(),
1363 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001364}
1365
1366
Chris Lattnere6327742008-04-02 05:18:44 +00001367/// getArrayDecayedType - Return the properly qualified result of decaying the
1368/// specified array type to a pointer. This operation is non-trivial when
1369/// handling typedefs etc. The canonical type of "T" must be an array type,
1370/// this returns a pointer to a properly qualified element of the array.
1371///
1372/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1373QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001374 // Get the element type with 'getAsArrayType' so that we don't lose any
1375 // typedefs in the element type of the array. This also handles propagation
1376 // of type qualifiers from the array type into the element type if present
1377 // (C99 6.7.3p8).
1378 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1379 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001380
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001381 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001382
1383 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001384 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001385}
1386
Reid Spencer5f016e22007-07-11 17:01:13 +00001387/// getFloatingRank - Return a relative rank for floating point types.
1388/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001389static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001390 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001392
Christopher Lambebb97e92008-02-04 02:31:56 +00001393 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001394 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 case BuiltinType::Float: return FloatRank;
1396 case BuiltinType::Double: return DoubleRank;
1397 case BuiltinType::LongDouble: return LongDoubleRank;
1398 }
1399}
1400
Steve Naroff716c7302007-08-27 01:41:48 +00001401/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1402/// point or a complex type (based on typeDomain/typeSize).
1403/// 'typeDomain' is a real floating point or complex type.
1404/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001405QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1406 QualType Domain) const {
1407 FloatingRank EltRank = getFloatingRank(Size);
1408 if (Domain->isComplexType()) {
1409 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001410 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001411 case FloatRank: return FloatComplexTy;
1412 case DoubleRank: return DoubleComplexTy;
1413 case LongDoubleRank: return LongDoubleComplexTy;
1414 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 }
Chris Lattner1361b112008-04-06 23:58:54 +00001416
1417 assert(Domain->isRealFloatingType() && "Unknown domain!");
1418 switch (EltRank) {
1419 default: assert(0 && "getFloatingRank(): illegal value for rank");
1420 case FloatRank: return FloatTy;
1421 case DoubleRank: return DoubleTy;
1422 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001423 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001424}
1425
Chris Lattner7cfeb082008-04-06 23:55:33 +00001426/// getFloatingTypeOrder - Compare the rank of the two specified floating
1427/// point types, ignoring the domain of the type (i.e. 'double' ==
1428/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1429/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001430int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1431 FloatingRank LHSR = getFloatingRank(LHS);
1432 FloatingRank RHSR = getFloatingRank(RHS);
1433
1434 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001435 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001436 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001437 return 1;
1438 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001439}
1440
Chris Lattnerf52ab252008-04-06 22:59:24 +00001441/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1442/// routine will assert if passed a built-in type that isn't an integer or enum,
1443/// or if it is not canonicalized.
1444static unsigned getIntegerRank(Type *T) {
1445 assert(T->isCanonical() && "T should be canonicalized");
1446 if (isa<EnumType>(T))
1447 return 4;
1448
1449 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001450 default: assert(0 && "getIntegerRank(): not a built-in integer");
1451 case BuiltinType::Bool:
1452 return 1;
1453 case BuiltinType::Char_S:
1454 case BuiltinType::Char_U:
1455 case BuiltinType::SChar:
1456 case BuiltinType::UChar:
1457 return 2;
1458 case BuiltinType::Short:
1459 case BuiltinType::UShort:
1460 return 3;
1461 case BuiltinType::Int:
1462 case BuiltinType::UInt:
1463 return 4;
1464 case BuiltinType::Long:
1465 case BuiltinType::ULong:
1466 return 5;
1467 case BuiltinType::LongLong:
1468 case BuiltinType::ULongLong:
1469 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001470 }
1471}
1472
Chris Lattner7cfeb082008-04-06 23:55:33 +00001473/// getIntegerTypeOrder - Returns the highest ranked integer type:
1474/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1475/// LHS < RHS, return -1.
1476int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001477 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1478 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001479 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001480
Chris Lattnerf52ab252008-04-06 22:59:24 +00001481 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1482 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001483
Chris Lattner7cfeb082008-04-06 23:55:33 +00001484 unsigned LHSRank = getIntegerRank(LHSC);
1485 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001486
Chris Lattner7cfeb082008-04-06 23:55:33 +00001487 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1488 if (LHSRank == RHSRank) return 0;
1489 return LHSRank > RHSRank ? 1 : -1;
1490 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001491
Chris Lattner7cfeb082008-04-06 23:55:33 +00001492 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1493 if (LHSUnsigned) {
1494 // If the unsigned [LHS] type is larger, return it.
1495 if (LHSRank >= RHSRank)
1496 return 1;
1497
1498 // If the signed type can represent all values of the unsigned type, it
1499 // wins. Because we are dealing with 2's complement and types that are
1500 // powers of two larger than each other, this is always safe.
1501 return -1;
1502 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001503
Chris Lattner7cfeb082008-04-06 23:55:33 +00001504 // If the unsigned [RHS] type is larger, return it.
1505 if (RHSRank >= LHSRank)
1506 return -1;
1507
1508 // If the signed type can represent all values of the unsigned type, it
1509 // wins. Because we are dealing with 2's complement and types that are
1510 // powers of two larger than each other, this is always safe.
1511 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001512}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001513
1514// getCFConstantStringType - Return the type used for constant CFStrings.
1515QualType ASTContext::getCFConstantStringType() {
1516 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001517 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001518 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001519 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001520 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001521
1522 // const int *isa;
1523 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001524 // int flags;
1525 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001526 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001527 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001528 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001529 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001530
Anders Carlsson71993dd2007-08-17 05:31:46 +00001531 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001532 for (unsigned i = 0; i < 4; ++i) {
1533 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1534 SourceLocation(), 0,
1535 FieldTypes[i], /*BitWidth=*/0,
1536 /*Mutable=*/false, /*PrevDecl=*/0);
1537 CFConstantStringTypeDecl->addDecl(*this, Field, true);
1538 }
1539
1540 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001541 }
1542
1543 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001544}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001545
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001546QualType ASTContext::getObjCFastEnumerationStateType()
1547{
1548 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001549 ObjCFastEnumerationStateTypeDecl =
1550 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1551 &Idents.get("__objcFastEnumerationState"));
1552
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001553 QualType FieldTypes[] = {
1554 UnsignedLongTy,
1555 getPointerType(ObjCIdType),
1556 getPointerType(UnsignedLongTy),
1557 getConstantArrayType(UnsignedLongTy,
1558 llvm::APInt(32, 5), ArrayType::Normal, 0)
1559 };
1560
Douglas Gregor44b43212008-12-11 16:49:14 +00001561 for (size_t i = 0; i < 4; ++i) {
1562 FieldDecl *Field = FieldDecl::Create(*this,
1563 ObjCFastEnumerationStateTypeDecl,
1564 SourceLocation(), 0,
1565 FieldTypes[i], /*BitWidth=*/0,
1566 /*Mutable=*/false, /*PrevDecl=*/0);
1567 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field, true);
1568 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001569
Douglas Gregor44b43212008-12-11 16:49:14 +00001570 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001571 }
1572
1573 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1574}
1575
Anders Carlssone8c49532007-10-29 06:33:42 +00001576// This returns true if a type has been typedefed to BOOL:
1577// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001578static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001579 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001580 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1581 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001582
1583 return false;
1584}
1585
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001586/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001587/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001588int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001589 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001590
1591 // Make all integer and enum types at least as large as an int
1592 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001593 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001594 // Treat arrays as pointers, since that's how they're passed in.
1595 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001596 sz = getTypeSize(VoidPtrTy);
1597 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001598}
1599
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001600/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001601/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001602void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001603 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001604 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001605 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001607 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001608 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001609 // Compute size of all parameters.
1610 // Start with computing size of a pointer in number of bytes.
1611 // FIXME: There might(should) be a better way of doing this computation!
1612 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001613 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001614 // The first two arguments (self and _cmd) are pointers; account for
1615 // their size.
1616 int ParmOffset = 2 * PtrSize;
1617 int NumOfParams = Decl->getNumParams();
1618 for (int i = 0; i < NumOfParams; i++) {
1619 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001620 int sz = getObjCEncodingTypeSize (PType);
1621 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001622 ParmOffset += sz;
1623 }
1624 S += llvm::utostr(ParmOffset);
1625 S += "@0:";
1626 S += llvm::utostr(PtrSize);
1627
1628 // Argument types.
1629 ParmOffset = 2 * PtrSize;
1630 for (int i = 0; i < NumOfParams; i++) {
1631 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001632 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001633 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001634 getObjCEncodingForTypeQualifier(
1635 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001636 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001637 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001638 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001639 }
1640}
1641
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001642/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1643/// method declaration. If non-NULL, Container must be either an
1644/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1645/// NULL when getting encodings for protocol properties.
1646void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1647 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001648 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001649 // Collect information from the property implementation decl(s).
1650 bool Dynamic = false;
1651 ObjCPropertyImplDecl *SynthesizePID = 0;
1652
1653 // FIXME: Duplicated code due to poor abstraction.
1654 if (Container) {
1655 if (const ObjCCategoryImplDecl *CID =
1656 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1657 for (ObjCCategoryImplDecl::propimpl_iterator
1658 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1659 ObjCPropertyImplDecl *PID = *i;
1660 if (PID->getPropertyDecl() == PD) {
1661 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1662 Dynamic = true;
1663 } else {
1664 SynthesizePID = PID;
1665 }
1666 }
1667 }
1668 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001669 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001670 for (ObjCCategoryImplDecl::propimpl_iterator
1671 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1672 ObjCPropertyImplDecl *PID = *i;
1673 if (PID->getPropertyDecl() == PD) {
1674 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1675 Dynamic = true;
1676 } else {
1677 SynthesizePID = PID;
1678 }
1679 }
1680 }
1681 }
1682 }
1683
1684 // FIXME: This is not very efficient.
1685 S = "T";
1686
1687 // Encode result type.
1688 // FIXME: GCC uses a generating_property_type_encoding mode during
1689 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001690 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001691
1692 if (PD->isReadOnly()) {
1693 S += ",R";
1694 } else {
1695 switch (PD->getSetterKind()) {
1696 case ObjCPropertyDecl::Assign: break;
1697 case ObjCPropertyDecl::Copy: S += ",C"; break;
1698 case ObjCPropertyDecl::Retain: S += ",&"; break;
1699 }
1700 }
1701
1702 // It really isn't clear at all what this means, since properties
1703 // are "dynamic by default".
1704 if (Dynamic)
1705 S += ",D";
1706
1707 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1708 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001709 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001710 }
1711
1712 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1713 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001714 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001715 }
1716
1717 if (SynthesizePID) {
1718 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1719 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001720 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001721 }
1722
1723 // FIXME: OBJCGC: weak & strong
1724}
1725
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001726void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001727 bool NameFields) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001728 // We follow the behavior of gcc, expanding structures which are
1729 // directly pointed to, and expanding embedded structures. Note that
1730 // these rules are sufficient to prevent recursive encoding of the
1731 // same type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001732 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001733}
1734
1735void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1736 bool ExpandPointedToStructures,
1737 bool ExpandStructures,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001738 bool NameFields) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001739 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001740 char encoding;
1741 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001742 default: assert(0 && "Unhandled builtin type kind");
1743 case BuiltinType::Void: encoding = 'v'; break;
1744 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001745 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001746 case BuiltinType::UChar: encoding = 'C'; break;
1747 case BuiltinType::UShort: encoding = 'S'; break;
1748 case BuiltinType::UInt: encoding = 'I'; break;
1749 case BuiltinType::ULong: encoding = 'L'; break;
1750 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001751 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001752 case BuiltinType::SChar: encoding = 'c'; break;
1753 case BuiltinType::Short: encoding = 's'; break;
1754 case BuiltinType::Int: encoding = 'i'; break;
1755 case BuiltinType::Long: encoding = 'l'; break;
1756 case BuiltinType::LongLong: encoding = 'q'; break;
1757 case BuiltinType::Float: encoding = 'f'; break;
1758 case BuiltinType::Double: encoding = 'd'; break;
1759 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001760 }
1761
1762 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001763 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001764 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001765 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001766 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1767 ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001768 ExpandStructures, NameFields);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001769 }
1770 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001771 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001772 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001773 S += '@';
1774 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001775 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001776 S += '#';
1777 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001778 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001779 S += ':';
1780 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001781 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001782
1783 if (PointeeTy->isCharType()) {
1784 // char pointer types should be encoded as '*' unless it is a
1785 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001786 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001787 S += '*';
1788 return;
1789 }
1790 }
1791
1792 S += '^';
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001793 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001794 false, ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001795 NameFields);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001796 } else if (const ArrayType *AT =
1797 // Ignore type qualifiers etc.
1798 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001799 S += '[';
1800
1801 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1802 S += llvm::utostr(CAT->getSize().getZExtValue());
1803 else
1804 assert(0 && "Unhandled array type!");
1805
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001806 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001807 false, ExpandStructures, NameFields);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001808 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001809 } else if (T->getAsFunctionType()) {
1810 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001811 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001812 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001813 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001814 // Anonymous structures print as '?'
1815 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1816 S += II->getName();
1817 } else {
1818 S += '?';
1819 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001820 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001821 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00001822 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1823 FieldEnd = RDecl->field_end();
1824 Field != FieldEnd; ++Field) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001825 if (NameFields) {
1826 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00001827 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001828 S += '"';
1829 }
1830
1831 // Special case bit-fields.
Douglas Gregor44b43212008-12-11 16:49:14 +00001832 if (const Expr *E = Field->getBitWidth()) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001833 // FIXME: Fix constness.
1834 ASTContext *Ctx = const_cast<ASTContext*>(this);
1835 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1836 // FIXME: Obj-C is losing information about the type size
1837 // here. Investigate if this is a problem.
1838 S += 'b';
1839 S += llvm::utostr(N);
1840 } else {
Douglas Gregor44b43212008-12-11 16:49:14 +00001841 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1842 NameFields);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001843 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001844 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001845 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001846 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001847 } else if (T->isEnumeralType()) {
1848 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001849 } else if (T->isBlockPointerType()) {
1850 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001851 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001852 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001853}
1854
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001855void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001856 std::string& S) const {
1857 if (QT & Decl::OBJC_TQ_In)
1858 S += 'n';
1859 if (QT & Decl::OBJC_TQ_Inout)
1860 S += 'N';
1861 if (QT & Decl::OBJC_TQ_Out)
1862 S += 'o';
1863 if (QT & Decl::OBJC_TQ_Bycopy)
1864 S += 'O';
1865 if (QT & Decl::OBJC_TQ_Byref)
1866 S += 'R';
1867 if (QT & Decl::OBJC_TQ_Oneway)
1868 S += 'V';
1869}
1870
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001871void ASTContext::setBuiltinVaListType(QualType T)
1872{
1873 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1874
1875 BuiltinVaListType = T;
1876}
1877
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001878void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001879{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001880 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001881
1882 // typedef struct objc_object *id;
1883 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1884 assert(ptr && "'id' incorrectly typed");
1885 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1886 assert(rec && "'id' incorrectly typed");
1887 IdStructType = rec;
1888}
1889
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001890void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001891{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001892 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001893
1894 // typedef struct objc_selector *SEL;
1895 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1896 assert(ptr && "'SEL' incorrectly typed");
1897 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1898 assert(rec && "'SEL' incorrectly typed");
1899 SelStructType = rec;
1900}
1901
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001902void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001903{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001904 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001905}
1906
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001907void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001908{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001909 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001910
1911 // typedef struct objc_class *Class;
1912 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1913 assert(ptr && "'Class' incorrectly typed");
1914 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1915 assert(rec && "'Class' incorrectly typed");
1916 ClassStructType = rec;
1917}
1918
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001919void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1920 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001921 "'NSConstantString' type already set!");
1922
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001923 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001924}
1925
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001926/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00001927/// TargetInfo, produce the corresponding type. The unsigned @p Type
1928/// is actually a value of type @c TargetInfo::IntType.
1929QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001930 switch (Type) {
1931 case TargetInfo::NoInt: return QualType();
1932 case TargetInfo::SignedShort: return ShortTy;
1933 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1934 case TargetInfo::SignedInt: return IntTy;
1935 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1936 case TargetInfo::SignedLong: return LongTy;
1937 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1938 case TargetInfo::SignedLongLong: return LongLongTy;
1939 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1940 }
1941
1942 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00001943 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001944}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001945
1946//===----------------------------------------------------------------------===//
1947// Type Predicates.
1948//===----------------------------------------------------------------------===//
1949
1950/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1951/// to an object type. This includes "id" and "Class" (two 'special' pointers
1952/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1953/// ID type).
1954bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1955 if (Ty->isObjCQualifiedIdType())
1956 return true;
1957
Steve Naroff6ae98502008-10-21 18:24:04 +00001958 // Blocks are objects.
1959 if (Ty->isBlockPointerType())
1960 return true;
1961
1962 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001963 if (!Ty->isPointerType())
1964 return false;
1965
1966 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1967 // pointer types. This looks for the typedef specifically, not for the
1968 // underlying type.
1969 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1970 return true;
1971
1972 // If this a pointer to an interface (e.g. NSString*), it is ok.
1973 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1974}
1975
Chris Lattner6ac46a42008-04-07 06:51:04 +00001976//===----------------------------------------------------------------------===//
1977// Type Compatibility Testing
1978//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001979
Steve Naroff1c7d0672008-09-04 15:10:53 +00001980/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001981/// block types. Types must be strictly compatible here. For example,
1982/// C unfortunately doesn't produce an error for the following:
1983///
1984/// int (*emptyArgFunc)();
1985/// int (*intArgList)(int) = emptyArgFunc;
1986///
1987/// For blocks, we will produce an error for the following (similar to C++):
1988///
1989/// int (^emptyArgBlock)();
1990/// int (^intArgBlock)(int) = emptyArgBlock;
1991///
1992/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1993///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001994bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00001995 const FunctionType *lbase = lhs->getAsFunctionType();
1996 const FunctionType *rbase = rhs->getAsFunctionType();
1997 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1998 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1999 if (lproto && rproto)
2000 return !mergeTypes(lhs, rhs).isNull();
2001 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002002}
2003
Chris Lattner6ac46a42008-04-07 06:51:04 +00002004/// areCompatVectorTypes - Return true if the two specified vector types are
2005/// compatible.
2006static bool areCompatVectorTypes(const VectorType *LHS,
2007 const VectorType *RHS) {
2008 assert(LHS->isCanonical() && RHS->isCanonical());
2009 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002010 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002011}
2012
Eli Friedman3d815e72008-08-22 00:56:42 +00002013/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002014/// compatible for assignment from RHS to LHS. This handles validation of any
2015/// protocol qualifiers on the LHS or RHS.
2016///
Eli Friedman3d815e72008-08-22 00:56:42 +00002017bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2018 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002019 // Verify that the base decls are compatible: the RHS must be a subclass of
2020 // the LHS.
2021 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2022 return false;
2023
2024 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2025 // protocol qualified at all, then we are good.
2026 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2027 return true;
2028
2029 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2030 // isn't a superset.
2031 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2032 return true; // FIXME: should return false!
2033
2034 // Finally, we must have two protocol-qualified interfaces.
2035 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2036 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2037 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2038 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2039 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2040 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2041
2042 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2043 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2044 // LHS in a single parallel scan until we run out of elements in LHS.
2045 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2046 ObjCProtocolDecl *LHSProto = *LHSPI;
2047
2048 while (RHSPI != RHSPE) {
2049 ObjCProtocolDecl *RHSProto = *RHSPI++;
2050 // If the RHS has a protocol that the LHS doesn't, ignore it.
2051 if (RHSProto != LHSProto)
2052 continue;
2053
2054 // Otherwise, the RHS does have this element.
2055 ++LHSPI;
2056 if (LHSPI == LHSPE)
2057 return true; // All protocols in LHS exist in RHS.
2058
2059 LHSProto = *LHSPI;
2060 }
2061
2062 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2063 return false;
2064}
2065
Steve Naroffec0550f2007-10-15 20:41:53 +00002066/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2067/// both shall have the identically qualified version of a compatible type.
2068/// C99 6.2.7p1: Two types have compatible types if their types are the
2069/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002070bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2071 return !mergeTypes(LHS, RHS).isNull();
2072}
2073
2074QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2075 const FunctionType *lbase = lhs->getAsFunctionType();
2076 const FunctionType *rbase = rhs->getAsFunctionType();
2077 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2078 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2079 bool allLTypes = true;
2080 bool allRTypes = true;
2081
2082 // Check return type
2083 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2084 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002085 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2086 allLTypes = false;
2087 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2088 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002089
2090 if (lproto && rproto) { // two C99 style function prototypes
2091 unsigned lproto_nargs = lproto->getNumArgs();
2092 unsigned rproto_nargs = rproto->getNumArgs();
2093
2094 // Compatible functions must have the same number of arguments
2095 if (lproto_nargs != rproto_nargs)
2096 return QualType();
2097
2098 // Variadic and non-variadic functions aren't compatible
2099 if (lproto->isVariadic() != rproto->isVariadic())
2100 return QualType();
2101
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002102 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2103 return QualType();
2104
Eli Friedman3d815e72008-08-22 00:56:42 +00002105 // Check argument compatibility
2106 llvm::SmallVector<QualType, 10> types;
2107 for (unsigned i = 0; i < lproto_nargs; i++) {
2108 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2109 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2110 QualType argtype = mergeTypes(largtype, rargtype);
2111 if (argtype.isNull()) return QualType();
2112 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002113 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2114 allLTypes = false;
2115 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2116 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002117 }
2118 if (allLTypes) return lhs;
2119 if (allRTypes) return rhs;
2120 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002121 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002122 }
2123
2124 if (lproto) allRTypes = false;
2125 if (rproto) allLTypes = false;
2126
2127 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2128 if (proto) {
2129 if (proto->isVariadic()) return QualType();
2130 // Check that the types are compatible with the types that
2131 // would result from default argument promotions (C99 6.7.5.3p15).
2132 // The only types actually affected are promotable integer
2133 // types and floats, which would be passed as a different
2134 // type depending on whether the prototype is visible.
2135 unsigned proto_nargs = proto->getNumArgs();
2136 for (unsigned i = 0; i < proto_nargs; ++i) {
2137 QualType argTy = proto->getArgType(i);
2138 if (argTy->isPromotableIntegerType() ||
2139 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2140 return QualType();
2141 }
2142
2143 if (allLTypes) return lhs;
2144 if (allRTypes) return rhs;
2145 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002146 proto->getNumArgs(), lproto->isVariadic(),
2147 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002148 }
2149
2150 if (allLTypes) return lhs;
2151 if (allRTypes) return rhs;
2152 return getFunctionTypeNoProto(retType);
2153}
2154
2155QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002156 // C++ [expr]: If an expression initially has the type "reference to T", the
2157 // type is adjusted to "T" prior to any further analysis, the expression
2158 // designates the object or function denoted by the reference, and the
2159 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002160 // FIXME: C++ shouldn't be going through here! The rules are different
2161 // enough that they should be handled separately.
2162 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002163 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002164 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002165 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002166
Eli Friedman3d815e72008-08-22 00:56:42 +00002167 QualType LHSCan = getCanonicalType(LHS),
2168 RHSCan = getCanonicalType(RHS);
2169
2170 // If two types are identical, they are compatible.
2171 if (LHSCan == RHSCan)
2172 return LHS;
2173
2174 // If the qualifiers are different, the types aren't compatible
2175 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2176 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2177 return QualType();
2178
2179 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2180 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2181
Chris Lattner1adb8832008-01-14 05:45:46 +00002182 // We want to consider the two function types to be the same for these
2183 // comparisons, just force one to the other.
2184 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2185 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002186
2187 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002188 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2189 LHSClass = Type::ConstantArray;
2190 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2191 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002192
Nate Begeman213541a2008-04-18 23:10:10 +00002193 // Canonicalize ExtVector -> Vector.
2194 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2195 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002196
Chris Lattnerb0489812008-04-07 06:38:24 +00002197 // Consider qualified interfaces and interfaces the same.
2198 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2199 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002200
Chris Lattnera36a61f2008-04-07 05:43:21 +00002201 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002202 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002203 // ID is compatible with all qualified id types.
2204 if (LHS->isObjCQualifiedIdType()) {
2205 if (const PointerType *PT = RHS->getAsPointerType()) {
2206 QualType pType = PT->getPointeeType();
2207 if (isObjCIdType(pType))
2208 return LHS;
2209 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2210 // Unfortunately, this API is part of Sema (which we don't have access
2211 // to. Need to refactor. The following check is insufficient, since we
2212 // need to make sure the class implements the protocol.
2213 if (pType->isObjCInterfaceType())
2214 return LHS;
2215 }
2216 }
2217 if (RHS->isObjCQualifiedIdType()) {
2218 if (const PointerType *PT = LHS->getAsPointerType()) {
2219 QualType pType = PT->getPointeeType();
2220 if (isObjCIdType(pType))
2221 return RHS;
2222 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2223 // Unfortunately, this API is part of Sema (which we don't have access
2224 // to. Need to refactor. The following check is insufficient, since we
2225 // need to make sure the class implements the protocol.
2226 if (pType->isObjCInterfaceType())
2227 return RHS;
2228 }
2229 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002230 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2231 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002232 if (const EnumType* ETy = LHS->getAsEnumType()) {
2233 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2234 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002235 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002236 if (const EnumType* ETy = RHS->getAsEnumType()) {
2237 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2238 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002239 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002240
Eli Friedman3d815e72008-08-22 00:56:42 +00002241 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002242 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002243
Steve Naroff4a746782008-01-09 22:43:08 +00002244 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002245 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002246 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002247 {
2248 // Merge two pointer types, while trying to preserve typedef info
2249 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2250 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2251 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2252 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002253 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2254 return LHS;
2255 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2256 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002257 return getPointerType(ResultType);
2258 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002259 case Type::BlockPointer:
2260 {
2261 // Merge two block pointer types, while trying to preserve typedef info
2262 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2263 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2264 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2265 if (ResultType.isNull()) return QualType();
2266 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2267 return LHS;
2268 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2269 return RHS;
2270 return getBlockPointerType(ResultType);
2271 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002272 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002273 {
2274 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2275 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2276 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2277 return QualType();
2278
2279 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2280 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2281 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2282 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002283 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2284 return LHS;
2285 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2286 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002287 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2288 ArrayType::ArraySizeModifier(), 0);
2289 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2290 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002291 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2292 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002293 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2294 return LHS;
2295 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2296 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002297 if (LVAT) {
2298 // FIXME: This isn't correct! But tricky to implement because
2299 // the array's size has to be the size of LHS, but the type
2300 // has to be different.
2301 return LHS;
2302 }
2303 if (RVAT) {
2304 // FIXME: This isn't correct! But tricky to implement because
2305 // the array's size has to be the size of RHS, but the type
2306 // has to be different.
2307 return RHS;
2308 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002309 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2310 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002311 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002312 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002313 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002314 return mergeFunctionTypes(LHS, RHS);
2315 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002316 // FIXME: Why are these compatible?
2317 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2318 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2319 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002320 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002321 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002322 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002323 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002324 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2325 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002326 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002327 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002328 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2329 // for checking assignment/comparison safety
2330 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002331 case Type::ObjCQualifiedId:
2332 // Distinct qualified id's are not compatible.
2333 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002334 default:
2335 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002336 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002337 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002338}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002339
Chris Lattner5426bf62008-04-07 07:01:58 +00002340//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002341// Integer Predicates
2342//===----------------------------------------------------------------------===//
2343unsigned ASTContext::getIntWidth(QualType T) {
2344 if (T == BoolTy)
2345 return 1;
2346 // At the moment, only bool has padding bits
2347 return (unsigned)getTypeSize(T);
2348}
2349
2350QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2351 assert(T->isSignedIntegerType() && "Unexpected type");
2352 if (const EnumType* ETy = T->getAsEnumType())
2353 T = ETy->getDecl()->getIntegerType();
2354 const BuiltinType* BTy = T->getAsBuiltinType();
2355 assert (BTy && "Unexpected signed integer type");
2356 switch (BTy->getKind()) {
2357 case BuiltinType::Char_S:
2358 case BuiltinType::SChar:
2359 return UnsignedCharTy;
2360 case BuiltinType::Short:
2361 return UnsignedShortTy;
2362 case BuiltinType::Int:
2363 return UnsignedIntTy;
2364 case BuiltinType::Long:
2365 return UnsignedLongTy;
2366 case BuiltinType::LongLong:
2367 return UnsignedLongLongTy;
2368 default:
2369 assert(0 && "Unexpected signed integer type");
2370 return QualType();
2371 }
2372}
2373
2374
2375//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002376// Serialization Support
2377//===----------------------------------------------------------------------===//
2378
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002379/// Emit - Serialize an ASTContext object to Bitcode.
2380void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002381 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002382 S.EmitRef(SourceMgr);
2383 S.EmitRef(Target);
2384 S.EmitRef(Idents);
2385 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002386
Ted Kremenekfee04522007-10-31 22:44:07 +00002387 // Emit the size of the type vector so that we can reserve that size
2388 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002389 S.EmitInt(Types.size());
2390
Ted Kremenek03ed4402007-11-13 22:02:55 +00002391 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2392 I!=E;++I)
2393 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002394
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002395 S.EmitOwnedPtr(TUDecl);
2396
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002397 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002398}
2399
Ted Kremenek0f84c002007-11-13 00:25:37 +00002400ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002401
2402 // Read the language options.
2403 LangOptions LOpts;
2404 LOpts.Read(D);
2405
Ted Kremenekfee04522007-10-31 22:44:07 +00002406 SourceManager &SM = D.ReadRef<SourceManager>();
2407 TargetInfo &t = D.ReadRef<TargetInfo>();
2408 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2409 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002410
Ted Kremenekfee04522007-10-31 22:44:07 +00002411 unsigned size_reserve = D.ReadInt();
2412
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002413 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2414 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002415
Ted Kremenek03ed4402007-11-13 22:02:55 +00002416 for (unsigned i = 0; i < size_reserve; ++i)
2417 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002418
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002419 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2420
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002421 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002422
2423 return A;
2424}