blob: 6520fa920e1335fec676d16a440da673eefa22f9 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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;
Chris Lattnerbeb66362007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Fariborz Jahanianc5692492007-12-17 21:03:50 +000052 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 }
Steve Naroff3f128ad2007-09-17 14:16:13 +000083 } else if (isa<ObjcInterfaceType>(T))
84 ++NumObjcInterfaces;
Chris Lattnerbeb66362007-12-12 06:43:05 +000085 else if (isa<ObjcQualifiedInterfaceType>(T))
86 ++NumObjcQualifiedInterfaces;
Fariborz Jahanianc5692492007-12-17 21:03:50 +000087 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);
Steve Naroff3f128ad2007-09-17 14:16:13 +0000109 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
111 NumObjcQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
113 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.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
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();
165 ObjcIdType = QualType();
166 IdStructType = 0;
Anders Carlsson8baaca52007-10-31 02:53:19 +0000167 ObjcClassType = QualType();
168 ClassStructType = 0;
169
Steve Naroff21988912007-10-15 23:35:17 +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>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000184 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000185 uint64_t Size;
186 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 Lattner030d8842007-07-19 22:06:24 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000201 Align = EltInfo.second;
202 break;
203 }
204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
206 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
207 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
208 // 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 Lattnera7674d82007-07-13 22:13:22 +0000213 case Type::Builtin: {
214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000216 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000218 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000221 case BuiltinType::Bool:
222 Target.getBoolInfo(Size, Align, getFullLoc(L));
223 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:
228 Target.getCharInfo(Size, Align, getFullLoc(L));
229 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000230 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000231 case BuiltinType::Short:
232 Target.getShortInfo(Size, Align, getFullLoc(L));
233 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000234 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000235 case BuiltinType::Int:
236 Target.getIntInfo(Size, Align, getFullLoc(L));
237 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000238 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000239 case BuiltinType::Long:
240 Target.getLongInfo(Size, Align, getFullLoc(L));
241 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000242 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000243 case BuiltinType::LongLong:
244 Target.getLongLongInfo(Size, Align, getFullLoc(L));
245 break;
246 case BuiltinType::Float:
247 Target.getFloatInfo(Size, Align, F, getFullLoc(L));
248 break;
249 case BuiltinType::Double:
250 Target.getDoubleInfo(Size, Align, F, getFullLoc(L));
251 break;
252 case BuiltinType::LongDouble:
253 Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L));
254 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000255 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000256 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 }
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000258 case Type::ObjcQualifiedId:
259 Target.getPointerInfo(Size, Align, getFullLoc(L));
260 break;
261 case Type::Pointer:
262 Target.getPointerInfo(Size, Align, getFullLoc(L));
263 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000264 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000265 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000266 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000267 // FIXME: This is wrong for struct layout: a reference in a struct has
268 // pointer size.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000269 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000270
271 case Type::Complex: {
272 // Complex types have the same alignment as their elements, but twice the
273 // size.
274 std::pair<uint64_t, unsigned> EltInfo =
275 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
276 Size = EltInfo.first*2;
277 Align = EltInfo.second;
278 break;
279 }
280 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000281 TagType *TT = cast<TagType>(T);
282 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000283 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000284 Size = Layout.getSize();
285 Align = Layout.getAlignment();
286 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000287 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000288 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000289 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000290 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000291 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000292 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000293
Chris Lattner464175b2007-07-18 17:52:12 +0000294 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000295 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000296}
297
Devang Patel88a981b2007-11-01 19:11:01 +0000298/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000299/// specified record (struct/union/class), which indicates its size and field
300/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000301const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
302 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +0000303 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
304
305 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000306 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000307 if (Entry) return *Entry;
308
Devang Patel88a981b2007-11-01 19:11:01 +0000309 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
310 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
311 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000312 Entry = NewEntry;
313
314 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
315 uint64_t RecordSize = 0;
316 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
317
318 if (D->getKind() != Decl::Union) {
319 // Layout each field, for now, just sequentially, respecting alignment. In
320 // the future, this will need to be tweakable by targets.
321 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
322 const FieldDecl *FD = D->getMember(i);
323 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
324 uint64_t FieldSize = FieldInfo.first;
325 unsigned FieldAlign = FieldInfo.second;
326
327 // Round up the current record size to the field's alignment boundary.
328 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
329
330 // Place this field at the current location.
331 FieldOffsets[i] = RecordSize;
332
333 // Reserve space for this field.
334 RecordSize += FieldSize;
335
336 // Remember max struct/class alignment.
337 RecordAlign = std::max(RecordAlign, FieldAlign);
338 }
339
340 // Finally, round the size of the total struct up to the alignment of the
341 // struct itself.
342 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
343 } else {
344 // Union layout just puts each member at the start of the record.
345 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
346 const FieldDecl *FD = D->getMember(i);
347 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
348 uint64_t FieldSize = FieldInfo.first;
349 unsigned FieldAlign = FieldInfo.second;
350
351 // Round up the current record size to the field's alignment boundary.
352 RecordSize = std::max(RecordSize, FieldSize);
353
354 // Place this field at the start of the record.
355 FieldOffsets[i] = 0;
356
357 // Remember max struct/class alignment.
358 RecordAlign = std::max(RecordAlign, FieldAlign);
359 }
360 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000361
362 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
363 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000364}
365
Chris Lattnera7674d82007-07-13 22:13:22 +0000366//===----------------------------------------------------------------------===//
367// Type creation/memoization methods
368//===----------------------------------------------------------------------===//
369
370
Reid Spencer5f016e22007-07-11 17:01:13 +0000371/// getComplexType - Return the uniqued reference to the type for a complex
372/// number with the specified element type.
373QualType ASTContext::getComplexType(QualType T) {
374 // Unique pointers, to guarantee there is only one pointer of a particular
375 // structure.
376 llvm::FoldingSetNodeID ID;
377 ComplexType::Profile(ID, T);
378
379 void *InsertPos = 0;
380 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
381 return QualType(CT, 0);
382
383 // If the pointee type isn't canonical, this won't be a canonical type either,
384 // so fill in the canonical type field.
385 QualType Canonical;
386 if (!T->isCanonical()) {
387 Canonical = getComplexType(T.getCanonicalType());
388
389 // Get the new insert position for the node we care about.
390 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
391 assert(NewIP == 0 && "Shouldn't be in the map!");
392 }
393 ComplexType *New = new ComplexType(T, Canonical);
394 Types.push_back(New);
395 ComplexTypes.InsertNode(New, InsertPos);
396 return QualType(New, 0);
397}
398
399
400/// getPointerType - Return the uniqued reference to the type for a pointer to
401/// the specified type.
402QualType ASTContext::getPointerType(QualType T) {
403 // Unique pointers, to guarantee there is only one pointer of a particular
404 // structure.
405 llvm::FoldingSetNodeID ID;
406 PointerType::Profile(ID, T);
407
408 void *InsertPos = 0;
409 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
410 return QualType(PT, 0);
411
412 // If the pointee type isn't canonical, this won't be a canonical type either,
413 // so fill in the canonical type field.
414 QualType Canonical;
415 if (!T->isCanonical()) {
416 Canonical = getPointerType(T.getCanonicalType());
417
418 // Get the new insert position for the node we care about.
419 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
420 assert(NewIP == 0 && "Shouldn't be in the map!");
421 }
422 PointerType *New = new PointerType(T, Canonical);
423 Types.push_back(New);
424 PointerTypes.InsertNode(New, InsertPos);
425 return QualType(New, 0);
426}
427
428/// getReferenceType - Return the uniqued reference to the type for a reference
429/// to the specified type.
430QualType ASTContext::getReferenceType(QualType T) {
431 // Unique pointers, to guarantee there is only one pointer of a particular
432 // structure.
433 llvm::FoldingSetNodeID ID;
434 ReferenceType::Profile(ID, T);
435
436 void *InsertPos = 0;
437 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
438 return QualType(RT, 0);
439
440 // If the referencee type isn't canonical, this won't be a canonical type
441 // either, so fill in the canonical type field.
442 QualType Canonical;
443 if (!T->isCanonical()) {
444 Canonical = getReferenceType(T.getCanonicalType());
445
446 // Get the new insert position for the node we care about.
447 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
448 assert(NewIP == 0 && "Shouldn't be in the map!");
449 }
450
451 ReferenceType *New = new ReferenceType(T, Canonical);
452 Types.push_back(New);
453 ReferenceTypes.InsertNode(New, InsertPos);
454 return QualType(New, 0);
455}
456
Steve Narofffb22d962007-08-30 01:06:46 +0000457/// getConstantArrayType - Return the unique reference to the type for an
458/// array of the specified element type.
459QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000460 const llvm::APInt &ArySize,
461 ArrayType::ArraySizeModifier ASM,
462 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000464 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
466 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000467 if (ConstantArrayType *ATP =
468 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 return QualType(ATP, 0);
470
471 // If the element type isn't canonical, this won't be a canonical type either,
472 // so fill in the canonical type field.
473 QualType Canonical;
474 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000475 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
476 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000478 ConstantArrayType *NewIP =
479 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
480
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 assert(NewIP == 0 && "Shouldn't be in the map!");
482 }
483
Steve Naroffc9406122007-08-30 18:10:14 +0000484 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
485 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000486 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 Types.push_back(New);
488 return QualType(New, 0);
489}
490
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000491/// getVariableArrayType - Returns a non-unique reference to the type for a
492/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000493QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
494 ArrayType::ArraySizeModifier ASM,
495 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000496 if (NumElts) {
497 // Since we don't unique expressions, it isn't possible to unique VLA's
498 // that have an expression provided for their size.
499
Ted Kremenek347b9f32007-10-30 16:41:53 +0000500 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
501 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000502
Ted Kremenek347b9f32007-10-30 16:41:53 +0000503 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000504 Types.push_back(New);
505 return QualType(New, 0);
506 }
507 else {
508 // No size is provided for the VLA. These we can unique.
509 llvm::FoldingSetNodeID ID;
510 VariableArrayType::Profile(ID, EltTy);
511
512 void *InsertPos = 0;
513 if (VariableArrayType *ATP =
514 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
515 return QualType(ATP, 0);
516
517 // If the element type isn't canonical, this won't be a canonical type
518 // either, so fill in the canonical type field.
519 QualType Canonical;
520
521 if (!EltTy->isCanonical()) {
522 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
523 ASM, EltTypeQuals);
524
525 // Get the new insert position for the node we care about.
526 VariableArrayType *NewIP =
527 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
528
529 assert(NewIP == 0 && "Shouldn't be in the map!");
530 }
531
532 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
533 ASM, EltTypeQuals);
534
535 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
536 Types.push_back(New);
537 return QualType(New, 0);
538 }
Steve Narofffb22d962007-08-30 01:06:46 +0000539}
540
Steve Naroff73322922007-07-18 18:00:27 +0000541/// getVectorType - Return the unique reference to a vector type of
542/// the specified element type and size. VectorType must be a built-in type.
543QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 BuiltinType *baseType;
545
546 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000547 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000548
549 // Check if we've already instantiated a vector of this type.
550 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000551 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 void *InsertPos = 0;
553 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
554 return QualType(VTP, 0);
555
556 // If the element type isn't canonical, this won't be a canonical type either,
557 // so fill in the canonical type field.
558 QualType Canonical;
559 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000560 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
562 // Get the new insert position for the node we care about.
563 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
564 assert(NewIP == 0 && "Shouldn't be in the map!");
565 }
566 VectorType *New = new VectorType(vecType, NumElts, Canonical);
567 VectorTypes.InsertNode(New, InsertPos);
568 Types.push_back(New);
569 return QualType(New, 0);
570}
571
Steve Naroff73322922007-07-18 18:00:27 +0000572/// getOCUVectorType - Return the unique reference to an OCU vector type of
573/// the specified element type and size. VectorType must be a built-in type.
574QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
575 BuiltinType *baseType;
576
577 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
578 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
579
580 // Check if we've already instantiated a vector of this type.
581 llvm::FoldingSetNodeID ID;
582 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
583 void *InsertPos = 0;
584 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
585 return QualType(VTP, 0);
586
587 // If the element type isn't canonical, this won't be a canonical type either,
588 // so fill in the canonical type field.
589 QualType Canonical;
590 if (!vecType->isCanonical()) {
591 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
592
593 // Get the new insert position for the node we care about.
594 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
595 assert(NewIP == 0 && "Shouldn't be in the map!");
596 }
597 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
598 VectorTypes.InsertNode(New, InsertPos);
599 Types.push_back(New);
600 return QualType(New, 0);
601}
602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
604///
605QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
606 // Unique functions, to guarantee there is only one function of a particular
607 // structure.
608 llvm::FoldingSetNodeID ID;
609 FunctionTypeNoProto::Profile(ID, ResultTy);
610
611 void *InsertPos = 0;
612 if (FunctionTypeNoProto *FT =
613 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
614 return QualType(FT, 0);
615
616 QualType Canonical;
617 if (!ResultTy->isCanonical()) {
618 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
619
620 // Get the new insert position for the node we care about.
621 FunctionTypeNoProto *NewIP =
622 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
623 assert(NewIP == 0 && "Shouldn't be in the map!");
624 }
625
626 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
627 Types.push_back(New);
628 FunctionTypeProtos.InsertNode(New, InsertPos);
629 return QualType(New, 0);
630}
631
632/// getFunctionType - Return a normal function type with a typed argument
633/// list. isVariadic indicates whether the argument list includes '...'.
634QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
635 unsigned NumArgs, bool isVariadic) {
636 // Unique functions, to guarantee there is only one function of a particular
637 // structure.
638 llvm::FoldingSetNodeID ID;
639 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
640
641 void *InsertPos = 0;
642 if (FunctionTypeProto *FTP =
643 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
644 return QualType(FTP, 0);
645
646 // Determine whether the type being created is already canonical or not.
647 bool isCanonical = ResultTy->isCanonical();
648 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
649 if (!ArgArray[i]->isCanonical())
650 isCanonical = false;
651
652 // If this type isn't canonical, get the canonical version of it.
653 QualType Canonical;
654 if (!isCanonical) {
655 llvm::SmallVector<QualType, 16> CanonicalArgs;
656 CanonicalArgs.reserve(NumArgs);
657 for (unsigned i = 0; i != NumArgs; ++i)
658 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
659
660 Canonical = getFunctionType(ResultTy.getCanonicalType(),
661 &CanonicalArgs[0], NumArgs,
662 isVariadic);
663
664 // Get the new insert position for the node we care about.
665 FunctionTypeProto *NewIP =
666 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
667 assert(NewIP == 0 && "Shouldn't be in the map!");
668 }
669
670 // FunctionTypeProto objects are not allocated with new because they have a
671 // variable size array (for parameter types) at the end of them.
672 FunctionTypeProto *FTP =
673 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000674 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
676 Canonical);
677 Types.push_back(FTP);
678 FunctionTypeProtos.InsertNode(FTP, InsertPos);
679 return QualType(FTP, 0);
680}
681
682/// getTypedefType - Return the unique reference to the type for the
683/// specified typename decl.
684QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
685 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
686
687 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000688 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 Types.push_back(Decl->TypeForDecl);
690 return QualType(Decl->TypeForDecl, 0);
691}
692
Steve Naroff3536b442007-09-06 21:24:23 +0000693/// getObjcInterfaceType - Return the unique reference to the type for the
694/// specified ObjC interface decl.
695QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
696 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
697
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000698 Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000699 Types.push_back(Decl->TypeForDecl);
700 return QualType(Decl->TypeForDecl, 0);
701}
702
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000703/// getObjcQualifiedInterfaceType - Return a
704/// ObjcQualifiedInterfaceType type for the given interface decl and
705/// the conforming protocol list.
706QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
707 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000708 llvm::FoldingSetNodeID ID;
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000709 ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000710
711 void *InsertPos = 0;
712 if (ObjcQualifiedInterfaceType *QT =
713 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
714 return QualType(QT, 0);
715
716 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000717 ObjcQualifiedInterfaceType *QType =
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000718 new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000719 Types.push_back(QType);
720 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
721 return QualType(QType, 0);
722}
723
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000724/// getObjcQualifiedIdType - Return a
Fariborz Jahanianc2937252007-12-17 21:48:49 +0000725/// getObjcQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000726/// the conforming protocol list.
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000727QualType ASTContext::getObjcQualifiedIdType(QualType idType,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000728 ObjcProtocolDecl **Protocols,
729 unsigned NumProtocols) {
730 llvm::FoldingSetNodeID ID;
731 ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols);
732
733 void *InsertPos = 0;
734 if (ObjcQualifiedIdType *QT =
735 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
736 return QualType(QT, 0);
737
738 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000739 QualType Canonical;
740 if (!idType->isCanonical()) {
741 Canonical = getObjcQualifiedIdType(idType.getCanonicalType(),
742 Protocols, NumProtocols);
743 ObjcQualifiedIdType *NewQT =
744 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
745 assert(NewQT == 0 && "Shouldn't be in the map!");
746 }
747
748 ObjcQualifiedIdType *QType =
749 new ObjcQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000750 Types.push_back(QType);
751 ObjcQualifiedIdTypes.InsertNode(QType, InsertPos);
752 return QualType(QType, 0);
753}
754
Steve Naroff9752f252007-08-01 18:02:17 +0000755/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
756/// TypeOfExpr AST's (since expression's are never shared). For example,
757/// multiple declarations that refer to "typeof(x)" all contain different
758/// DeclRefExpr's. This doesn't effect the type checker, since it operates
759/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000760QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000761 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000762 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
763 Types.push_back(toe);
764 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000765}
766
Steve Naroff9752f252007-08-01 18:02:17 +0000767/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
768/// TypeOfType AST's. The only motivation to unique these nodes would be
769/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
770/// an issue. This doesn't effect the type checker, since it operates
771/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000772QualType ASTContext::getTypeOfType(QualType tofType) {
773 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000774 TypeOfType *tot = new TypeOfType(tofType, Canonical);
775 Types.push_back(tot);
776 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000777}
778
Reid Spencer5f016e22007-07-11 17:01:13 +0000779/// getTagDeclType - Return the unique reference to the type for the
780/// specified TagDecl (struct/union/class/enum) decl.
781QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000782 assert (Decl);
783
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000784 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000785 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000786
787 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000788 Types.push_back(T);
789 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000790
791 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000792}
793
794/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
795/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
796/// needs to agree with the definition in <stddef.h>.
797QualType ASTContext::getSizeType() const {
798 // On Darwin, size_t is defined as a "long unsigned int".
799 // FIXME: should derive from "Target".
800 return UnsignedLongTy;
801}
802
Chris Lattner8b9023b2007-07-13 03:05:23 +0000803/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
804/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
805QualType ASTContext::getPointerDiffType() const {
806 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
807 // FIXME: should derive from "Target".
808 return IntTy;
809}
810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
812/// routine will assert if passed a built-in type that isn't an integer or enum.
813static int getIntegerRank(QualType t) {
814 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
815 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
816 return 4;
817 }
818
819 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
820 switch (BT->getKind()) {
821 default:
822 assert(0 && "getIntegerRank(): not a built-in integer");
823 case BuiltinType::Bool:
824 return 1;
825 case BuiltinType::Char_S:
826 case BuiltinType::Char_U:
827 case BuiltinType::SChar:
828 case BuiltinType::UChar:
829 return 2;
830 case BuiltinType::Short:
831 case BuiltinType::UShort:
832 return 3;
833 case BuiltinType::Int:
834 case BuiltinType::UInt:
835 return 4;
836 case BuiltinType::Long:
837 case BuiltinType::ULong:
838 return 5;
839 case BuiltinType::LongLong:
840 case BuiltinType::ULongLong:
841 return 6;
842 }
843}
844
845/// getFloatingRank - Return a relative rank for floating point types.
846/// This routine will assert if passed a built-in type that isn't a float.
847static int getFloatingRank(QualType T) {
848 T = T.getCanonicalType();
849 if (ComplexType *CT = dyn_cast<ComplexType>(T))
850 return getFloatingRank(CT->getElementType());
851
852 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000853 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 case BuiltinType::Float: return FloatRank;
855 case BuiltinType::Double: return DoubleRank;
856 case BuiltinType::LongDouble: return LongDoubleRank;
857 }
858}
859
Steve Naroff716c7302007-08-27 01:41:48 +0000860/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
861/// point or a complex type (based on typeDomain/typeSize).
862/// 'typeDomain' is a real floating point or complex type.
863/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000864QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
865 QualType typeSize, QualType typeDomain) const {
866 if (typeDomain->isComplexType()) {
867 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000868 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000869 case FloatRank: return FloatComplexTy;
870 case DoubleRank: return DoubleComplexTy;
871 case LongDoubleRank: return LongDoubleComplexTy;
872 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 }
Steve Narofff1448a02007-08-27 01:27:54 +0000874 if (typeDomain->isRealFloatingType()) {
875 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000876 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000877 case FloatRank: return FloatTy;
878 case DoubleRank: return DoubleTy;
879 case LongDoubleRank: return LongDoubleTy;
880 }
881 }
882 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000883 //an invalid return value, but the assert
884 //will ensure that this code is never reached.
885 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000886}
887
Steve Narofffb0d4962007-08-27 15:30:22 +0000888/// compareFloatingType - Handles 3 different combos:
889/// float/float, float/complex, complex/complex.
890/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
891int ASTContext::compareFloatingType(QualType lt, QualType rt) {
892 if (getFloatingRank(lt) == getFloatingRank(rt))
893 return 0;
894 if (getFloatingRank(lt) > getFloatingRank(rt))
895 return 1;
896 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000897}
898
899// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
900// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
901QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
902 if (lhs == rhs) return lhs;
903
904 bool t1Unsigned = lhs->isUnsignedIntegerType();
905 bool t2Unsigned = rhs->isUnsignedIntegerType();
906
907 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
908 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
909
910 // We have two integer types with differing signs
911 QualType unsignedType = t1Unsigned ? lhs : rhs;
912 QualType signedType = t1Unsigned ? rhs : lhs;
913
914 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
915 return unsignedType;
916 else {
917 // FIXME: Need to check if the signed type can represent all values of the
918 // unsigned type. If it can, then the result is the signed type.
919 // If it can't, then the result is the unsigned version of the signed type.
920 // Should probably add a helper that returns a signed integer type from
921 // an unsigned (and vice versa). C99 6.3.1.8.
922 return signedType;
923 }
924}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000925
926// getCFConstantStringType - Return the type used for constant CFStrings.
927QualType ASTContext::getCFConstantStringType() {
928 if (!CFConstantStringTypeDecl) {
929 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000930 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000931 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000932 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000933
934 // const int *isa;
935 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000936 // int flags;
937 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000938 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000939 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000940 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000941 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000942 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000943 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000944
Anders Carlssonf06273f2007-11-19 00:25:30 +0000945 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000946 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000947
948 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
949 }
950
951 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000952}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000953
Anders Carlssone8c49532007-10-29 06:33:42 +0000954// This returns true if a type has been typedefed to BOOL:
955// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000956static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000957 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000958 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000959
960 return false;
961}
962
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000963/// getObjcEncodingTypeSize returns size of type for objective-c encoding
964/// purpose.
965int ASTContext::getObjcEncodingTypeSize(QualType type) {
966 SourceLocation Loc;
967 uint64_t sz = getTypeSize(type, Loc);
968
969 // Make all integer and enum types at least as large as an int
970 if (sz > 0 && type->isIntegralType())
971 sz = std::max(sz, getTypeSize(IntTy, Loc));
972 // Treat arrays as pointers, since that's how they're passed in.
973 else if (type->isArrayType())
974 sz = getTypeSize(VoidPtrTy, Loc);
975 return sz / getTypeSize(CharTy, Loc);
976}
977
978/// getObjcEncodingForMethodDecl - Return the encoded type for this method
979/// declaration.
980void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
981 std::string& S)
982{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000983 // Encode type qualifer, 'in', 'inout', etc. for the return type.
984 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000985 // Encode result type.
986 getObjcEncodingForType(Decl->getResultType(), S);
987 // Compute size of all parameters.
988 // Start with computing size of a pointer in number of bytes.
989 // FIXME: There might(should) be a better way of doing this computation!
990 SourceLocation Loc;
991 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
992 // The first two arguments (self and _cmd) are pointers; account for
993 // their size.
994 int ParmOffset = 2 * PtrSize;
995 int NumOfParams = Decl->getNumParams();
996 for (int i = 0; i < NumOfParams; i++) {
997 QualType PType = Decl->getParamDecl(i)->getType();
998 int sz = getObjcEncodingTypeSize (PType);
999 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
1000 ParmOffset += sz;
1001 }
1002 S += llvm::utostr(ParmOffset);
1003 S += "@0:";
1004 S += llvm::utostr(PtrSize);
1005
1006 // Argument types.
1007 ParmOffset = 2 * PtrSize;
1008 for (int i = 0; i < NumOfParams; i++) {
1009 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001010 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001011 // 'in', 'inout', etc.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001012 getObjcEncodingForTypeQualifier(
1013 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001014 getObjcEncodingForType(PType, S);
1015 S += llvm::utostr(ParmOffset);
1016 ParmOffset += getObjcEncodingTypeSize(PType);
1017 }
1018}
1019
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001020void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
1021{
Anders Carlssone8c49532007-10-29 06:33:42 +00001022 // FIXME: This currently doesn't encode:
1023 // @ An object (whether statically typed or typed id)
1024 // # A class object (Class)
1025 // : A method selector (SEL)
1026 // {name=type...} A structure
1027 // (name=type...) A union
1028 // bnum A bit field of num bits
1029
1030 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001031 char encoding;
1032 switch (BT->getKind()) {
1033 case BuiltinType::Void:
1034 encoding = 'v';
1035 break;
1036 case BuiltinType::Bool:
1037 encoding = 'B';
1038 break;
1039 case BuiltinType::Char_U:
1040 case BuiltinType::UChar:
1041 encoding = 'C';
1042 break;
1043 case BuiltinType::UShort:
1044 encoding = 'S';
1045 break;
1046 case BuiltinType::UInt:
1047 encoding = 'I';
1048 break;
1049 case BuiltinType::ULong:
1050 encoding = 'L';
1051 break;
1052 case BuiltinType::ULongLong:
1053 encoding = 'Q';
1054 break;
1055 case BuiltinType::Char_S:
1056 case BuiltinType::SChar:
1057 encoding = 'c';
1058 break;
1059 case BuiltinType::Short:
1060 encoding = 's';
1061 break;
1062 case BuiltinType::Int:
1063 encoding = 'i';
1064 break;
1065 case BuiltinType::Long:
1066 encoding = 'l';
1067 break;
1068 case BuiltinType::LongLong:
1069 encoding = 'q';
1070 break;
1071 case BuiltinType::Float:
1072 encoding = 'f';
1073 break;
1074 case BuiltinType::Double:
1075 encoding = 'd';
1076 break;
1077 case BuiltinType::LongDouble:
1078 encoding = 'd';
1079 break;
1080 default:
1081 assert(0 && "Unhandled builtin type kind");
1082 }
1083
1084 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001085 }
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001086 else if (T->isObjcQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001087 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001088 return getObjcEncodingForType(getObjcIdType(), S);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001089
1090 }
1091 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001092 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson8baaca52007-10-31 02:53:19 +00001093 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001094 S += '@';
1095 return;
Anders Carlsson8baaca52007-10-31 02:53:19 +00001096 } else if (isObjcClassType(PointeeTy)) {
1097 S += '#';
1098 return;
1099 } else if (isObjcSelType(PointeeTy)) {
1100 S += ':';
1101 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001102 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001103
1104 if (PointeeTy->isCharType()) {
1105 // char pointer types should be encoded as '*' unless it is a
1106 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001107 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001108 S += '*';
1109 return;
1110 }
1111 }
1112
1113 S += '^';
1114 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +00001115 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001116 S += '[';
1117
1118 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1119 S += llvm::utostr(CAT->getSize().getZExtValue());
1120 else
1121 assert(0 && "Unhandled array type!");
1122
1123 getObjcEncodingForType(AT->getElementType(), S);
1124 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001125 } else if (T->getAsFunctionType()) {
1126 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001127 } else if (const RecordType *RTy = T->getAsRecordType()) {
1128 RecordDecl *RDecl= RTy->getDecl();
1129 S += '{';
1130 S += RDecl->getName();
1131 S += '=';
1132 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1133 FieldDecl *field = RDecl->getMember(i);
1134 getObjcEncodingForType(field->getType(), S);
1135 }
1136 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001137 } else if (T->isEnumeralType()) {
1138 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001139 } else
Steve Naroff5e711242007-12-12 22:30:11 +00001140 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001141}
1142
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001143void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1144 std::string& S) const {
1145 if (QT & Decl::OBJC_TQ_In)
1146 S += 'n';
1147 if (QT & Decl::OBJC_TQ_Inout)
1148 S += 'N';
1149 if (QT & Decl::OBJC_TQ_Out)
1150 S += 'o';
1151 if (QT & Decl::OBJC_TQ_Bycopy)
1152 S += 'O';
1153 if (QT & Decl::OBJC_TQ_Byref)
1154 S += 'R';
1155 if (QT & Decl::OBJC_TQ_Oneway)
1156 S += 'V';
1157}
1158
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001159void ASTContext::setBuiltinVaListType(QualType T)
1160{
1161 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1162
1163 BuiltinVaListType = T;
1164}
1165
Steve Naroff7e219e42007-10-15 14:41:52 +00001166void ASTContext::setObjcIdType(TypedefDecl *TD)
1167{
1168 assert(ObjcIdType.isNull() && "'id' type already set!");
1169
1170 ObjcIdType = getTypedefType(TD);
1171
1172 // typedef struct objc_object *id;
1173 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1174 assert(ptr && "'id' incorrectly typed");
1175 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1176 assert(rec && "'id' incorrectly typed");
1177 IdStructType = rec;
1178}
1179
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001180void ASTContext::setObjcSelType(TypedefDecl *TD)
1181{
1182 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1183
1184 ObjcSelType = getTypedefType(TD);
1185
1186 // typedef struct objc_selector *SEL;
1187 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1188 assert(ptr && "'SEL' incorrectly typed");
1189 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1190 assert(rec && "'SEL' incorrectly typed");
1191 SelStructType = rec;
1192}
1193
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001194void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001195{
1196 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001197 ObjcProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001198}
1199
Anders Carlsson8baaca52007-10-31 02:53:19 +00001200void ASTContext::setObjcClassType(TypedefDecl *TD)
1201{
1202 assert(ObjcClassType.isNull() && "'Class' type already set!");
1203
1204 ObjcClassType = getTypedefType(TD);
1205
1206 // typedef struct objc_class *Class;
1207 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1208 assert(ptr && "'Class' incorrectly typed");
1209 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1210 assert(rec && "'Class' incorrectly typed");
1211 ClassStructType = rec;
1212}
1213
Steve Naroff21988912007-10-15 23:35:17 +00001214void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1215 assert(ObjcConstantStringType.isNull() &&
1216 "'NSConstantString' type already set!");
1217
1218 ObjcConstantStringType = getObjcInterfaceType(Decl);
1219}
1220
Steve Naroffec0550f2007-10-15 20:41:53 +00001221bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1222 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1223 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1224
1225 return lBuiltin->getKind() == rBuiltin->getKind();
1226}
1227
1228
1229bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1230 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1231 return true;
1232 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1233 return true;
1234 return false;
1235}
1236
1237bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1238 return true; // FIXME: IMPLEMENT.
1239}
1240
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001241bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1242 QualType rhs) {
1243 ObjcQualifiedInterfaceType *lhsQI =
1244 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1245 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1246 ObjcQualifiedInterfaceType *rhsQI =
1247 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1248 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian06cef252007-12-13 20:47:42 +00001249 if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()),
1250 getObjcInterfaceType(rhsQI->getDecl())))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001251 return false;
1252 /* All protocols in lhs must have a presense in rhs. */
1253 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1254 bool match = false;
1255 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1256 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1257 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1258 if (lhsProto == rhsProto) {
1259 match = true;
1260 break;
1261 }
1262 }
1263 if (!match)
1264 return false;
1265 }
1266 return true;
1267}
1268
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001269/// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least
1270/// one of which is a protocol qualified 'id' type.
1271bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs,
1272 QualType rhs) {
1273 // match id<P..> with an 'id' type in all cases.
1274 if (const PointerType *PT = lhs->getAsPointerType()) {
1275 QualType PointeeTy = PT->getPointeeType();
1276 if (isObjcIdType(PointeeTy))
1277 return true;
1278
1279 }
1280 else if (const PointerType *PT = rhs->getAsPointerType()) {
1281 QualType PointeeTy = PT->getPointeeType();
1282 if (isObjcIdType(PointeeTy))
1283 return true;
1284
1285 }
1286
1287 ObjcQualifiedInterfaceType *lhsQI = 0;
1288 ObjcQualifiedInterfaceType *rhsQI = 0;
1289 ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs);
1290 ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs);
1291
1292 if (lhsQID) {
1293 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1294 QualType rtype =
1295 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1296 rhsQI =
1297 dyn_cast<ObjcQualifiedInterfaceType>(
1298 rtype.getCanonicalType().getTypePtr());
1299 }
1300 if (!rhsQI && !rhsQID)
1301 return false;
1302
1303 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
1304 bool match = false;
1305 ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i);
1306 unsigned numRhsProtocols;
1307 ObjcProtocolDecl **rhsProtoList;
1308 if (rhsQI) {
1309 numRhsProtocols = rhsQI->getNumProtocols();
1310 rhsProtoList = rhsQI->getReferencedProtocols();
1311 }
1312 else {
1313 numRhsProtocols = rhsQID->getNumProtocols();
1314 rhsProtoList = rhsQID->getReferencedProtocols();
1315 }
1316 for (unsigned j = 0; j < numRhsProtocols; j++) {
1317 ObjcProtocolDecl *rhsProto = rhsProtoList[j];
1318 if (lhsProto == rhsProto) {
1319 match = true;
1320 break;
1321 }
1322 }
1323 if (!match)
1324 return false;
1325 }
1326 }
1327 else if (rhsQID) {
1328 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1329 QualType ltype =
1330 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1331 lhsQI =
1332 dyn_cast<ObjcQualifiedInterfaceType>(
1333 ltype.getCanonicalType().getTypePtr());
1334 }
1335 if (!lhsQI && !lhsQID)
1336 return false;
1337 unsigned numLhsProtocols;
1338 ObjcProtocolDecl **lhsProtoList;
1339 if (lhsQI) {
1340 numLhsProtocols = lhsQI->getNumProtocols();
1341 lhsProtoList = lhsQI->getReferencedProtocols();
1342 }
1343 else {
1344 numLhsProtocols = lhsQID->getNumProtocols();
1345 lhsProtoList = lhsQID->getReferencedProtocols();
1346 }
1347 for (unsigned i =0; i < numLhsProtocols; i++) {
1348 bool match = false;
1349 ObjcProtocolDecl *lhsProto = lhsProtoList[i];
1350 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1351 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
1352 if (lhsProto == rhsProto) {
1353 match = true;
1354 break;
1355 }
1356 }
1357 if (!match)
1358 return false;
1359 }
1360 }
1361 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001362}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001363
Chris Lattner770951b2007-11-01 05:03:41 +00001364bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1365 const VectorType *lVector = lhs->getAsVectorType();
1366 const VectorType *rVector = rhs->getAsVectorType();
1367
1368 if ((lVector->getElementType().getCanonicalType() ==
1369 rVector->getElementType().getCanonicalType()) &&
1370 (lVector->getNumElements() == rVector->getNumElements()))
1371 return true;
1372 return false;
1373}
1374
Steve Naroffec0550f2007-10-15 20:41:53 +00001375// C99 6.2.7p1: If both are complete types, then the following additional
1376// requirements apply...FIXME (handle compatibility across source files).
1377bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1378 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1379 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1380
1381 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1382 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1383 return true;
1384 }
1385 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1386 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1387 return true;
1388 }
Steve Naroffab373092007-11-07 06:03:51 +00001389 // "Class" and "id" are compatible built-in structure types.
1390 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1391 isObjcClassType(lhs) && isObjcIdType(rhs))
1392 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001393 return false;
1394}
1395
1396bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1397 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1398 // identically qualified and both shall be pointers to compatible types.
1399 if (lhs.getQualifiers() != rhs.getQualifiers())
1400 return false;
1401
1402 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1403 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1404
1405 return typesAreCompatible(ltype, rtype);
1406}
1407
Bill Wendling43d69752007-12-03 07:33:35 +00001408// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001409// reference to T, the operation assigns to the object of type T denoted by the
1410// reference.
1411bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1412 QualType ltype = lhs;
1413
1414 if (lhs->isReferenceType())
1415 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1416
1417 QualType rtype = rhs;
1418
1419 if (rhs->isReferenceType())
1420 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1421
1422 return typesAreCompatible(ltype, rtype);
1423}
1424
1425bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1426 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1427 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1428 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1429 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1430
1431 // first check the return types (common between C99 and K&R).
1432 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1433 return false;
1434
1435 if (lproto && rproto) { // two C99 style function prototypes
1436 unsigned lproto_nargs = lproto->getNumArgs();
1437 unsigned rproto_nargs = rproto->getNumArgs();
1438
1439 if (lproto_nargs != rproto_nargs)
1440 return false;
1441
1442 // both prototypes have the same number of arguments.
1443 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1444 (rproto->isVariadic() && !lproto->isVariadic()))
1445 return false;
1446
1447 // The use of ellipsis agree...now check the argument types.
1448 for (unsigned i = 0; i < lproto_nargs; i++)
1449 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1450 return false;
1451 return true;
1452 }
1453 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1454 return true;
1455
1456 // we have a mixture of K&R style with C99 prototypes
1457 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1458
1459 if (proto->isVariadic())
1460 return false;
1461
1462 // FIXME: Each parameter type T in the prototype must be compatible with the
1463 // type resulting from applying the usual argument conversions to T.
1464 return true;
1465}
1466
1467bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1468 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1469 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1470
1471 if (!typesAreCompatible(ltype, rtype))
1472 return false;
1473
1474 // FIXME: If both types specify constant sizes, then the sizes must also be
1475 // the same. Even if the sizes are the same, GCC produces an error.
1476 return true;
1477}
1478
1479/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1480/// both shall have the identically qualified version of a compatible type.
1481/// C99 6.2.7p1: Two types have compatible types if their types are the
1482/// same. See 6.7.[2,3,5] for additional rules.
1483bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1484 QualType lcanon = lhs.getCanonicalType();
1485 QualType rcanon = rhs.getCanonicalType();
1486
1487 // If two types are identical, they are are compatible
1488 if (lcanon == rcanon)
1489 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001490
1491 // C++ [expr]: If an expression initially has the type "reference to T", the
1492 // type is adjusted to "T" prior to any further analysis, the expression
1493 // designates the object or function denoted by the reference, and the
1494 // expression is an lvalue.
1495 if (lcanon->getTypeClass() == Type::Reference)
1496 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1497 if (rcanon->getTypeClass() == Type::Reference)
1498 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001499
1500 // If the canonical type classes don't match, they can't be compatible
1501 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1502 // For Objective-C, it is possible for two types to be compatible
1503 // when their classes don't match (when dealing with "id"). If either type
1504 // is an interface, we defer to objcTypesAreCompatible().
1505 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1506 return objcTypesAreCompatible(lcanon, rcanon);
1507 return false;
1508 }
1509 switch (lcanon->getTypeClass()) {
1510 case Type::Pointer:
1511 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001512 case Type::ConstantArray:
1513 case Type::VariableArray:
1514 return arrayTypesAreCompatible(lcanon, rcanon);
1515 case Type::FunctionNoProto:
1516 case Type::FunctionProto:
1517 return functionTypesAreCompatible(lcanon, rcanon);
1518 case Type::Tagged: // handle structures, unions
1519 return tagTypesAreCompatible(lcanon, rcanon);
1520 case Type::Builtin:
1521 return builtinTypesAreCompatible(lcanon, rcanon);
1522 case Type::ObjcInterface:
1523 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001524 case Type::Vector:
1525 case Type::OCUVector:
1526 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001527 case Type::ObjcQualifiedInterface:
1528 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001529 default:
1530 assert(0 && "unexpected type");
1531 }
1532 return true; // should never get here...
1533}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001534
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001535/// Emit - Serialize an ASTContext object to Bitcode.
1536void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001537 S.EmitRef(SourceMgr);
1538 S.EmitRef(Target);
1539 S.EmitRef(Idents);
1540 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001541
Ted Kremenekfee04522007-10-31 22:44:07 +00001542 // Emit the size of the type vector so that we can reserve that size
1543 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001544 S.EmitInt(Types.size());
1545
Ted Kremenek03ed4402007-11-13 22:02:55 +00001546 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1547 I!=E;++I)
1548 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001549
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001550 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001551}
1552
Ted Kremenek0f84c002007-11-13 00:25:37 +00001553ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001554 SourceManager &SM = D.ReadRef<SourceManager>();
1555 TargetInfo &t = D.ReadRef<TargetInfo>();
1556 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1557 SelectorTable &sels = D.ReadRef<SelectorTable>();
1558
1559 unsigned size_reserve = D.ReadInt();
1560
1561 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1562
Ted Kremenek03ed4402007-11-13 22:02:55 +00001563 for (unsigned i = 0; i < size_reserve; ++i)
1564 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001565
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001566 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001567
1568 return A;
1569}