blob: 4caf16c4b095cd54b70b6e959e218097fe9edc40 [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) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000486 D = D->getDefinition(*this);
487 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000488
Chris Lattner464175b2007-07-18 17:52:12 +0000489 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000490 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000491 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000492
Devang Patel88a981b2007-11-01 19:11:01 +0000493 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
494 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
495 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000496 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000497
Devang Patel8b277042008-06-04 21:22:16 +0000498 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000499 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000500 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000501
Eli Friedman4bd998b2008-05-30 09:31:38 +0000502 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000503 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
504 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000505
Eli Friedman4bd998b2008-05-30 09:31:38 +0000506 // Layout each field, for now, just sequentially, respecting alignment. In
507 // the future, this will need to be tweakable by targets.
508 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
509 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000510 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000511 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000512
513 // Finally, round the size of the total struct up to the alignment of the
514 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000515 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000516 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000517}
518
Chris Lattnera7674d82007-07-13 22:13:22 +0000519//===----------------------------------------------------------------------===//
520// Type creation/memoization methods
521//===----------------------------------------------------------------------===//
522
Christopher Lambebb97e92008-02-04 02:31:56 +0000523QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000524 QualType CanT = getCanonicalType(T);
525 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000526 return T;
527
528 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
529 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000530 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000531 "Type is already address space qualified");
532
533 // Check if we've already instantiated an address space qual'd type of this
534 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000535 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000536 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000537 void *InsertPos = 0;
538 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
539 return QualType(ASQy, 0);
540
541 // If the base type isn't canonical, this won't be a canonical type either,
542 // so fill in the canonical type field.
543 QualType Canonical;
544 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000545 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000546
547 // Get the new insert position for the node we care about.
548 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
549 assert(NewIP == 0 && "Shouldn't be in the map!");
550 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000551 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 ASQualTypes.InsertNode(New, InsertPos);
553 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000554 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000555}
556
Chris Lattnera7674d82007-07-13 22:13:22 +0000557
Reid Spencer5f016e22007-07-11 17:01:13 +0000558/// getComplexType - Return the uniqued reference to the type for a complex
559/// number with the specified element type.
560QualType ASTContext::getComplexType(QualType T) {
561 // Unique pointers, to guarantee there is only one pointer of a particular
562 // structure.
563 llvm::FoldingSetNodeID ID;
564 ComplexType::Profile(ID, T);
565
566 void *InsertPos = 0;
567 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
568 return QualType(CT, 0);
569
570 // If the pointee type isn't canonical, this won't be a canonical type either,
571 // so fill in the canonical type field.
572 QualType Canonical;
573 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000574 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000575
576 // Get the new insert position for the node we care about.
577 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
578 assert(NewIP == 0 && "Shouldn't be in the map!");
579 }
580 ComplexType *New = new ComplexType(T, Canonical);
581 Types.push_back(New);
582 ComplexTypes.InsertNode(New, InsertPos);
583 return QualType(New, 0);
584}
585
586
587/// getPointerType - Return the uniqued reference to the type for a pointer to
588/// the specified type.
589QualType ASTContext::getPointerType(QualType T) {
590 // Unique pointers, to guarantee there is only one pointer of a particular
591 // structure.
592 llvm::FoldingSetNodeID ID;
593 PointerType::Profile(ID, T);
594
595 void *InsertPos = 0;
596 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
597 return QualType(PT, 0);
598
599 // If the pointee type isn't canonical, this won't be a canonical type either,
600 // so fill in the canonical type field.
601 QualType Canonical;
602 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000603 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000604
605 // Get the new insert position for the node we care about.
606 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
607 assert(NewIP == 0 && "Shouldn't be in the map!");
608 }
609 PointerType *New = new PointerType(T, Canonical);
610 Types.push_back(New);
611 PointerTypes.InsertNode(New, InsertPos);
612 return QualType(New, 0);
613}
614
Steve Naroff5618bd42008-08-27 16:04:49 +0000615/// getBlockPointerType - Return the uniqued reference to the type for
616/// a pointer to the specified block.
617QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000618 assert(T->isFunctionType() && "block of function types only");
619 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000620 // structure.
621 llvm::FoldingSetNodeID ID;
622 BlockPointerType::Profile(ID, T);
623
624 void *InsertPos = 0;
625 if (BlockPointerType *PT =
626 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
627 return QualType(PT, 0);
628
Steve Naroff296e8d52008-08-28 19:20:44 +0000629 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000630 // type either so fill in the canonical type field.
631 QualType Canonical;
632 if (!T->isCanonical()) {
633 Canonical = getBlockPointerType(getCanonicalType(T));
634
635 // Get the new insert position for the node we care about.
636 BlockPointerType *NewIP =
637 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
638 assert(NewIP == 0 && "Shouldn't be in the map!");
639 }
640 BlockPointerType *New = new BlockPointerType(T, Canonical);
641 Types.push_back(New);
642 BlockPointerTypes.InsertNode(New, InsertPos);
643 return QualType(New, 0);
644}
645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646/// getReferenceType - Return the uniqued reference to the type for a reference
647/// to the specified type.
648QualType ASTContext::getReferenceType(QualType T) {
649 // Unique pointers, to guarantee there is only one pointer of a particular
650 // structure.
651 llvm::FoldingSetNodeID ID;
652 ReferenceType::Profile(ID, T);
653
654 void *InsertPos = 0;
655 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
656 return QualType(RT, 0);
657
658 // If the referencee type isn't canonical, this won't be a canonical type
659 // either, so fill in the canonical type field.
660 QualType Canonical;
661 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000662 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000663
664 // Get the new insert position for the node we care about.
665 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
666 assert(NewIP == 0 && "Shouldn't be in the map!");
667 }
668
669 ReferenceType *New = new ReferenceType(T, Canonical);
670 Types.push_back(New);
671 ReferenceTypes.InsertNode(New, InsertPos);
672 return QualType(New, 0);
673}
674
Steve Narofffb22d962007-08-30 01:06:46 +0000675/// getConstantArrayType - Return the unique reference to the type for an
676/// array of the specified element type.
677QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000678 const llvm::APInt &ArySize,
679 ArrayType::ArraySizeModifier ASM,
680 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000682 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000683
684 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000685 if (ConstantArrayType *ATP =
686 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return QualType(ATP, 0);
688
689 // If the element type isn't canonical, this won't be a canonical type either,
690 // so fill in the canonical type field.
691 QualType Canonical;
692 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000693 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000694 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000696 ConstantArrayType *NewIP =
697 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 assert(NewIP == 0 && "Shouldn't be in the map!");
700 }
701
Steve Naroffc9406122007-08-30 18:10:14 +0000702 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
703 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000704 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 Types.push_back(New);
706 return QualType(New, 0);
707}
708
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000709/// getVariableArrayType - Returns a non-unique reference to the type for a
710/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000711QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
712 ArrayType::ArraySizeModifier ASM,
713 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000714 // Since we don't unique expressions, it isn't possible to unique VLA's
715 // that have an expression provided for their size.
716
717 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
718 ASM, EltTypeQuals);
719
720 VariableArrayTypes.push_back(New);
721 Types.push_back(New);
722 return QualType(New, 0);
723}
724
725QualType ASTContext::getIncompleteArrayType(QualType EltTy,
726 ArrayType::ArraySizeModifier ASM,
727 unsigned EltTypeQuals) {
728 llvm::FoldingSetNodeID ID;
729 IncompleteArrayType::Profile(ID, EltTy);
730
731 void *InsertPos = 0;
732 if (IncompleteArrayType *ATP =
733 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
734 return QualType(ATP, 0);
735
736 // If the element type isn't canonical, this won't be a canonical type
737 // either, so fill in the canonical type field.
738 QualType Canonical;
739
740 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000741 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000742 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000743
744 // Get the new insert position for the node we care about.
745 IncompleteArrayType *NewIP =
746 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
747
748 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000749 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000750
751 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
752 ASM, EltTypeQuals);
753
754 IncompleteArrayTypes.InsertNode(New, InsertPos);
755 Types.push_back(New);
756 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000757}
758
Steve Naroff73322922007-07-18 18:00:27 +0000759/// getVectorType - Return the unique reference to a vector type of
760/// the specified element type and size. VectorType must be a built-in type.
761QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 BuiltinType *baseType;
763
Chris Lattnerf52ab252008-04-06 22:59:24 +0000764 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000765 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000766
767 // Check if we've already instantiated a vector of this type.
768 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000769 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 void *InsertPos = 0;
771 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
772 return QualType(VTP, 0);
773
774 // If the element type isn't canonical, this won't be a canonical type either,
775 // so fill in the canonical type field.
776 QualType Canonical;
777 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000778 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
780 // Get the new insert position for the node we care about.
781 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
782 assert(NewIP == 0 && "Shouldn't be in the map!");
783 }
784 VectorType *New = new VectorType(vecType, NumElts, Canonical);
785 VectorTypes.InsertNode(New, InsertPos);
786 Types.push_back(New);
787 return QualType(New, 0);
788}
789
Nate Begeman213541a2008-04-18 23:10:10 +0000790/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000791/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000792QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000793 BuiltinType *baseType;
794
Chris Lattnerf52ab252008-04-06 22:59:24 +0000795 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000796 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000797
798 // Check if we've already instantiated a vector of this type.
799 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000800 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000801 void *InsertPos = 0;
802 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
803 return QualType(VTP, 0);
804
805 // If the element type isn't canonical, this won't be a canonical type either,
806 // so fill in the canonical type field.
807 QualType Canonical;
808 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000809 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000810
811 // Get the new insert position for the node we care about.
812 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
813 assert(NewIP == 0 && "Shouldn't be in the map!");
814 }
Nate Begeman213541a2008-04-18 23:10:10 +0000815 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000816 VectorTypes.InsertNode(New, InsertPos);
817 Types.push_back(New);
818 return QualType(New, 0);
819}
820
Reid Spencer5f016e22007-07-11 17:01:13 +0000821/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
822///
823QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
824 // Unique functions, to guarantee there is only one function of a particular
825 // structure.
826 llvm::FoldingSetNodeID ID;
827 FunctionTypeNoProto::Profile(ID, ResultTy);
828
829 void *InsertPos = 0;
830 if (FunctionTypeNoProto *FT =
831 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
832 return QualType(FT, 0);
833
834 QualType Canonical;
835 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000836 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000837
838 // Get the new insert position for the node we care about.
839 FunctionTypeNoProto *NewIP =
840 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
841 assert(NewIP == 0 && "Shouldn't be in the map!");
842 }
843
844 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
845 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000846 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 return QualType(New, 0);
848}
849
850/// getFunctionType - Return a normal function type with a typed argument
851/// list. isVariadic indicates whether the argument list includes '...'.
Eli Friedman86da77f2008-08-22 00:59:49 +0000852QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray,
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 unsigned NumArgs, bool isVariadic) {
854 // Unique functions, to guarantee there is only one function of a particular
855 // structure.
856 llvm::FoldingSetNodeID ID;
857 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
858
859 void *InsertPos = 0;
860 if (FunctionTypeProto *FTP =
861 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
862 return QualType(FTP, 0);
863
864 // Determine whether the type being created is already canonical or not.
865 bool isCanonical = ResultTy->isCanonical();
866 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
867 if (!ArgArray[i]->isCanonical())
868 isCanonical = false;
869
870 // If this type isn't canonical, get the canonical version of it.
871 QualType Canonical;
872 if (!isCanonical) {
873 llvm::SmallVector<QualType, 16> CanonicalArgs;
874 CanonicalArgs.reserve(NumArgs);
875 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000876 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
Chris Lattnerf52ab252008-04-06 22:59:24 +0000878 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 &CanonicalArgs[0], NumArgs,
880 isVariadic);
881
882 // Get the new insert position for the node we care about.
883 FunctionTypeProto *NewIP =
884 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
885 assert(NewIP == 0 && "Shouldn't be in the map!");
886 }
887
888 // FunctionTypeProto objects are not allocated with new because they have a
889 // variable size array (for parameter types) at the end of them.
890 FunctionTypeProto *FTP =
891 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000892 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
894 Canonical);
895 Types.push_back(FTP);
896 FunctionTypeProtos.InsertNode(FTP, InsertPos);
897 return QualType(FTP, 0);
898}
899
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000900/// getTypeDeclType - Return the unique reference to the type for the
901/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000902QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000903 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
904
905 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
906 return getTypedefType(Typedef);
907 else if (ObjCInterfaceDecl *ObjCInterface
908 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
909 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000910
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000911 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl)) {
912 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
913 : new CXXRecordType(CXXRecord);
914 }
915 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
916 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
917 : new RecordType(Record);
918 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000919 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000920 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000921 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000922 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000923
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000924 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000925 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000926}
927
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000928/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
929/// about which RecordDecl serves as the definition of a particular
930/// struct/union/class. This will eventually be used by enums as well.
931void ASTContext::setTagDefinition(TagDecl* D) {
932 assert (D->isDefinition());
933 cast<TagType>(D->TypeForDecl)->decl = D;
934}
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936/// getTypedefType - Return the unique reference to the type for the
937/// specified typename decl.
938QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
939 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
940
Chris Lattnerf52ab252008-04-06 22:59:24 +0000941 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000942 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 Types.push_back(Decl->TypeForDecl);
944 return QualType(Decl->TypeForDecl, 0);
945}
946
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000947/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000948/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000949QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000950 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
951
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000952 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000953 Types.push_back(Decl->TypeForDecl);
954 return QualType(Decl->TypeForDecl, 0);
955}
956
Chris Lattner88cb27a2008-04-07 04:56:42 +0000957/// CmpProtocolNames - Comparison predicate for sorting protocols
958/// alphabetically.
959static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
960 const ObjCProtocolDecl *RHS) {
961 return strcmp(LHS->getName(), RHS->getName()) < 0;
962}
963
964static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
965 unsigned &NumProtocols) {
966 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
967
968 // Sort protocols, keyed by name.
969 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
970
971 // Remove duplicates.
972 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
973 NumProtocols = ProtocolsEnd-Protocols;
974}
975
976
Chris Lattner065f0d72008-04-07 04:44:08 +0000977/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
978/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
980 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000981 // Sort the protocol list alphabetically to canonicalize it.
982 SortAndUniqueProtocols(Protocols, NumProtocols);
983
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000984 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000985 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000986
987 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988 if (ObjCQualifiedInterfaceType *QT =
989 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000990 return QualType(QT, 0);
991
992 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993 ObjCQualifiedInterfaceType *QType =
994 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000995 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000997 return QualType(QType, 0);
998}
999
Chris Lattner88cb27a2008-04-07 04:56:42 +00001000/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1001/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001002QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001003 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001004 // Sort the protocol list alphabetically to canonicalize it.
1005 SortAndUniqueProtocols(Protocols, NumProtocols);
1006
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001007 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001009
1010 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001012 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001013 return QualType(QT, 0);
1014
1015 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001016 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001017 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001019 return QualType(QType, 0);
1020}
1021
Steve Naroff9752f252007-08-01 18:02:17 +00001022/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1023/// TypeOfExpr AST's (since expression's are never shared). For example,
1024/// multiple declarations that refer to "typeof(x)" all contain different
1025/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1026/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001027QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001028 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001029 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1030 Types.push_back(toe);
1031 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001032}
1033
Steve Naroff9752f252007-08-01 18:02:17 +00001034/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1035/// TypeOfType AST's. The only motivation to unique these nodes would be
1036/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1037/// an issue. This doesn't effect the type checker, since it operates
1038/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001039QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001040 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001041 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1042 Types.push_back(tot);
1043 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001044}
1045
Reid Spencer5f016e22007-07-11 17:01:13 +00001046/// getTagDeclType - Return the unique reference to the type for the
1047/// specified TagDecl (struct/union/class/enum) decl.
1048QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001049 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001050 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051}
1052
1053/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1054/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1055/// needs to agree with the definition in <stddef.h>.
1056QualType ASTContext::getSizeType() const {
1057 // On Darwin, size_t is defined as a "long unsigned int".
1058 // FIXME: should derive from "Target".
1059 return UnsignedLongTy;
1060}
1061
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001062/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001063/// width of characters in wide strings, The value is target dependent and
1064/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001065QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001066 if (LangOpts.CPlusPlus)
1067 return WCharTy;
1068
Eli Friedmanfd888a52008-02-12 08:29:21 +00001069 // On Darwin, wchar_t is defined as a "int".
1070 // FIXME: should derive from "Target".
1071 return IntTy;
1072}
1073
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001074/// getSignedWCharType - Return the type of "signed wchar_t".
1075/// Used when in C++, as a GCC extension.
1076QualType ASTContext::getSignedWCharType() const {
1077 // FIXME: derive from "Target" ?
1078 return WCharTy;
1079}
1080
1081/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1082/// Used when in C++, as a GCC extension.
1083QualType ASTContext::getUnsignedWCharType() const {
1084 // FIXME: derive from "Target" ?
1085 return UnsignedIntTy;
1086}
1087
Chris Lattner8b9023b2007-07-13 03:05:23 +00001088/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1089/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1090QualType ASTContext::getPointerDiffType() const {
1091 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1092 // FIXME: should derive from "Target".
1093 return IntTy;
1094}
1095
Chris Lattnere6327742008-04-02 05:18:44 +00001096//===----------------------------------------------------------------------===//
1097// Type Operators
1098//===----------------------------------------------------------------------===//
1099
Chris Lattner77c96472008-04-06 22:41:35 +00001100/// getCanonicalType - Return the canonical (structural) type corresponding to
1101/// the specified potentially non-canonical type. The non-canonical version
1102/// of a type may have many "decorated" versions of types. Decorators can
1103/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1104/// to be free of any of these, allowing two canonical types to be compared
1105/// for exact equality with a simple pointer comparison.
1106QualType ASTContext::getCanonicalType(QualType T) {
1107 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001108
1109 // If the result has type qualifiers, make sure to canonicalize them as well.
1110 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1111 if (TypeQuals == 0) return CanType;
1112
1113 // If the type qualifiers are on an array type, get the canonical type of the
1114 // array with the qualifiers applied to the element type.
1115 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1116 if (!AT)
1117 return CanType.getQualifiedType(TypeQuals);
1118
1119 // Get the canonical version of the element with the extra qualifiers on it.
1120 // This can recursively sink qualifiers through multiple levels of arrays.
1121 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1122 NewEltTy = getCanonicalType(NewEltTy);
1123
1124 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1125 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1126 CAT->getIndexTypeQualifier());
1127 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1128 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1129 IAT->getIndexTypeQualifier());
1130
1131 // FIXME: What is the ownership of size expressions in VLAs?
1132 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1133 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1134 VAT->getSizeModifier(),
1135 VAT->getIndexTypeQualifier());
1136}
1137
1138
1139const ArrayType *ASTContext::getAsArrayType(QualType T) {
1140 // Handle the non-qualified case efficiently.
1141 if (T.getCVRQualifiers() == 0) {
1142 // Handle the common positive case fast.
1143 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1144 return AT;
1145 }
1146
1147 // Handle the common negative case fast, ignoring CVR qualifiers.
1148 QualType CType = T->getCanonicalTypeInternal();
1149
1150 // Make sure to look through type qualifiers (like ASQuals) for the negative
1151 // test.
1152 if (!isa<ArrayType>(CType) &&
1153 !isa<ArrayType>(CType.getUnqualifiedType()))
1154 return 0;
1155
1156 // Apply any CVR qualifiers from the array type to the element type. This
1157 // implements C99 6.7.3p8: "If the specification of an array type includes
1158 // any type qualifiers, the element type is so qualified, not the array type."
1159
1160 // If we get here, we either have type qualifiers on the type, or we have
1161 // sugar such as a typedef in the way. If we have type qualifiers on the type
1162 // we must propagate them down into the elemeng type.
1163 unsigned CVRQuals = T.getCVRQualifiers();
1164 unsigned AddrSpace = 0;
1165 Type *Ty = T.getTypePtr();
1166
1167 // Rip through ASQualType's and typedefs to get to a concrete type.
1168 while (1) {
1169 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1170 AddrSpace = ASQT->getAddressSpace();
1171 Ty = ASQT->getBaseType();
1172 } else {
1173 T = Ty->getDesugaredType();
1174 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1175 break;
1176 CVRQuals |= T.getCVRQualifiers();
1177 Ty = T.getTypePtr();
1178 }
1179 }
1180
1181 // If we have a simple case, just return now.
1182 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1183 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1184 return ATy;
1185
1186 // Otherwise, we have an array and we have qualifiers on it. Push the
1187 // qualifiers into the array element type and return a new array type.
1188 // Get the canonical version of the element with the extra qualifiers on it.
1189 // This can recursively sink qualifiers through multiple levels of arrays.
1190 QualType NewEltTy = ATy->getElementType();
1191 if (AddrSpace)
1192 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1193 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1194
1195 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1196 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1197 CAT->getSizeModifier(),
1198 CAT->getIndexTypeQualifier()));
1199 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1200 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1201 IAT->getSizeModifier(),
1202 IAT->getIndexTypeQualifier()));
1203
1204 // FIXME: What is the ownership of size expressions in VLAs?
1205 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1206 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1207 VAT->getSizeModifier(),
1208 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001209}
1210
1211
Chris Lattnere6327742008-04-02 05:18:44 +00001212/// getArrayDecayedType - Return the properly qualified result of decaying the
1213/// specified array type to a pointer. This operation is non-trivial when
1214/// handling typedefs etc. The canonical type of "T" must be an array type,
1215/// this returns a pointer to a properly qualified element of the array.
1216///
1217/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1218QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001219 // Get the element type with 'getAsArrayType' so that we don't lose any
1220 // typedefs in the element type of the array. This also handles propagation
1221 // of type qualifiers from the array type into the element type if present
1222 // (C99 6.7.3p8).
1223 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1224 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001225
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001226 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001227
1228 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001229 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001230}
1231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232/// getFloatingRank - Return a relative rank for floating point types.
1233/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001234static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001235 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001237
Christopher Lambebb97e92008-02-04 02:31:56 +00001238 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001239 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 case BuiltinType::Float: return FloatRank;
1241 case BuiltinType::Double: return DoubleRank;
1242 case BuiltinType::LongDouble: return LongDoubleRank;
1243 }
1244}
1245
Steve Naroff716c7302007-08-27 01:41:48 +00001246/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1247/// point or a complex type (based on typeDomain/typeSize).
1248/// 'typeDomain' is a real floating point or complex type.
1249/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001250QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1251 QualType Domain) const {
1252 FloatingRank EltRank = getFloatingRank(Size);
1253 if (Domain->isComplexType()) {
1254 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001255 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001256 case FloatRank: return FloatComplexTy;
1257 case DoubleRank: return DoubleComplexTy;
1258 case LongDoubleRank: return LongDoubleComplexTy;
1259 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 }
Chris Lattner1361b112008-04-06 23:58:54 +00001261
1262 assert(Domain->isRealFloatingType() && "Unknown domain!");
1263 switch (EltRank) {
1264 default: assert(0 && "getFloatingRank(): illegal value for rank");
1265 case FloatRank: return FloatTy;
1266 case DoubleRank: return DoubleTy;
1267 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001268 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001269}
1270
Chris Lattner7cfeb082008-04-06 23:55:33 +00001271/// getFloatingTypeOrder - Compare the rank of the two specified floating
1272/// point types, ignoring the domain of the type (i.e. 'double' ==
1273/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1274/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001275int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1276 FloatingRank LHSR = getFloatingRank(LHS);
1277 FloatingRank RHSR = getFloatingRank(RHS);
1278
1279 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001280 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001281 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001282 return 1;
1283 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001284}
1285
Chris Lattnerf52ab252008-04-06 22:59:24 +00001286/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1287/// routine will assert if passed a built-in type that isn't an integer or enum,
1288/// or if it is not canonicalized.
1289static unsigned getIntegerRank(Type *T) {
1290 assert(T->isCanonical() && "T should be canonicalized");
1291 if (isa<EnumType>(T))
1292 return 4;
1293
1294 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001295 default: assert(0 && "getIntegerRank(): not a built-in integer");
1296 case BuiltinType::Bool:
1297 return 1;
1298 case BuiltinType::Char_S:
1299 case BuiltinType::Char_U:
1300 case BuiltinType::SChar:
1301 case BuiltinType::UChar:
1302 return 2;
1303 case BuiltinType::Short:
1304 case BuiltinType::UShort:
1305 return 3;
1306 case BuiltinType::Int:
1307 case BuiltinType::UInt:
1308 return 4;
1309 case BuiltinType::Long:
1310 case BuiltinType::ULong:
1311 return 5;
1312 case BuiltinType::LongLong:
1313 case BuiltinType::ULongLong:
1314 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001315 }
1316}
1317
Chris Lattner7cfeb082008-04-06 23:55:33 +00001318/// getIntegerTypeOrder - Returns the highest ranked integer type:
1319/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1320/// LHS < RHS, return -1.
1321int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001322 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1323 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001324 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001325
Chris Lattnerf52ab252008-04-06 22:59:24 +00001326 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1327 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001328
Chris Lattner7cfeb082008-04-06 23:55:33 +00001329 unsigned LHSRank = getIntegerRank(LHSC);
1330 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001331
Chris Lattner7cfeb082008-04-06 23:55:33 +00001332 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1333 if (LHSRank == RHSRank) return 0;
1334 return LHSRank > RHSRank ? 1 : -1;
1335 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001336
Chris Lattner7cfeb082008-04-06 23:55:33 +00001337 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1338 if (LHSUnsigned) {
1339 // If the unsigned [LHS] type is larger, return it.
1340 if (LHSRank >= RHSRank)
1341 return 1;
1342
1343 // If the signed type can represent all values of the unsigned type, it
1344 // wins. Because we are dealing with 2's complement and types that are
1345 // powers of two larger than each other, this is always safe.
1346 return -1;
1347 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001348
Chris Lattner7cfeb082008-04-06 23:55:33 +00001349 // If the unsigned [RHS] type is larger, return it.
1350 if (RHSRank >= LHSRank)
1351 return -1;
1352
1353 // If the signed type can represent all values of the unsigned type, it
1354 // wins. Because we are dealing with 2's complement and types that are
1355 // powers of two larger than each other, this is always safe.
1356 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001357}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001358
1359// getCFConstantStringType - Return the type used for constant CFStrings.
1360QualType ASTContext::getCFConstantStringType() {
1361 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001362 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001363 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001364 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001365 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001366
1367 // const int *isa;
1368 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001369 // int flags;
1370 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001371 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001372 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001373 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001374 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001375 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001376 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001377
Anders Carlssonf06273f2007-11-19 00:25:30 +00001378 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001379 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001380 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001381
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001382 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001383 }
1384
1385 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001386}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001387
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001388QualType ASTContext::getObjCFastEnumerationStateType()
1389{
1390 if (!ObjCFastEnumerationStateTypeDecl) {
1391 QualType FieldTypes[] = {
1392 UnsignedLongTy,
1393 getPointerType(ObjCIdType),
1394 getPointerType(UnsignedLongTy),
1395 getConstantArrayType(UnsignedLongTy,
1396 llvm::APInt(32, 5), ArrayType::Normal, 0)
1397 };
1398
1399 FieldDecl *FieldDecls[4];
1400 for (size_t i = 0; i < 4; ++i)
1401 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1402 FieldTypes[i]);
1403
1404 ObjCFastEnumerationStateTypeDecl =
1405 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001406 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001407
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001408 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001409 }
1410
1411 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1412}
1413
Anders Carlssone8c49532007-10-29 06:33:42 +00001414// This returns true if a type has been typedefed to BOOL:
1415// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001416static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001417 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001418 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001419
1420 return false;
1421}
1422
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001423/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001424/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001425int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001426 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001427
1428 // Make all integer and enum types at least as large as an int
1429 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001430 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001431 // Treat arrays as pointers, since that's how they're passed in.
1432 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001433 sz = getTypeSize(VoidPtrTy);
1434 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001435}
1436
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001438/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001439void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001440 std::string& S)
1441{
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001442 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001443 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001445 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001446 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001447 // Compute size of all parameters.
1448 // Start with computing size of a pointer in number of bytes.
1449 // FIXME: There might(should) be a better way of doing this computation!
1450 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001451 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001452 // The first two arguments (self and _cmd) are pointers; account for
1453 // their size.
1454 int ParmOffset = 2 * PtrSize;
1455 int NumOfParams = Decl->getNumParams();
1456 for (int i = 0; i < NumOfParams; i++) {
1457 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001458 int sz = getObjCEncodingTypeSize (PType);
1459 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001460 ParmOffset += sz;
1461 }
1462 S += llvm::utostr(ParmOffset);
1463 S += "@0:";
1464 S += llvm::utostr(PtrSize);
1465
1466 // Argument types.
1467 ParmOffset = 2 * PtrSize;
1468 for (int i = 0; i < NumOfParams; i++) {
1469 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001470 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001471 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001472 getObjCEncodingForTypeQualifier(
1473 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001474 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001475 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001477 }
1478}
1479
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001480/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1481/// method declaration. If non-NULL, Container must be either an
1482/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1483/// NULL when getting encodings for protocol properties.
1484void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1485 const Decl *Container,
1486 std::string& S)
1487{
1488 // Collect information from the property implementation decl(s).
1489 bool Dynamic = false;
1490 ObjCPropertyImplDecl *SynthesizePID = 0;
1491
1492 // FIXME: Duplicated code due to poor abstraction.
1493 if (Container) {
1494 if (const ObjCCategoryImplDecl *CID =
1495 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1496 for (ObjCCategoryImplDecl::propimpl_iterator
1497 i = CID->propimpl_begin(), e = CID->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 } else {
1508 const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container);
1509 for (ObjCCategoryImplDecl::propimpl_iterator
1510 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1511 ObjCPropertyImplDecl *PID = *i;
1512 if (PID->getPropertyDecl() == PD) {
1513 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1514 Dynamic = true;
1515 } else {
1516 SynthesizePID = PID;
1517 }
1518 }
1519 }
1520 }
1521 }
1522
1523 // FIXME: This is not very efficient.
1524 S = "T";
1525
1526 // Encode result type.
1527 // FIXME: GCC uses a generating_property_type_encoding mode during
1528 // this part. Investigate.
1529 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1530
1531 if (PD->isReadOnly()) {
1532 S += ",R";
1533 } else {
1534 switch (PD->getSetterKind()) {
1535 case ObjCPropertyDecl::Assign: break;
1536 case ObjCPropertyDecl::Copy: S += ",C"; break;
1537 case ObjCPropertyDecl::Retain: S += ",&"; break;
1538 }
1539 }
1540
1541 // It really isn't clear at all what this means, since properties
1542 // are "dynamic by default".
1543 if (Dynamic)
1544 S += ",D";
1545
1546 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1547 S += ",G";
1548 S += PD->getGetterName().getName();
1549 }
1550
1551 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1552 S += ",S";
1553 S += PD->getSetterName().getName();
1554 }
1555
1556 if (SynthesizePID) {
1557 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1558 S += ",V";
1559 S += OID->getName();
1560 }
1561
1562 // FIXME: OBJCGC: weak & strong
1563}
1564
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001565void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001566 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001567 // FIXME: This currently doesn't encode:
1568 // @ An object (whether statically typed or typed id)
1569 // # A class object (Class)
1570 // : A method selector (SEL)
1571 // {name=type...} A structure
1572 // (name=type...) A union
1573 // bnum A bit field of num bits
1574
1575 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001576 char encoding;
1577 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001578 default: assert(0 && "Unhandled builtin type kind");
1579 case BuiltinType::Void: encoding = 'v'; break;
1580 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001581 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001582 case BuiltinType::UChar: encoding = 'C'; break;
1583 case BuiltinType::UShort: encoding = 'S'; break;
1584 case BuiltinType::UInt: encoding = 'I'; break;
1585 case BuiltinType::ULong: encoding = 'L'; break;
1586 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001587 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001588 case BuiltinType::SChar: encoding = 'c'; break;
1589 case BuiltinType::Short: encoding = 's'; break;
1590 case BuiltinType::Int: encoding = 'i'; break;
1591 case BuiltinType::Long: encoding = 'l'; break;
1592 case BuiltinType::LongLong: encoding = 'q'; break;
1593 case BuiltinType::Float: encoding = 'f'; break;
1594 case BuiltinType::Double: encoding = 'd'; break;
1595 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001596 }
1597
1598 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001599 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001600 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001601 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001602 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001603
1604 }
1605 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001606 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001607 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001608 S += '@';
1609 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001611 S += '#';
1612 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001614 S += ':';
1615 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001616 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001617
1618 if (PointeeTy->isCharType()) {
1619 // char pointer types should be encoded as '*' unless it is a
1620 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001621 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001622 S += '*';
1623 return;
1624 }
1625 }
1626
1627 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001628 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001629 } else if (const ArrayType *AT =
1630 // Ignore type qualifiers etc.
1631 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001632 S += '[';
1633
1634 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1635 S += llvm::utostr(CAT->getSize().getZExtValue());
1636 else
1637 assert(0 && "Unhandled array type!");
1638
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001639 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001640 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001641 } else if (T->getAsFunctionType()) {
1642 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001643 } else if (const RecordType *RTy = T->getAsRecordType()) {
1644 RecordDecl *RDecl= RTy->getDecl();
Steve Narofffaf37e72008-08-14 15:00:38 +00001645 // This mimics the behavior in gcc's encode_aggregate_within().
1646 // The idea is to only inline structure definitions for top level pointers
1647 // to structures and embedded structures.
1648 bool inlining = (S.size() == 1 && S[0] == '^' ||
1649 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001650 S += '{';
1651 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001652 bool found = false;
1653 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1654 if (ERType[i] == RTy) {
1655 found = true;
1656 break;
1657 }
Steve Narofffaf37e72008-08-14 15:00:38 +00001658 if (!found && inlining) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001659 ERType.push_back(RTy);
1660 S += '=';
1661 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1662 FieldDecl *field = RDecl->getMember(i);
1663 getObjCEncodingForType(field->getType(), S, ERType);
1664 }
1665 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1666 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001667 }
1668 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001669 } else if (T->isEnumeralType()) {
1670 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001671 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001672 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001673}
1674
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001675void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001676 std::string& S) const {
1677 if (QT & Decl::OBJC_TQ_In)
1678 S += 'n';
1679 if (QT & Decl::OBJC_TQ_Inout)
1680 S += 'N';
1681 if (QT & Decl::OBJC_TQ_Out)
1682 S += 'o';
1683 if (QT & Decl::OBJC_TQ_Bycopy)
1684 S += 'O';
1685 if (QT & Decl::OBJC_TQ_Byref)
1686 S += 'R';
1687 if (QT & Decl::OBJC_TQ_Oneway)
1688 S += 'V';
1689}
1690
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001691void ASTContext::setBuiltinVaListType(QualType T)
1692{
1693 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1694
1695 BuiltinVaListType = T;
1696}
1697
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001698void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001699{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001700 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001701
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001702 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001703
1704 // typedef struct objc_object *id;
1705 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1706 assert(ptr && "'id' incorrectly typed");
1707 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1708 assert(rec && "'id' incorrectly typed");
1709 IdStructType = rec;
1710}
1711
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001712void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001713{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001714 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001715
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001716 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001717
1718 // typedef struct objc_selector *SEL;
1719 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1720 assert(ptr && "'SEL' incorrectly typed");
1721 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1722 assert(rec && "'SEL' incorrectly typed");
1723 SelStructType = rec;
1724}
1725
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001726void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001727{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001728 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1729 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001730}
1731
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001732void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001733{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001735
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001736 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001737
1738 // typedef struct objc_class *Class;
1739 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1740 assert(ptr && "'Class' incorrectly typed");
1741 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1742 assert(rec && "'Class' incorrectly typed");
1743 ClassStructType = rec;
1744}
1745
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001746void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1747 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001748 "'NSConstantString' type already set!");
1749
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001750 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001751}
1752
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001753
1754//===----------------------------------------------------------------------===//
1755// Type Predicates.
1756//===----------------------------------------------------------------------===//
1757
1758/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1759/// to an object type. This includes "id" and "Class" (two 'special' pointers
1760/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1761/// ID type).
1762bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1763 if (Ty->isObjCQualifiedIdType())
1764 return true;
1765
1766 if (!Ty->isPointerType())
1767 return false;
1768
1769 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1770 // pointer types. This looks for the typedef specifically, not for the
1771 // underlying type.
1772 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1773 return true;
1774
1775 // If this a pointer to an interface (e.g. NSString*), it is ok.
1776 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1777}
1778
Chris Lattner6ac46a42008-04-07 06:51:04 +00001779//===----------------------------------------------------------------------===//
1780// Type Compatibility Testing
1781//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001782
Steve Naroff1c7d0672008-09-04 15:10:53 +00001783/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffbfdcae62008-09-04 15:31:07 +00001784/// block types. Types must be strictly compatible here.
Steve Naroff1c7d0672008-09-04 15:10:53 +00001785bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
1786 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers())
1787 return false;
1788
1789 QualType lcanon = getCanonicalType(lhs);
1790 QualType rcanon = getCanonicalType(rhs);
1791
1792 // If two types are identical, they are are compatible
1793 if (lcanon == rcanon)
1794 return true;
1795 if (isa<FunctionType>(lcanon) && isa<FunctionType>(rcanon)) {
1796 const FunctionType *lbase = cast<FunctionType>(lcanon);
1797 const FunctionType *rbase = cast<FunctionType>(rcanon);
1798
1799 // First check the return types.
1800 if (!typesAreBlockCompatible(lbase->getResultType(),rbase->getResultType()))
1801 return false;
1802
1803 // Return types matched, now check the argument types.
1804 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1805 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1806
1807 if (lproto && rproto) { // two C99 style function prototypes
1808 unsigned lproto_nargs = lproto->getNumArgs();
1809 unsigned rproto_nargs = rproto->getNumArgs();
1810
1811 if (lproto_nargs != rproto_nargs)
1812 return false;
1813
1814 if (lproto->isVariadic() || rproto->isVariadic())
1815 return false;
1816
1817 // The use of ellipsis agree...now check the argument types.
1818 for (unsigned i = 0; i < lproto_nargs; i++)
1819 if (!typesAreBlockCompatible(lproto->getArgType(i),
1820 rproto->getArgType(i)))
1821 return false;
1822 return true;
1823 }
1824 return (!lproto && !rproto); // two K&R style function decls match.
1825 }
1826 return false;
1827}
1828
Chris Lattner6ac46a42008-04-07 06:51:04 +00001829/// areCompatVectorTypes - Return true if the two specified vector types are
1830/// compatible.
1831static bool areCompatVectorTypes(const VectorType *LHS,
1832 const VectorType *RHS) {
1833 assert(LHS->isCanonical() && RHS->isCanonical());
1834 return LHS->getElementType() == RHS->getElementType() &&
1835 LHS->getNumElements() == RHS->getNumElements();
1836}
1837
Eli Friedman3d815e72008-08-22 00:56:42 +00001838/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001839/// compatible for assignment from RHS to LHS. This handles validation of any
1840/// protocol qualifiers on the LHS or RHS.
1841///
Eli Friedman3d815e72008-08-22 00:56:42 +00001842bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1843 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001844 // Verify that the base decls are compatible: the RHS must be a subclass of
1845 // the LHS.
1846 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1847 return false;
1848
1849 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1850 // protocol qualified at all, then we are good.
1851 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1852 return true;
1853
1854 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1855 // isn't a superset.
1856 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1857 return true; // FIXME: should return false!
1858
1859 // Finally, we must have two protocol-qualified interfaces.
1860 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1861 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1862 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1863 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1864 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1865 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1866
1867 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1868 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1869 // LHS in a single parallel scan until we run out of elements in LHS.
1870 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1871 ObjCProtocolDecl *LHSProto = *LHSPI;
1872
1873 while (RHSPI != RHSPE) {
1874 ObjCProtocolDecl *RHSProto = *RHSPI++;
1875 // If the RHS has a protocol that the LHS doesn't, ignore it.
1876 if (RHSProto != LHSProto)
1877 continue;
1878
1879 // Otherwise, the RHS does have this element.
1880 ++LHSPI;
1881 if (LHSPI == LHSPE)
1882 return true; // All protocols in LHS exist in RHS.
1883
1884 LHSProto = *LHSPI;
1885 }
1886
1887 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1888 return false;
1889}
1890
Steve Naroffec0550f2007-10-15 20:41:53 +00001891/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1892/// both shall have the identically qualified version of a compatible type.
1893/// C99 6.2.7p1: Two types have compatible types if their types are the
1894/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001895bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1896 return !mergeTypes(LHS, RHS).isNull();
1897}
1898
1899QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1900 const FunctionType *lbase = lhs->getAsFunctionType();
1901 const FunctionType *rbase = rhs->getAsFunctionType();
1902 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1903 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1904 bool allLTypes = true;
1905 bool allRTypes = true;
1906
1907 // Check return type
1908 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1909 if (retType.isNull()) return QualType();
1910 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false;
1911 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false;
1912
1913 if (lproto && rproto) { // two C99 style function prototypes
1914 unsigned lproto_nargs = lproto->getNumArgs();
1915 unsigned rproto_nargs = rproto->getNumArgs();
1916
1917 // Compatible functions must have the same number of arguments
1918 if (lproto_nargs != rproto_nargs)
1919 return QualType();
1920
1921 // Variadic and non-variadic functions aren't compatible
1922 if (lproto->isVariadic() != rproto->isVariadic())
1923 return QualType();
1924
1925 // Check argument compatibility
1926 llvm::SmallVector<QualType, 10> types;
1927 for (unsigned i = 0; i < lproto_nargs; i++) {
1928 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1929 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1930 QualType argtype = mergeTypes(largtype, rargtype);
1931 if (argtype.isNull()) return QualType();
1932 types.push_back(argtype);
1933 if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false;
1934 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false;
1935 }
1936 if (allLTypes) return lhs;
1937 if (allRTypes) return rhs;
1938 return getFunctionType(retType, types.begin(), types.size(),
1939 lproto->isVariadic());
1940 }
1941
1942 if (lproto) allRTypes = false;
1943 if (rproto) allLTypes = false;
1944
1945 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1946 if (proto) {
1947 if (proto->isVariadic()) return QualType();
1948 // Check that the types are compatible with the types that
1949 // would result from default argument promotions (C99 6.7.5.3p15).
1950 // The only types actually affected are promotable integer
1951 // types and floats, which would be passed as a different
1952 // type depending on whether the prototype is visible.
1953 unsigned proto_nargs = proto->getNumArgs();
1954 for (unsigned i = 0; i < proto_nargs; ++i) {
1955 QualType argTy = proto->getArgType(i);
1956 if (argTy->isPromotableIntegerType() ||
1957 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1958 return QualType();
1959 }
1960
1961 if (allLTypes) return lhs;
1962 if (allRTypes) return rhs;
1963 return getFunctionType(retType, proto->arg_type_begin(),
1964 proto->getNumArgs(), lproto->isVariadic());
1965 }
1966
1967 if (allLTypes) return lhs;
1968 if (allRTypes) return rhs;
1969 return getFunctionTypeNoProto(retType);
1970}
1971
1972QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00001973 // C++ [expr]: If an expression initially has the type "reference to T", the
1974 // type is adjusted to "T" prior to any further analysis, the expression
1975 // designates the object or function denoted by the reference, and the
1976 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00001977 // FIXME: C++ shouldn't be going through here! The rules are different
1978 // enough that they should be handled separately.
1979 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001980 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00001981 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001982 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001983
Eli Friedman3d815e72008-08-22 00:56:42 +00001984 QualType LHSCan = getCanonicalType(LHS),
1985 RHSCan = getCanonicalType(RHS);
1986
1987 // If two types are identical, they are compatible.
1988 if (LHSCan == RHSCan)
1989 return LHS;
1990
1991 // If the qualifiers are different, the types aren't compatible
1992 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
1993 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
1994 return QualType();
1995
1996 Type::TypeClass LHSClass = LHSCan->getTypeClass();
1997 Type::TypeClass RHSClass = RHSCan->getTypeClass();
1998
Chris Lattner1adb8832008-01-14 05:45:46 +00001999 // We want to consider the two function types to be the same for these
2000 // comparisons, just force one to the other.
2001 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2002 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002003
2004 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002005 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2006 LHSClass = Type::ConstantArray;
2007 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2008 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002009
Nate Begeman213541a2008-04-18 23:10:10 +00002010 // Canonicalize ExtVector -> Vector.
2011 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2012 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002013
Chris Lattnerb0489812008-04-07 06:38:24 +00002014 // Consider qualified interfaces and interfaces the same.
2015 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2016 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002017
Chris Lattnera36a61f2008-04-07 05:43:21 +00002018 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002019 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00002020 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00002021 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00002022 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00002023 if (isObjCIdType(PT->getPointeeType()))
2024 return LHS;
Steve Naroff97341622008-06-04 15:07:33 +00002025 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002026 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00002027 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00002028 if (isObjCIdType(PT->getPointeeType()))
2029 return RHS;
2030 }
2031
Chris Lattner1adb8832008-01-14 05:45:46 +00002032 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2033 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002034 if (const EnumType* ETy = LHS->getAsEnumType()) {
2035 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2036 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002037 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002038 if (const EnumType* ETy = RHS->getAsEnumType()) {
2039 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2040 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002041 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002042
Eli Friedman3d815e72008-08-22 00:56:42 +00002043 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002044 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002045
Steve Naroff4a746782008-01-09 22:43:08 +00002046 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002047 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002048 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002049 {
2050 // Merge two pointer types, while trying to preserve typedef info
2051 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2052 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2053 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2054 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00002055 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS;
2056 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002057 return getPointerType(ResultType);
2058 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002059 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002060 {
2061 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2062 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2063 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2064 return QualType();
2065
2066 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2067 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2068 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2069 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00002070 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2071 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
2072 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2073 ArrayType::ArraySizeModifier(), 0);
2074 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2075 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002076 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2077 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Eli Friedman3bc0f452008-08-22 01:48:21 +00002078 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2079 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002080 if (LVAT) {
2081 // FIXME: This isn't correct! But tricky to implement because
2082 // the array's size has to be the size of LHS, but the type
2083 // has to be different.
2084 return LHS;
2085 }
2086 if (RVAT) {
2087 // FIXME: This isn't correct! But tricky to implement because
2088 // the array's size has to be the size of RHS, but the type
2089 // has to be different.
2090 return RHS;
2091 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002092 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2093 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002094 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0);
2095 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002096 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002097 return mergeFunctionTypes(LHS, RHS);
2098 case Type::Tagged:
2099 {
2100 // FIXME: Why are these compatible?
2101 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2102 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2103 return QualType();
2104 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002105 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002106 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002107 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002108 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002109 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2110 return LHS;
Chris Lattner1adb8832008-01-14 05:45:46 +00002111 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002112 {
2113 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2114 // for checking assignment/comparison safety
2115 return QualType();
2116 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002117 default:
2118 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002119 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002120 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002121}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002122
Chris Lattner5426bf62008-04-07 07:01:58 +00002123//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002124// Integer Predicates
2125//===----------------------------------------------------------------------===//
2126unsigned ASTContext::getIntWidth(QualType T) {
2127 if (T == BoolTy)
2128 return 1;
2129 // At the moment, only bool has padding bits
2130 return (unsigned)getTypeSize(T);
2131}
2132
2133QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2134 assert(T->isSignedIntegerType() && "Unexpected type");
2135 if (const EnumType* ETy = T->getAsEnumType())
2136 T = ETy->getDecl()->getIntegerType();
2137 const BuiltinType* BTy = T->getAsBuiltinType();
2138 assert (BTy && "Unexpected signed integer type");
2139 switch (BTy->getKind()) {
2140 case BuiltinType::Char_S:
2141 case BuiltinType::SChar:
2142 return UnsignedCharTy;
2143 case BuiltinType::Short:
2144 return UnsignedShortTy;
2145 case BuiltinType::Int:
2146 return UnsignedIntTy;
2147 case BuiltinType::Long:
2148 return UnsignedLongTy;
2149 case BuiltinType::LongLong:
2150 return UnsignedLongLongTy;
2151 default:
2152 assert(0 && "Unexpected signed integer type");
2153 return QualType();
2154 }
2155}
2156
2157
2158//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002159// Serialization Support
2160//===----------------------------------------------------------------------===//
2161
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002162/// Emit - Serialize an ASTContext object to Bitcode.
2163void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002164 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002165 S.EmitRef(SourceMgr);
2166 S.EmitRef(Target);
2167 S.EmitRef(Idents);
2168 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002169
Ted Kremenekfee04522007-10-31 22:44:07 +00002170 // Emit the size of the type vector so that we can reserve that size
2171 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002172 S.EmitInt(Types.size());
2173
Ted Kremenek03ed4402007-11-13 22:02:55 +00002174 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2175 I!=E;++I)
2176 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002177
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002178 S.EmitOwnedPtr(TUDecl);
2179
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002180 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002181}
2182
Ted Kremenek0f84c002007-11-13 00:25:37 +00002183ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002184
2185 // Read the language options.
2186 LangOptions LOpts;
2187 LOpts.Read(D);
2188
Ted Kremenekfee04522007-10-31 22:44:07 +00002189 SourceManager &SM = D.ReadRef<SourceManager>();
2190 TargetInfo &t = D.ReadRef<TargetInfo>();
2191 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2192 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002193
Ted Kremenekfee04522007-10-31 22:44:07 +00002194 unsigned size_reserve = D.ReadInt();
2195
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002196 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002197
Ted Kremenek03ed4402007-11-13 22:02:55 +00002198 for (unsigned i = 0; i < size_reserve; ++i)
2199 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002200
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002201 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2202
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002203 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002204
2205 return A;
2206}