blob: 132224cbd70642b6d570a4838351e782f2934459 [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;
Steve Naroff3f128ad2007-09-17 14:16:13 +000051 unsigned NumObjcInterfaces = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000052
53 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
54 Type *T = Types[i];
55 if (isa<BuiltinType>(T))
56 ++NumBuiltin;
57 else if (isa<PointerType>(T))
58 ++NumPointer;
59 else if (isa<ReferenceType>(T))
60 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000061 else if (isa<ComplexType>(T))
62 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 else if (isa<ArrayType>(T))
64 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000065 else if (isa<VectorType>(T))
66 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 else if (isa<FunctionTypeNoProto>(T))
68 ++NumFunctionNP;
69 else if (isa<FunctionTypeProto>(T))
70 ++NumFunctionP;
71 else if (isa<TypedefType>(T))
72 ++NumTypeName;
73 else if (TagType *TT = dyn_cast<TagType>(T)) {
74 ++NumTagged;
75 switch (TT->getDecl()->getKind()) {
76 default: assert(0 && "Unknown tagged type!");
77 case Decl::Struct: ++NumTagStruct; break;
78 case Decl::Union: ++NumTagUnion; break;
79 case Decl::Class: ++NumTagClass; break;
80 case Decl::Enum: ++NumTagEnum; break;
81 }
Steve Naroff3f128ad2007-09-17 14:16:13 +000082 } else if (isa<ObjcInterfaceType>(T))
83 ++NumObjcInterfaces;
84 else {
Reid Spencer5f016e22007-07-11 17:01:13 +000085 assert(0 && "Unknown type!");
86 }
87 }
88
89 fprintf(stderr, " %d builtin types\n", NumBuiltin);
90 fprintf(stderr, " %d pointer types\n", NumPointer);
91 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000092 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000093 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000094 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000095 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
96 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
97 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
98 fprintf(stderr, " %d tagged types\n", NumTagged);
99 fprintf(stderr, " %d struct types\n", NumTagStruct);
100 fprintf(stderr, " %d union types\n", NumTagUnion);
101 fprintf(stderr, " %d class types\n", NumTagClass);
102 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff3f128ad2007-09-17 14:16:13 +0000103 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
105 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000106 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 NumFunctionP*sizeof(FunctionTypeProto)+
108 NumFunctionNP*sizeof(FunctionTypeNoProto)+
109 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
110}
111
112
113void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
114 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
115}
116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117void ASTContext::InitBuiltinTypes() {
118 assert(VoidTy.isNull() && "Context reinitialized?");
119
120 // C99 6.2.5p19.
121 InitBuiltinType(VoidTy, BuiltinType::Void);
122
123 // C99 6.2.5p2.
124 InitBuiltinType(BoolTy, BuiltinType::Bool);
125 // C99 6.2.5p3.
126 if (Target.isCharSigned(SourceLocation()))
127 InitBuiltinType(CharTy, BuiltinType::Char_S);
128 else
129 InitBuiltinType(CharTy, BuiltinType::Char_U);
130 // C99 6.2.5p4.
131 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
132 InitBuiltinType(ShortTy, BuiltinType::Short);
133 InitBuiltinType(IntTy, BuiltinType::Int);
134 InitBuiltinType(LongTy, BuiltinType::Long);
135 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
136
137 // C99 6.2.5p6.
138 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
139 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
140 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
141 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
142 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
143
144 // C99 6.2.5p10.
145 InitBuiltinType(FloatTy, BuiltinType::Float);
146 InitBuiltinType(DoubleTy, BuiltinType::Double);
147 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
148
149 // C99 6.2.5p11.
150 FloatComplexTy = getComplexType(FloatTy);
151 DoubleComplexTy = getComplexType(DoubleTy);
152 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000153
154 BuiltinVaListType = QualType();
155 ObjcIdType = QualType();
156 IdStructType = 0;
Anders Carlsson8baaca52007-10-31 02:53:19 +0000157 ObjcClassType = QualType();
158 ClassStructType = 0;
159
Steve Naroff21988912007-10-15 23:35:17 +0000160 ObjcConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000161
162 // void * type
163 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164}
165
Chris Lattner464175b2007-07-18 17:52:12 +0000166//===----------------------------------------------------------------------===//
167// Type Sizing and Analysis
168//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000169
170/// getTypeSize - Return the size of the specified type, in bits. This method
171/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000172std::pair<uint64_t, unsigned>
173ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000174 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000175 uint64_t Size;
176 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000177 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000178 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000179 case Type::FunctionNoProto:
180 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000181 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000182 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000183 case Type::VariableArray:
184 assert(0 && "VLAs not implemented yet!");
185 case Type::ConstantArray: {
186 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
187
Chris Lattner030d8842007-07-19 22:06:24 +0000188 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000189 getTypeInfo(CAT->getElementType(), L);
190 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000191 Align = EltInfo.second;
192 break;
193 }
194 case Type::Vector: {
195 std::pair<uint64_t, unsigned> EltInfo =
196 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
197 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
198 // FIXME: Vector alignment is not the alignment of its elements.
199 Align = EltInfo.second;
200 break;
201 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000202
Chris Lattnera7674d82007-07-13 22:13:22 +0000203 case Type::Builtin: {
204 // FIXME: need to use TargetInfo to derive the target specific sizes. This
205 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000206 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000207 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000208 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000209 case BuiltinType::Void:
210 assert(0 && "Incomplete types have no size!");
211 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000212 case BuiltinType::Char_S:
213 case BuiltinType::Char_U:
214 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000215 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000216 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000217 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000218 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000219 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000220 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000221 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000222 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000223 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000224 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
225 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
226 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000227 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000228 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000229 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000230 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000231 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000232 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000233 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
234 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000235 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000236
237 case Type::Complex: {
238 // Complex types have the same alignment as their elements, but twice the
239 // size.
240 std::pair<uint64_t, unsigned> EltInfo =
241 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
242 Size = EltInfo.first*2;
243 Align = EltInfo.second;
244 break;
245 }
246 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000247 TagType *TT = cast<TagType>(T);
248 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000249 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000250 Size = Layout.getSize();
251 Align = Layout.getAlignment();
252 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000253 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000254 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000255 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000256 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000257 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000258 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000259
Chris Lattner464175b2007-07-18 17:52:12 +0000260 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000261 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000262}
263
Devang Patel88a981b2007-11-01 19:11:01 +0000264/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000265/// specified record (struct/union/class), which indicates its size and field
266/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000267const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
268 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +0000269 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
270
271 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000272 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000273 if (Entry) return *Entry;
274
Devang Patel88a981b2007-11-01 19:11:01 +0000275 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
276 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
277 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000278 Entry = NewEntry;
279
280 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
281 uint64_t RecordSize = 0;
282 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
283
284 if (D->getKind() != Decl::Union) {
285 // Layout each field, for now, just sequentially, respecting alignment. In
286 // the future, this will need to be tweakable by targets.
287 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
288 const FieldDecl *FD = D->getMember(i);
289 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
290 uint64_t FieldSize = FieldInfo.first;
291 unsigned FieldAlign = FieldInfo.second;
292
293 // Round up the current record size to the field's alignment boundary.
294 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
295
296 // Place this field at the current location.
297 FieldOffsets[i] = RecordSize;
298
299 // Reserve space for this field.
300 RecordSize += FieldSize;
301
302 // Remember max struct/class alignment.
303 RecordAlign = std::max(RecordAlign, FieldAlign);
304 }
305
306 // Finally, round the size of the total struct up to the alignment of the
307 // struct itself.
308 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
309 } else {
310 // Union layout just puts each member at the start of the record.
311 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
312 const FieldDecl *FD = D->getMember(i);
313 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
314 uint64_t FieldSize = FieldInfo.first;
315 unsigned FieldAlign = FieldInfo.second;
316
317 // Round up the current record size to the field's alignment boundary.
318 RecordSize = std::max(RecordSize, FieldSize);
319
320 // Place this field at the start of the record.
321 FieldOffsets[i] = 0;
322
323 // Remember max struct/class alignment.
324 RecordAlign = std::max(RecordAlign, FieldAlign);
325 }
326 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000327
328 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
329 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000330}
331
Chris Lattnera7674d82007-07-13 22:13:22 +0000332//===----------------------------------------------------------------------===//
333// Type creation/memoization methods
334//===----------------------------------------------------------------------===//
335
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337/// getComplexType - Return the uniqued reference to the type for a complex
338/// number with the specified element type.
339QualType ASTContext::getComplexType(QualType T) {
340 // Unique pointers, to guarantee there is only one pointer of a particular
341 // structure.
342 llvm::FoldingSetNodeID ID;
343 ComplexType::Profile(ID, T);
344
345 void *InsertPos = 0;
346 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
347 return QualType(CT, 0);
348
349 // If the pointee type isn't canonical, this won't be a canonical type either,
350 // so fill in the canonical type field.
351 QualType Canonical;
352 if (!T->isCanonical()) {
353 Canonical = getComplexType(T.getCanonicalType());
354
355 // Get the new insert position for the node we care about.
356 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
357 assert(NewIP == 0 && "Shouldn't be in the map!");
358 }
359 ComplexType *New = new ComplexType(T, Canonical);
360 Types.push_back(New);
361 ComplexTypes.InsertNode(New, InsertPos);
362 return QualType(New, 0);
363}
364
365
366/// getPointerType - Return the uniqued reference to the type for a pointer to
367/// the specified type.
368QualType ASTContext::getPointerType(QualType T) {
369 // Unique pointers, to guarantee there is only one pointer of a particular
370 // structure.
371 llvm::FoldingSetNodeID ID;
372 PointerType::Profile(ID, T);
373
374 void *InsertPos = 0;
375 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
376 return QualType(PT, 0);
377
378 // If the pointee type isn't canonical, this won't be a canonical type either,
379 // so fill in the canonical type field.
380 QualType Canonical;
381 if (!T->isCanonical()) {
382 Canonical = getPointerType(T.getCanonicalType());
383
384 // Get the new insert position for the node we care about.
385 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
386 assert(NewIP == 0 && "Shouldn't be in the map!");
387 }
388 PointerType *New = new PointerType(T, Canonical);
389 Types.push_back(New);
390 PointerTypes.InsertNode(New, InsertPos);
391 return QualType(New, 0);
392}
393
394/// getReferenceType - Return the uniqued reference to the type for a reference
395/// to the specified type.
396QualType ASTContext::getReferenceType(QualType T) {
397 // Unique pointers, to guarantee there is only one pointer of a particular
398 // structure.
399 llvm::FoldingSetNodeID ID;
400 ReferenceType::Profile(ID, T);
401
402 void *InsertPos = 0;
403 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
404 return QualType(RT, 0);
405
406 // If the referencee type isn't canonical, this won't be a canonical type
407 // either, so fill in the canonical type field.
408 QualType Canonical;
409 if (!T->isCanonical()) {
410 Canonical = getReferenceType(T.getCanonicalType());
411
412 // Get the new insert position for the node we care about.
413 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
414 assert(NewIP == 0 && "Shouldn't be in the map!");
415 }
416
417 ReferenceType *New = new ReferenceType(T, Canonical);
418 Types.push_back(New);
419 ReferenceTypes.InsertNode(New, InsertPos);
420 return QualType(New, 0);
421}
422
Steve Narofffb22d962007-08-30 01:06:46 +0000423/// getConstantArrayType - Return the unique reference to the type for an
424/// array of the specified element type.
425QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000426 const llvm::APInt &ArySize,
427 ArrayType::ArraySizeModifier ASM,
428 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000430 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431
432 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000433 if (ConstantArrayType *ATP =
434 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 return QualType(ATP, 0);
436
437 // If the element type isn't canonical, this won't be a canonical type either,
438 // so fill in the canonical type field.
439 QualType Canonical;
440 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000441 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
442 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000444 ConstantArrayType *NewIP =
445 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
446
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 assert(NewIP == 0 && "Shouldn't be in the map!");
448 }
449
Steve Naroffc9406122007-08-30 18:10:14 +0000450 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
451 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000452 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 Types.push_back(New);
454 return QualType(New, 0);
455}
456
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000457/// getVariableArrayType - Returns a non-unique reference to the type for a
458/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000459QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
460 ArrayType::ArraySizeModifier ASM,
461 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000462 if (NumElts) {
463 // Since we don't unique expressions, it isn't possible to unique VLA's
464 // that have an expression provided for their size.
465
Ted Kremenek347b9f32007-10-30 16:41:53 +0000466 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
467 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000468
Ted Kremenek347b9f32007-10-30 16:41:53 +0000469 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000470 Types.push_back(New);
471 return QualType(New, 0);
472 }
473 else {
474 // No size is provided for the VLA. These we can unique.
475 llvm::FoldingSetNodeID ID;
476 VariableArrayType::Profile(ID, EltTy);
477
478 void *InsertPos = 0;
479 if (VariableArrayType *ATP =
480 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
481 return QualType(ATP, 0);
482
483 // If the element type isn't canonical, this won't be a canonical type
484 // either, so fill in the canonical type field.
485 QualType Canonical;
486
487 if (!EltTy->isCanonical()) {
488 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
489 ASM, EltTypeQuals);
490
491 // Get the new insert position for the node we care about.
492 VariableArrayType *NewIP =
493 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
494
495 assert(NewIP == 0 && "Shouldn't be in the map!");
496 }
497
498 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
499 ASM, EltTypeQuals);
500
501 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
502 Types.push_back(New);
503 return QualType(New, 0);
504 }
Steve Narofffb22d962007-08-30 01:06:46 +0000505}
506
Steve Naroff73322922007-07-18 18:00:27 +0000507/// getVectorType - Return the unique reference to a vector type of
508/// the specified element type and size. VectorType must be a built-in type.
509QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 BuiltinType *baseType;
511
512 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000513 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000514
515 // Check if we've already instantiated a vector of this type.
516 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000517 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 void *InsertPos = 0;
519 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(VTP, 0);
521
522 // If the element type isn't canonical, this won't be a canonical type either,
523 // so fill in the canonical type field.
524 QualType Canonical;
525 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000526 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000527
528 // Get the new insert position for the node we care about.
529 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532 VectorType *New = new VectorType(vecType, NumElts, Canonical);
533 VectorTypes.InsertNode(New, InsertPos);
534 Types.push_back(New);
535 return QualType(New, 0);
536}
537
Steve Naroff73322922007-07-18 18:00:27 +0000538/// getOCUVectorType - Return the unique reference to an OCU vector type of
539/// the specified element type and size. VectorType must be a built-in type.
540QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
541 BuiltinType *baseType;
542
543 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
544 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
545
546 // Check if we've already instantiated a vector of this type.
547 llvm::FoldingSetNodeID ID;
548 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
549 void *InsertPos = 0;
550 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
551 return QualType(VTP, 0);
552
553 // If the element type isn't canonical, this won't be a canonical type either,
554 // so fill in the canonical type field.
555 QualType Canonical;
556 if (!vecType->isCanonical()) {
557 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
558
559 // Get the new insert position for the node we care about.
560 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
561 assert(NewIP == 0 && "Shouldn't be in the map!");
562 }
563 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
564 VectorTypes.InsertNode(New, InsertPos);
565 Types.push_back(New);
566 return QualType(New, 0);
567}
568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
570///
571QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
572 // Unique functions, to guarantee there is only one function of a particular
573 // structure.
574 llvm::FoldingSetNodeID ID;
575 FunctionTypeNoProto::Profile(ID, ResultTy);
576
577 void *InsertPos = 0;
578 if (FunctionTypeNoProto *FT =
579 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
580 return QualType(FT, 0);
581
582 QualType Canonical;
583 if (!ResultTy->isCanonical()) {
584 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
585
586 // Get the new insert position for the node we care about.
587 FunctionTypeNoProto *NewIP =
588 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
589 assert(NewIP == 0 && "Shouldn't be in the map!");
590 }
591
592 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
593 Types.push_back(New);
594 FunctionTypeProtos.InsertNode(New, InsertPos);
595 return QualType(New, 0);
596}
597
598/// getFunctionType - Return a normal function type with a typed argument
599/// list. isVariadic indicates whether the argument list includes '...'.
600QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
601 unsigned NumArgs, bool isVariadic) {
602 // Unique functions, to guarantee there is only one function of a particular
603 // structure.
604 llvm::FoldingSetNodeID ID;
605 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
606
607 void *InsertPos = 0;
608 if (FunctionTypeProto *FTP =
609 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
610 return QualType(FTP, 0);
611
612 // Determine whether the type being created is already canonical or not.
613 bool isCanonical = ResultTy->isCanonical();
614 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
615 if (!ArgArray[i]->isCanonical())
616 isCanonical = false;
617
618 // If this type isn't canonical, get the canonical version of it.
619 QualType Canonical;
620 if (!isCanonical) {
621 llvm::SmallVector<QualType, 16> CanonicalArgs;
622 CanonicalArgs.reserve(NumArgs);
623 for (unsigned i = 0; i != NumArgs; ++i)
624 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
625
626 Canonical = getFunctionType(ResultTy.getCanonicalType(),
627 &CanonicalArgs[0], NumArgs,
628 isVariadic);
629
630 // Get the new insert position for the node we care about.
631 FunctionTypeProto *NewIP =
632 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
633 assert(NewIP == 0 && "Shouldn't be in the map!");
634 }
635
636 // FunctionTypeProto objects are not allocated with new because they have a
637 // variable size array (for parameter types) at the end of them.
638 FunctionTypeProto *FTP =
639 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000640 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
642 Canonical);
643 Types.push_back(FTP);
644 FunctionTypeProtos.InsertNode(FTP, InsertPos);
645 return QualType(FTP, 0);
646}
647
648/// getTypedefType - Return the unique reference to the type for the
649/// specified typename decl.
650QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
651 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
652
653 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
654 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
655 Types.push_back(Decl->TypeForDecl);
656 return QualType(Decl->TypeForDecl, 0);
657}
658
Steve Naroff3536b442007-09-06 21:24:23 +0000659/// getObjcInterfaceType - Return the unique reference to the type for the
660/// specified ObjC interface decl.
661QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
662 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
663
664 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
665 Types.push_back(Decl->TypeForDecl);
666 return QualType(Decl->TypeForDecl, 0);
667}
668
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000669/// getObjcQualifiedInterfaceType - Return a
670/// ObjcQualifiedInterfaceType type for the given interface decl and
671/// the conforming protocol list.
672QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
673 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
674 ObjcInterfaceType *IType =
675 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
676
677 llvm::FoldingSetNodeID ID;
678 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
679
680 void *InsertPos = 0;
681 if (ObjcQualifiedInterfaceType *QT =
682 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
683 return QualType(QT, 0);
684
685 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000686 ObjcQualifiedInterfaceType *QType =
687 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000688 Types.push_back(QType);
689 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
690 return QualType(QType, 0);
691}
692
Steve Naroff9752f252007-08-01 18:02:17 +0000693/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
694/// TypeOfExpr AST's (since expression's are never shared). For example,
695/// multiple declarations that refer to "typeof(x)" all contain different
696/// DeclRefExpr's. This doesn't effect the type checker, since it operates
697/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000698QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000699 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000700 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
701 Types.push_back(toe);
702 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000703}
704
Steve Naroff9752f252007-08-01 18:02:17 +0000705/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
706/// TypeOfType AST's. The only motivation to unique these nodes would be
707/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
708/// an issue. This doesn't effect the type checker, since it operates
709/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000710QualType ASTContext::getTypeOfType(QualType tofType) {
711 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000712 TypeOfType *tot = new TypeOfType(tofType, Canonical);
713 Types.push_back(tot);
714 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000715}
716
Reid Spencer5f016e22007-07-11 17:01:13 +0000717/// getTagDeclType - Return the unique reference to the type for the
718/// specified TagDecl (struct/union/class/enum) decl.
719QualType ASTContext::getTagDeclType(TagDecl *Decl) {
720 // The decl stores the type cache.
721 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
722
723 Decl->TypeForDecl = new TagType(Decl, QualType());
724 Types.push_back(Decl->TypeForDecl);
725 return QualType(Decl->TypeForDecl, 0);
726}
727
728/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
729/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
730/// needs to agree with the definition in <stddef.h>.
731QualType ASTContext::getSizeType() const {
732 // On Darwin, size_t is defined as a "long unsigned int".
733 // FIXME: should derive from "Target".
734 return UnsignedLongTy;
735}
736
Chris Lattner8b9023b2007-07-13 03:05:23 +0000737/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
738/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
739QualType ASTContext::getPointerDiffType() const {
740 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
741 // FIXME: should derive from "Target".
742 return IntTy;
743}
744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
746/// routine will assert if passed a built-in type that isn't an integer or enum.
747static int getIntegerRank(QualType t) {
748 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
749 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
750 return 4;
751 }
752
753 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
754 switch (BT->getKind()) {
755 default:
756 assert(0 && "getIntegerRank(): not a built-in integer");
757 case BuiltinType::Bool:
758 return 1;
759 case BuiltinType::Char_S:
760 case BuiltinType::Char_U:
761 case BuiltinType::SChar:
762 case BuiltinType::UChar:
763 return 2;
764 case BuiltinType::Short:
765 case BuiltinType::UShort:
766 return 3;
767 case BuiltinType::Int:
768 case BuiltinType::UInt:
769 return 4;
770 case BuiltinType::Long:
771 case BuiltinType::ULong:
772 return 5;
773 case BuiltinType::LongLong:
774 case BuiltinType::ULongLong:
775 return 6;
776 }
777}
778
779/// getFloatingRank - Return a relative rank for floating point types.
780/// This routine will assert if passed a built-in type that isn't a float.
781static int getFloatingRank(QualType T) {
782 T = T.getCanonicalType();
783 if (ComplexType *CT = dyn_cast<ComplexType>(T))
784 return getFloatingRank(CT->getElementType());
785
786 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000787 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 case BuiltinType::Float: return FloatRank;
789 case BuiltinType::Double: return DoubleRank;
790 case BuiltinType::LongDouble: return LongDoubleRank;
791 }
792}
793
Steve Naroff716c7302007-08-27 01:41:48 +0000794/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
795/// point or a complex type (based on typeDomain/typeSize).
796/// 'typeDomain' is a real floating point or complex type.
797/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000798QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
799 QualType typeSize, QualType typeDomain) const {
800 if (typeDomain->isComplexType()) {
801 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000802 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000803 case FloatRank: return FloatComplexTy;
804 case DoubleRank: return DoubleComplexTy;
805 case LongDoubleRank: return LongDoubleComplexTy;
806 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Steve Narofff1448a02007-08-27 01:27:54 +0000808 if (typeDomain->isRealFloatingType()) {
809 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000810 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000811 case FloatRank: return FloatTy;
812 case DoubleRank: return DoubleTy;
813 case LongDoubleRank: return LongDoubleTy;
814 }
815 }
816 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000817 //an invalid return value, but the assert
818 //will ensure that this code is never reached.
819 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820}
821
Steve Narofffb0d4962007-08-27 15:30:22 +0000822/// compareFloatingType - Handles 3 different combos:
823/// float/float, float/complex, complex/complex.
824/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
825int ASTContext::compareFloatingType(QualType lt, QualType rt) {
826 if (getFloatingRank(lt) == getFloatingRank(rt))
827 return 0;
828 if (getFloatingRank(lt) > getFloatingRank(rt))
829 return 1;
830 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
833// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
834// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
835QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
836 if (lhs == rhs) return lhs;
837
838 bool t1Unsigned = lhs->isUnsignedIntegerType();
839 bool t2Unsigned = rhs->isUnsignedIntegerType();
840
841 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
842 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
843
844 // We have two integer types with differing signs
845 QualType unsignedType = t1Unsigned ? lhs : rhs;
846 QualType signedType = t1Unsigned ? rhs : lhs;
847
848 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
849 return unsignedType;
850 else {
851 // FIXME: Need to check if the signed type can represent all values of the
852 // unsigned type. If it can, then the result is the signed type.
853 // If it can't, then the result is the unsigned version of the signed type.
854 // Should probably add a helper that returns a signed integer type from
855 // an unsigned (and vice versa). C99 6.3.1.8.
856 return signedType;
857 }
858}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000859
860// getCFConstantStringType - Return the type used for constant CFStrings.
861QualType ASTContext::getCFConstantStringType() {
862 if (!CFConstantStringTypeDecl) {
863 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000864 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000865 0);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000866 QualType FieldTypes[3];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000867
868 // const int *isa;
869 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000870 // const char *str;
Steve Naroffbeaf2992007-11-03 11:27:19 +0000871 FieldTypes[1] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000872 // long length;
Steve Naroffbeaf2992007-11-03 11:27:19 +0000873 FieldTypes[2] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000874 // Create fields
Steve Naroffbeaf2992007-11-03 11:27:19 +0000875 FieldDecl *FieldDecls[3];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000876
Steve Naroffbeaf2992007-11-03 11:27:19 +0000877 for (unsigned i = 0; i < 3; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000878 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000879
880 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
881 }
882
883 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000884}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000885
Anders Carlssone8c49532007-10-29 06:33:42 +0000886// This returns true if a type has been typedefed to BOOL:
887// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000888static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000889 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000890 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000891
892 return false;
893}
894
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000895/// getObjcEncodingTypeSize returns size of type for objective-c encoding
896/// purpose.
897int ASTContext::getObjcEncodingTypeSize(QualType type) {
898 SourceLocation Loc;
899 uint64_t sz = getTypeSize(type, Loc);
900
901 // Make all integer and enum types at least as large as an int
902 if (sz > 0 && type->isIntegralType())
903 sz = std::max(sz, getTypeSize(IntTy, Loc));
904 // Treat arrays as pointers, since that's how they're passed in.
905 else if (type->isArrayType())
906 sz = getTypeSize(VoidPtrTy, Loc);
907 return sz / getTypeSize(CharTy, Loc);
908}
909
910/// getObjcEncodingForMethodDecl - Return the encoded type for this method
911/// declaration.
912void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
913 std::string& S)
914{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000915 // Encode type qualifer, 'in', 'inout', etc. for the return type.
916 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000917 // Encode result type.
918 getObjcEncodingForType(Decl->getResultType(), S);
919 // Compute size of all parameters.
920 // Start with computing size of a pointer in number of bytes.
921 // FIXME: There might(should) be a better way of doing this computation!
922 SourceLocation Loc;
923 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
924 // The first two arguments (self and _cmd) are pointers; account for
925 // their size.
926 int ParmOffset = 2 * PtrSize;
927 int NumOfParams = Decl->getNumParams();
928 for (int i = 0; i < NumOfParams; i++) {
929 QualType PType = Decl->getParamDecl(i)->getType();
930 int sz = getObjcEncodingTypeSize (PType);
931 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
932 ParmOffset += sz;
933 }
934 S += llvm::utostr(ParmOffset);
935 S += "@0:";
936 S += llvm::utostr(PtrSize);
937
938 // Argument types.
939 ParmOffset = 2 * PtrSize;
940 for (int i = 0; i < NumOfParams; i++) {
941 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000942 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000943 // 'in', 'inout', etc.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000944 getObjcEncodingForTypeQualifier(
945 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000946 getObjcEncodingForType(PType, S);
947 S += llvm::utostr(ParmOffset);
948 ParmOffset += getObjcEncodingTypeSize(PType);
949 }
950}
951
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000952void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
953{
Anders Carlssone8c49532007-10-29 06:33:42 +0000954 // FIXME: This currently doesn't encode:
955 // @ An object (whether statically typed or typed id)
956 // # A class object (Class)
957 // : A method selector (SEL)
958 // {name=type...} A structure
959 // (name=type...) A union
960 // bnum A bit field of num bits
961
962 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000963 char encoding;
964 switch (BT->getKind()) {
965 case BuiltinType::Void:
966 encoding = 'v';
967 break;
968 case BuiltinType::Bool:
969 encoding = 'B';
970 break;
971 case BuiltinType::Char_U:
972 case BuiltinType::UChar:
973 encoding = 'C';
974 break;
975 case BuiltinType::UShort:
976 encoding = 'S';
977 break;
978 case BuiltinType::UInt:
979 encoding = 'I';
980 break;
981 case BuiltinType::ULong:
982 encoding = 'L';
983 break;
984 case BuiltinType::ULongLong:
985 encoding = 'Q';
986 break;
987 case BuiltinType::Char_S:
988 case BuiltinType::SChar:
989 encoding = 'c';
990 break;
991 case BuiltinType::Short:
992 encoding = 's';
993 break;
994 case BuiltinType::Int:
995 encoding = 'i';
996 break;
997 case BuiltinType::Long:
998 encoding = 'l';
999 break;
1000 case BuiltinType::LongLong:
1001 encoding = 'q';
1002 break;
1003 case BuiltinType::Float:
1004 encoding = 'f';
1005 break;
1006 case BuiltinType::Double:
1007 encoding = 'd';
1008 break;
1009 case BuiltinType::LongDouble:
1010 encoding = 'd';
1011 break;
1012 default:
1013 assert(0 && "Unhandled builtin type kind");
1014 }
1015
1016 S += encoding;
Anders Carlssone8c49532007-10-29 06:33:42 +00001017 } else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001018 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson8baaca52007-10-31 02:53:19 +00001019 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001020 S += '@';
1021 return;
Anders Carlsson8baaca52007-10-31 02:53:19 +00001022 } else if (isObjcClassType(PointeeTy)) {
1023 S += '#';
1024 return;
1025 } else if (isObjcSelType(PointeeTy)) {
1026 S += ':';
1027 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001028 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001029
1030 if (PointeeTy->isCharType()) {
1031 // char pointer types should be encoded as '*' unless it is a
1032 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001033 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001034 S += '*';
1035 return;
1036 }
1037 }
1038
1039 S += '^';
1040 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +00001041 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001042 S += '[';
1043
1044 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1045 S += llvm::utostr(CAT->getSize().getZExtValue());
1046 else
1047 assert(0 && "Unhandled array type!");
1048
1049 getObjcEncodingForType(AT->getElementType(), S);
1050 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001051 } else if (T->getAsFunctionType()) {
1052 S += '?';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001053 } else
Anders Carlssone8c49532007-10-29 06:33:42 +00001054 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001055}
1056
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001057void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1058 std::string& S) const {
1059 if (QT & Decl::OBJC_TQ_In)
1060 S += 'n';
1061 if (QT & Decl::OBJC_TQ_Inout)
1062 S += 'N';
1063 if (QT & Decl::OBJC_TQ_Out)
1064 S += 'o';
1065 if (QT & Decl::OBJC_TQ_Bycopy)
1066 S += 'O';
1067 if (QT & Decl::OBJC_TQ_Byref)
1068 S += 'R';
1069 if (QT & Decl::OBJC_TQ_Oneway)
1070 S += 'V';
1071}
1072
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001073void ASTContext::setBuiltinVaListType(QualType T)
1074{
1075 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1076
1077 BuiltinVaListType = T;
1078}
1079
Steve Naroff7e219e42007-10-15 14:41:52 +00001080void ASTContext::setObjcIdType(TypedefDecl *TD)
1081{
1082 assert(ObjcIdType.isNull() && "'id' type already set!");
1083
1084 ObjcIdType = getTypedefType(TD);
1085
1086 // typedef struct objc_object *id;
1087 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1088 assert(ptr && "'id' incorrectly typed");
1089 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1090 assert(rec && "'id' incorrectly typed");
1091 IdStructType = rec;
1092}
1093
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001094void ASTContext::setObjcSelType(TypedefDecl *TD)
1095{
1096 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1097
1098 ObjcSelType = getTypedefType(TD);
1099
1100 // typedef struct objc_selector *SEL;
1101 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1102 assert(ptr && "'SEL' incorrectly typed");
1103 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1104 assert(rec && "'SEL' incorrectly typed");
1105 SelStructType = rec;
1106}
1107
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001108void ASTContext::setObjcProtoType(TypedefDecl *TD)
1109{
1110 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
1111
1112 // typedef struct Protocol Protocol;
1113 ObjcProtoType = TD->getUnderlyingType();
1114 // Protocol * type
1115 ObjcProtoType = getPointerType(ObjcProtoType);
1116 ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
1117}
1118
Anders Carlsson8baaca52007-10-31 02:53:19 +00001119void ASTContext::setObjcClassType(TypedefDecl *TD)
1120{
1121 assert(ObjcClassType.isNull() && "'Class' type already set!");
1122
1123 ObjcClassType = getTypedefType(TD);
1124
1125 // typedef struct objc_class *Class;
1126 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1127 assert(ptr && "'Class' incorrectly typed");
1128 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1129 assert(rec && "'Class' incorrectly typed");
1130 ClassStructType = rec;
1131}
1132
Steve Naroff21988912007-10-15 23:35:17 +00001133void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1134 assert(ObjcConstantStringType.isNull() &&
1135 "'NSConstantString' type already set!");
1136
1137 ObjcConstantStringType = getObjcInterfaceType(Decl);
1138}
1139
Steve Naroffec0550f2007-10-15 20:41:53 +00001140bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1141 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1142 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1143
1144 return lBuiltin->getKind() == rBuiltin->getKind();
1145}
1146
1147
1148bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1149 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1150 return true;
1151 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1152 return true;
1153 return false;
1154}
1155
1156bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1157 return true; // FIXME: IMPLEMENT.
1158}
1159
Chris Lattner770951b2007-11-01 05:03:41 +00001160bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1161 const VectorType *lVector = lhs->getAsVectorType();
1162 const VectorType *rVector = rhs->getAsVectorType();
1163
1164 if ((lVector->getElementType().getCanonicalType() ==
1165 rVector->getElementType().getCanonicalType()) &&
1166 (lVector->getNumElements() == rVector->getNumElements()))
1167 return true;
1168 return false;
1169}
1170
Steve Naroffec0550f2007-10-15 20:41:53 +00001171// C99 6.2.7p1: If both are complete types, then the following additional
1172// requirements apply...FIXME (handle compatibility across source files).
1173bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1174 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1175 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1176
1177 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1178 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1179 return true;
1180 }
1181 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1182 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1183 return true;
1184 }
Steve Naroffab373092007-11-07 06:03:51 +00001185 // "Class" and "id" are compatible built-in structure types.
1186 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1187 isObjcClassType(lhs) && isObjcIdType(rhs))
1188 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001189 return false;
1190}
1191
1192bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1193 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1194 // identically qualified and both shall be pointers to compatible types.
1195 if (lhs.getQualifiers() != rhs.getQualifiers())
1196 return false;
1197
1198 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1199 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1200
1201 return typesAreCompatible(ltype, rtype);
1202}
1203
1204// C++ 5.17p6: When the left opperand of an assignment operator denotes a
1205// reference to T, the operation assigns to the object of type T denoted by the
1206// reference.
1207bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1208 QualType ltype = lhs;
1209
1210 if (lhs->isReferenceType())
1211 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1212
1213 QualType rtype = rhs;
1214
1215 if (rhs->isReferenceType())
1216 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1217
1218 return typesAreCompatible(ltype, rtype);
1219}
1220
1221bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1222 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1223 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1224 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1225 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1226
1227 // first check the return types (common between C99 and K&R).
1228 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1229 return false;
1230
1231 if (lproto && rproto) { // two C99 style function prototypes
1232 unsigned lproto_nargs = lproto->getNumArgs();
1233 unsigned rproto_nargs = rproto->getNumArgs();
1234
1235 if (lproto_nargs != rproto_nargs)
1236 return false;
1237
1238 // both prototypes have the same number of arguments.
1239 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1240 (rproto->isVariadic() && !lproto->isVariadic()))
1241 return false;
1242
1243 // The use of ellipsis agree...now check the argument types.
1244 for (unsigned i = 0; i < lproto_nargs; i++)
1245 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1246 return false;
1247 return true;
1248 }
1249 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1250 return true;
1251
1252 // we have a mixture of K&R style with C99 prototypes
1253 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1254
1255 if (proto->isVariadic())
1256 return false;
1257
1258 // FIXME: Each parameter type T in the prototype must be compatible with the
1259 // type resulting from applying the usual argument conversions to T.
1260 return true;
1261}
1262
1263bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1264 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1265 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1266
1267 if (!typesAreCompatible(ltype, rtype))
1268 return false;
1269
1270 // FIXME: If both types specify constant sizes, then the sizes must also be
1271 // the same. Even if the sizes are the same, GCC produces an error.
1272 return true;
1273}
1274
1275/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1276/// both shall have the identically qualified version of a compatible type.
1277/// C99 6.2.7p1: Two types have compatible types if their types are the
1278/// same. See 6.7.[2,3,5] for additional rules.
1279bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1280 QualType lcanon = lhs.getCanonicalType();
1281 QualType rcanon = rhs.getCanonicalType();
1282
1283 // If two types are identical, they are are compatible
1284 if (lcanon == rcanon)
1285 return true;
1286
1287 // If the canonical type classes don't match, they can't be compatible
1288 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1289 // For Objective-C, it is possible for two types to be compatible
1290 // when their classes don't match (when dealing with "id"). If either type
1291 // is an interface, we defer to objcTypesAreCompatible().
1292 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1293 return objcTypesAreCompatible(lcanon, rcanon);
1294 return false;
1295 }
1296 switch (lcanon->getTypeClass()) {
1297 case Type::Pointer:
1298 return pointerTypesAreCompatible(lcanon, rcanon);
1299 case Type::Reference:
1300 return referenceTypesAreCompatible(lcanon, rcanon);
1301 case Type::ConstantArray:
1302 case Type::VariableArray:
1303 return arrayTypesAreCompatible(lcanon, rcanon);
1304 case Type::FunctionNoProto:
1305 case Type::FunctionProto:
1306 return functionTypesAreCompatible(lcanon, rcanon);
1307 case Type::Tagged: // handle structures, unions
1308 return tagTypesAreCompatible(lcanon, rcanon);
1309 case Type::Builtin:
1310 return builtinTypesAreCompatible(lcanon, rcanon);
1311 case Type::ObjcInterface:
1312 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001313 case Type::Vector:
1314 case Type::OCUVector:
1315 return vectorTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001316 default:
1317 assert(0 && "unexpected type");
1318 }
1319 return true; // should never get here...
1320}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001321
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001322/// Emit - Serialize an ASTContext object to Bitcode.
1323void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001324 S.EmitRef(SourceMgr);
1325 S.EmitRef(Target);
1326 S.EmitRef(Idents);
1327 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001328
Ted Kremenekfee04522007-10-31 22:44:07 +00001329 // Emit the size of the type vector so that we can reserve that size
1330 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001331 S.EmitInt(Types.size());
1332
Ted Kremenek03ed4402007-11-13 22:02:55 +00001333 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1334 I!=E;++I)
1335 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001336
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001337 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001338}
1339
Ted Kremenek0f84c002007-11-13 00:25:37 +00001340ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001341 SourceManager &SM = D.ReadRef<SourceManager>();
1342 TargetInfo &t = D.ReadRef<TargetInfo>();
1343 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1344 SelectorTable &sels = D.ReadRef<SelectorTable>();
1345
1346 unsigned size_reserve = D.ReadInt();
1347
1348 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1349
Ted Kremenek03ed4402007-11-13 22:02:55 +00001350 for (unsigned i = 0; i < size_reserve; ++i)
1351 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001352
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001353 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001354
1355 return A;
1356}