blob: 6344ce570cf7b4ff586891ed788c4ad8991859ce [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000032 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000033 Types.pop_back();
34 }
Eli Friedman65489b72008-05-27 03:08:09 +000035
36 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000037}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
43 unsigned NumVector = 0, NumComplex = 0;
44 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000047 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
48 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000049 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
59 else if (isa<ComplexType>(T))
60 ++NumComplex;
61 else if (isa<ArrayType>(T))
62 ++NumArray;
63 else if (isa<VectorType>(T))
64 ++NumVector;
65 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
73 switch (TT->getDecl()->getKind()) {
74 default: assert(0 && "Unknown tagged type!");
75 case Decl::Struct: ++NumTagStruct; break;
76 case Decl::Union: ++NumTagUnion; break;
77 case Decl::Class: ++NumTagClass; break;
78 case Decl::Enum: ++NumTagEnum; break;
79 }
Ted Kremenek42730c52008-01-07 19:49:32 +000080 } else if (isa<ObjCInterfaceType>(T))
81 ++NumObjCInterfaces;
82 else if (isa<ObjCQualifiedInterfaceType>(T))
83 ++NumObjCQualifiedInterfaces;
84 else if (isa<ObjCQualifiedIdType>(T))
85 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000086 else if (isa<TypeOfType>(T))
87 ++NumTypeOfTypes;
88 else if (isa<TypeOfExpr>(T))
89 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000090 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000091 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000092 assert(0 && "Unknown type!");
93 }
94 }
95
96 fprintf(stderr, " %d builtin types\n", NumBuiltin);
97 fprintf(stderr, " %d pointer types\n", NumPointer);
98 fprintf(stderr, " %d reference types\n", NumReference);
99 fprintf(stderr, " %d complex types\n", NumComplex);
100 fprintf(stderr, " %d array types\n", NumArray);
101 fprintf(stderr, " %d vector types\n", NumVector);
102 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
103 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
104 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
105 fprintf(stderr, " %d tagged types\n", NumTagged);
106 fprintf(stderr, " %d struct types\n", NumTagStruct);
107 fprintf(stderr, " %d union types\n", NumTagUnion);
108 fprintf(stderr, " %d class types\n", NumTagClass);
109 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000111 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000113 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000114 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000115 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
116 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
117
Chris Lattner4b009652007-07-25 00:24:17 +0000118 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
119 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
120 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
121 NumFunctionP*sizeof(FunctionTypeProto)+
122 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000123 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
124 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
129 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
130}
131
Chris Lattner4b009652007-07-25 00:24:17 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000141 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
163
164 // C99 6.2.5p11.
165 FloatComplexTy = getComplexType(FloatTy);
166 DoubleComplexTy = getComplexType(DoubleTy);
167 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000168
169 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000171 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000172 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000173 ClassStructType = 0;
174
Ted Kremenek42730c52008-01-07 19:49:32 +0000175 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000176
177 // void * type
178 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000179}
180
181//===----------------------------------------------------------------------===//
182// Type Sizing and Analysis
183//===----------------------------------------------------------------------===//
184
185/// getTypeSize - Return the size of the specified type, in bits. This method
186/// does not work on incomplete types.
187std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000188ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000189 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000190 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000191 unsigned Align;
192 switch (T->getTypeClass()) {
193 case Type::TypeName: assert(0 && "Not a canonical type!");
194 case Type::FunctionNoProto:
195 case Type::FunctionProto:
196 default:
197 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000198 case Type::VariableArray:
199 assert(0 && "VLAs not implemented yet!");
200 case Type::ConstantArray: {
201 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
202
Chris Lattner8cd0e932008-03-05 18:54:05 +0000203 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000204 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000205 Align = EltInfo.second;
206 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000207 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000208 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000209 case Type::Vector: {
210 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000211 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000212 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner4b009652007-07-25 00:24:17 +0000213 // FIXME: Vector alignment is not the alignment of its elements.
214 Align = EltInfo.second;
215 break;
216 }
217
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000218 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000219 switch (cast<BuiltinType>(T)->getKind()) {
220 default: assert(0 && "Unknown builtin type!");
221 case BuiltinType::Void:
222 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000223 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000224 Width = Target.getBoolWidth();
225 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000226 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000227 case BuiltinType::Char_S:
228 case BuiltinType::Char_U:
229 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000231 Width = Target.getCharWidth();
232 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 Width = Target.getShortWidth();
237 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000241 Width = Target.getIntWidth();
242 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000246 Width = Target.getLongWidth();
247 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000249 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000250 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000251 Width = Target.getLongLongWidth();
252 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000253 break;
254 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000255 Width = Target.getFloatWidth();
256 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 break;
258 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000259 Width = Target.getDoubleWidth();
260 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 break;
262 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 Width = Target.getLongDoubleWidth();
264 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 }
267 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000268 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000269 // FIXME: Pointers into different addr spaces could have different sizes and
270 // alignment requirements: getPointerInfo should take an AddrSpace.
271 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000273 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000274 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000276 case Type::Pointer: {
277 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000278 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000279 Align = Target.getPointerAlign(AS);
280 break;
281 }
Chris Lattner4b009652007-07-25 00:24:17 +0000282 case Type::Reference:
283 // "When applied to a reference or a reference type, the result is the size
284 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000285 // FIXME: This is wrong for struct layout: a reference in a struct has
286 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000287 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000288
289 case Type::Complex: {
290 // Complex types have the same alignment as their elements, but twice the
291 // size.
292 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000293 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000294 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 Align = EltInfo.second;
296 break;
297 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000298 case Type::Tagged: {
299 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
300 return getTypeInfo(ET->getDecl()->getIntegerType());
301
302 RecordType *RT = cast<RecordType>(T);
303 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
304 Width = Layout.getSize();
305 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000306 break;
307 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000308 }
Chris Lattner4b009652007-07-25 00:24:17 +0000309
310 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000311 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000312}
313
Devang Patel7a78e432007-11-01 19:11:01 +0000314/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000315/// specified record (struct/union/class), which indicates its size and field
316/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000317const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
319
320 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000321 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000322 if (Entry) return *Entry;
323
Devang Patel7a78e432007-11-01 19:11:01 +0000324 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
325 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
326 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000327 Entry = NewEntry;
328
329 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
330 uint64_t RecordSize = 0;
331 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
332
333 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000334 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
335 RecordAlign = std::max(RecordAlign, AA->getAlignment());
336
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000337 bool StructIsPacked = D->getAttr<PackedAttr>();
338
Chris Lattner4b009652007-07-25 00:24:17 +0000339 // Layout each field, for now, just sequentially, respecting alignment. In
340 // the future, this will need to be tweakable by targets.
341 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
342 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000343 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000344 uint64_t FieldSize;
345 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000346
347 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
348 llvm::APSInt I(32);
349 bool BitWidthIsICE =
350 BitWidthExpr->isIntegerConstantExpr(I, *this);
351 assert (BitWidthIsICE && "Invalid BitField size expression");
352 FieldSize = I.getZExtValue();
353
Chris Lattner8cd0e932008-03-05 18:54:05 +0000354 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000355 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000356
357 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
358 FieldAlign = AA->getAlignment();
359 else if (FieldIsPacked)
360 FieldAlign = 8;
361 else {
Eli Friedmanf8382542008-05-20 15:17:39 +0000362 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000363 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000364
Anders Carlsson058237f2008-02-18 07:13:09 +0000365 // Check if we need to add padding to give the field the correct
366 // alignment.
367 if (RecordSize % FieldAlign + FieldSize > TypeSize)
368 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
369
370 } else {
371 if (FD->getType()->isIncompleteType()) {
372 // This must be a flexible array member; we can't directly
373 // query getTypeInfo about these, so we figure it out here.
374 // Flexible array members don't have any size, but they
375 // have to be aligned appropriately for their element type.
376
377 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
378 FieldAlign = AA->getAlignment();
379 else if (FieldIsPacked)
380 FieldAlign = 8;
381 else {
382 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000383 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000384 }
385 FieldSize = 0;
386 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000387 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000388 FieldSize = FieldInfo.first;
389
390 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
391 FieldAlign = AA->getAlignment();
392 else if (FieldIsPacked)
393 FieldAlign = 8;
394 else
395 FieldAlign = FieldInfo.second;
396 }
397
398 // Round up the current record size to the field's alignment boundary.
399 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
400 }
Chris Lattner4b009652007-07-25 00:24:17 +0000401
402 // Place this field at the current location.
403 FieldOffsets[i] = RecordSize;
404
405 // Reserve space for this field.
406 RecordSize += FieldSize;
407
408 // Remember max struct/class alignment.
409 RecordAlign = std::max(RecordAlign, FieldAlign);
410 }
411
412 // Finally, round the size of the total struct up to the alignment of the
413 // struct itself.
414 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
415 } else {
416 // Union layout just puts each member at the start of the record.
417 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
418 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000419 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000420 uint64_t FieldSize = FieldInfo.first;
421 unsigned FieldAlign = FieldInfo.second;
Anders Carlsson058237f2008-02-18 07:13:09 +0000422
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Round up the current record size to the field's alignment boundary.
424 RecordSize = std::max(RecordSize, FieldSize);
Eli Friedmanf8382542008-05-20 15:17:39 +0000425
Chris Lattner4b009652007-07-25 00:24:17 +0000426 // Place this field at the start of the record.
427 FieldOffsets[i] = 0;
Eli Friedmanf8382542008-05-20 15:17:39 +0000428
Chris Lattner4b009652007-07-25 00:24:17 +0000429 // Remember max struct/class alignment.
430 RecordAlign = std::max(RecordAlign, FieldAlign);
431 }
432 }
433
434 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
435 return *NewEntry;
436}
437
Chris Lattner4b009652007-07-25 00:24:17 +0000438//===----------------------------------------------------------------------===//
439// Type creation/memoization methods
440//===----------------------------------------------------------------------===//
441
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000442QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000443 QualType CanT = getCanonicalType(T);
444 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000445 return T;
446
447 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
448 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000449 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000450 "Type is already address space qualified");
451
452 // Check if we've already instantiated an address space qual'd type of this
453 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000454 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000455 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000456 void *InsertPos = 0;
457 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
458 return QualType(ASQy, 0);
459
460 // If the base type isn't canonical, this won't be a canonical type either,
461 // so fill in the canonical type field.
462 QualType Canonical;
463 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000464 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000465
466 // Get the new insert position for the node we care about.
467 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
468 assert(NewIP == 0 && "Shouldn't be in the map!");
469 }
Chris Lattner35fef522008-02-20 20:55:12 +0000470 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000471 ASQualTypes.InsertNode(New, InsertPos);
472 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000473 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000474}
475
Chris Lattner4b009652007-07-25 00:24:17 +0000476
477/// getComplexType - Return the uniqued reference to the type for a complex
478/// number with the specified element type.
479QualType ASTContext::getComplexType(QualType T) {
480 // Unique pointers, to guarantee there is only one pointer of a particular
481 // structure.
482 llvm::FoldingSetNodeID ID;
483 ComplexType::Profile(ID, T);
484
485 void *InsertPos = 0;
486 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
487 return QualType(CT, 0);
488
489 // If the pointee type isn't canonical, this won't be a canonical type either,
490 // so fill in the canonical type field.
491 QualType Canonical;
492 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000493 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000494
495 // Get the new insert position for the node we care about.
496 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
497 assert(NewIP == 0 && "Shouldn't be in the map!");
498 }
499 ComplexType *New = new ComplexType(T, Canonical);
500 Types.push_back(New);
501 ComplexTypes.InsertNode(New, InsertPos);
502 return QualType(New, 0);
503}
504
505
506/// getPointerType - Return the uniqued reference to the type for a pointer to
507/// the specified type.
508QualType ASTContext::getPointerType(QualType T) {
509 // Unique pointers, to guarantee there is only one pointer of a particular
510 // structure.
511 llvm::FoldingSetNodeID ID;
512 PointerType::Profile(ID, T);
513
514 void *InsertPos = 0;
515 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
516 return QualType(PT, 0);
517
518 // If the pointee type isn't canonical, this won't be a canonical type either,
519 // so fill in the canonical type field.
520 QualType Canonical;
521 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000522 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000523
524 // Get the new insert position for the node we care about.
525 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
526 assert(NewIP == 0 && "Shouldn't be in the map!");
527 }
528 PointerType *New = new PointerType(T, Canonical);
529 Types.push_back(New);
530 PointerTypes.InsertNode(New, InsertPos);
531 return QualType(New, 0);
532}
533
534/// getReferenceType - Return the uniqued reference to the type for a reference
535/// to the specified type.
536QualType ASTContext::getReferenceType(QualType T) {
537 // Unique pointers, to guarantee there is only one pointer of a particular
538 // structure.
539 llvm::FoldingSetNodeID ID;
540 ReferenceType::Profile(ID, T);
541
542 void *InsertPos = 0;
543 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
544 return QualType(RT, 0);
545
546 // If the referencee type isn't canonical, this won't be a canonical type
547 // either, so fill in the canonical type field.
548 QualType Canonical;
549 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000550 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000551
552 // Get the new insert position for the node we care about.
553 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
554 assert(NewIP == 0 && "Shouldn't be in the map!");
555 }
556
557 ReferenceType *New = new ReferenceType(T, Canonical);
558 Types.push_back(New);
559 ReferenceTypes.InsertNode(New, InsertPos);
560 return QualType(New, 0);
561}
562
Steve Naroff83c13012007-08-30 01:06:46 +0000563/// getConstantArrayType - Return the unique reference to the type for an
564/// array of the specified element type.
565QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000566 const llvm::APInt &ArySize,
567 ArrayType::ArraySizeModifier ASM,
568 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000569 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000570 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000571
572 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000573 if (ConstantArrayType *ATP =
574 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000575 return QualType(ATP, 0);
576
577 // If the element type isn't canonical, this won't be a canonical type either,
578 // so fill in the canonical type field.
579 QualType Canonical;
580 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000581 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000582 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000583 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000584 ConstantArrayType *NewIP =
585 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
586
Chris Lattner4b009652007-07-25 00:24:17 +0000587 assert(NewIP == 0 && "Shouldn't be in the map!");
588 }
589
Steve Naroff24c9b982007-08-30 18:10:14 +0000590 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
591 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000592 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000593 Types.push_back(New);
594 return QualType(New, 0);
595}
596
Steve Naroffe2579e32007-08-30 18:14:25 +0000597/// getVariableArrayType - Returns a non-unique reference to the type for a
598/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000599QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
600 ArrayType::ArraySizeModifier ASM,
601 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000602 // Since we don't unique expressions, it isn't possible to unique VLA's
603 // that have an expression provided for their size.
604
605 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
606 ASM, EltTypeQuals);
607
608 VariableArrayTypes.push_back(New);
609 Types.push_back(New);
610 return QualType(New, 0);
611}
612
613QualType ASTContext::getIncompleteArrayType(QualType EltTy,
614 ArrayType::ArraySizeModifier ASM,
615 unsigned EltTypeQuals) {
616 llvm::FoldingSetNodeID ID;
617 IncompleteArrayType::Profile(ID, EltTy);
618
619 void *InsertPos = 0;
620 if (IncompleteArrayType *ATP =
621 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
622 return QualType(ATP, 0);
623
624 // If the element type isn't canonical, this won't be a canonical type
625 // either, so fill in the canonical type field.
626 QualType Canonical;
627
628 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000629 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000630 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000631
632 // Get the new insert position for the node we care about.
633 IncompleteArrayType *NewIP =
634 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
635
636 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000637 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000638
639 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
640 ASM, EltTypeQuals);
641
642 IncompleteArrayTypes.InsertNode(New, InsertPos);
643 Types.push_back(New);
644 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000645}
646
Chris Lattner4b009652007-07-25 00:24:17 +0000647/// getVectorType - Return the unique reference to a vector type of
648/// the specified element type and size. VectorType must be a built-in type.
649QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
650 BuiltinType *baseType;
651
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000652 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000653 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
654
655 // Check if we've already instantiated a vector of this type.
656 llvm::FoldingSetNodeID ID;
657 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
658 void *InsertPos = 0;
659 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
660 return QualType(VTP, 0);
661
662 // If the element type isn't canonical, this won't be a canonical type either,
663 // so fill in the canonical type field.
664 QualType Canonical;
665 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000666 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000667
668 // Get the new insert position for the node we care about.
669 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
670 assert(NewIP == 0 && "Shouldn't be in the map!");
671 }
672 VectorType *New = new VectorType(vecType, NumElts, Canonical);
673 VectorTypes.InsertNode(New, InsertPos);
674 Types.push_back(New);
675 return QualType(New, 0);
676}
677
Nate Begemanaf6ed502008-04-18 23:10:10 +0000678/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000679/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000680QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000681 BuiltinType *baseType;
682
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000683 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000684 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000685
686 // Check if we've already instantiated a vector of this type.
687 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000688 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000689 void *InsertPos = 0;
690 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
691 return QualType(VTP, 0);
692
693 // If the element type isn't canonical, this won't be a canonical type either,
694 // so fill in the canonical type field.
695 QualType Canonical;
696 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000697 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000698
699 // Get the new insert position for the node we care about.
700 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
701 assert(NewIP == 0 && "Shouldn't be in the map!");
702 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000703 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000704 VectorTypes.InsertNode(New, InsertPos);
705 Types.push_back(New);
706 return QualType(New, 0);
707}
708
709/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
710///
711QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
712 // Unique functions, to guarantee there is only one function of a particular
713 // structure.
714 llvm::FoldingSetNodeID ID;
715 FunctionTypeNoProto::Profile(ID, ResultTy);
716
717 void *InsertPos = 0;
718 if (FunctionTypeNoProto *FT =
719 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
720 return QualType(FT, 0);
721
722 QualType Canonical;
723 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000724 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000725
726 // Get the new insert position for the node we care about.
727 FunctionTypeNoProto *NewIP =
728 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
729 assert(NewIP == 0 && "Shouldn't be in the map!");
730 }
731
732 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
733 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000734 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000735 return QualType(New, 0);
736}
737
738/// getFunctionType - Return a normal function type with a typed argument
739/// list. isVariadic indicates whether the argument list includes '...'.
740QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
741 unsigned NumArgs, bool isVariadic) {
742 // Unique functions, to guarantee there is only one function of a particular
743 // structure.
744 llvm::FoldingSetNodeID ID;
745 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
746
747 void *InsertPos = 0;
748 if (FunctionTypeProto *FTP =
749 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
750 return QualType(FTP, 0);
751
752 // Determine whether the type being created is already canonical or not.
753 bool isCanonical = ResultTy->isCanonical();
754 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
755 if (!ArgArray[i]->isCanonical())
756 isCanonical = false;
757
758 // If this type isn't canonical, get the canonical version of it.
759 QualType Canonical;
760 if (!isCanonical) {
761 llvm::SmallVector<QualType, 16> CanonicalArgs;
762 CanonicalArgs.reserve(NumArgs);
763 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000764 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000765
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000766 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000767 &CanonicalArgs[0], NumArgs,
768 isVariadic);
769
770 // Get the new insert position for the node we care about.
771 FunctionTypeProto *NewIP =
772 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
773 assert(NewIP == 0 && "Shouldn't be in the map!");
774 }
775
776 // FunctionTypeProto objects are not allocated with new because they have a
777 // variable size array (for parameter types) at the end of them.
778 FunctionTypeProto *FTP =
779 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
780 NumArgs*sizeof(QualType));
781 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
782 Canonical);
783 Types.push_back(FTP);
784 FunctionTypeProtos.InsertNode(FTP, InsertPos);
785 return QualType(FTP, 0);
786}
787
Douglas Gregor1d661552008-04-13 21:07:44 +0000788/// getTypeDeclType - Return the unique reference to the type for the
789/// specified type declaration.
790QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
791 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
792
793 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
794 return getTypedefType(Typedef);
795 else if (ObjCInterfaceDecl *ObjCInterface
796 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
797 return getObjCInterfaceType(ObjCInterface);
798 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
799 Decl->TypeForDecl = new RecordType(Record);
800 Types.push_back(Decl->TypeForDecl);
801 return QualType(Decl->TypeForDecl, 0);
802 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
803 Decl->TypeForDecl = new EnumType(Enum);
804 Types.push_back(Decl->TypeForDecl);
805 return QualType(Decl->TypeForDecl, 0);
806 } else
807 assert(false && "TypeDecl without a type?");
808}
809
Chris Lattner4b009652007-07-25 00:24:17 +0000810/// getTypedefType - Return the unique reference to the type for the
811/// specified typename decl.
812QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
813 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
814
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000815 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000816 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000817 Types.push_back(Decl->TypeForDecl);
818 return QualType(Decl->TypeForDecl, 0);
819}
820
Ted Kremenek42730c52008-01-07 19:49:32 +0000821/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000822/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000823QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000824 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
825
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000827 Types.push_back(Decl->TypeForDecl);
828 return QualType(Decl->TypeForDecl, 0);
829}
830
Chris Lattnere1352302008-04-07 04:56:42 +0000831/// CmpProtocolNames - Comparison predicate for sorting protocols
832/// alphabetically.
833static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
834 const ObjCProtocolDecl *RHS) {
835 return strcmp(LHS->getName(), RHS->getName()) < 0;
836}
837
838static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
839 unsigned &NumProtocols) {
840 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
841
842 // Sort protocols, keyed by name.
843 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
844
845 // Remove duplicates.
846 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
847 NumProtocols = ProtocolsEnd-Protocols;
848}
849
850
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000851/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
852/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000853QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
854 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000855 // Sort the protocol list alphabetically to canonicalize it.
856 SortAndUniqueProtocols(Protocols, NumProtocols);
857
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000858 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000859 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000860
861 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000862 if (ObjCQualifiedInterfaceType *QT =
863 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000864 return QualType(QT, 0);
865
866 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000867 ObjCQualifiedInterfaceType *QType =
868 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000869 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000870 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000871 return QualType(QType, 0);
872}
873
Chris Lattnere1352302008-04-07 04:56:42 +0000874/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
875/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000876QualType ASTContext::getObjCQualifiedIdType(QualType idType,
877 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000878 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000879 // Sort the protocol list alphabetically to canonicalize it.
880 SortAndUniqueProtocols(Protocols, NumProtocols);
881
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000882 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000883 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000884
885 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 if (ObjCQualifiedIdType *QT =
887 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000888 return QualType(QT, 0);
889
890 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000891 QualType Canonical;
892 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000893 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000894 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 ObjCQualifiedIdType *NewQT =
896 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000897 assert(NewQT == 0 && "Shouldn't be in the map!");
898 }
899
Ted Kremenek42730c52008-01-07 19:49:32 +0000900 ObjCQualifiedIdType *QType =
901 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000902 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000904 return QualType(QType, 0);
905}
906
Steve Naroff0604dd92007-08-01 18:02:17 +0000907/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
908/// TypeOfExpr AST's (since expression's are never shared). For example,
909/// multiple declarations that refer to "typeof(x)" all contain different
910/// DeclRefExpr's. This doesn't effect the type checker, since it operates
911/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000912QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000913 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000914 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
915 Types.push_back(toe);
916 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000917}
918
Steve Naroff0604dd92007-08-01 18:02:17 +0000919/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
920/// TypeOfType AST's. The only motivation to unique these nodes would be
921/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
922/// an issue. This doesn't effect the type checker, since it operates
923/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000924QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000925 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000926 TypeOfType *tot = new TypeOfType(tofType, Canonical);
927 Types.push_back(tot);
928 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000929}
930
Chris Lattner4b009652007-07-25 00:24:17 +0000931/// getTagDeclType - Return the unique reference to the type for the
932/// specified TagDecl (struct/union/class/enum) decl.
933QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000934 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000935 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000936}
937
938/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
939/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
940/// needs to agree with the definition in <stddef.h>.
941QualType ASTContext::getSizeType() const {
942 // On Darwin, size_t is defined as a "long unsigned int".
943 // FIXME: should derive from "Target".
944 return UnsignedLongTy;
945}
946
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000947/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
948/// width of characters in wide strings, The value is target dependent and
949/// needs to agree with the definition in <stddef.h>.
950QualType ASTContext::getWcharType() const {
951 // On Darwin, wchar_t is defined as a "int".
952 // FIXME: should derive from "Target".
953 return IntTy;
954}
955
Chris Lattner4b009652007-07-25 00:24:17 +0000956/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
957/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
958QualType ASTContext::getPointerDiffType() const {
959 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
960 // FIXME: should derive from "Target".
961 return IntTy;
962}
963
Chris Lattner19eb97e2008-04-02 05:18:44 +0000964//===----------------------------------------------------------------------===//
965// Type Operators
966//===----------------------------------------------------------------------===//
967
Chris Lattner3dae6f42008-04-06 22:41:35 +0000968/// getCanonicalType - Return the canonical (structural) type corresponding to
969/// the specified potentially non-canonical type. The non-canonical version
970/// of a type may have many "decorated" versions of types. Decorators can
971/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
972/// to be free of any of these, allowing two canonical types to be compared
973/// for exact equality with a simple pointer comparison.
974QualType ASTContext::getCanonicalType(QualType T) {
975 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
976 return QualType(CanType.getTypePtr(),
977 T.getCVRQualifiers() | CanType.getCVRQualifiers());
978}
979
980
Chris Lattner19eb97e2008-04-02 05:18:44 +0000981/// getArrayDecayedType - Return the properly qualified result of decaying the
982/// specified array type to a pointer. This operation is non-trivial when
983/// handling typedefs etc. The canonical type of "T" must be an array type,
984/// this returns a pointer to a properly qualified element of the array.
985///
986/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
987QualType ASTContext::getArrayDecayedType(QualType Ty) {
988 // Handle the common case where typedefs are not involved directly.
989 QualType EltTy;
990 unsigned ArrayQuals = 0;
991 unsigned PointerQuals = 0;
992 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
993 // Since T "isa" an array type, it could not have had an address space
994 // qualifier, just CVR qualifiers. The properly qualified element pointer
995 // gets the union of the CVR qualifiers from the element and the array, and
996 // keeps any address space qualifier on the element type if present.
997 EltTy = AT->getElementType();
998 ArrayQuals = Ty.getCVRQualifiers();
999 PointerQuals = AT->getIndexTypeQualifier();
1000 } else {
1001 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1002 // lose qualifiers when dealing with typedefs. Example:
1003 // typedef int arr[10];
1004 // void test2() {
1005 // const arr b;
1006 // b[4] = 1;
1007 // }
1008 //
1009 // The decayed type of b is "const int*" even though the element type of the
1010 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001011 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001012 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1013 assert(PrettyArrayType && "Not an array type!");
1014
1015 // Get the element type with 'getAsArrayType' so that we don't lose any
1016 // typedefs in the element type of the array.
1017 EltTy = PrettyArrayType->getElementType();
1018
1019 // If the array was address-space qualifier, make sure to ASQual the element
1020 // type. We can just grab the address space from the canonical type.
1021 if (unsigned AS = CanTy.getAddressSpace())
1022 EltTy = getASQualType(EltTy, AS);
1023
1024 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1025 // the CVR qualifiers directly from the canonical type, which is guaranteed
1026 // to have the full set unioned together.
1027 ArrayQuals = CanTy.getCVRQualifiers();
1028 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1029 }
1030
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001031 // Apply any CVR qualifiers from the array type to the element type. This
1032 // implements C99 6.7.3p8: "If the specification of an array type includes
1033 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001034 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1035
1036 QualType PtrTy = getPointerType(EltTy);
1037
1038 // int x[restrict 4] -> int *restrict
1039 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1040
1041 return PtrTy;
1042}
1043
Chris Lattner4b009652007-07-25 00:24:17 +00001044/// getFloatingRank - Return a relative rank for floating point types.
1045/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001046static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001047 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001048 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001049
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001050 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001051 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001052 case BuiltinType::Float: return FloatRank;
1053 case BuiltinType::Double: return DoubleRank;
1054 case BuiltinType::LongDouble: return LongDoubleRank;
1055 }
1056}
1057
Steve Narofffa0c4532007-08-27 01:41:48 +00001058/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1059/// point or a complex type (based on typeDomain/typeSize).
1060/// 'typeDomain' is a real floating point or complex type.
1061/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001062QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1063 QualType Domain) const {
1064 FloatingRank EltRank = getFloatingRank(Size);
1065 if (Domain->isComplexType()) {
1066 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001067 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001068 case FloatRank: return FloatComplexTy;
1069 case DoubleRank: return DoubleComplexTy;
1070 case LongDoubleRank: return LongDoubleComplexTy;
1071 }
Chris Lattner4b009652007-07-25 00:24:17 +00001072 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001073
1074 assert(Domain->isRealFloatingType() && "Unknown domain!");
1075 switch (EltRank) {
1076 default: assert(0 && "getFloatingRank(): illegal value for rank");
1077 case FloatRank: return FloatTy;
1078 case DoubleRank: return DoubleTy;
1079 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001080 }
Chris Lattner4b009652007-07-25 00:24:17 +00001081}
1082
Chris Lattner51285d82008-04-06 23:55:33 +00001083/// getFloatingTypeOrder - Compare the rank of the two specified floating
1084/// point types, ignoring the domain of the type (i.e. 'double' ==
1085/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1086/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001087int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1088 FloatingRank LHSR = getFloatingRank(LHS);
1089 FloatingRank RHSR = getFloatingRank(RHS);
1090
1091 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001092 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001093 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001094 return 1;
1095 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001096}
1097
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001098/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1099/// routine will assert if passed a built-in type that isn't an integer or enum,
1100/// or if it is not canonicalized.
1101static unsigned getIntegerRank(Type *T) {
1102 assert(T->isCanonical() && "T should be canonicalized");
1103 if (isa<EnumType>(T))
1104 return 4;
1105
1106 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001107 default: assert(0 && "getIntegerRank(): not a built-in integer");
1108 case BuiltinType::Bool:
1109 return 1;
1110 case BuiltinType::Char_S:
1111 case BuiltinType::Char_U:
1112 case BuiltinType::SChar:
1113 case BuiltinType::UChar:
1114 return 2;
1115 case BuiltinType::Short:
1116 case BuiltinType::UShort:
1117 return 3;
1118 case BuiltinType::Int:
1119 case BuiltinType::UInt:
1120 return 4;
1121 case BuiltinType::Long:
1122 case BuiltinType::ULong:
1123 return 5;
1124 case BuiltinType::LongLong:
1125 case BuiltinType::ULongLong:
1126 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001127 }
1128}
1129
Chris Lattner51285d82008-04-06 23:55:33 +00001130/// getIntegerTypeOrder - Returns the highest ranked integer type:
1131/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1132/// LHS < RHS, return -1.
1133int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001134 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1135 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001136 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001137
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001138 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1139 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001140
Chris Lattner51285d82008-04-06 23:55:33 +00001141 unsigned LHSRank = getIntegerRank(LHSC);
1142 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001143
Chris Lattner51285d82008-04-06 23:55:33 +00001144 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1145 if (LHSRank == RHSRank) return 0;
1146 return LHSRank > RHSRank ? 1 : -1;
1147 }
Chris Lattner4b009652007-07-25 00:24:17 +00001148
Chris Lattner51285d82008-04-06 23:55:33 +00001149 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1150 if (LHSUnsigned) {
1151 // If the unsigned [LHS] type is larger, return it.
1152 if (LHSRank >= RHSRank)
1153 return 1;
1154
1155 // If the signed type can represent all values of the unsigned type, it
1156 // wins. Because we are dealing with 2's complement and types that are
1157 // powers of two larger than each other, this is always safe.
1158 return -1;
1159 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001160
Chris Lattner51285d82008-04-06 23:55:33 +00001161 // If the unsigned [RHS] type is larger, return it.
1162 if (RHSRank >= LHSRank)
1163 return -1;
1164
1165 // If the signed type can represent all values of the unsigned type, it
1166 // wins. Because we are dealing with 2's complement and types that are
1167 // powers of two larger than each other, this is always safe.
1168 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001169}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001170
1171// getCFConstantStringType - Return the type used for constant CFStrings.
1172QualType ASTContext::getCFConstantStringType() {
1173 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001174 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001175 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001176 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001177 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001178
1179 // const int *isa;
1180 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001181 // int flags;
1182 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001183 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001184 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001185 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001186 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001187 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001188 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001189
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001190 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001191 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001192 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001193
1194 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1195 }
1196
1197 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001198}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001199
Anders Carlssone3f02572007-10-29 06:33:42 +00001200// This returns true if a type has been typedefed to BOOL:
1201// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001202static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001203 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001204 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001205
1206 return false;
1207}
1208
Ted Kremenek42730c52008-01-07 19:49:32 +00001209/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001210/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001211int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001212 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001213
1214 // Make all integer and enum types at least as large as an int
1215 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001216 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001217 // Treat arrays as pointers, since that's how they're passed in.
1218 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001219 sz = getTypeSize(VoidPtrTy);
1220 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001221}
1222
Ted Kremenek42730c52008-01-07 19:49:32 +00001223/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001224/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001225void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001226 std::string& S)
1227{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001228 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001229 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001230 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001231 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001232 // Compute size of all parameters.
1233 // Start with computing size of a pointer in number of bytes.
1234 // FIXME: There might(should) be a better way of doing this computation!
1235 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001236 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001237 // The first two arguments (self and _cmd) are pointers; account for
1238 // their size.
1239 int ParmOffset = 2 * PtrSize;
1240 int NumOfParams = Decl->getNumParams();
1241 for (int i = 0; i < NumOfParams; i++) {
1242 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001243 int sz = getObjCEncodingTypeSize (PType);
1244 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001245 ParmOffset += sz;
1246 }
1247 S += llvm::utostr(ParmOffset);
1248 S += "@0:";
1249 S += llvm::utostr(PtrSize);
1250
1251 // Argument types.
1252 ParmOffset = 2 * PtrSize;
1253 for (int i = 0; i < NumOfParams; i++) {
1254 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001255 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001256 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001257 getObjCEncodingForTypeQualifier(
1258 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001259 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001260 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001261 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001262 }
1263}
1264
Fariborz Jahanian248db262008-01-22 22:44:46 +00001265void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1266 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001267{
Anders Carlssone3f02572007-10-29 06:33:42 +00001268 // FIXME: This currently doesn't encode:
1269 // @ An object (whether statically typed or typed id)
1270 // # A class object (Class)
1271 // : A method selector (SEL)
1272 // {name=type...} A structure
1273 // (name=type...) A union
1274 // bnum A bit field of num bits
1275
1276 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001277 char encoding;
1278 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001279 default: assert(0 && "Unhandled builtin type kind");
1280 case BuiltinType::Void: encoding = 'v'; break;
1281 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001282 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001283 case BuiltinType::UChar: encoding = 'C'; break;
1284 case BuiltinType::UShort: encoding = 'S'; break;
1285 case BuiltinType::UInt: encoding = 'I'; break;
1286 case BuiltinType::ULong: encoding = 'L'; break;
1287 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001288 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001289 case BuiltinType::SChar: encoding = 'c'; break;
1290 case BuiltinType::Short: encoding = 's'; break;
1291 case BuiltinType::Int: encoding = 'i'; break;
1292 case BuiltinType::Long: encoding = 'l'; break;
1293 case BuiltinType::LongLong: encoding = 'q'; break;
1294 case BuiltinType::Float: encoding = 'f'; break;
1295 case BuiltinType::Double: encoding = 'd'; break;
1296 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001297 }
1298
1299 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001300 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001301 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001302 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001303 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001304
1305 }
1306 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001307 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001308 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001309 S += '@';
1310 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001311 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001312 S += '#';
1313 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001315 S += ':';
1316 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001317 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001318
1319 if (PointeeTy->isCharType()) {
1320 // char pointer types should be encoded as '*' unless it is a
1321 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001322 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001323 S += '*';
1324 return;
1325 }
1326 }
1327
1328 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001329 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001330 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001331 S += '[';
1332
1333 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1334 S += llvm::utostr(CAT->getSize().getZExtValue());
1335 else
1336 assert(0 && "Unhandled array type!");
1337
Fariborz Jahanian248db262008-01-22 22:44:46 +00001338 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001339 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001340 } else if (T->getAsFunctionType()) {
1341 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001342 } else if (const RecordType *RTy = T->getAsRecordType()) {
1343 RecordDecl *RDecl= RTy->getDecl();
1344 S += '{';
1345 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001346 bool found = false;
1347 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1348 if (ERType[i] == RTy) {
1349 found = true;
1350 break;
1351 }
1352 if (!found) {
1353 ERType.push_back(RTy);
1354 S += '=';
1355 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1356 FieldDecl *field = RDecl->getMember(i);
1357 getObjCEncodingForType(field->getType(), S, ERType);
1358 }
1359 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1360 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001361 }
1362 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001363 } else if (T->isEnumeralType()) {
1364 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001365 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001366 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001367}
1368
Ted Kremenek42730c52008-01-07 19:49:32 +00001369void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001370 std::string& S) const {
1371 if (QT & Decl::OBJC_TQ_In)
1372 S += 'n';
1373 if (QT & Decl::OBJC_TQ_Inout)
1374 S += 'N';
1375 if (QT & Decl::OBJC_TQ_Out)
1376 S += 'o';
1377 if (QT & Decl::OBJC_TQ_Bycopy)
1378 S += 'O';
1379 if (QT & Decl::OBJC_TQ_Byref)
1380 S += 'R';
1381 if (QT & Decl::OBJC_TQ_Oneway)
1382 S += 'V';
1383}
1384
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001385void ASTContext::setBuiltinVaListType(QualType T)
1386{
1387 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1388
1389 BuiltinVaListType = T;
1390}
1391
Ted Kremenek42730c52008-01-07 19:49:32 +00001392void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001393{
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001395
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001397
1398 // typedef struct objc_object *id;
1399 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1400 assert(ptr && "'id' incorrectly typed");
1401 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1402 assert(rec && "'id' incorrectly typed");
1403 IdStructType = rec;
1404}
1405
Ted Kremenek42730c52008-01-07 19:49:32 +00001406void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001407{
Ted Kremenek42730c52008-01-07 19:49:32 +00001408 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001409
Ted Kremenek42730c52008-01-07 19:49:32 +00001410 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001411
1412 // typedef struct objc_selector *SEL;
1413 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1414 assert(ptr && "'SEL' incorrectly typed");
1415 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1416 assert(rec && "'SEL' incorrectly typed");
1417 SelStructType = rec;
1418}
1419
Ted Kremenek42730c52008-01-07 19:49:32 +00001420void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001421{
Ted Kremenek42730c52008-01-07 19:49:32 +00001422 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1423 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001424}
1425
Ted Kremenek42730c52008-01-07 19:49:32 +00001426void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001427{
Ted Kremenek42730c52008-01-07 19:49:32 +00001428 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001429
Ted Kremenek42730c52008-01-07 19:49:32 +00001430 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001431
1432 // typedef struct objc_class *Class;
1433 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1434 assert(ptr && "'Class' incorrectly typed");
1435 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1436 assert(rec && "'Class' incorrectly typed");
1437 ClassStructType = rec;
1438}
1439
Ted Kremenek42730c52008-01-07 19:49:32 +00001440void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1441 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001442 "'NSConstantString' type already set!");
1443
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001445}
1446
Chris Lattner6ff358b2008-04-07 06:51:04 +00001447//===----------------------------------------------------------------------===//
1448// Type Compatibility Testing
1449//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001450
Chris Lattner390564e2008-04-07 06:49:41 +00001451/// C99 6.2.7p1: If both are complete types, then the following additional
1452/// requirements apply.
1453/// FIXME (handle compatibility across source files).
1454static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1455 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001456 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001457 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1458 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001459 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001460
Chris Lattner390564e2008-04-07 06:49:41 +00001461 // Within a translation unit a tag type is only compatible with itself. Self
1462 // equality is already handled by the time we get here.
1463 assert(LHS != RHS && "Self equality not handled!");
1464 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001465}
1466
1467bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1468 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1469 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001470 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1471 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001472 return false;
1473
1474 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1475 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1476
1477 return typesAreCompatible(ltype, rtype);
1478}
1479
Steve Naroff85f0dc52007-10-15 20:41:53 +00001480bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1481 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1482 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1483 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1484 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1485
1486 // first check the return types (common between C99 and K&R).
1487 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1488 return false;
1489
1490 if (lproto && rproto) { // two C99 style function prototypes
1491 unsigned lproto_nargs = lproto->getNumArgs();
1492 unsigned rproto_nargs = rproto->getNumArgs();
1493
1494 if (lproto_nargs != rproto_nargs)
1495 return false;
1496
1497 // both prototypes have the same number of arguments.
1498 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1499 (rproto->isVariadic() && !lproto->isVariadic()))
1500 return false;
1501
1502 // The use of ellipsis agree...now check the argument types.
1503 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001504 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1505 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001506 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001507 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001508 return false;
1509 return true;
1510 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001511
Steve Naroff85f0dc52007-10-15 20:41:53 +00001512 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1513 return true;
1514
1515 // we have a mixture of K&R style with C99 prototypes
1516 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001517 if (proto->isVariadic())
1518 return false;
1519
1520 // FIXME: Each parameter type T in the prototype must be compatible with the
1521 // type resulting from applying the usual argument conversions to T.
1522 return true;
1523}
1524
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001525// C99 6.7.5.2p6
1526static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001527 // Constant arrays must be the same size to be compatible.
1528 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1529 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1530 if (RCAT->getSize() != LCAT->getSize())
1531 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001532
Chris Lattnerc8971d72008-04-07 06:58:21 +00001533 // Compatible arrays must have compatible element types
1534 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001535}
1536
Chris Lattner6ff358b2008-04-07 06:51:04 +00001537/// areCompatVectorTypes - Return true if the two specified vector types are
1538/// compatible.
1539static bool areCompatVectorTypes(const VectorType *LHS,
1540 const VectorType *RHS) {
1541 assert(LHS->isCanonical() && RHS->isCanonical());
1542 return LHS->getElementType() == RHS->getElementType() &&
1543 LHS->getNumElements() == RHS->getNumElements();
1544}
1545
1546/// areCompatObjCInterfaces - Return true if the two interface types are
1547/// compatible for assignment from RHS to LHS. This handles validation of any
1548/// protocol qualifiers on the LHS or RHS.
1549///
Chris Lattner1d78a862008-04-07 07:01:58 +00001550static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1551 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001552 // Verify that the base decls are compatible: the RHS must be a subclass of
1553 // the LHS.
1554 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1555 return false;
1556
1557 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1558 // protocol qualified at all, then we are good.
1559 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1560 return true;
1561
1562 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1563 // isn't a superset.
1564 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1565 return true; // FIXME: should return false!
1566
1567 // Finally, we must have two protocol-qualified interfaces.
1568 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1569 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1570 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1571 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1572 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1573 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1574
1575 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1576 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1577 // LHS in a single parallel scan until we run out of elements in LHS.
1578 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1579 ObjCProtocolDecl *LHSProto = *LHSPI;
1580
1581 while (RHSPI != RHSPE) {
1582 ObjCProtocolDecl *RHSProto = *RHSPI++;
1583 // If the RHS has a protocol that the LHS doesn't, ignore it.
1584 if (RHSProto != LHSProto)
1585 continue;
1586
1587 // Otherwise, the RHS does have this element.
1588 ++LHSPI;
1589 if (LHSPI == LHSPE)
1590 return true; // All protocols in LHS exist in RHS.
1591
1592 LHSProto = *LHSPI;
1593 }
1594
1595 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1596 return false;
1597}
1598
1599
Steve Naroff85f0dc52007-10-15 20:41:53 +00001600/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1601/// both shall have the identically qualified version of a compatible type.
1602/// C99 6.2.7p1: Two types have compatible types if their types are the
1603/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001604bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1605 QualType LHS = LHS_NC.getCanonicalType();
1606 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001607
Bill Wendling6a9d8542007-12-03 07:33:35 +00001608 // C++ [expr]: If an expression initially has the type "reference to T", the
1609 // type is adjusted to "T" prior to any further analysis, the expression
1610 // designates the object or function denoted by the reference, and the
1611 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001612 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1613 LHS = RT->getPointeeType();
1614 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1615 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001616
Chris Lattnerd47d6042008-04-07 05:37:56 +00001617 // If two types are identical, they are compatible.
1618 if (LHS == RHS)
1619 return true;
1620
1621 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001622 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1623 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001624 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001625
1626 // Strip off ASQual's if present.
1627 if (LHSAS) {
1628 LHS = LHS.getUnqualifiedType();
1629 RHS = RHS.getUnqualifiedType();
1630 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001631
Chris Lattner855fed42008-04-07 04:07:56 +00001632 Type::TypeClass LHSClass = LHS->getTypeClass();
1633 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001634
1635 // We want to consider the two function types to be the same for these
1636 // comparisons, just force one to the other.
1637 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1638 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001639
1640 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001641 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1642 LHSClass = Type::ConstantArray;
1643 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1644 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001645
Nate Begemanaf6ed502008-04-18 23:10:10 +00001646 // Canonicalize ExtVector -> Vector.
1647 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1648 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001649
Chris Lattner7cdcb252008-04-07 06:38:24 +00001650 // Consider qualified interfaces and interfaces the same.
1651 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1652 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1653
Chris Lattnerb5709e22008-04-07 05:43:21 +00001654 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001655 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001656 // ID is compatible with all interface types.
1657 if (isa<ObjCInterfaceType>(LHS))
1658 return isObjCIdType(RHS);
1659 if (isa<ObjCInterfaceType>(RHS))
1660 return isObjCIdType(LHS);
Chris Lattner0d3e6452008-04-07 05:53:18 +00001661
Chris Lattnerc38d4522008-01-14 05:45:46 +00001662 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1663 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001664 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1665 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1666 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001667 }
Chris Lattner855fed42008-04-07 04:07:56 +00001668 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1669 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1670 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001671 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001672
Steve Naroff85f0dc52007-10-15 20:41:53 +00001673 return false;
1674 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001675
Steve Naroffc88babe2008-01-09 22:43:08 +00001676 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001677 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001678 case Type::ASQual:
1679 case Type::FunctionProto:
1680 case Type::VariableArray:
1681 case Type::IncompleteArray:
1682 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001683 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001684 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001685 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001686 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001687 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001688 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1689 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001690 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001691 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001692 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001693 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001694 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001695 // Only exactly equal builtin types are compatible, which is tested above.
1696 return false;
1697 case Type::Vector:
1698 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001699 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001700 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1701 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001702 default:
1703 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001704 }
1705 return true; // should never get here...
1706}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001707
Chris Lattner1d78a862008-04-07 07:01:58 +00001708//===----------------------------------------------------------------------===//
1709// Serialization Support
1710//===----------------------------------------------------------------------===//
1711
Ted Kremenek738e6c02007-10-31 17:10:13 +00001712/// Emit - Serialize an ASTContext object to Bitcode.
1713void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001714 S.EmitRef(SourceMgr);
1715 S.EmitRef(Target);
1716 S.EmitRef(Idents);
1717 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001718
Ted Kremenek68228a92007-10-31 22:44:07 +00001719 // Emit the size of the type vector so that we can reserve that size
1720 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001721 S.EmitInt(Types.size());
1722
Ted Kremenek034a78c2007-11-13 22:02:55 +00001723 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1724 I!=E;++I)
1725 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001726
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001727 S.EmitOwnedPtr(TUDecl);
1728
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001729 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001730}
1731
Ted Kremenekacba3612007-11-13 00:25:37 +00001732ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001733 SourceManager &SM = D.ReadRef<SourceManager>();
1734 TargetInfo &t = D.ReadRef<TargetInfo>();
1735 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1736 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001737
Ted Kremenek68228a92007-10-31 22:44:07 +00001738 unsigned size_reserve = D.ReadInt();
1739
1740 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1741
Ted Kremenek034a78c2007-11-13 22:02:55 +00001742 for (unsigned i = 0; i < size_reserve; ++i)
1743 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001744
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001745 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1746
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001747 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001748
1749 return A;
1750}