blob: 0b513ecbd194539773928f9c3c59a96e88fda2c1 [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"
15#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000047 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000062 else if (isa<ComplexType>(T))
63 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 else if (isa<ArrayType>(T))
65 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000066 else if (isa<VectorType>(T))
67 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff3f128ad2007-09-17 14:16:13 +000089 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000098 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000100 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattner464175b2007-07-18 17:52:12 +0000176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000182std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000184 T = T.getCanonicalType();
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000185 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000186 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000187 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000188 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000191 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000192 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner98be4942008-03-05 18:54:05 +0000198 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000199 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000200 Align = EltInfo.second;
201 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000202 }
203 case Type::OCUVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000206 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000207 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner030d8842007-07-19 22:06:24 +0000208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000212
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000213 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
216 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000217 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000218 case BuiltinType::Void:
219 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000220 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000221 Width = Target.getBoolWidth();
222 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000223 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000227 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000228 Width = Target.getCharWidth();
229 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000230 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000231 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000233 Width = Target.getShortWidth();
234 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000235 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000236 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000237 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000238 Width = Target.getIntWidth();
239 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000241 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000242 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000243 Width = Target.getLongWidth();
244 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000245 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000246 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000247 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000248 Width = Target.getLongLongWidth();
249 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000250 break;
251 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000252 Width = Target.getFloatWidth();
253 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000254 break;
255 case BuiltinType::Double:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000256 Width = Target.getDoubleWidth();
257 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000258 break;
259 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000260 Width = Target.getLongDoubleWidth();
261 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000262 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000263 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000264 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000266 // FIXME: Pointers into different addr spaces could have different sizes and
267 // alignment requirements: getPointerInfo should take an AddrSpace.
268 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000269 case Type::ObjCQualifiedId:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000270 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000271 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000272 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000273 case Type::Pointer: {
274 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000275 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000276 Align = Target.getPointerAlign(AS);
277 break;
278 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000279 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000280 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000281 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000282 // FIXME: This is wrong for struct layout: a reference in a struct has
283 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000284 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000285
286 case Type::Complex: {
287 // Complex types have the same alignment as their elements, but twice the
288 // size.
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000290 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000292 Align = EltInfo.second;
293 break;
294 }
Chris Lattner71763312008-04-06 22:05:18 +0000295 case Type::Tagged: {
296 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
297 return getTypeInfo(ET->getDecl()->getIntegerType());
298
299 RecordType *RT = cast<RecordType>(T);
300 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
301 Width = Layout.getSize();
302 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000303 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000304 }
Chris Lattner71763312008-04-06 22:05:18 +0000305 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000306
Chris Lattner464175b2007-07-18 17:52:12 +0000307 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000308 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000309}
310
Devang Patel88a981b2007-11-01 19:11:01 +0000311/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000312/// specified record (struct/union/class), which indicates its size and field
313/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000314const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000315 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
316
317 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000318 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000319 if (Entry) return *Entry;
320
Devang Patel88a981b2007-11-01 19:11:01 +0000321 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
322 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
323 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000324 Entry = NewEntry;
325
326 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
327 uint64_t RecordSize = 0;
328 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
329
330 if (D->getKind() != Decl::Union) {
Anders Carlsson042c4e72008-02-16 19:51:27 +0000331 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
332 RecordAlign = std::max(RecordAlign, AA->getAlignment());
333
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000334 bool StructIsPacked = D->getAttr<PackedAttr>();
335
Chris Lattner464175b2007-07-18 17:52:12 +0000336 // Layout each field, for now, just sequentially, respecting alignment. In
337 // the future, this will need to be tweakable by targets.
338 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
339 const FieldDecl *FD = D->getMember(i);
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000340 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman75afb582008-02-06 05:33:51 +0000341 uint64_t FieldSize;
342 unsigned FieldAlign;
Anders Carlsson8af226a2008-02-18 07:13:09 +0000343
344 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
345 llvm::APSInt I(32);
346 bool BitWidthIsICE =
347 BitWidthExpr->isIntegerConstantExpr(I, *this);
348 assert (BitWidthIsICE && "Invalid BitField size expression");
349 FieldSize = I.getZExtValue();
350
Chris Lattner98be4942008-03-05 18:54:05 +0000351 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000352 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson042c4e72008-02-16 19:51:27 +0000353
354 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
355 FieldAlign = AA->getAlignment();
356 else if (FieldIsPacked)
357 FieldAlign = 8;
358 else {
Anders Carlsson8af226a2008-02-18 07:13:09 +0000359 // FIXME: This is X86 specific, use 32-bit alignment for long long.
360 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
361 FieldAlign = 32;
362 else
363 FieldAlign = TypeInfo.second;
Anders Carlsson042c4e72008-02-16 19:51:27 +0000364 }
Eli Friedman75afb582008-02-06 05:33:51 +0000365
Anders Carlsson8af226a2008-02-18 07:13:09 +0000366 // Check if we need to add padding to give the field the correct
367 // alignment.
368 if (RecordSize % FieldAlign + FieldSize > TypeSize)
369 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
370
371 } else {
372 if (FD->getType()->isIncompleteType()) {
373 // This must be a flexible array member; we can't directly
374 // query getTypeInfo about these, so we figure it out here.
375 // Flexible array members don't have any size, but they
376 // have to be aligned appropriately for their element type.
377
378 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
379 FieldAlign = AA->getAlignment();
380 else if (FieldIsPacked)
381 FieldAlign = 8;
382 else {
383 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner98be4942008-03-05 18:54:05 +0000384 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000385 }
386 FieldSize = 0;
387 } else {
Chris Lattner98be4942008-03-05 18:54:05 +0000388 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000389 FieldSize = FieldInfo.first;
390
391 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
392 FieldAlign = AA->getAlignment();
393 else if (FieldIsPacked)
394 FieldAlign = 8;
395 else
396 FieldAlign = FieldInfo.second;
397 }
398
399 // Round up the current record size to the field's alignment boundary.
400 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
401 }
Chris Lattner464175b2007-07-18 17:52:12 +0000402
403 // Place this field at the current location.
404 FieldOffsets[i] = RecordSize;
405
406 // Reserve space for this field.
407 RecordSize += FieldSize;
408
409 // Remember max struct/class alignment.
410 RecordAlign = std::max(RecordAlign, FieldAlign);
411 }
412
413 // Finally, round the size of the total struct up to the alignment of the
414 // struct itself.
415 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
416 } else {
417 // Union layout just puts each member at the start of the record.
418 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
419 const FieldDecl *FD = D->getMember(i);
Chris Lattner98be4942008-03-05 18:54:05 +0000420 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner464175b2007-07-18 17:52:12 +0000421 uint64_t FieldSize = FieldInfo.first;
422 unsigned FieldAlign = FieldInfo.second;
423
Anders Carlsson8af226a2008-02-18 07:13:09 +0000424 // FIXME: This is X86 specific, use 32-bit alignment for long long.
425 if (FD->getType()->isIntegerType() && FieldAlign > 32)
426 FieldAlign = 32;
427
Chris Lattner464175b2007-07-18 17:52:12 +0000428 // Round up the current record size to the field's alignment boundary.
429 RecordSize = std::max(RecordSize, FieldSize);
430
431 // Place this field at the start of the record.
432 FieldOffsets[i] = 0;
433
434 // Remember max struct/class alignment.
435 RecordAlign = std::max(RecordAlign, FieldAlign);
436 }
437 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000438
439 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
440 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000441}
442
Chris Lattnera7674d82007-07-13 22:13:22 +0000443//===----------------------------------------------------------------------===//
444// Type creation/memoization methods
445//===----------------------------------------------------------------------===//
446
Christopher Lambebb97e92008-02-04 02:31:56 +0000447QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000448 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
449 return T;
450
451 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
452 // with CVR qualifiers from here on out.
453 assert(T.getCanonicalType().getAddressSpace() == 0 &&
454 "Type is already address space qualified");
455
456 // Check if we've already instantiated an address space qual'd type of this
457 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000458 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000459 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 void *InsertPos = 0;
461 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
462 return QualType(ASQy, 0);
463
464 // If the base type isn't canonical, this won't be a canonical type either,
465 // so fill in the canonical type field.
466 QualType Canonical;
467 if (!T->isCanonical()) {
468 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
469
470 // Get the new insert position for the node we care about.
471 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
472 assert(NewIP == 0 && "Shouldn't be in the map!");
473 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000474 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000475 ASQualTypes.InsertNode(New, InsertPos);
476 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000477 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000478}
479
Chris Lattnera7674d82007-07-13 22:13:22 +0000480
Reid Spencer5f016e22007-07-11 17:01:13 +0000481/// getComplexType - Return the uniqued reference to the type for a complex
482/// number with the specified element type.
483QualType ASTContext::getComplexType(QualType T) {
484 // Unique pointers, to guarantee there is only one pointer of a particular
485 // structure.
486 llvm::FoldingSetNodeID ID;
487 ComplexType::Profile(ID, T);
488
489 void *InsertPos = 0;
490 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
491 return QualType(CT, 0);
492
493 // If the pointee type isn't canonical, this won't be a canonical type either,
494 // so fill in the canonical type field.
495 QualType Canonical;
496 if (!T->isCanonical()) {
497 Canonical = getComplexType(T.getCanonicalType());
498
499 // Get the new insert position for the node we care about.
500 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
501 assert(NewIP == 0 && "Shouldn't be in the map!");
502 }
503 ComplexType *New = new ComplexType(T, Canonical);
504 Types.push_back(New);
505 ComplexTypes.InsertNode(New, InsertPos);
506 return QualType(New, 0);
507}
508
509
510/// getPointerType - Return the uniqued reference to the type for a pointer to
511/// the specified type.
512QualType ASTContext::getPointerType(QualType T) {
513 // Unique pointers, to guarantee there is only one pointer of a particular
514 // structure.
515 llvm::FoldingSetNodeID ID;
516 PointerType::Profile(ID, T);
517
518 void *InsertPos = 0;
519 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(PT, 0);
521
522 // If the pointee type isn't canonical, this won't be a canonical type either,
523 // so fill in the canonical type field.
524 QualType Canonical;
525 if (!T->isCanonical()) {
526 Canonical = getPointerType(T.getCanonicalType());
527
528 // Get the new insert position for the node we care about.
529 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532 PointerType *New = new PointerType(T, Canonical);
533 Types.push_back(New);
534 PointerTypes.InsertNode(New, InsertPos);
535 return QualType(New, 0);
536}
537
538/// getReferenceType - Return the uniqued reference to the type for a reference
539/// to the specified type.
540QualType ASTContext::getReferenceType(QualType T) {
541 // Unique pointers, to guarantee there is only one pointer of a particular
542 // structure.
543 llvm::FoldingSetNodeID ID;
544 ReferenceType::Profile(ID, T);
545
546 void *InsertPos = 0;
547 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
548 return QualType(RT, 0);
549
550 // If the referencee type isn't canonical, this won't be a canonical type
551 // either, so fill in the canonical type field.
552 QualType Canonical;
553 if (!T->isCanonical()) {
554 Canonical = getReferenceType(T.getCanonicalType());
555
556 // Get the new insert position for the node we care about.
557 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
558 assert(NewIP == 0 && "Shouldn't be in the map!");
559 }
560
561 ReferenceType *New = new ReferenceType(T, Canonical);
562 Types.push_back(New);
563 ReferenceTypes.InsertNode(New, InsertPos);
564 return QualType(New, 0);
565}
566
Steve Narofffb22d962007-08-30 01:06:46 +0000567/// getConstantArrayType - Return the unique reference to the type for an
568/// array of the specified element type.
569QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000570 const llvm::APInt &ArySize,
571 ArrayType::ArraySizeModifier ASM,
572 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000574 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575
576 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000577 if (ConstantArrayType *ATP =
578 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 return QualType(ATP, 0);
580
581 // If the element type isn't canonical, this won't be a canonical type either,
582 // so fill in the canonical type field.
583 QualType Canonical;
584 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000585 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
586 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000588 ConstantArrayType *NewIP =
589 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 assert(NewIP == 0 && "Shouldn't be in the map!");
592 }
593
Steve Naroffc9406122007-08-30 18:10:14 +0000594 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
595 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000596 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 Types.push_back(New);
598 return QualType(New, 0);
599}
600
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000601/// getVariableArrayType - Returns a non-unique reference to the type for a
602/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000603QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
604 ArrayType::ArraySizeModifier ASM,
605 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000606 // Since we don't unique expressions, it isn't possible to unique VLA's
607 // that have an expression provided for their size.
608
609 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
610 ASM, EltTypeQuals);
611
612 VariableArrayTypes.push_back(New);
613 Types.push_back(New);
614 return QualType(New, 0);
615}
616
617QualType ASTContext::getIncompleteArrayType(QualType EltTy,
618 ArrayType::ArraySizeModifier ASM,
619 unsigned EltTypeQuals) {
620 llvm::FoldingSetNodeID ID;
621 IncompleteArrayType::Profile(ID, EltTy);
622
623 void *InsertPos = 0;
624 if (IncompleteArrayType *ATP =
625 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
626 return QualType(ATP, 0);
627
628 // If the element type isn't canonical, this won't be a canonical type
629 // either, so fill in the canonical type field.
630 QualType Canonical;
631
632 if (!EltTy->isCanonical()) {
633 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000634 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000635
636 // Get the new insert position for the node we care about.
637 IncompleteArrayType *NewIP =
638 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
639
640 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000641 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000642
643 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
644 ASM, EltTypeQuals);
645
646 IncompleteArrayTypes.InsertNode(New, InsertPos);
647 Types.push_back(New);
648 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000649}
650
Steve Naroff73322922007-07-18 18:00:27 +0000651/// getVectorType - Return the unique reference to a vector type of
652/// the specified element type and size. VectorType must be a built-in type.
653QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 BuiltinType *baseType;
655
656 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000657 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000658
659 // Check if we've already instantiated a vector of this type.
660 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000661 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 void *InsertPos = 0;
663 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
664 return QualType(VTP, 0);
665
666 // If the element type isn't canonical, this won't be a canonical type either,
667 // so fill in the canonical type field.
668 QualType Canonical;
669 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000670 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000671
672 // Get the new insert position for the node we care about.
673 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
674 assert(NewIP == 0 && "Shouldn't be in the map!");
675 }
676 VectorType *New = new VectorType(vecType, NumElts, Canonical);
677 VectorTypes.InsertNode(New, InsertPos);
678 Types.push_back(New);
679 return QualType(New, 0);
680}
681
Steve Naroff73322922007-07-18 18:00:27 +0000682/// getOCUVectorType - Return the unique reference to an OCU vector type of
683/// the specified element type and size. VectorType must be a built-in type.
684QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
685 BuiltinType *baseType;
686
687 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
688 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
689
690 // Check if we've already instantiated a vector of this type.
691 llvm::FoldingSetNodeID ID;
692 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
693 void *InsertPos = 0;
694 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
695 return QualType(VTP, 0);
696
697 // If the element type isn't canonical, this won't be a canonical type either,
698 // so fill in the canonical type field.
699 QualType Canonical;
700 if (!vecType->isCanonical()) {
701 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
702
703 // Get the new insert position for the node we care about.
704 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
705 assert(NewIP == 0 && "Shouldn't be in the map!");
706 }
707 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
708 VectorTypes.InsertNode(New, InsertPos);
709 Types.push_back(New);
710 return QualType(New, 0);
711}
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
714///
715QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
716 // Unique functions, to guarantee there is only one function of a particular
717 // structure.
718 llvm::FoldingSetNodeID ID;
719 FunctionTypeNoProto::Profile(ID, ResultTy);
720
721 void *InsertPos = 0;
722 if (FunctionTypeNoProto *FT =
723 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(FT, 0);
725
726 QualType Canonical;
727 if (!ResultTy->isCanonical()) {
728 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
729
730 // Get the new insert position for the node we care about.
731 FunctionTypeNoProto *NewIP =
732 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
733 assert(NewIP == 0 && "Shouldn't be in the map!");
734 }
735
736 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
737 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000738 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 return QualType(New, 0);
740}
741
742/// getFunctionType - Return a normal function type with a typed argument
743/// list. isVariadic indicates whether the argument list includes '...'.
744QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
745 unsigned NumArgs, bool isVariadic) {
746 // Unique functions, to guarantee there is only one function of a particular
747 // structure.
748 llvm::FoldingSetNodeID ID;
749 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
750
751 void *InsertPos = 0;
752 if (FunctionTypeProto *FTP =
753 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
754 return QualType(FTP, 0);
755
756 // Determine whether the type being created is already canonical or not.
757 bool isCanonical = ResultTy->isCanonical();
758 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
759 if (!ArgArray[i]->isCanonical())
760 isCanonical = false;
761
762 // If this type isn't canonical, get the canonical version of it.
763 QualType Canonical;
764 if (!isCanonical) {
765 llvm::SmallVector<QualType, 16> CanonicalArgs;
766 CanonicalArgs.reserve(NumArgs);
767 for (unsigned i = 0; i != NumArgs; ++i)
768 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
769
770 Canonical = getFunctionType(ResultTy.getCanonicalType(),
771 &CanonicalArgs[0], NumArgs,
772 isVariadic);
773
774 // Get the new insert position for the node we care about.
775 FunctionTypeProto *NewIP =
776 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
777 assert(NewIP == 0 && "Shouldn't be in the map!");
778 }
779
780 // FunctionTypeProto objects are not allocated with new because they have a
781 // variable size array (for parameter types) at the end of them.
782 FunctionTypeProto *FTP =
783 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000784 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
786 Canonical);
787 Types.push_back(FTP);
788 FunctionTypeProtos.InsertNode(FTP, InsertPos);
789 return QualType(FTP, 0);
790}
791
792/// getTypedefType - Return the unique reference to the type for the
793/// specified typename decl.
794QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
795 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
796
797 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000798 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 Types.push_back(Decl->TypeForDecl);
800 return QualType(Decl->TypeForDecl, 0);
801}
802
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000804/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000806 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
807
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000809 Types.push_back(Decl->TypeForDecl);
810 return QualType(Decl->TypeForDecl, 0);
811}
812
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000813/// getObjCQualifiedInterfaceType - Return a
814/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000815/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
817 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000818 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000819 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000820
821 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 if (ObjCQualifiedInterfaceType *QT =
823 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000824 return QualType(QT, 0);
825
826 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000827 ObjCQualifiedInterfaceType *QType =
828 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000829 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000830 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000831 return QualType(QType, 0);
832}
833
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000834/// getObjCQualifiedIdType - Return a
835/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000836/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000837QualType ASTContext::getObjCQualifiedIdType(QualType idType,
838 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000839 unsigned NumProtocols) {
840 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000841 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000842
843 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000844 if (ObjCQualifiedIdType *QT =
845 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000846 return QualType(QT, 0);
847
848 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000849 QualType Canonical;
850 if (!idType->isCanonical()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000852 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 ObjCQualifiedIdType *NewQT =
854 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000855 assert(NewQT == 0 && "Shouldn't be in the map!");
856 }
857
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000858 ObjCQualifiedIdType *QType =
859 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000860 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000862 return QualType(QType, 0);
863}
864
Steve Naroff9752f252007-08-01 18:02:17 +0000865/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
866/// TypeOfExpr AST's (since expression's are never shared). For example,
867/// multiple declarations that refer to "typeof(x)" all contain different
868/// DeclRefExpr's. This doesn't effect the type checker, since it operates
869/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000870QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000871 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000872 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
873 Types.push_back(toe);
874 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000875}
876
Steve Naroff9752f252007-08-01 18:02:17 +0000877/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
878/// TypeOfType AST's. The only motivation to unique these nodes would be
879/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
880/// an issue. This doesn't effect the type checker, since it operates
881/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000882QualType ASTContext::getTypeOfType(QualType tofType) {
883 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000884 TypeOfType *tot = new TypeOfType(tofType, Canonical);
885 Types.push_back(tot);
886 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000887}
888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889/// getTagDeclType - Return the unique reference to the type for the
890/// specified TagDecl (struct/union/class/enum) decl.
891QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000892 assert (Decl);
893
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000894 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000895 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000896
897 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000898 Types.push_back(T);
899 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000900
901 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902}
903
904/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
905/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
906/// needs to agree with the definition in <stddef.h>.
907QualType ASTContext::getSizeType() const {
908 // On Darwin, size_t is defined as a "long unsigned int".
909 // FIXME: should derive from "Target".
910 return UnsignedLongTy;
911}
912
Eli Friedmanfd888a52008-02-12 08:29:21 +0000913/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
914/// width of characters in wide strings, The value is target dependent and
915/// needs to agree with the definition in <stddef.h>.
916QualType ASTContext::getWcharType() const {
917 // On Darwin, wchar_t is defined as a "int".
918 // FIXME: should derive from "Target".
919 return IntTy;
920}
921
Chris Lattner8b9023b2007-07-13 03:05:23 +0000922/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
923/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
924QualType ASTContext::getPointerDiffType() const {
925 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
926 // FIXME: should derive from "Target".
927 return IntTy;
928}
929
Chris Lattnere6327742008-04-02 05:18:44 +0000930//===----------------------------------------------------------------------===//
931// Type Operators
932//===----------------------------------------------------------------------===//
933
Chris Lattner77c96472008-04-06 22:41:35 +0000934/// getCanonicalType - Return the canonical (structural) type corresponding to
935/// the specified potentially non-canonical type. The non-canonical version
936/// of a type may have many "decorated" versions of types. Decorators can
937/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
938/// to be free of any of these, allowing two canonical types to be compared
939/// for exact equality with a simple pointer comparison.
940QualType ASTContext::getCanonicalType(QualType T) {
941 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
942 return QualType(CanType.getTypePtr(),
943 T.getCVRQualifiers() | CanType.getCVRQualifiers());
944}
945
946
Chris Lattnere6327742008-04-02 05:18:44 +0000947/// getArrayDecayedType - Return the properly qualified result of decaying the
948/// specified array type to a pointer. This operation is non-trivial when
949/// handling typedefs etc. The canonical type of "T" must be an array type,
950/// this returns a pointer to a properly qualified element of the array.
951///
952/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
953QualType ASTContext::getArrayDecayedType(QualType Ty) {
954 // Handle the common case where typedefs are not involved directly.
955 QualType EltTy;
956 unsigned ArrayQuals = 0;
957 unsigned PointerQuals = 0;
958 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
959 // Since T "isa" an array type, it could not have had an address space
960 // qualifier, just CVR qualifiers. The properly qualified element pointer
961 // gets the union of the CVR qualifiers from the element and the array, and
962 // keeps any address space qualifier on the element type if present.
963 EltTy = AT->getElementType();
964 ArrayQuals = Ty.getCVRQualifiers();
965 PointerQuals = AT->getIndexTypeQualifier();
966 } else {
967 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
968 // lose qualifiers when dealing with typedefs. Example:
969 // typedef int arr[10];
970 // void test2() {
971 // const arr b;
972 // b[4] = 1;
973 // }
974 //
975 // The decayed type of b is "const int*" even though the element type of the
976 // array is "int".
977 QualType CanTy = Ty.getCanonicalType();
978 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
979 assert(PrettyArrayType && "Not an array type!");
980
981 // Get the element type with 'getAsArrayType' so that we don't lose any
982 // typedefs in the element type of the array.
983 EltTy = PrettyArrayType->getElementType();
984
985 // If the array was address-space qualifier, make sure to ASQual the element
986 // type. We can just grab the address space from the canonical type.
987 if (unsigned AS = CanTy.getAddressSpace())
988 EltTy = getASQualType(EltTy, AS);
989
990 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
991 // the CVR qualifiers directly from the canonical type, which is guaranteed
992 // to have the full set unioned together.
993 ArrayQuals = CanTy.getCVRQualifiers();
994 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
995 }
996
Chris Lattnerd9654552008-04-02 06:06:35 +0000997 // Apply any CVR qualifiers from the array type to the element type. This
998 // implements C99 6.7.3p8: "If the specification of an array type includes
999 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattnere6327742008-04-02 05:18:44 +00001000 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1001
1002 QualType PtrTy = getPointerType(EltTy);
1003
1004 // int x[restrict 4] -> int *restrict
1005 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1006
1007 return PtrTy;
1008}
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1011/// routine will assert if passed a built-in type that isn't an integer or enum.
1012static int getIntegerRank(QualType t) {
Chris Lattner71763312008-04-06 22:05:18 +00001013 if (isa<EnumType>(t.getCanonicalType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 return 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001015
Christopher Lambebb97e92008-02-04 02:31:56 +00001016 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 switch (BT->getKind()) {
1018 default:
1019 assert(0 && "getIntegerRank(): not a built-in integer");
1020 case BuiltinType::Bool:
1021 return 1;
1022 case BuiltinType::Char_S:
1023 case BuiltinType::Char_U:
1024 case BuiltinType::SChar:
1025 case BuiltinType::UChar:
1026 return 2;
1027 case BuiltinType::Short:
1028 case BuiltinType::UShort:
1029 return 3;
1030 case BuiltinType::Int:
1031 case BuiltinType::UInt:
1032 return 4;
1033 case BuiltinType::Long:
1034 case BuiltinType::ULong:
1035 return 5;
1036 case BuiltinType::LongLong:
1037 case BuiltinType::ULongLong:
1038 return 6;
1039 }
1040}
1041
1042/// getFloatingRank - Return a relative rank for floating point types.
1043/// This routine will assert if passed a built-in type that isn't a float.
1044static int getFloatingRank(QualType T) {
1045 T = T.getCanonicalType();
Christopher Lambebb97e92008-02-04 02:31:56 +00001046 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 return getFloatingRank(CT->getElementType());
1048
Christopher Lambebb97e92008-02-04 02:31:56 +00001049 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +00001050 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 case BuiltinType::Float: return FloatRank;
1052 case BuiltinType::Double: return DoubleRank;
1053 case BuiltinType::LongDouble: return LongDoubleRank;
1054 }
1055}
1056
Steve Naroff716c7302007-08-27 01:41:48 +00001057/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1058/// point or a complex type (based on typeDomain/typeSize).
1059/// 'typeDomain' is a real floating point or complex type.
1060/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +00001061QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
1062 QualType typeSize, QualType typeDomain) const {
1063 if (typeDomain->isComplexType()) {
1064 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +00001065 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001066 case FloatRank: return FloatComplexTy;
1067 case DoubleRank: return DoubleComplexTy;
1068 case LongDoubleRank: return LongDoubleComplexTy;
1069 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 }
Steve Narofff1448a02007-08-27 01:27:54 +00001071 if (typeDomain->isRealFloatingType()) {
1072 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +00001073 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001074 case FloatRank: return FloatTy;
1075 case DoubleRank: return DoubleTy;
1076 case LongDoubleRank: return LongDoubleTy;
1077 }
1078 }
1079 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001080 //an invalid return value, but the assert
1081 //will ensure that this code is never reached.
1082 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001083}
1084
Steve Narofffb0d4962007-08-27 15:30:22 +00001085/// compareFloatingType - Handles 3 different combos:
1086/// float/float, float/complex, complex/complex.
1087/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
1088int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1089 if (getFloatingRank(lt) == getFloatingRank(rt))
1090 return 0;
1091 if (getFloatingRank(lt) > getFloatingRank(rt))
1092 return 1;
1093 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094}
1095
1096// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1097// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1098QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1099 if (lhs == rhs) return lhs;
1100
1101 bool t1Unsigned = lhs->isUnsignedIntegerType();
1102 bool t2Unsigned = rhs->isUnsignedIntegerType();
1103
1104 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1105 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1106
1107 // We have two integer types with differing signs
1108 QualType unsignedType = t1Unsigned ? lhs : rhs;
1109 QualType signedType = t1Unsigned ? rhs : lhs;
1110
1111 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1112 return unsignedType;
1113 else {
1114 // FIXME: Need to check if the signed type can represent all values of the
1115 // unsigned type. If it can, then the result is the signed type.
1116 // If it can't, then the result is the unsigned version of the signed type.
1117 // Should probably add a helper that returns a signed integer type from
1118 // an unsigned (and vice versa). C99 6.3.1.8.
1119 return signedType;
1120 }
1121}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001122
1123// getCFConstantStringType - Return the type used for constant CFStrings.
1124QualType ASTContext::getCFConstantStringType() {
1125 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001126 CFConstantStringTypeDecl =
Chris Lattner0ed844b2008-04-04 06:12:32 +00001127 RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001128 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001129 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001130
1131 // const int *isa;
1132 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001133 // int flags;
1134 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001135 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001136 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001137 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001138 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001139 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001140 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001141
Anders Carlssonf06273f2007-11-19 00:25:30 +00001142 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001143 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001144 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001145
1146 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1147 }
1148
1149 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001150}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001151
Anders Carlssone8c49532007-10-29 06:33:42 +00001152// This returns true if a type has been typedefed to BOOL:
1153// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001154static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001155 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001156 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001157
1158 return false;
1159}
1160
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001161/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001162/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001163int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001164 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001165
1166 // Make all integer and enum types at least as large as an int
1167 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001168 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001169 // Treat arrays as pointers, since that's how they're passed in.
1170 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001171 sz = getTypeSize(VoidPtrTy);
1172 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001173}
1174
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001175/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001176/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001177void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001178 std::string& S)
1179{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001180 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001181 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001182 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001183 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001184 // Compute size of all parameters.
1185 // Start with computing size of a pointer in number of bytes.
1186 // FIXME: There might(should) be a better way of doing this computation!
1187 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001188 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001189 // The first two arguments (self and _cmd) are pointers; account for
1190 // their size.
1191 int ParmOffset = 2 * PtrSize;
1192 int NumOfParams = Decl->getNumParams();
1193 for (int i = 0; i < NumOfParams; i++) {
1194 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001195 int sz = getObjCEncodingTypeSize (PType);
1196 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001197 ParmOffset += sz;
1198 }
1199 S += llvm::utostr(ParmOffset);
1200 S += "@0:";
1201 S += llvm::utostr(PtrSize);
1202
1203 // Argument types.
1204 ParmOffset = 2 * PtrSize;
1205 for (int i = 0; i < NumOfParams; i++) {
1206 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001207 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001208 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001209 getObjCEncodingForTypeQualifier(
1210 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001211 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001212 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001213 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001214 }
1215}
1216
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001217void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1218 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001219{
Anders Carlssone8c49532007-10-29 06:33:42 +00001220 // FIXME: This currently doesn't encode:
1221 // @ An object (whether statically typed or typed id)
1222 // # A class object (Class)
1223 // : A method selector (SEL)
1224 // {name=type...} A structure
1225 // (name=type...) A union
1226 // bnum A bit field of num bits
1227
1228 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001229 char encoding;
1230 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001231 default: assert(0 && "Unhandled builtin type kind");
1232 case BuiltinType::Void: encoding = 'v'; break;
1233 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001234 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001235 case BuiltinType::UChar: encoding = 'C'; break;
1236 case BuiltinType::UShort: encoding = 'S'; break;
1237 case BuiltinType::UInt: encoding = 'I'; break;
1238 case BuiltinType::ULong: encoding = 'L'; break;
1239 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001240 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001241 case BuiltinType::SChar: encoding = 'c'; break;
1242 case BuiltinType::Short: encoding = 's'; break;
1243 case BuiltinType::Int: encoding = 'i'; break;
1244 case BuiltinType::Long: encoding = 'l'; break;
1245 case BuiltinType::LongLong: encoding = 'q'; break;
1246 case BuiltinType::Float: encoding = 'f'; break;
1247 case BuiltinType::Double: encoding = 'd'; break;
1248 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001249 }
1250
1251 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001252 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001254 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001255 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001256
1257 }
1258 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001259 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001261 S += '@';
1262 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001264 S += '#';
1265 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001266 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001267 S += ':';
1268 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001269 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001270
1271 if (PointeeTy->isCharType()) {
1272 // char pointer types should be encoded as '*' unless it is a
1273 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001274 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001275 S += '*';
1276 return;
1277 }
1278 }
1279
1280 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001281 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001282 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001283 S += '[';
1284
1285 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1286 S += llvm::utostr(CAT->getSize().getZExtValue());
1287 else
1288 assert(0 && "Unhandled array type!");
1289
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001290 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001291 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001292 } else if (T->getAsFunctionType()) {
1293 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001294 } else if (const RecordType *RTy = T->getAsRecordType()) {
1295 RecordDecl *RDecl= RTy->getDecl();
1296 S += '{';
1297 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001298 bool found = false;
1299 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1300 if (ERType[i] == RTy) {
1301 found = true;
1302 break;
1303 }
1304 if (!found) {
1305 ERType.push_back(RTy);
1306 S += '=';
1307 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1308 FieldDecl *field = RDecl->getMember(i);
1309 getObjCEncodingForType(field->getType(), S, ERType);
1310 }
1311 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1312 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001313 }
1314 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001315 } else if (T->isEnumeralType()) {
1316 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001317 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001318 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001319}
1320
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001322 std::string& S) const {
1323 if (QT & Decl::OBJC_TQ_In)
1324 S += 'n';
1325 if (QT & Decl::OBJC_TQ_Inout)
1326 S += 'N';
1327 if (QT & Decl::OBJC_TQ_Out)
1328 S += 'o';
1329 if (QT & Decl::OBJC_TQ_Bycopy)
1330 S += 'O';
1331 if (QT & Decl::OBJC_TQ_Byref)
1332 S += 'R';
1333 if (QT & Decl::OBJC_TQ_Oneway)
1334 S += 'V';
1335}
1336
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001337void ASTContext::setBuiltinVaListType(QualType T)
1338{
1339 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1340
1341 BuiltinVaListType = T;
1342}
1343
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001345{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001347
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001349
1350 // typedef struct objc_object *id;
1351 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1352 assert(ptr && "'id' incorrectly typed");
1353 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1354 assert(rec && "'id' incorrectly typed");
1355 IdStructType = rec;
1356}
1357
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001359{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001360 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001361
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001362 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001363
1364 // typedef struct objc_selector *SEL;
1365 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1366 assert(ptr && "'SEL' incorrectly typed");
1367 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1368 assert(rec && "'SEL' incorrectly typed");
1369 SelStructType = rec;
1370}
1371
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001372void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001373{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001374 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1375 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001376}
1377
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001378void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001379{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001380 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001381
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001382 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001383
1384 // typedef struct objc_class *Class;
1385 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1386 assert(ptr && "'Class' incorrectly typed");
1387 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1388 assert(rec && "'Class' incorrectly typed");
1389 ClassStructType = rec;
1390}
1391
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1393 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001394 "'NSConstantString' type already set!");
1395
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001396 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001397}
1398
Steve Naroffec0550f2007-10-15 20:41:53 +00001399bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1400 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1401 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1402
1403 return lBuiltin->getKind() == rBuiltin->getKind();
1404}
1405
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001406/// objcTypesAreCompatible - This routine is called when two types
1407/// are of different class; one is interface type or is
1408/// a qualified interface type and the other type is of a different class.
1409/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001410bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroffec0550f2007-10-15 20:41:53 +00001412 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001413 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001414 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 if (ObjCInterfaceType *lhsIT =
1416 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1417 ObjCQualifiedInterfaceType *rhsQI =
1418 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001419 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1420 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001421 else if (ObjCInterfaceType *rhsIT =
1422 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1423 ObjCQualifiedInterfaceType *lhsQI =
1424 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001425 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1426 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001427 return false;
1428}
1429
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001430/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1431/// must be canonical types.
Steve Naroffec0550f2007-10-15 20:41:53 +00001432bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001433 assert (lhs->isCanonical() &&
1434 "interfaceTypesAreCompatible strip typedefs of lhs");
1435 assert (rhs->isCanonical() &&
1436 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001437 if (lhs == rhs)
1438 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001439 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1440 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1441 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1442 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001443 // rhs is derived from lhs it is OK; else it is not OK.
1444 while (rhsIDecl != NULL) {
1445 if (rhsIDecl == lhsIDecl)
1446 return true;
1447 rhsIDecl = rhsIDecl->getSuperClass();
1448 }
1449 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001450}
1451
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001452bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1453 QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001454 ObjCQualifiedInterfaceType *lhsQI =
1455 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001456 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001457 ObjCQualifiedInterfaceType *rhsQI =
1458 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001459 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001460 if (!interfaceTypesAreCompatible(
1461 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1462 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001463 return false;
1464 /* All protocols in lhs must have a presense in rhs. */
1465 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1466 bool match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001467 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001468 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001470 if (lhsProto == rhsProto) {
1471 match = true;
1472 break;
1473 }
1474 }
1475 if (!match)
1476 return false;
1477 }
1478 return true;
1479}
1480
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001481/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1482/// inheritance hierarchy of 'rProto'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001483static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1484 ObjCProtocolDecl *rProto) {
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001485 if (lProto == rProto)
1486 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001488 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1489 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1490 return true;
1491 return false;
1492}
1493
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001494/// ClassImplementsProtocol - Checks that 'lProto' protocol
1495/// has been implemented in IDecl class, its super class or categories (if
1496/// lookupCategory is true).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1498 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001499 bool lookupCategory) {
1500
1501 // 1st, look up the class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001502 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001503 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1504 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1505 return true;
1506 }
1507
1508 // 2nd, look up the category.
1509 if (lookupCategory)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001511 CDecl = CDecl->getNextClassCategory()) {
1512 protoList = CDecl->getReferencedProtocols();
1513 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1514 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1515 return true;
1516 }
1517 }
1518
1519 // 3rd, look up the super class(s)
1520 if (IDecl->getSuperClass())
1521 return
1522 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1523
1524 return false;
1525}
1526
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001527/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001528/// one of which is a protocol qualified 'id' type. When 'compare'
1529/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001531 QualType rhs,
1532 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001533 // match id<P..> with an 'id' type in all cases.
1534 if (const PointerType *PT = lhs->getAsPointerType()) {
1535 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001537 return true;
1538
1539 }
1540 else if (const PointerType *PT = rhs->getAsPointerType()) {
1541 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001542 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001543 return true;
1544
1545 }
1546
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001547 ObjCQualifiedInterfaceType *lhsQI = 0;
1548 ObjCQualifiedInterfaceType *rhsQI = 0;
1549 ObjCInterfaceDecl *lhsID = 0;
1550 ObjCInterfaceDecl *rhsID = 0;
1551 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1552 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001553
1554 if (lhsQID) {
1555 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1556 QualType rtype =
1557 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1558 rhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001560 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001561 if (!rhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001562 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001563 rtype.getCanonicalType().getTypePtr());
1564 if (IT)
1565 rhsID = IT->getDecl();
1566 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001567 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001568 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001569 return false;
1570
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001571 unsigned numRhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001573 if (rhsQI) {
1574 numRhsProtocols = rhsQI->getNumProtocols();
1575 rhsProtoList = rhsQI->getReferencedProtocols();
1576 }
1577 else if (rhsQID) {
1578 numRhsProtocols = rhsQID->getNumProtocols();
1579 rhsProtoList = rhsQID->getReferencedProtocols();
1580 }
1581
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001582 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001583 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001584 bool match = false;
1585
1586 // when comparing an id<P> on lhs with a static type on rhs,
1587 // see if static class implements all of id's protocols, directly or
1588 // through its super class and categories.
1589 if (rhsID) {
1590 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1591 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001592 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001593 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001594 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001595 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1596 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001597 match = true;
1598 break;
1599 }
1600 }
1601 if (!match)
1602 return false;
1603 }
1604 }
1605 else if (rhsQID) {
1606 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1607 QualType ltype =
1608 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1609 lhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001611 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001612 if (!lhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001614 ltype.getCanonicalType().getTypePtr());
1615 if (IT)
1616 lhsID = IT->getDecl();
1617 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001618 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001619 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001620 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001621
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001622 unsigned numLhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001623 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001624 if (lhsQI) {
1625 numLhsProtocols = lhsQI->getNumProtocols();
1626 lhsProtoList = lhsQI->getReferencedProtocols();
1627 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001628 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001629 numLhsProtocols = lhsQID->getNumProtocols();
1630 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001631 }
1632 bool match = false;
1633 // for static type vs. qualified 'id' type, check that class implements
1634 // one of 'id's protocols.
1635 if (lhsID) {
1636 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001637 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001638 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1639 match = true;
1640 break;
1641 }
1642 }
1643 }
1644 else for (unsigned i =0; i < numLhsProtocols; i++) {
1645 match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001646 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001647 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001648 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001649 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1650 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001651 match = true;
1652 break;
1653 }
1654 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001655 }
1656 if (!match)
1657 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001658 }
1659 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001660}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001661
Chris Lattner770951b2007-11-01 05:03:41 +00001662bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1663 const VectorType *lVector = lhs->getAsVectorType();
1664 const VectorType *rVector = rhs->getAsVectorType();
1665
1666 if ((lVector->getElementType().getCanonicalType() ==
1667 rVector->getElementType().getCanonicalType()) &&
1668 (lVector->getNumElements() == rVector->getNumElements()))
1669 return true;
1670 return false;
1671}
1672
Steve Naroffec0550f2007-10-15 20:41:53 +00001673// C99 6.2.7p1: If both are complete types, then the following additional
1674// requirements apply...FIXME (handle compatibility across source files).
1675bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroffab373092007-11-07 06:03:51 +00001676 // "Class" and "id" are compatible built-in structure types.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001677 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1678 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroffab373092007-11-07 06:03:51 +00001679 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001680
1681 // Within a translation unit a tag type is
1682 // only compatible with itself.
1683 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001684}
1685
1686bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1687 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1688 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001689 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1690 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001691 return false;
1692
1693 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1694 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1695
1696 return typesAreCompatible(ltype, rtype);
1697}
1698
Bill Wendling43d69752007-12-03 07:33:35 +00001699// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001700// reference to T, the operation assigns to the object of type T denoted by the
1701// reference.
1702bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1703 QualType ltype = lhs;
1704
1705 if (lhs->isReferenceType())
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001706 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001707
1708 QualType rtype = rhs;
1709
1710 if (rhs->isReferenceType())
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001711 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001712
1713 return typesAreCompatible(ltype, rtype);
1714}
1715
1716bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1717 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1718 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1719 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1720 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1721
1722 // first check the return types (common between C99 and K&R).
1723 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1724 return false;
1725
1726 if (lproto && rproto) { // two C99 style function prototypes
1727 unsigned lproto_nargs = lproto->getNumArgs();
1728 unsigned rproto_nargs = rproto->getNumArgs();
1729
1730 if (lproto_nargs != rproto_nargs)
1731 return false;
1732
1733 // both prototypes have the same number of arguments.
1734 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1735 (rproto->isVariadic() && !lproto->isVariadic()))
1736 return false;
1737
1738 // The use of ellipsis agree...now check the argument types.
1739 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001740 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1741 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001742 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001743 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001744 return false;
1745 return true;
1746 }
1747 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1748 return true;
1749
1750 // we have a mixture of K&R style with C99 prototypes
1751 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1752
1753 if (proto->isVariadic())
1754 return false;
1755
1756 // FIXME: Each parameter type T in the prototype must be compatible with the
1757 // type resulting from applying the usual argument conversions to T.
1758 return true;
1759}
1760
1761bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman4e92acf2008-02-06 04:53:22 +00001762 // Compatible arrays must have compatible element types
1763 QualType ltype = lhs->getAsArrayType()->getElementType();
1764 QualType rtype = rhs->getAsArrayType()->getElementType();
1765
Steve Naroffec0550f2007-10-15 20:41:53 +00001766 if (!typesAreCompatible(ltype, rtype))
1767 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001768
1769 // Compatible arrays must be the same size
1770 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1771 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1772 return RCAT->getSize() == LCAT->getSize();
1773
Steve Naroffec0550f2007-10-15 20:41:53 +00001774 return true;
1775}
1776
1777/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1778/// both shall have the identically qualified version of a compatible type.
1779/// C99 6.2.7p1: Two types have compatible types if their types are the
1780/// same. See 6.7.[2,3,5] for additional rules.
1781bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1782 QualType lcanon = lhs.getCanonicalType();
1783 QualType rcanon = rhs.getCanonicalType();
Chris Lattner988ee6e2008-04-03 05:07:04 +00001784
Steve Naroffec0550f2007-10-15 20:41:53 +00001785 // If two types are identical, they are are compatible
1786 if (lcanon == rcanon)
1787 return true;
Chris Lattner988ee6e2008-04-03 05:07:04 +00001788
1789 if (lcanon.getCVRQualifiers() != rcanon.getCVRQualifiers() ||
1790 lcanon.getAddressSpace() != rcanon.getAddressSpace())
1791 return false;
Bill Wendling43d69752007-12-03 07:33:35 +00001792
1793 // C++ [expr]: If an expression initially has the type "reference to T", the
1794 // type is adjusted to "T" prior to any further analysis, the expression
1795 // designates the object or function denoted by the reference, and the
1796 // expression is an lvalue.
Chris Lattner1adb8832008-01-14 05:45:46 +00001797 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001798 lcanon = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001799 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001800 rcanon = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001801
1802 Type::TypeClass LHSClass = lcanon->getTypeClass();
1803 Type::TypeClass RHSClass = rcanon->getTypeClass();
1804
1805 // We want to consider the two function types to be the same for these
1806 // comparisons, just force one to the other.
1807 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1808 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001809
1810 // Same as above for arrays
1811 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1812 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedmanc5773c42008-02-15 18:16:39 +00001813 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1814 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001815
Steve Naroff4a746782008-01-09 22:43:08 +00001816 // If the canonical type classes don't match...
Chris Lattner1adb8832008-01-14 05:45:46 +00001817 if (LHSClass != RHSClass) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001818 // For Objective-C, it is possible for two types to be compatible
1819 // when their classes don't match (when dealing with "id"). If either type
1820 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001821 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001822 return objcTypesAreCompatible(lcanon, rcanon);
Steve Narofff69cc5d2008-01-30 19:17:43 +00001823
Chris Lattner1adb8832008-01-14 05:45:46 +00001824 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1825 // a signed integer type, or an unsigned integer type.
Eli Friedmanbab96962008-02-12 08:46:17 +00001826 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
Chris Lattner71763312008-04-06 22:05:18 +00001827 EnumDecl* EDecl = cast<EnumType>(lcanon)->getDecl();
Eli Friedmanbab96962008-02-12 08:46:17 +00001828 return EDecl->getIntegerType() == rcanon;
1829 }
1830 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
Chris Lattner71763312008-04-06 22:05:18 +00001831 EnumDecl* EDecl = cast<EnumType>(rcanon)->getDecl();
Eli Friedmanbab96962008-02-12 08:46:17 +00001832 return EDecl->getIntegerType() == lcanon;
1833 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001834
Steve Naroffec0550f2007-10-15 20:41:53 +00001835 return false;
1836 }
Steve Naroff4a746782008-01-09 22:43:08 +00001837 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001838 switch (LHSClass) {
1839 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1840 case Type::Pointer:
1841 return pointerTypesAreCompatible(lcanon, rcanon);
1842 case Type::ConstantArray:
1843 case Type::VariableArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +00001844 case Type::IncompleteArray:
Chris Lattner1adb8832008-01-14 05:45:46 +00001845 return arrayTypesAreCompatible(lcanon, rcanon);
1846 case Type::FunctionNoProto:
1847 return functionTypesAreCompatible(lcanon, rcanon);
1848 case Type::Tagged: // handle structures, unions
1849 return tagTypesAreCompatible(lcanon, rcanon);
1850 case Type::Builtin:
1851 return builtinTypesAreCompatible(lcanon, rcanon);
1852 case Type::ObjCInterface:
1853 return interfaceTypesAreCompatible(lcanon, rcanon);
1854 case Type::Vector:
1855 case Type::OCUVector:
1856 return vectorTypesAreCompatible(lcanon, rcanon);
1857 case Type::ObjCQualifiedInterface:
1858 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1859 default:
1860 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001861 }
1862 return true; // should never get here...
1863}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001864
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001865/// Emit - Serialize an ASTContext object to Bitcode.
1866void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001867 S.EmitRef(SourceMgr);
1868 S.EmitRef(Target);
1869 S.EmitRef(Idents);
1870 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001871
Ted Kremenekfee04522007-10-31 22:44:07 +00001872 // Emit the size of the type vector so that we can reserve that size
1873 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001874 S.EmitInt(Types.size());
1875
Ted Kremenek03ed4402007-11-13 22:02:55 +00001876 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1877 I!=E;++I)
1878 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001879
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001880 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001881}
1882
Ted Kremenek0f84c002007-11-13 00:25:37 +00001883ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001884 SourceManager &SM = D.ReadRef<SourceManager>();
1885 TargetInfo &t = D.ReadRef<TargetInfo>();
1886 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1887 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001888
Ted Kremenekfee04522007-10-31 22:44:07 +00001889 unsigned size_reserve = D.ReadInt();
1890
1891 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1892
Ted Kremenek03ed4402007-11-13 22:02:55 +00001893 for (unsigned i = 0; i < size_reserve; ++i)
1894 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001895
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001896 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001897
1898 return A;
1899}