blob: 2acdfaca77f83971e76620038fe5654def54c45a [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
Daniel Dunbare91593e2008-08-11 04:54:23 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
31 IdentifierTable &idents, SelectorTable &sels,
32 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000033 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
34 SourceMgr(SM), LangOpts(LOpts), Target(t),
Daniel Dunbare91593e2008-08-11 04:54:23 +000035 Idents(idents), Selectors(sels)
36{
37 if (size_reserve > 0) Types.reserve(size_reserve);
38 InitBuiltinTypes();
39 BuiltinInfo.InitializeBuiltins(idents, Target);
40 TUDecl = TranslationUnitDecl::Create(*this);
41}
42
Reid Spencer5f016e22007-07-11 17:01:13 +000043ASTContext::~ASTContext() {
44 // Deallocate all the types.
45 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000046 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 Types.pop_back();
48 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000049
50 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53void ASTContext::PrintStats() const {
54 fprintf(stderr, "*** AST Context Stats:\n");
55 fprintf(stderr, " %d types total.\n", (int)Types.size());
56 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000057 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
59
60 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
62 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000063 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
65 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
66 Type *T = Types[i];
67 if (isa<BuiltinType>(T))
68 ++NumBuiltin;
69 else if (isa<PointerType>(T))
70 ++NumPointer;
71 else if (isa<ReferenceType>(T))
72 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000073 else if (isa<ComplexType>(T))
74 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 else if (isa<ArrayType>(T))
76 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000077 else if (isa<VectorType>(T))
78 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 else if (isa<FunctionTypeNoProto>(T))
80 ++NumFunctionNP;
81 else if (isa<FunctionTypeProto>(T))
82 ++NumFunctionP;
83 else if (isa<TypedefType>(T))
84 ++NumTypeName;
85 else if (TagType *TT = dyn_cast<TagType>(T)) {
86 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000087 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000089 case TagDecl::TK_struct: ++NumTagStruct; break;
90 case TagDecl::TK_union: ++NumTagUnion; break;
91 case TagDecl::TK_class: ++NumTagClass; break;
92 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000093 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000094 } else if (isa<ObjCInterfaceType>(T))
95 ++NumObjCInterfaces;
96 else if (isa<ObjCQualifiedInterfaceType>(T))
97 ++NumObjCQualifiedInterfaces;
98 else if (isa<ObjCQualifiedIdType>(T))
99 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000100 else if (isa<TypeOfType>(T))
101 ++NumTypeOfTypes;
102 else if (isa<TypeOfExpr>(T))
103 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000104 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000105 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 assert(0 && "Unknown type!");
107 }
108 }
109
110 fprintf(stderr, " %d builtin types\n", NumBuiltin);
111 fprintf(stderr, " %d pointer types\n", NumPointer);
112 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000113 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000115 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
117 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
118 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
119 fprintf(stderr, " %d tagged types\n", NumTagged);
120 fprintf(stderr, " %d struct types\n", NumTagStruct);
121 fprintf(stderr, " %d union types\n", NumTagUnion);
122 fprintf(stderr, " %d class types\n", NumTagClass);
123 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000124 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000125 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000126 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000127 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000129 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
130 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
133 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000134 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 NumFunctionP*sizeof(FunctionTypeProto)+
136 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000137 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
138 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000139}
140
141
142void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
143 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
144}
145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
148
149 // C99 6.2.5p19.
150 InitBuiltinType(VoidTy, BuiltinType::Void);
151
152 // C99 6.2.5p2.
153 InitBuiltinType(BoolTy, BuiltinType::Bool);
154 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000155 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
159 // C99 6.2.5p4.
160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
165
166 // C99 6.2.5p6.
167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
172
173 // C99 6.2.5p10.
174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
178 // C++ 3.9.1p5
179 InitBuiltinType(WCharTy, BuiltinType::WChar);
180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // C99 6.2.5p11.
182 FloatComplexTy = getComplexType(FloatTy);
183 DoubleComplexTy = getComplexType(DoubleTy);
184 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000185
186 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000188 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000189 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000190 ClassStructType = 0;
191
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000193
194 // void * type
195 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
Chris Lattner464175b2007-07-18 17:52:12 +0000198//===----------------------------------------------------------------------===//
199// Type Sizing and Analysis
200//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000201
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000202/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
203/// scalar floating point type.
204const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
205 const BuiltinType *BT = T->getAsBuiltinType();
206 assert(BT && "Not a floating point type!");
207 switch (BT->getKind()) {
208 default: assert(0 && "Not a floating point type!");
209 case BuiltinType::Float: return Target.getFloatFormat();
210 case BuiltinType::Double: return Target.getDoubleFormat();
211 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
212 }
213}
214
215
Chris Lattnera7674d82007-07-13 22:13:22 +0000216/// getTypeSize - Return the size of the specified type, in bits. This method
217/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000218std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000219ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000220 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000221 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000222 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000223 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000224 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000225 case Type::FunctionNoProto:
226 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000227 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000228 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000229 case Type::VariableArray:
230 assert(0 && "VLAs not implemented yet!");
231 case Type::ConstantArray: {
232 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
233
Chris Lattner98be4942008-03-05 18:54:05 +0000234 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000235 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000236 Align = EltInfo.second;
237 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000238 }
Nate Begeman213541a2008-04-18 23:10:10 +0000239 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000240 case Type::Vector: {
241 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000242 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000243 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000244 // FIXME: This isn't right for unusual vectors
245 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000246 break;
247 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000248
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000249 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000250 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000251 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000252 case BuiltinType::Void:
253 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000254 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000255 Width = Target.getBoolWidth();
256 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000257 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000258 case BuiltinType::Char_S:
259 case BuiltinType::Char_U:
260 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000261 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000262 Width = Target.getCharWidth();
263 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000264 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000265 case BuiltinType::WChar:
266 Width = Target.getWCharWidth();
267 Align = Target.getWCharAlign();
268 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000269 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000270 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000271 Width = Target.getShortWidth();
272 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000273 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000274 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000275 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000276 Width = Target.getIntWidth();
277 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000278 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000279 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000280 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000281 Width = Target.getLongWidth();
282 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000283 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000284 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000285 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000286 Width = Target.getLongLongWidth();
287 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000288 break;
289 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000290 Width = Target.getFloatWidth();
291 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000292 break;
293 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000294 Width = Target.getDoubleWidth();
295 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000296 break;
297 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000298 Width = Target.getLongDoubleWidth();
299 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000300 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000301 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000302 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000303 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000304 // FIXME: Pointers into different addr spaces could have different sizes and
305 // alignment requirements: getPointerInfo should take an AddrSpace.
306 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000307 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000308 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000309 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000310 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000311 case Type::Pointer: {
312 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000313 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000314 Align = Target.getPointerAlign(AS);
315 break;
316 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000317 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000318 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000319 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000320 // FIXME: This is wrong for struct layout: a reference in a struct has
321 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000322 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000323
324 case Type::Complex: {
325 // Complex types have the same alignment as their elements, but twice the
326 // size.
327 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000328 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000329 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000330 Align = EltInfo.second;
331 break;
332 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000333 case Type::ObjCInterface: {
334 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
335 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
336 Width = Layout.getSize();
337 Align = Layout.getAlignment();
338 break;
339 }
Chris Lattner71763312008-04-06 22:05:18 +0000340 case Type::Tagged: {
Chris Lattner8389eab2008-08-09 21:35:13 +0000341 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
342 Width = 1;
343 Align = 1;
344 break;
345 }
346
Chris Lattner71763312008-04-06 22:05:18 +0000347 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
348 return getTypeInfo(ET->getDecl()->getIntegerType());
349
350 RecordType *RT = cast<RecordType>(T);
351 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
352 Width = Layout.getSize();
353 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000354 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000355 }
Chris Lattner71763312008-04-06 22:05:18 +0000356 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000357
Chris Lattner464175b2007-07-18 17:52:12 +0000358 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000360}
361
Devang Patel8b277042008-06-04 21:22:16 +0000362/// LayoutField - Field layout.
363void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
364 bool IsUnion, bool StructIsPacked,
365 ASTContext &Context) {
366 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
367 uint64_t FieldOffset = IsUnion ? 0 : Size;
368 uint64_t FieldSize;
369 unsigned FieldAlign;
370
371 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
372 // TODO: Need to check this algorithm on other targets!
373 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000374 FieldSize =
375 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000376
377 std::pair<uint64_t, unsigned> FieldInfo =
378 Context.getTypeInfo(FD->getType());
379 uint64_t TypeSize = FieldInfo.first;
380
381 FieldAlign = FieldInfo.second;
382 if (FieldIsPacked)
383 FieldAlign = 1;
384 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
385 FieldAlign = std::max(FieldAlign, AA->getAlignment());
386
387 // Check if we need to add padding to give the field the correct
388 // alignment.
389 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
390 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
391
392 // Padding members don't affect overall alignment
393 if (!FD->getIdentifier())
394 FieldAlign = 1;
395 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000396 if (FD->getType()->isIncompleteArrayType()) {
397 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000398 // query getTypeInfo about these, so we figure it out here.
399 // Flexible array members don't have any size, but they
400 // have to be aligned appropriately for their element type.
401 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000402 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000403 FieldAlign = Context.getTypeAlign(ATy->getElementType());
404 } else {
405 std::pair<uint64_t, unsigned> FieldInfo =
406 Context.getTypeInfo(FD->getType());
407 FieldSize = FieldInfo.first;
408 FieldAlign = FieldInfo.second;
409 }
410
411 if (FieldIsPacked)
412 FieldAlign = 8;
413 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
414 FieldAlign = std::max(FieldAlign, AA->getAlignment());
415
416 // Round up the current record size to the field's alignment boundary.
417 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
418 }
419
420 // Place this field at the current location.
421 FieldOffsets[FieldNo] = FieldOffset;
422
423 // Reserve space for this field.
424 if (IsUnion) {
425 Size = std::max(Size, FieldSize);
426 } else {
427 Size = FieldOffset + FieldSize;
428 }
429
430 // Remember max struct/class alignment.
431 Alignment = std::max(Alignment, FieldAlign);
432}
433
Devang Patel44a3dde2008-06-04 21:54:36 +0000434
435/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
436/// specified Objective C, which indicates its size and ivar
437/// position information.
438const ASTRecordLayout &
439ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
440 // Look up this layout, if already laid out, return what we have.
441 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
442 if (Entry) return *Entry;
443
444 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
445 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000446 ASTRecordLayout *NewEntry = NULL;
447 unsigned FieldCount = D->ivar_size();
448 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
449 FieldCount++;
450 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
451 unsigned Alignment = SL.getAlignment();
452 uint64_t Size = SL.getSize();
453 NewEntry = new ASTRecordLayout(Size, Alignment);
454 NewEntry->InitializeLayout(FieldCount);
455 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
456 } else {
457 NewEntry = new ASTRecordLayout();
458 NewEntry->InitializeLayout(FieldCount);
459 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000460 Entry = NewEntry;
461
Devang Patel44a3dde2008-06-04 21:54:36 +0000462 bool IsPacked = D->getAttr<PackedAttr>();
463
464 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
465 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
466 AA->getAlignment()));
467
468 // Layout each ivar sequentially.
469 unsigned i = 0;
470 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
471 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
472 const ObjCIvarDecl* Ivar = (*IVI);
473 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
474 }
475
476 // Finally, round the size of the total struct up to the alignment of the
477 // struct itself.
478 NewEntry->FinalizeLayout();
479 return *NewEntry;
480}
481
Devang Patel88a981b2007-11-01 19:11:01 +0000482/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000483/// specified record (struct/union/class), which indicates its size and field
484/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000485const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000486 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000487
Chris Lattner464175b2007-07-18 17:52:12 +0000488 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000489 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000490 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000491
Devang Patel88a981b2007-11-01 19:11:01 +0000492 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
493 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
494 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000495 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000496
Devang Patel8b277042008-06-04 21:22:16 +0000497 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000498 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000499 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000500
Eli Friedman4bd998b2008-05-30 09:31:38 +0000501 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000502 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
503 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000504
Eli Friedman4bd998b2008-05-30 09:31:38 +0000505 // Layout each field, for now, just sequentially, respecting alignment. In
506 // the future, this will need to be tweakable by targets.
507 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
508 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000509 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000510 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000511
512 // Finally, round the size of the total struct up to the alignment of the
513 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000514 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000515 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000516}
517
Chris Lattnera7674d82007-07-13 22:13:22 +0000518//===----------------------------------------------------------------------===//
519// Type creation/memoization methods
520//===----------------------------------------------------------------------===//
521
Christopher Lambebb97e92008-02-04 02:31:56 +0000522QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000523 QualType CanT = getCanonicalType(T);
524 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000525 return T;
526
527 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
528 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000529 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000530 "Type is already address space qualified");
531
532 // Check if we've already instantiated an address space qual'd type of this
533 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000534 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000535 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000536 void *InsertPos = 0;
537 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
538 return QualType(ASQy, 0);
539
540 // If the base type isn't canonical, this won't be a canonical type either,
541 // so fill in the canonical type field.
542 QualType Canonical;
543 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000544 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000545
546 // Get the new insert position for the node we care about.
547 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
548 assert(NewIP == 0 && "Shouldn't be in the map!");
549 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000550 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000551 ASQualTypes.InsertNode(New, InsertPos);
552 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000553 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000554}
555
Chris Lattnera7674d82007-07-13 22:13:22 +0000556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557/// getComplexType - Return the uniqued reference to the type for a complex
558/// number with the specified element type.
559QualType ASTContext::getComplexType(QualType T) {
560 // Unique pointers, to guarantee there is only one pointer of a particular
561 // structure.
562 llvm::FoldingSetNodeID ID;
563 ComplexType::Profile(ID, T);
564
565 void *InsertPos = 0;
566 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
567 return QualType(CT, 0);
568
569 // If the pointee type isn't canonical, this won't be a canonical type either,
570 // so fill in the canonical type field.
571 QualType Canonical;
572 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000573 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000574
575 // Get the new insert position for the node we care about.
576 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
577 assert(NewIP == 0 && "Shouldn't be in the map!");
578 }
579 ComplexType *New = new ComplexType(T, Canonical);
580 Types.push_back(New);
581 ComplexTypes.InsertNode(New, InsertPos);
582 return QualType(New, 0);
583}
584
585
586/// getPointerType - Return the uniqued reference to the type for a pointer to
587/// the specified type.
588QualType ASTContext::getPointerType(QualType T) {
589 // Unique pointers, to guarantee there is only one pointer of a particular
590 // structure.
591 llvm::FoldingSetNodeID ID;
592 PointerType::Profile(ID, T);
593
594 void *InsertPos = 0;
595 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
596 return QualType(PT, 0);
597
598 // If the pointee type isn't canonical, this won't be a canonical type either,
599 // so fill in the canonical type field.
600 QualType Canonical;
601 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000602 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000603
604 // Get the new insert position for the node we care about.
605 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
606 assert(NewIP == 0 && "Shouldn't be in the map!");
607 }
608 PointerType *New = new PointerType(T, Canonical);
609 Types.push_back(New);
610 PointerTypes.InsertNode(New, InsertPos);
611 return QualType(New, 0);
612}
613
Steve Naroff5618bd42008-08-27 16:04:49 +0000614/// getBlockPointerType - Return the uniqued reference to the type for
615/// a pointer to the specified block.
616QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000617 assert(T->isFunctionType() && "block of function types only");
618 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000619 // structure.
620 llvm::FoldingSetNodeID ID;
621 BlockPointerType::Profile(ID, T);
622
623 void *InsertPos = 0;
624 if (BlockPointerType *PT =
625 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
626 return QualType(PT, 0);
627
Steve Naroff296e8d52008-08-28 19:20:44 +0000628 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000629 // type either so fill in the canonical type field.
630 QualType Canonical;
631 if (!T->isCanonical()) {
632 Canonical = getBlockPointerType(getCanonicalType(T));
633
634 // Get the new insert position for the node we care about.
635 BlockPointerType *NewIP =
636 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
637 assert(NewIP == 0 && "Shouldn't be in the map!");
638 }
639 BlockPointerType *New = new BlockPointerType(T, Canonical);
640 Types.push_back(New);
641 BlockPointerTypes.InsertNode(New, InsertPos);
642 return QualType(New, 0);
643}
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645/// getReferenceType - Return the uniqued reference to the type for a reference
646/// to the specified type.
647QualType ASTContext::getReferenceType(QualType T) {
648 // Unique pointers, to guarantee there is only one pointer of a particular
649 // structure.
650 llvm::FoldingSetNodeID ID;
651 ReferenceType::Profile(ID, T);
652
653 void *InsertPos = 0;
654 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
655 return QualType(RT, 0);
656
657 // If the referencee type isn't canonical, this won't be a canonical type
658 // either, so fill in the canonical type field.
659 QualType Canonical;
660 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000661 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
663 // Get the new insert position for the node we care about.
664 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
665 assert(NewIP == 0 && "Shouldn't be in the map!");
666 }
667
668 ReferenceType *New = new ReferenceType(T, Canonical);
669 Types.push_back(New);
670 ReferenceTypes.InsertNode(New, InsertPos);
671 return QualType(New, 0);
672}
673
Steve Narofffb22d962007-08-30 01:06:46 +0000674/// getConstantArrayType - Return the unique reference to the type for an
675/// array of the specified element type.
676QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000677 const llvm::APInt &ArySize,
678 ArrayType::ArraySizeModifier ASM,
679 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000681 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682
683 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000684 if (ConstantArrayType *ATP =
685 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 return QualType(ATP, 0);
687
688 // If the element type isn't canonical, this won't be a canonical type either,
689 // so fill in the canonical type field.
690 QualType Canonical;
691 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000692 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000693 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000695 ConstantArrayType *NewIP =
696 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
697
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 assert(NewIP == 0 && "Shouldn't be in the map!");
699 }
700
Steve Naroffc9406122007-08-30 18:10:14 +0000701 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
702 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000703 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 Types.push_back(New);
705 return QualType(New, 0);
706}
707
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000708/// getVariableArrayType - Returns a non-unique reference to the type for a
709/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000710QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
711 ArrayType::ArraySizeModifier ASM,
712 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000713 // Since we don't unique expressions, it isn't possible to unique VLA's
714 // that have an expression provided for their size.
715
716 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
717 ASM, EltTypeQuals);
718
719 VariableArrayTypes.push_back(New);
720 Types.push_back(New);
721 return QualType(New, 0);
722}
723
724QualType ASTContext::getIncompleteArrayType(QualType EltTy,
725 ArrayType::ArraySizeModifier ASM,
726 unsigned EltTypeQuals) {
727 llvm::FoldingSetNodeID ID;
728 IncompleteArrayType::Profile(ID, EltTy);
729
730 void *InsertPos = 0;
731 if (IncompleteArrayType *ATP =
732 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
733 return QualType(ATP, 0);
734
735 // If the element type isn't canonical, this won't be a canonical type
736 // either, so fill in the canonical type field.
737 QualType Canonical;
738
739 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000740 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000741 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000742
743 // Get the new insert position for the node we care about.
744 IncompleteArrayType *NewIP =
745 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
746
747 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000748 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000749
750 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
751 ASM, EltTypeQuals);
752
753 IncompleteArrayTypes.InsertNode(New, InsertPos);
754 Types.push_back(New);
755 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000756}
757
Steve Naroff73322922007-07-18 18:00:27 +0000758/// getVectorType - Return the unique reference to a vector type of
759/// the specified element type and size. VectorType must be a built-in type.
760QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 BuiltinType *baseType;
762
Chris Lattnerf52ab252008-04-06 22:59:24 +0000763 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000764 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000765
766 // Check if we've already instantiated a vector of this type.
767 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000768 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 void *InsertPos = 0;
770 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
771 return QualType(VTP, 0);
772
773 // If the element type isn't canonical, this won't be a canonical type either,
774 // so fill in the canonical type field.
775 QualType Canonical;
776 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000777 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000778
779 // Get the new insert position for the node we care about.
780 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
781 assert(NewIP == 0 && "Shouldn't be in the map!");
782 }
783 VectorType *New = new VectorType(vecType, NumElts, Canonical);
784 VectorTypes.InsertNode(New, InsertPos);
785 Types.push_back(New);
786 return QualType(New, 0);
787}
788
Nate Begeman213541a2008-04-18 23:10:10 +0000789/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000790/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000791QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000792 BuiltinType *baseType;
793
Chris Lattnerf52ab252008-04-06 22:59:24 +0000794 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000795 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000796
797 // Check if we've already instantiated a vector of this type.
798 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000799 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000800 void *InsertPos = 0;
801 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
802 return QualType(VTP, 0);
803
804 // If the element type isn't canonical, this won't be a canonical type either,
805 // so fill in the canonical type field.
806 QualType Canonical;
807 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000808 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000809
810 // Get the new insert position for the node we care about.
811 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
812 assert(NewIP == 0 && "Shouldn't be in the map!");
813 }
Nate Begeman213541a2008-04-18 23:10:10 +0000814 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000815 VectorTypes.InsertNode(New, InsertPos);
816 Types.push_back(New);
817 return QualType(New, 0);
818}
819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
821///
822QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
823 // Unique functions, to guarantee there is only one function of a particular
824 // structure.
825 llvm::FoldingSetNodeID ID;
826 FunctionTypeNoProto::Profile(ID, ResultTy);
827
828 void *InsertPos = 0;
829 if (FunctionTypeNoProto *FT =
830 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
831 return QualType(FT, 0);
832
833 QualType Canonical;
834 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000835 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000836
837 // Get the new insert position for the node we care about.
838 FunctionTypeNoProto *NewIP =
839 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
840 assert(NewIP == 0 && "Shouldn't be in the map!");
841 }
842
843 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
844 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000845 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 return QualType(New, 0);
847}
848
849/// getFunctionType - Return a normal function type with a typed argument
850/// list. isVariadic indicates whether the argument list includes '...'.
Eli Friedman86da77f2008-08-22 00:59:49 +0000851QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray,
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 unsigned NumArgs, bool isVariadic) {
853 // Unique functions, to guarantee there is only one function of a particular
854 // structure.
855 llvm::FoldingSetNodeID ID;
856 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
857
858 void *InsertPos = 0;
859 if (FunctionTypeProto *FTP =
860 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
861 return QualType(FTP, 0);
862
863 // Determine whether the type being created is already canonical or not.
864 bool isCanonical = ResultTy->isCanonical();
865 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
866 if (!ArgArray[i]->isCanonical())
867 isCanonical = false;
868
869 // If this type isn't canonical, get the canonical version of it.
870 QualType Canonical;
871 if (!isCanonical) {
872 llvm::SmallVector<QualType, 16> CanonicalArgs;
873 CanonicalArgs.reserve(NumArgs);
874 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000875 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000876
Chris Lattnerf52ab252008-04-06 22:59:24 +0000877 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 &CanonicalArgs[0], NumArgs,
879 isVariadic);
880
881 // Get the new insert position for the node we care about.
882 FunctionTypeProto *NewIP =
883 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
884 assert(NewIP == 0 && "Shouldn't be in the map!");
885 }
886
887 // FunctionTypeProto objects are not allocated with new because they have a
888 // variable size array (for parameter types) at the end of them.
889 FunctionTypeProto *FTP =
890 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000891 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
893 Canonical);
894 Types.push_back(FTP);
895 FunctionTypeProtos.InsertNode(FTP, InsertPos);
896 return QualType(FTP, 0);
897}
898
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000899/// getTypeDeclType - Return the unique reference to the type for the
900/// specified type declaration.
901QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
902 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
903
904 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
905 return getTypedefType(Typedef);
906 else if (ObjCInterfaceDecl *ObjCInterface
907 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
908 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000909
910 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
911 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
912 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000913 Decl->TypeForDecl = new RecordType(Record);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000914 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000915 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000916 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000917 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000918
919 Types.push_back(Decl->TypeForDecl);
920 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000921}
922
Reid Spencer5f016e22007-07-11 17:01:13 +0000923/// getTypedefType - Return the unique reference to the type for the
924/// specified typename decl.
925QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
926 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
927
Chris Lattnerf52ab252008-04-06 22:59:24 +0000928 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000929 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 Types.push_back(Decl->TypeForDecl);
931 return QualType(Decl->TypeForDecl, 0);
932}
933
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000935/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000937 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
938
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000940 Types.push_back(Decl->TypeForDecl);
941 return QualType(Decl->TypeForDecl, 0);
942}
943
Chris Lattner88cb27a2008-04-07 04:56:42 +0000944/// CmpProtocolNames - Comparison predicate for sorting protocols
945/// alphabetically.
946static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
947 const ObjCProtocolDecl *RHS) {
948 return strcmp(LHS->getName(), RHS->getName()) < 0;
949}
950
951static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
952 unsigned &NumProtocols) {
953 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
954
955 // Sort protocols, keyed by name.
956 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
957
958 // Remove duplicates.
959 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
960 NumProtocols = ProtocolsEnd-Protocols;
961}
962
963
Chris Lattner065f0d72008-04-07 04:44:08 +0000964/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
965/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
967 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000968 // Sort the protocol list alphabetically to canonicalize it.
969 SortAndUniqueProtocols(Protocols, NumProtocols);
970
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000971 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000972 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000973
974 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975 if (ObjCQualifiedInterfaceType *QT =
976 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000977 return QualType(QT, 0);
978
979 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000980 ObjCQualifiedInterfaceType *QType =
981 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000982 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000984 return QualType(QType, 0);
985}
986
Chris Lattner88cb27a2008-04-07 04:56:42 +0000987/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
988/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000989QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000990 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000991 // Sort the protocol list alphabetically to canonicalize it.
992 SortAndUniqueProtocols(Protocols, NumProtocols);
993
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000994 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000995 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000996
997 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000999 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001000 return QualType(QT, 0);
1001
1002 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001003 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001004 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001006 return QualType(QType, 0);
1007}
1008
Steve Naroff9752f252007-08-01 18:02:17 +00001009/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1010/// TypeOfExpr AST's (since expression's are never shared). For example,
1011/// multiple declarations that refer to "typeof(x)" all contain different
1012/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1013/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001014QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001015 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001016 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1017 Types.push_back(toe);
1018 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001019}
1020
Steve Naroff9752f252007-08-01 18:02:17 +00001021/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1022/// TypeOfType AST's. The only motivation to unique these nodes would be
1023/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1024/// an issue. This doesn't effect the type checker, since it operates
1025/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001026QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001027 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001028 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1029 Types.push_back(tot);
1030 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001031}
1032
Reid Spencer5f016e22007-07-11 17:01:13 +00001033/// getTagDeclType - Return the unique reference to the type for the
1034/// specified TagDecl (struct/union/class/enum) decl.
1035QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001036 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001037 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038}
1039
1040/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1041/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1042/// needs to agree with the definition in <stddef.h>.
1043QualType ASTContext::getSizeType() const {
1044 // On Darwin, size_t is defined as a "long unsigned int".
1045 // FIXME: should derive from "Target".
1046 return UnsignedLongTy;
1047}
1048
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001049/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001050/// width of characters in wide strings, The value is target dependent and
1051/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001052QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001053 if (LangOpts.CPlusPlus)
1054 return WCharTy;
1055
Eli Friedmanfd888a52008-02-12 08:29:21 +00001056 // On Darwin, wchar_t is defined as a "int".
1057 // FIXME: should derive from "Target".
1058 return IntTy;
1059}
1060
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001061/// getSignedWCharType - Return the type of "signed wchar_t".
1062/// Used when in C++, as a GCC extension.
1063QualType ASTContext::getSignedWCharType() const {
1064 // FIXME: derive from "Target" ?
1065 return WCharTy;
1066}
1067
1068/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1069/// Used when in C++, as a GCC extension.
1070QualType ASTContext::getUnsignedWCharType() const {
1071 // FIXME: derive from "Target" ?
1072 return UnsignedIntTy;
1073}
1074
Chris Lattner8b9023b2007-07-13 03:05:23 +00001075/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1076/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1077QualType ASTContext::getPointerDiffType() const {
1078 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1079 // FIXME: should derive from "Target".
1080 return IntTy;
1081}
1082
Chris Lattnere6327742008-04-02 05:18:44 +00001083//===----------------------------------------------------------------------===//
1084// Type Operators
1085//===----------------------------------------------------------------------===//
1086
Chris Lattner77c96472008-04-06 22:41:35 +00001087/// getCanonicalType - Return the canonical (structural) type corresponding to
1088/// the specified potentially non-canonical type. The non-canonical version
1089/// of a type may have many "decorated" versions of types. Decorators can
1090/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1091/// to be free of any of these, allowing two canonical types to be compared
1092/// for exact equality with a simple pointer comparison.
1093QualType ASTContext::getCanonicalType(QualType T) {
1094 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001095
1096 // If the result has type qualifiers, make sure to canonicalize them as well.
1097 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1098 if (TypeQuals == 0) return CanType;
1099
1100 // If the type qualifiers are on an array type, get the canonical type of the
1101 // array with the qualifiers applied to the element type.
1102 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1103 if (!AT)
1104 return CanType.getQualifiedType(TypeQuals);
1105
1106 // Get the canonical version of the element with the extra qualifiers on it.
1107 // This can recursively sink qualifiers through multiple levels of arrays.
1108 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1109 NewEltTy = getCanonicalType(NewEltTy);
1110
1111 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1112 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1113 CAT->getIndexTypeQualifier());
1114 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1115 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1116 IAT->getIndexTypeQualifier());
1117
1118 // FIXME: What is the ownership of size expressions in VLAs?
1119 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1120 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1121 VAT->getSizeModifier(),
1122 VAT->getIndexTypeQualifier());
1123}
1124
1125
1126const ArrayType *ASTContext::getAsArrayType(QualType T) {
1127 // Handle the non-qualified case efficiently.
1128 if (T.getCVRQualifiers() == 0) {
1129 // Handle the common positive case fast.
1130 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1131 return AT;
1132 }
1133
1134 // Handle the common negative case fast, ignoring CVR qualifiers.
1135 QualType CType = T->getCanonicalTypeInternal();
1136
1137 // Make sure to look through type qualifiers (like ASQuals) for the negative
1138 // test.
1139 if (!isa<ArrayType>(CType) &&
1140 !isa<ArrayType>(CType.getUnqualifiedType()))
1141 return 0;
1142
1143 // Apply any CVR qualifiers from the array type to the element type. This
1144 // implements C99 6.7.3p8: "If the specification of an array type includes
1145 // any type qualifiers, the element type is so qualified, not the array type."
1146
1147 // If we get here, we either have type qualifiers on the type, or we have
1148 // sugar such as a typedef in the way. If we have type qualifiers on the type
1149 // we must propagate them down into the elemeng type.
1150 unsigned CVRQuals = T.getCVRQualifiers();
1151 unsigned AddrSpace = 0;
1152 Type *Ty = T.getTypePtr();
1153
1154 // Rip through ASQualType's and typedefs to get to a concrete type.
1155 while (1) {
1156 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1157 AddrSpace = ASQT->getAddressSpace();
1158 Ty = ASQT->getBaseType();
1159 } else {
1160 T = Ty->getDesugaredType();
1161 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1162 break;
1163 CVRQuals |= T.getCVRQualifiers();
1164 Ty = T.getTypePtr();
1165 }
1166 }
1167
1168 // If we have a simple case, just return now.
1169 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1170 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1171 return ATy;
1172
1173 // Otherwise, we have an array and we have qualifiers on it. Push the
1174 // qualifiers into the array element type and return a new array type.
1175 // Get the canonical version of the element with the extra qualifiers on it.
1176 // This can recursively sink qualifiers through multiple levels of arrays.
1177 QualType NewEltTy = ATy->getElementType();
1178 if (AddrSpace)
1179 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1180 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1181
1182 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1183 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1184 CAT->getSizeModifier(),
1185 CAT->getIndexTypeQualifier()));
1186 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1187 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1188 IAT->getSizeModifier(),
1189 IAT->getIndexTypeQualifier()));
1190
1191 // FIXME: What is the ownership of size expressions in VLAs?
1192 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1193 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1194 VAT->getSizeModifier(),
1195 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001196}
1197
1198
Chris Lattnere6327742008-04-02 05:18:44 +00001199/// getArrayDecayedType - Return the properly qualified result of decaying the
1200/// specified array type to a pointer. This operation is non-trivial when
1201/// handling typedefs etc. The canonical type of "T" must be an array type,
1202/// this returns a pointer to a properly qualified element of the array.
1203///
1204/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1205QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001206 // Get the element type with 'getAsArrayType' so that we don't lose any
1207 // typedefs in the element type of the array. This also handles propagation
1208 // of type qualifiers from the array type into the element type if present
1209 // (C99 6.7.3p8).
1210 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1211 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001212
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001213 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001214
1215 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001216 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001217}
1218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219/// getFloatingRank - Return a relative rank for floating point types.
1220/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001221static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001222 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001224
Christopher Lambebb97e92008-02-04 02:31:56 +00001225 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001226 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 case BuiltinType::Float: return FloatRank;
1228 case BuiltinType::Double: return DoubleRank;
1229 case BuiltinType::LongDouble: return LongDoubleRank;
1230 }
1231}
1232
Steve Naroff716c7302007-08-27 01:41:48 +00001233/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1234/// point or a complex type (based on typeDomain/typeSize).
1235/// 'typeDomain' is a real floating point or complex type.
1236/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001237QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1238 QualType Domain) const {
1239 FloatingRank EltRank = getFloatingRank(Size);
1240 if (Domain->isComplexType()) {
1241 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001242 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001243 case FloatRank: return FloatComplexTy;
1244 case DoubleRank: return DoubleComplexTy;
1245 case LongDoubleRank: return LongDoubleComplexTy;
1246 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 }
Chris Lattner1361b112008-04-06 23:58:54 +00001248
1249 assert(Domain->isRealFloatingType() && "Unknown domain!");
1250 switch (EltRank) {
1251 default: assert(0 && "getFloatingRank(): illegal value for rank");
1252 case FloatRank: return FloatTy;
1253 case DoubleRank: return DoubleTy;
1254 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001255 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001256}
1257
Chris Lattner7cfeb082008-04-06 23:55:33 +00001258/// getFloatingTypeOrder - Compare the rank of the two specified floating
1259/// point types, ignoring the domain of the type (i.e. 'double' ==
1260/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1261/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001262int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1263 FloatingRank LHSR = getFloatingRank(LHS);
1264 FloatingRank RHSR = getFloatingRank(RHS);
1265
1266 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001267 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001268 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001269 return 1;
1270 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271}
1272
Chris Lattnerf52ab252008-04-06 22:59:24 +00001273/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1274/// routine will assert if passed a built-in type that isn't an integer or enum,
1275/// or if it is not canonicalized.
1276static unsigned getIntegerRank(Type *T) {
1277 assert(T->isCanonical() && "T should be canonicalized");
1278 if (isa<EnumType>(T))
1279 return 4;
1280
1281 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001282 default: assert(0 && "getIntegerRank(): not a built-in integer");
1283 case BuiltinType::Bool:
1284 return 1;
1285 case BuiltinType::Char_S:
1286 case BuiltinType::Char_U:
1287 case BuiltinType::SChar:
1288 case BuiltinType::UChar:
1289 return 2;
1290 case BuiltinType::Short:
1291 case BuiltinType::UShort:
1292 return 3;
1293 case BuiltinType::Int:
1294 case BuiltinType::UInt:
1295 return 4;
1296 case BuiltinType::Long:
1297 case BuiltinType::ULong:
1298 return 5;
1299 case BuiltinType::LongLong:
1300 case BuiltinType::ULongLong:
1301 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001302 }
1303}
1304
Chris Lattner7cfeb082008-04-06 23:55:33 +00001305/// getIntegerTypeOrder - Returns the highest ranked integer type:
1306/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1307/// LHS < RHS, return -1.
1308int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001309 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1310 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001311 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001312
Chris Lattnerf52ab252008-04-06 22:59:24 +00001313 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1314 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001315
Chris Lattner7cfeb082008-04-06 23:55:33 +00001316 unsigned LHSRank = getIntegerRank(LHSC);
1317 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318
Chris Lattner7cfeb082008-04-06 23:55:33 +00001319 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1320 if (LHSRank == RHSRank) return 0;
1321 return LHSRank > RHSRank ? 1 : -1;
1322 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001323
Chris Lattner7cfeb082008-04-06 23:55:33 +00001324 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1325 if (LHSUnsigned) {
1326 // If the unsigned [LHS] type is larger, return it.
1327 if (LHSRank >= RHSRank)
1328 return 1;
1329
1330 // If the signed type can represent all values of the unsigned type, it
1331 // wins. Because we are dealing with 2's complement and types that are
1332 // powers of two larger than each other, this is always safe.
1333 return -1;
1334 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001335
Chris Lattner7cfeb082008-04-06 23:55:33 +00001336 // If the unsigned [RHS] type is larger, return it.
1337 if (RHSRank >= LHSRank)
1338 return -1;
1339
1340 // If the signed type can represent all values of the unsigned type, it
1341 // wins. Because we are dealing with 2's complement and types that are
1342 // powers of two larger than each other, this is always safe.
1343 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001344}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001345
1346// getCFConstantStringType - Return the type used for constant CFStrings.
1347QualType ASTContext::getCFConstantStringType() {
1348 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001349 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001350 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001351 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001352 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001353
1354 // const int *isa;
1355 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001356 // int flags;
1357 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001358 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001359 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001360 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001361 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001362 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001363 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001364
Anders Carlssonf06273f2007-11-19 00:25:30 +00001365 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001366 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001367 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001368
1369 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1370 }
1371
1372 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001373}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001374
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001375QualType ASTContext::getObjCFastEnumerationStateType()
1376{
1377 if (!ObjCFastEnumerationStateTypeDecl) {
1378 QualType FieldTypes[] = {
1379 UnsignedLongTy,
1380 getPointerType(ObjCIdType),
1381 getPointerType(UnsignedLongTy),
1382 getConstantArrayType(UnsignedLongTy,
1383 llvm::APInt(32, 5), ArrayType::Normal, 0)
1384 };
1385
1386 FieldDecl *FieldDecls[4];
1387 for (size_t i = 0; i < 4; ++i)
1388 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1389 FieldTypes[i]);
1390
1391 ObjCFastEnumerationStateTypeDecl =
1392 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1393 &Idents.get("__objcFastEnumerationState"), 0);
1394
1395 ObjCFastEnumerationStateTypeDecl->defineBody(FieldDecls, 4);
1396 }
1397
1398 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1399}
1400
Anders Carlssone8c49532007-10-29 06:33:42 +00001401// This returns true if a type has been typedefed to BOOL:
1402// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001403static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001404 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001405 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001406
1407 return false;
1408}
1409
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001410/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001411/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001413 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001414
1415 // Make all integer and enum types at least as large as an int
1416 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001417 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001418 // Treat arrays as pointers, since that's how they're passed in.
1419 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001420 sz = getTypeSize(VoidPtrTy);
1421 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001422}
1423
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001424/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001425/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001426void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001427 std::string& S)
1428{
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001429 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001430 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001432 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001433 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001434 // Compute size of all parameters.
1435 // Start with computing size of a pointer in number of bytes.
1436 // FIXME: There might(should) be a better way of doing this computation!
1437 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001438 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001439 // The first two arguments (self and _cmd) are pointers; account for
1440 // their size.
1441 int ParmOffset = 2 * PtrSize;
1442 int NumOfParams = Decl->getNumParams();
1443 for (int i = 0; i < NumOfParams; i++) {
1444 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001445 int sz = getObjCEncodingTypeSize (PType);
1446 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001447 ParmOffset += sz;
1448 }
1449 S += llvm::utostr(ParmOffset);
1450 S += "@0:";
1451 S += llvm::utostr(PtrSize);
1452
1453 // Argument types.
1454 ParmOffset = 2 * PtrSize;
1455 for (int i = 0; i < NumOfParams; i++) {
1456 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001457 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001458 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001459 getObjCEncodingForTypeQualifier(
1460 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001461 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001462 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001464 }
1465}
1466
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001467/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1468/// method declaration. If non-NULL, Container must be either an
1469/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1470/// NULL when getting encodings for protocol properties.
1471void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1472 const Decl *Container,
1473 std::string& S)
1474{
1475 // Collect information from the property implementation decl(s).
1476 bool Dynamic = false;
1477 ObjCPropertyImplDecl *SynthesizePID = 0;
1478
1479 // FIXME: Duplicated code due to poor abstraction.
1480 if (Container) {
1481 if (const ObjCCategoryImplDecl *CID =
1482 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1483 for (ObjCCategoryImplDecl::propimpl_iterator
1484 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1485 ObjCPropertyImplDecl *PID = *i;
1486 if (PID->getPropertyDecl() == PD) {
1487 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1488 Dynamic = true;
1489 } else {
1490 SynthesizePID = PID;
1491 }
1492 }
1493 }
1494 } else {
1495 const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container);
1496 for (ObjCCategoryImplDecl::propimpl_iterator
1497 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1498 ObjCPropertyImplDecl *PID = *i;
1499 if (PID->getPropertyDecl() == PD) {
1500 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1501 Dynamic = true;
1502 } else {
1503 SynthesizePID = PID;
1504 }
1505 }
1506 }
1507 }
1508 }
1509
1510 // FIXME: This is not very efficient.
1511 S = "T";
1512
1513 // Encode result type.
1514 // FIXME: GCC uses a generating_property_type_encoding mode during
1515 // this part. Investigate.
1516 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1517
1518 if (PD->isReadOnly()) {
1519 S += ",R";
1520 } else {
1521 switch (PD->getSetterKind()) {
1522 case ObjCPropertyDecl::Assign: break;
1523 case ObjCPropertyDecl::Copy: S += ",C"; break;
1524 case ObjCPropertyDecl::Retain: S += ",&"; break;
1525 }
1526 }
1527
1528 // It really isn't clear at all what this means, since properties
1529 // are "dynamic by default".
1530 if (Dynamic)
1531 S += ",D";
1532
1533 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1534 S += ",G";
1535 S += PD->getGetterName().getName();
1536 }
1537
1538 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1539 S += ",S";
1540 S += PD->getSetterName().getName();
1541 }
1542
1543 if (SynthesizePID) {
1544 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1545 S += ",V";
1546 S += OID->getName();
1547 }
1548
1549 // FIXME: OBJCGC: weak & strong
1550}
1551
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001552void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001553 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001554 // FIXME: This currently doesn't encode:
1555 // @ An object (whether statically typed or typed id)
1556 // # A class object (Class)
1557 // : A method selector (SEL)
1558 // {name=type...} A structure
1559 // (name=type...) A union
1560 // bnum A bit field of num bits
1561
1562 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001563 char encoding;
1564 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001565 default: assert(0 && "Unhandled builtin type kind");
1566 case BuiltinType::Void: encoding = 'v'; break;
1567 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001568 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001569 case BuiltinType::UChar: encoding = 'C'; break;
1570 case BuiltinType::UShort: encoding = 'S'; break;
1571 case BuiltinType::UInt: encoding = 'I'; break;
1572 case BuiltinType::ULong: encoding = 'L'; break;
1573 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001574 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001575 case BuiltinType::SChar: encoding = 'c'; break;
1576 case BuiltinType::Short: encoding = 's'; break;
1577 case BuiltinType::Int: encoding = 'i'; break;
1578 case BuiltinType::Long: encoding = 'l'; break;
1579 case BuiltinType::LongLong: encoding = 'q'; break;
1580 case BuiltinType::Float: encoding = 'f'; break;
1581 case BuiltinType::Double: encoding = 'd'; break;
1582 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001583 }
1584
1585 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001586 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001588 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001589 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001590
1591 }
1592 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001593 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001594 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001595 S += '@';
1596 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001597 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001598 S += '#';
1599 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001600 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001601 S += ':';
1602 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001603 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001604
1605 if (PointeeTy->isCharType()) {
1606 // char pointer types should be encoded as '*' unless it is a
1607 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001608 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001609 S += '*';
1610 return;
1611 }
1612 }
1613
1614 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001615 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001616 } else if (const ArrayType *AT =
1617 // Ignore type qualifiers etc.
1618 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001619 S += '[';
1620
1621 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1622 S += llvm::utostr(CAT->getSize().getZExtValue());
1623 else
1624 assert(0 && "Unhandled array type!");
1625
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001626 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001627 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001628 } else if (T->getAsFunctionType()) {
1629 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001630 } else if (const RecordType *RTy = T->getAsRecordType()) {
1631 RecordDecl *RDecl= RTy->getDecl();
Steve Narofffaf37e72008-08-14 15:00:38 +00001632 // This mimics the behavior in gcc's encode_aggregate_within().
1633 // The idea is to only inline structure definitions for top level pointers
1634 // to structures and embedded structures.
1635 bool inlining = (S.size() == 1 && S[0] == '^' ||
1636 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001637 S += '{';
1638 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001639 bool found = false;
1640 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1641 if (ERType[i] == RTy) {
1642 found = true;
1643 break;
1644 }
Steve Narofffaf37e72008-08-14 15:00:38 +00001645 if (!found && inlining) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001646 ERType.push_back(RTy);
1647 S += '=';
1648 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1649 FieldDecl *field = RDecl->getMember(i);
1650 getObjCEncodingForType(field->getType(), S, ERType);
1651 }
1652 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1653 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001654 }
1655 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001656 } else if (T->isEnumeralType()) {
1657 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001658 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001659 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001660}
1661
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001662void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001663 std::string& S) const {
1664 if (QT & Decl::OBJC_TQ_In)
1665 S += 'n';
1666 if (QT & Decl::OBJC_TQ_Inout)
1667 S += 'N';
1668 if (QT & Decl::OBJC_TQ_Out)
1669 S += 'o';
1670 if (QT & Decl::OBJC_TQ_Bycopy)
1671 S += 'O';
1672 if (QT & Decl::OBJC_TQ_Byref)
1673 S += 'R';
1674 if (QT & Decl::OBJC_TQ_Oneway)
1675 S += 'V';
1676}
1677
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001678void ASTContext::setBuiltinVaListType(QualType T)
1679{
1680 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1681
1682 BuiltinVaListType = T;
1683}
1684
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001685void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001686{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001687 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001688
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001689 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001690
1691 // typedef struct objc_object *id;
1692 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1693 assert(ptr && "'id' incorrectly typed");
1694 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1695 assert(rec && "'id' incorrectly typed");
1696 IdStructType = rec;
1697}
1698
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001699void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001700{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001701 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001702
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001703 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001704
1705 // typedef struct objc_selector *SEL;
1706 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1707 assert(ptr && "'SEL' incorrectly typed");
1708 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1709 assert(rec && "'SEL' incorrectly typed");
1710 SelStructType = rec;
1711}
1712
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001713void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001714{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001715 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1716 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001717}
1718
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001719void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001720{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001721 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001722
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001723 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001724
1725 // typedef struct objc_class *Class;
1726 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1727 assert(ptr && "'Class' incorrectly typed");
1728 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1729 assert(rec && "'Class' incorrectly typed");
1730 ClassStructType = rec;
1731}
1732
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001733void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1734 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001735 "'NSConstantString' type already set!");
1736
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001738}
1739
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001740
1741//===----------------------------------------------------------------------===//
1742// Type Predicates.
1743//===----------------------------------------------------------------------===//
1744
1745/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1746/// to an object type. This includes "id" and "Class" (two 'special' pointers
1747/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1748/// ID type).
1749bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1750 if (Ty->isObjCQualifiedIdType())
1751 return true;
1752
1753 if (!Ty->isPointerType())
1754 return false;
1755
1756 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1757 // pointer types. This looks for the typedef specifically, not for the
1758 // underlying type.
1759 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1760 return true;
1761
1762 // If this a pointer to an interface (e.g. NSString*), it is ok.
1763 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1764}
1765
Chris Lattner6ac46a42008-04-07 06:51:04 +00001766//===----------------------------------------------------------------------===//
1767// Type Compatibility Testing
1768//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001769
Chris Lattner6ac46a42008-04-07 06:51:04 +00001770/// areCompatVectorTypes - Return true if the two specified vector types are
1771/// compatible.
1772static bool areCompatVectorTypes(const VectorType *LHS,
1773 const VectorType *RHS) {
1774 assert(LHS->isCanonical() && RHS->isCanonical());
1775 return LHS->getElementType() == RHS->getElementType() &&
1776 LHS->getNumElements() == RHS->getNumElements();
1777}
1778
Eli Friedman3d815e72008-08-22 00:56:42 +00001779/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001780/// compatible for assignment from RHS to LHS. This handles validation of any
1781/// protocol qualifiers on the LHS or RHS.
1782///
Eli Friedman3d815e72008-08-22 00:56:42 +00001783bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1784 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001785 // Verify that the base decls are compatible: the RHS must be a subclass of
1786 // the LHS.
1787 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1788 return false;
1789
1790 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1791 // protocol qualified at all, then we are good.
1792 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1793 return true;
1794
1795 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1796 // isn't a superset.
1797 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1798 return true; // FIXME: should return false!
1799
1800 // Finally, we must have two protocol-qualified interfaces.
1801 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1802 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1803 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1804 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1805 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1806 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1807
1808 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1809 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1810 // LHS in a single parallel scan until we run out of elements in LHS.
1811 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1812 ObjCProtocolDecl *LHSProto = *LHSPI;
1813
1814 while (RHSPI != RHSPE) {
1815 ObjCProtocolDecl *RHSProto = *RHSPI++;
1816 // If the RHS has a protocol that the LHS doesn't, ignore it.
1817 if (RHSProto != LHSProto)
1818 continue;
1819
1820 // Otherwise, the RHS does have this element.
1821 ++LHSPI;
1822 if (LHSPI == LHSPE)
1823 return true; // All protocols in LHS exist in RHS.
1824
1825 LHSProto = *LHSPI;
1826 }
1827
1828 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1829 return false;
1830}
1831
Steve Naroffec0550f2007-10-15 20:41:53 +00001832/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1833/// both shall have the identically qualified version of a compatible type.
1834/// C99 6.2.7p1: Two types have compatible types if their types are the
1835/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001836bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1837 return !mergeTypes(LHS, RHS).isNull();
1838}
1839
1840QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1841 const FunctionType *lbase = lhs->getAsFunctionType();
1842 const FunctionType *rbase = rhs->getAsFunctionType();
1843 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1844 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1845 bool allLTypes = true;
1846 bool allRTypes = true;
1847
1848 // Check return type
1849 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1850 if (retType.isNull()) return QualType();
1851 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false;
1852 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false;
1853
1854 if (lproto && rproto) { // two C99 style function prototypes
1855 unsigned lproto_nargs = lproto->getNumArgs();
1856 unsigned rproto_nargs = rproto->getNumArgs();
1857
1858 // Compatible functions must have the same number of arguments
1859 if (lproto_nargs != rproto_nargs)
1860 return QualType();
1861
1862 // Variadic and non-variadic functions aren't compatible
1863 if (lproto->isVariadic() != rproto->isVariadic())
1864 return QualType();
1865
1866 // Check argument compatibility
1867 llvm::SmallVector<QualType, 10> types;
1868 for (unsigned i = 0; i < lproto_nargs; i++) {
1869 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1870 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1871 QualType argtype = mergeTypes(largtype, rargtype);
1872 if (argtype.isNull()) return QualType();
1873 types.push_back(argtype);
1874 if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false;
1875 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false;
1876 }
1877 if (allLTypes) return lhs;
1878 if (allRTypes) return rhs;
1879 return getFunctionType(retType, types.begin(), types.size(),
1880 lproto->isVariadic());
1881 }
1882
1883 if (lproto) allRTypes = false;
1884 if (rproto) allLTypes = false;
1885
1886 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1887 if (proto) {
1888 if (proto->isVariadic()) return QualType();
1889 // Check that the types are compatible with the types that
1890 // would result from default argument promotions (C99 6.7.5.3p15).
1891 // The only types actually affected are promotable integer
1892 // types and floats, which would be passed as a different
1893 // type depending on whether the prototype is visible.
1894 unsigned proto_nargs = proto->getNumArgs();
1895 for (unsigned i = 0; i < proto_nargs; ++i) {
1896 QualType argTy = proto->getArgType(i);
1897 if (argTy->isPromotableIntegerType() ||
1898 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1899 return QualType();
1900 }
1901
1902 if (allLTypes) return lhs;
1903 if (allRTypes) return rhs;
1904 return getFunctionType(retType, proto->arg_type_begin(),
1905 proto->getNumArgs(), lproto->isVariadic());
1906 }
1907
1908 if (allLTypes) return lhs;
1909 if (allRTypes) return rhs;
1910 return getFunctionTypeNoProto(retType);
1911}
1912
1913QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00001914 // C++ [expr]: If an expression initially has the type "reference to T", the
1915 // type is adjusted to "T" prior to any further analysis, the expression
1916 // designates the object or function denoted by the reference, and the
1917 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00001918 // FIXME: C++ shouldn't be going through here! The rules are different
1919 // enough that they should be handled separately.
1920 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001921 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00001922 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001923 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001924
Eli Friedman3d815e72008-08-22 00:56:42 +00001925 QualType LHSCan = getCanonicalType(LHS),
1926 RHSCan = getCanonicalType(RHS);
1927
1928 // If two types are identical, they are compatible.
1929 if (LHSCan == RHSCan)
1930 return LHS;
1931
1932 // If the qualifiers are different, the types aren't compatible
1933 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
1934 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
1935 return QualType();
1936
1937 Type::TypeClass LHSClass = LHSCan->getTypeClass();
1938 Type::TypeClass RHSClass = RHSCan->getTypeClass();
1939
Chris Lattner1adb8832008-01-14 05:45:46 +00001940 // We want to consider the two function types to be the same for these
1941 // comparisons, just force one to the other.
1942 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1943 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001944
1945 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001946 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1947 LHSClass = Type::ConstantArray;
1948 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1949 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001950
Nate Begeman213541a2008-04-18 23:10:10 +00001951 // Canonicalize ExtVector -> Vector.
1952 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1953 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001954
Chris Lattnerb0489812008-04-07 06:38:24 +00001955 // Consider qualified interfaces and interfaces the same.
1956 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1957 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00001958
Chris Lattnera36a61f2008-04-07 05:43:21 +00001959 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001960 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00001961 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00001962 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00001963 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00001964 if (isObjCIdType(PT->getPointeeType()))
1965 return LHS;
Steve Naroff97341622008-06-04 15:07:33 +00001966 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001967 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00001968 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00001969 if (isObjCIdType(PT->getPointeeType()))
1970 return RHS;
1971 }
1972
Chris Lattner1adb8832008-01-14 05:45:46 +00001973 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1974 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00001975 if (const EnumType* ETy = LHS->getAsEnumType()) {
1976 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
1977 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001978 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001979 if (const EnumType* ETy = RHS->getAsEnumType()) {
1980 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
1981 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001982 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001983
Eli Friedman3d815e72008-08-22 00:56:42 +00001984 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001985 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001986
Steve Naroff4a746782008-01-09 22:43:08 +00001987 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001988 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00001989 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00001990 {
1991 // Merge two pointer types, while trying to preserve typedef info
1992 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
1993 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
1994 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
1995 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00001996 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS;
1997 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00001998 return getPointerType(ResultType);
1999 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002000 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002001 {
2002 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2003 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2004 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2005 return QualType();
2006
2007 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2008 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2009 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2010 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00002011 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2012 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
2013 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2014 ArrayType::ArraySizeModifier(), 0);
2015 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2016 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002017 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2018 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Eli Friedman3bc0f452008-08-22 01:48:21 +00002019 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2020 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002021 if (LVAT) {
2022 // FIXME: This isn't correct! But tricky to implement because
2023 // the array's size has to be the size of LHS, but the type
2024 // has to be different.
2025 return LHS;
2026 }
2027 if (RVAT) {
2028 // FIXME: This isn't correct! But tricky to implement because
2029 // the array's size has to be the size of RHS, but the type
2030 // has to be different.
2031 return RHS;
2032 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002033 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2034 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002035 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0);
2036 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002037 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002038 return mergeFunctionTypes(LHS, RHS);
2039 case Type::Tagged:
2040 {
2041 // FIXME: Why are these compatible?
2042 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2043 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2044 return QualType();
2045 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002046 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002047 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002048 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002049 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002050 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2051 return LHS;
Chris Lattner1adb8832008-01-14 05:45:46 +00002052 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002053 {
2054 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2055 // for checking assignment/comparison safety
2056 return QualType();
2057 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002058 default:
2059 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002060 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002061 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002062}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002063
Chris Lattner5426bf62008-04-07 07:01:58 +00002064//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002065// Integer Predicates
2066//===----------------------------------------------------------------------===//
2067unsigned ASTContext::getIntWidth(QualType T) {
2068 if (T == BoolTy)
2069 return 1;
2070 // At the moment, only bool has padding bits
2071 return (unsigned)getTypeSize(T);
2072}
2073
2074QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2075 assert(T->isSignedIntegerType() && "Unexpected type");
2076 if (const EnumType* ETy = T->getAsEnumType())
2077 T = ETy->getDecl()->getIntegerType();
2078 const BuiltinType* BTy = T->getAsBuiltinType();
2079 assert (BTy && "Unexpected signed integer type");
2080 switch (BTy->getKind()) {
2081 case BuiltinType::Char_S:
2082 case BuiltinType::SChar:
2083 return UnsignedCharTy;
2084 case BuiltinType::Short:
2085 return UnsignedShortTy;
2086 case BuiltinType::Int:
2087 return UnsignedIntTy;
2088 case BuiltinType::Long:
2089 return UnsignedLongTy;
2090 case BuiltinType::LongLong:
2091 return UnsignedLongLongTy;
2092 default:
2093 assert(0 && "Unexpected signed integer type");
2094 return QualType();
2095 }
2096}
2097
2098
2099//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002100// Serialization Support
2101//===----------------------------------------------------------------------===//
2102
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002103/// Emit - Serialize an ASTContext object to Bitcode.
2104void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002105 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002106 S.EmitRef(SourceMgr);
2107 S.EmitRef(Target);
2108 S.EmitRef(Idents);
2109 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002110
Ted Kremenekfee04522007-10-31 22:44:07 +00002111 // Emit the size of the type vector so that we can reserve that size
2112 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002113 S.EmitInt(Types.size());
2114
Ted Kremenek03ed4402007-11-13 22:02:55 +00002115 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2116 I!=E;++I)
2117 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002118
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002119 S.EmitOwnedPtr(TUDecl);
2120
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002121 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002122}
2123
Ted Kremenek0f84c002007-11-13 00:25:37 +00002124ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002125
2126 // Read the language options.
2127 LangOptions LOpts;
2128 LOpts.Read(D);
2129
Ted Kremenekfee04522007-10-31 22:44:07 +00002130 SourceManager &SM = D.ReadRef<SourceManager>();
2131 TargetInfo &t = D.ReadRef<TargetInfo>();
2132 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2133 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002134
Ted Kremenekfee04522007-10-31 22:44:07 +00002135 unsigned size_reserve = D.ReadInt();
2136
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002137 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002138
Ted Kremenek03ed4402007-11-13 22:02:55 +00002139 for (unsigned i = 0; i < size_reserve; ++i)
2140 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002141
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002142 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2143
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002144 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002145
2146 return A;
2147}