blob: 187871b32e00763256bb44e6a0aae555acb6ee1e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000047 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000062 else if (isa<ComplexType>(T))
63 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 else if (isa<ArrayType>(T))
65 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000066 else if (isa<VectorType>(T))
67 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff3f128ad2007-09-17 14:16:13 +000089 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000098 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000100 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattner464175b2007-07-18 17:52:12 +0000176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000182std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000184 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000185 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000186 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000187 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000188 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000191 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000192 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner98be4942008-03-05 18:54:05 +0000198 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000199 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000200 Align = EltInfo.second;
201 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000202 }
203 case Type::OCUVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000206 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000207 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner030d8842007-07-19 22:06:24 +0000208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000212
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000213 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
216 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000217 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000218 case BuiltinType::Void:
219 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000220 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000221 Width = Target.getBoolWidth();
222 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000223 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000227 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000228 Width = Target.getCharWidth();
229 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000230 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000231 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000233 Width = Target.getShortWidth();
234 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000235 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000236 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000237 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000238 Width = Target.getIntWidth();
239 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000241 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000242 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000243 Width = Target.getLongWidth();
244 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000245 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000246 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000247 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000248 Width = Target.getLongLongWidth();
249 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000250 break;
251 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000252 Width = Target.getFloatWidth();
253 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000254 break;
255 case BuiltinType::Double:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000256 Width = Target.getDoubleWidth();
257 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000258 break;
259 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000260 Width = Target.getLongDoubleWidth();
261 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000262 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000263 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000264 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000266 // FIXME: Pointers into different addr spaces could have different sizes and
267 // alignment requirements: getPointerInfo should take an AddrSpace.
268 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000269 case Type::ObjCQualifiedId:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000270 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000271 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000272 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000273 case Type::Pointer: {
274 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000275 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000276 Align = Target.getPointerAlign(AS);
277 break;
278 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000279 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000280 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000281 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000282 // FIXME: This is wrong for struct layout: a reference in a struct has
283 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000284 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000285
286 case Type::Complex: {
287 // Complex types have the same alignment as their elements, but twice the
288 // size.
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000290 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000292 Align = EltInfo.second;
293 break;
294 }
Chris Lattner71763312008-04-06 22:05:18 +0000295 case Type::Tagged: {
296 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
297 return getTypeInfo(ET->getDecl()->getIntegerType());
298
299 RecordType *RT = cast<RecordType>(T);
300 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
301 Width = Layout.getSize();
302 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000303 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000304 }
Chris Lattner71763312008-04-06 22:05:18 +0000305 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000306
Chris Lattner464175b2007-07-18 17:52:12 +0000307 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000308 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000309}
310
Devang Patel88a981b2007-11-01 19:11:01 +0000311/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000312/// specified record (struct/union/class), which indicates its size and field
313/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000314const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000315 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
316
317 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000318 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000319 if (Entry) return *Entry;
320
Devang Patel88a981b2007-11-01 19:11:01 +0000321 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
322 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
323 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000324 Entry = NewEntry;
325
326 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
327 uint64_t RecordSize = 0;
328 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
329
330 if (D->getKind() != Decl::Union) {
Anders Carlsson042c4e72008-02-16 19:51:27 +0000331 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
332 RecordAlign = std::max(RecordAlign, AA->getAlignment());
333
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000334 bool StructIsPacked = D->getAttr<PackedAttr>();
335
Chris Lattner464175b2007-07-18 17:52:12 +0000336 // Layout each field, for now, just sequentially, respecting alignment. In
337 // the future, this will need to be tweakable by targets.
338 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
339 const FieldDecl *FD = D->getMember(i);
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000340 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman75afb582008-02-06 05:33:51 +0000341 uint64_t FieldSize;
342 unsigned FieldAlign;
Anders Carlsson8af226a2008-02-18 07:13:09 +0000343
344 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
345 llvm::APSInt I(32);
346 bool BitWidthIsICE =
347 BitWidthExpr->isIntegerConstantExpr(I, *this);
348 assert (BitWidthIsICE && "Invalid BitField size expression");
349 FieldSize = I.getZExtValue();
350
Chris Lattner98be4942008-03-05 18:54:05 +0000351 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000352 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson042c4e72008-02-16 19:51:27 +0000353
354 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
355 FieldAlign = AA->getAlignment();
356 else if (FieldIsPacked)
357 FieldAlign = 8;
358 else {
Anders Carlsson8af226a2008-02-18 07:13:09 +0000359 // FIXME: This is X86 specific, use 32-bit alignment for long long.
360 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
361 FieldAlign = 32;
362 else
363 FieldAlign = TypeInfo.second;
Anders Carlsson042c4e72008-02-16 19:51:27 +0000364 }
Eli Friedman75afb582008-02-06 05:33:51 +0000365
Anders Carlsson8af226a2008-02-18 07:13:09 +0000366 // Check if we need to add padding to give the field the correct
367 // alignment.
368 if (RecordSize % FieldAlign + FieldSize > TypeSize)
369 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
370
371 } else {
372 if (FD->getType()->isIncompleteType()) {
373 // This must be a flexible array member; we can't directly
374 // query getTypeInfo about these, so we figure it out here.
375 // Flexible array members don't have any size, but they
376 // have to be aligned appropriately for their element type.
377
378 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
379 FieldAlign = AA->getAlignment();
380 else if (FieldIsPacked)
381 FieldAlign = 8;
382 else {
383 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner98be4942008-03-05 18:54:05 +0000384 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000385 }
386 FieldSize = 0;
387 } else {
Chris Lattner98be4942008-03-05 18:54:05 +0000388 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson8af226a2008-02-18 07:13:09 +0000389 FieldSize = FieldInfo.first;
390
391 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
392 FieldAlign = AA->getAlignment();
393 else if (FieldIsPacked)
394 FieldAlign = 8;
395 else
396 FieldAlign = FieldInfo.second;
397 }
398
399 // Round up the current record size to the field's alignment boundary.
400 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
401 }
Chris Lattner464175b2007-07-18 17:52:12 +0000402
403 // Place this field at the current location.
404 FieldOffsets[i] = RecordSize;
405
406 // Reserve space for this field.
407 RecordSize += FieldSize;
408
409 // Remember max struct/class alignment.
410 RecordAlign = std::max(RecordAlign, FieldAlign);
411 }
412
413 // Finally, round the size of the total struct up to the alignment of the
414 // struct itself.
415 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
416 } else {
417 // Union layout just puts each member at the start of the record.
418 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
419 const FieldDecl *FD = D->getMember(i);
Chris Lattner98be4942008-03-05 18:54:05 +0000420 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner464175b2007-07-18 17:52:12 +0000421 uint64_t FieldSize = FieldInfo.first;
422 unsigned FieldAlign = FieldInfo.second;
423
Anders Carlsson8af226a2008-02-18 07:13:09 +0000424 // FIXME: This is X86 specific, use 32-bit alignment for long long.
425 if (FD->getType()->isIntegerType() && FieldAlign > 32)
426 FieldAlign = 32;
427
Chris Lattner464175b2007-07-18 17:52:12 +0000428 // Round up the current record size to the field's alignment boundary.
429 RecordSize = std::max(RecordSize, FieldSize);
430
431 // Place this field at the start of the record.
432 FieldOffsets[i] = 0;
433
434 // Remember max struct/class alignment.
435 RecordAlign = std::max(RecordAlign, FieldAlign);
436 }
437 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000438
439 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
440 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000441}
442
Chris Lattnera7674d82007-07-13 22:13:22 +0000443//===----------------------------------------------------------------------===//
444// Type creation/memoization methods
445//===----------------------------------------------------------------------===//
446
Christopher Lambebb97e92008-02-04 02:31:56 +0000447QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000448 QualType CanT = getCanonicalType(T);
449 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000450 return T;
451
452 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
453 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000454 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000455 "Type is already address space qualified");
456
457 // Check if we've already instantiated an address space qual'd type of this
458 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000459 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000460 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000461 void *InsertPos = 0;
462 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
463 return QualType(ASQy, 0);
464
465 // If the base type isn't canonical, this won't be a canonical type either,
466 // so fill in the canonical type field.
467 QualType Canonical;
468 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000469 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000470
471 // Get the new insert position for the node we care about.
472 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
473 assert(NewIP == 0 && "Shouldn't be in the map!");
474 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000475 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000476 ASQualTypes.InsertNode(New, InsertPos);
477 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000478 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000479}
480
Chris Lattnera7674d82007-07-13 22:13:22 +0000481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482/// getComplexType - Return the uniqued reference to the type for a complex
483/// number with the specified element type.
484QualType ASTContext::getComplexType(QualType T) {
485 // Unique pointers, to guarantee there is only one pointer of a particular
486 // structure.
487 llvm::FoldingSetNodeID ID;
488 ComplexType::Profile(ID, T);
489
490 void *InsertPos = 0;
491 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
492 return QualType(CT, 0);
493
494 // If the pointee type isn't canonical, this won't be a canonical type either,
495 // so fill in the canonical type field.
496 QualType Canonical;
497 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000498 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000499
500 // Get the new insert position for the node we care about.
501 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
502 assert(NewIP == 0 && "Shouldn't be in the map!");
503 }
504 ComplexType *New = new ComplexType(T, Canonical);
505 Types.push_back(New);
506 ComplexTypes.InsertNode(New, InsertPos);
507 return QualType(New, 0);
508}
509
510
511/// getPointerType - Return the uniqued reference to the type for a pointer to
512/// the specified type.
513QualType ASTContext::getPointerType(QualType T) {
514 // Unique pointers, to guarantee there is only one pointer of a particular
515 // structure.
516 llvm::FoldingSetNodeID ID;
517 PointerType::Profile(ID, T);
518
519 void *InsertPos = 0;
520 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
521 return QualType(PT, 0);
522
523 // If the pointee type isn't canonical, this won't be a canonical type either,
524 // so fill in the canonical type field.
525 QualType Canonical;
526 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000527 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000528
529 // Get the new insert position for the node we care about.
530 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
531 assert(NewIP == 0 && "Shouldn't be in the map!");
532 }
533 PointerType *New = new PointerType(T, Canonical);
534 Types.push_back(New);
535 PointerTypes.InsertNode(New, InsertPos);
536 return QualType(New, 0);
537}
538
539/// getReferenceType - Return the uniqued reference to the type for a reference
540/// to the specified type.
541QualType ASTContext::getReferenceType(QualType T) {
542 // Unique pointers, to guarantee there is only one pointer of a particular
543 // structure.
544 llvm::FoldingSetNodeID ID;
545 ReferenceType::Profile(ID, T);
546
547 void *InsertPos = 0;
548 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
549 return QualType(RT, 0);
550
551 // If the referencee type isn't canonical, this won't be a canonical type
552 // either, so fill in the canonical type field.
553 QualType Canonical;
554 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000555 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
557 // Get the new insert position for the node we care about.
558 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
559 assert(NewIP == 0 && "Shouldn't be in the map!");
560 }
561
562 ReferenceType *New = new ReferenceType(T, Canonical);
563 Types.push_back(New);
564 ReferenceTypes.InsertNode(New, InsertPos);
565 return QualType(New, 0);
566}
567
Steve Narofffb22d962007-08-30 01:06:46 +0000568/// getConstantArrayType - Return the unique reference to the type for an
569/// array of the specified element type.
570QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000571 const llvm::APInt &ArySize,
572 ArrayType::ArraySizeModifier ASM,
573 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000575 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000576
577 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000578 if (ConstantArrayType *ATP =
579 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 return QualType(ATP, 0);
581
582 // If the element type isn't canonical, this won't be a canonical type either,
583 // so fill in the canonical type field.
584 QualType Canonical;
585 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000586 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000587 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000589 ConstantArrayType *NewIP =
590 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 assert(NewIP == 0 && "Shouldn't be in the map!");
593 }
594
Steve Naroffc9406122007-08-30 18:10:14 +0000595 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
596 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000597 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 Types.push_back(New);
599 return QualType(New, 0);
600}
601
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000602/// getVariableArrayType - Returns a non-unique reference to the type for a
603/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000604QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
605 ArrayType::ArraySizeModifier ASM,
606 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000607 // Since we don't unique expressions, it isn't possible to unique VLA's
608 // that have an expression provided for their size.
609
610 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
611 ASM, EltTypeQuals);
612
613 VariableArrayTypes.push_back(New);
614 Types.push_back(New);
615 return QualType(New, 0);
616}
617
618QualType ASTContext::getIncompleteArrayType(QualType EltTy,
619 ArrayType::ArraySizeModifier ASM,
620 unsigned EltTypeQuals) {
621 llvm::FoldingSetNodeID ID;
622 IncompleteArrayType::Profile(ID, EltTy);
623
624 void *InsertPos = 0;
625 if (IncompleteArrayType *ATP =
626 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
627 return QualType(ATP, 0);
628
629 // If the element type isn't canonical, this won't be a canonical type
630 // either, so fill in the canonical type field.
631 QualType Canonical;
632
633 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000634 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000635 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000636
637 // Get the new insert position for the node we care about.
638 IncompleteArrayType *NewIP =
639 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
640
641 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000642 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000643
644 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
645 ASM, EltTypeQuals);
646
647 IncompleteArrayTypes.InsertNode(New, InsertPos);
648 Types.push_back(New);
649 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000650}
651
Steve Naroff73322922007-07-18 18:00:27 +0000652/// getVectorType - Return the unique reference to a vector type of
653/// the specified element type and size. VectorType must be a built-in type.
654QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 BuiltinType *baseType;
656
Chris Lattnerf52ab252008-04-06 22:59:24 +0000657 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000658 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000659
660 // Check if we've already instantiated a vector of this type.
661 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000662 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 void *InsertPos = 0;
664 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
665 return QualType(VTP, 0);
666
667 // If the element type isn't canonical, this won't be a canonical type either,
668 // so fill in the canonical type field.
669 QualType Canonical;
670 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000671 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672
673 // Get the new insert position for the node we care about.
674 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
675 assert(NewIP == 0 && "Shouldn't be in the map!");
676 }
677 VectorType *New = new VectorType(vecType, NumElts, Canonical);
678 VectorTypes.InsertNode(New, InsertPos);
679 Types.push_back(New);
680 return QualType(New, 0);
681}
682
Steve Naroff73322922007-07-18 18:00:27 +0000683/// getOCUVectorType - Return the unique reference to an OCU vector type of
684/// the specified element type and size. VectorType must be a built-in type.
685QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
686 BuiltinType *baseType;
687
Chris Lattnerf52ab252008-04-06 22:59:24 +0000688 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000689 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
690
691 // Check if we've already instantiated a vector of this type.
692 llvm::FoldingSetNodeID ID;
693 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
694 void *InsertPos = 0;
695 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
696 return QualType(VTP, 0);
697
698 // If the element type isn't canonical, this won't be a canonical type either,
699 // so fill in the canonical type field.
700 QualType Canonical;
701 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000702 Canonical = getOCUVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000703
704 // Get the new insert position for the node we care about.
705 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
706 assert(NewIP == 0 && "Shouldn't be in the map!");
707 }
708 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
709 VectorTypes.InsertNode(New, InsertPos);
710 Types.push_back(New);
711 return QualType(New, 0);
712}
713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
715///
716QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
717 // Unique functions, to guarantee there is only one function of a particular
718 // structure.
719 llvm::FoldingSetNodeID ID;
720 FunctionTypeNoProto::Profile(ID, ResultTy);
721
722 void *InsertPos = 0;
723 if (FunctionTypeNoProto *FT =
724 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
725 return QualType(FT, 0);
726
727 QualType Canonical;
728 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000729 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000730
731 // Get the new insert position for the node we care about.
732 FunctionTypeNoProto *NewIP =
733 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
734 assert(NewIP == 0 && "Shouldn't be in the map!");
735 }
736
737 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
738 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000739 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return QualType(New, 0);
741}
742
743/// getFunctionType - Return a normal function type with a typed argument
744/// list. isVariadic indicates whether the argument list includes '...'.
745QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
746 unsigned NumArgs, bool isVariadic) {
747 // Unique functions, to guarantee there is only one function of a particular
748 // structure.
749 llvm::FoldingSetNodeID ID;
750 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
751
752 void *InsertPos = 0;
753 if (FunctionTypeProto *FTP =
754 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
755 return QualType(FTP, 0);
756
757 // Determine whether the type being created is already canonical or not.
758 bool isCanonical = ResultTy->isCanonical();
759 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
760 if (!ArgArray[i]->isCanonical())
761 isCanonical = false;
762
763 // If this type isn't canonical, get the canonical version of it.
764 QualType Canonical;
765 if (!isCanonical) {
766 llvm::SmallVector<QualType, 16> CanonicalArgs;
767 CanonicalArgs.reserve(NumArgs);
768 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000769 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000770
Chris Lattnerf52ab252008-04-06 22:59:24 +0000771 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 &CanonicalArgs[0], NumArgs,
773 isVariadic);
774
775 // Get the new insert position for the node we care about.
776 FunctionTypeProto *NewIP =
777 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
778 assert(NewIP == 0 && "Shouldn't be in the map!");
779 }
780
781 // FunctionTypeProto objects are not allocated with new because they have a
782 // variable size array (for parameter types) at the end of them.
783 FunctionTypeProto *FTP =
784 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000785 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
787 Canonical);
788 Types.push_back(FTP);
789 FunctionTypeProtos.InsertNode(FTP, InsertPos);
790 return QualType(FTP, 0);
791}
792
793/// getTypedefType - Return the unique reference to the type for the
794/// specified typename decl.
795QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
796 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
797
Chris Lattnerf52ab252008-04-06 22:59:24 +0000798 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000799 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 Types.push_back(Decl->TypeForDecl);
801 return QualType(Decl->TypeForDecl, 0);
802}
803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000805/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000807 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
808
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000810 Types.push_back(Decl->TypeForDecl);
811 return QualType(Decl->TypeForDecl, 0);
812}
813
Chris Lattner88cb27a2008-04-07 04:56:42 +0000814/// CmpProtocolNames - Comparison predicate for sorting protocols
815/// alphabetically.
816static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
817 const ObjCProtocolDecl *RHS) {
818 return strcmp(LHS->getName(), RHS->getName()) < 0;
819}
820
821static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
822 unsigned &NumProtocols) {
823 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
824
825 // Sort protocols, keyed by name.
826 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
827
828 // Remove duplicates.
829 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
830 NumProtocols = ProtocolsEnd-Protocols;
831}
832
833
Chris Lattner065f0d72008-04-07 04:44:08 +0000834/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
835/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
837 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000838 // Sort the protocol list alphabetically to canonicalize it.
839 SortAndUniqueProtocols(Protocols, NumProtocols);
840
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000841 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000842 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000843
844 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000845 if (ObjCQualifiedInterfaceType *QT =
846 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000847 return QualType(QT, 0);
848
849 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 ObjCQualifiedInterfaceType *QType =
851 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000852 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000854 return QualType(QType, 0);
855}
856
Chris Lattner88cb27a2008-04-07 04:56:42 +0000857/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
858/// and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000859QualType ASTContext::getObjCQualifiedIdType(QualType idType,
860 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000861 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000862 // Sort the protocol list alphabetically to canonicalize it.
863 SortAndUniqueProtocols(Protocols, NumProtocols);
864
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000865 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000867
868 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869 if (ObjCQualifiedIdType *QT =
870 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000871 return QualType(QT, 0);
872
873 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000874 QualType Canonical;
875 if (!idType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000876 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000877 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000878 ObjCQualifiedIdType *NewQT =
879 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000880 assert(NewQT == 0 && "Shouldn't be in the map!");
881 }
882
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883 ObjCQualifiedIdType *QType =
884 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000885 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000887 return QualType(QType, 0);
888}
889
Steve Naroff9752f252007-08-01 18:02:17 +0000890/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
891/// TypeOfExpr AST's (since expression's are never shared). For example,
892/// multiple declarations that refer to "typeof(x)" all contain different
893/// DeclRefExpr's. This doesn't effect the type checker, since it operates
894/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000895QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000896 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +0000897 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
898 Types.push_back(toe);
899 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000900}
901
Steve Naroff9752f252007-08-01 18:02:17 +0000902/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
903/// TypeOfType AST's. The only motivation to unique these nodes would be
904/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
905/// an issue. This doesn't effect the type checker, since it operates
906/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000907QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000908 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +0000909 TypeOfType *tot = new TypeOfType(tofType, Canonical);
910 Types.push_back(tot);
911 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000912}
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914/// getTagDeclType - Return the unique reference to the type for the
915/// specified TagDecl (struct/union/class/enum) decl.
916QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000917 assert (Decl);
918
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000919 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000920 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000921
922 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000923 Types.push_back(T);
924 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000925
926 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
928
929/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
930/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
931/// needs to agree with the definition in <stddef.h>.
932QualType ASTContext::getSizeType() const {
933 // On Darwin, size_t is defined as a "long unsigned int".
934 // FIXME: should derive from "Target".
935 return UnsignedLongTy;
936}
937
Eli Friedmanfd888a52008-02-12 08:29:21 +0000938/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
939/// width of characters in wide strings, The value is target dependent and
940/// needs to agree with the definition in <stddef.h>.
941QualType ASTContext::getWcharType() const {
942 // On Darwin, wchar_t is defined as a "int".
943 // FIXME: should derive from "Target".
944 return IntTy;
945}
946
Chris Lattner8b9023b2007-07-13 03:05:23 +0000947/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
948/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
949QualType ASTContext::getPointerDiffType() const {
950 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
951 // FIXME: should derive from "Target".
952 return IntTy;
953}
954
Chris Lattnere6327742008-04-02 05:18:44 +0000955//===----------------------------------------------------------------------===//
956// Type Operators
957//===----------------------------------------------------------------------===//
958
Chris Lattner77c96472008-04-06 22:41:35 +0000959/// getCanonicalType - Return the canonical (structural) type corresponding to
960/// the specified potentially non-canonical type. The non-canonical version
961/// of a type may have many "decorated" versions of types. Decorators can
962/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
963/// to be free of any of these, allowing two canonical types to be compared
964/// for exact equality with a simple pointer comparison.
965QualType ASTContext::getCanonicalType(QualType T) {
966 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
967 return QualType(CanType.getTypePtr(),
968 T.getCVRQualifiers() | CanType.getCVRQualifiers());
969}
970
971
Chris Lattnere6327742008-04-02 05:18:44 +0000972/// getArrayDecayedType - Return the properly qualified result of decaying the
973/// specified array type to a pointer. This operation is non-trivial when
974/// handling typedefs etc. The canonical type of "T" must be an array type,
975/// this returns a pointer to a properly qualified element of the array.
976///
977/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
978QualType ASTContext::getArrayDecayedType(QualType Ty) {
979 // Handle the common case where typedefs are not involved directly.
980 QualType EltTy;
981 unsigned ArrayQuals = 0;
982 unsigned PointerQuals = 0;
983 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
984 // Since T "isa" an array type, it could not have had an address space
985 // qualifier, just CVR qualifiers. The properly qualified element pointer
986 // gets the union of the CVR qualifiers from the element and the array, and
987 // keeps any address space qualifier on the element type if present.
988 EltTy = AT->getElementType();
989 ArrayQuals = Ty.getCVRQualifiers();
990 PointerQuals = AT->getIndexTypeQualifier();
991 } else {
992 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
993 // lose qualifiers when dealing with typedefs. Example:
994 // typedef int arr[10];
995 // void test2() {
996 // const arr b;
997 // b[4] = 1;
998 // }
999 //
1000 // The decayed type of b is "const int*" even though the element type of the
1001 // array is "int".
Chris Lattnerf52ab252008-04-06 22:59:24 +00001002 QualType CanTy = getCanonicalType(Ty);
Chris Lattnere6327742008-04-02 05:18:44 +00001003 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1004 assert(PrettyArrayType && "Not an array type!");
1005
1006 // Get the element type with 'getAsArrayType' so that we don't lose any
1007 // typedefs in the element type of the array.
1008 EltTy = PrettyArrayType->getElementType();
1009
1010 // If the array was address-space qualifier, make sure to ASQual the element
1011 // type. We can just grab the address space from the canonical type.
1012 if (unsigned AS = CanTy.getAddressSpace())
1013 EltTy = getASQualType(EltTy, AS);
1014
1015 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1016 // the CVR qualifiers directly from the canonical type, which is guaranteed
1017 // to have the full set unioned together.
1018 ArrayQuals = CanTy.getCVRQualifiers();
1019 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1020 }
1021
Chris Lattnerd9654552008-04-02 06:06:35 +00001022 // Apply any CVR qualifiers from the array type to the element type. This
1023 // implements C99 6.7.3p8: "If the specification of an array type includes
1024 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattnere6327742008-04-02 05:18:44 +00001025 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1026
1027 QualType PtrTy = getPointerType(EltTy);
1028
1029 // int x[restrict 4] -> int *restrict
1030 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1031
1032 return PtrTy;
1033}
1034
Reid Spencer5f016e22007-07-11 17:01:13 +00001035/// getFloatingRank - Return a relative rank for floating point types.
1036/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001037static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001038 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001040
Christopher Lambebb97e92008-02-04 02:31:56 +00001041 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001042 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 case BuiltinType::Float: return FloatRank;
1044 case BuiltinType::Double: return DoubleRank;
1045 case BuiltinType::LongDouble: return LongDoubleRank;
1046 }
1047}
1048
Steve Naroff716c7302007-08-27 01:41:48 +00001049/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1050/// point or a complex type (based on typeDomain/typeSize).
1051/// 'typeDomain' is a real floating point or complex type.
1052/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001053QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1054 QualType Domain) const {
1055 FloatingRank EltRank = getFloatingRank(Size);
1056 if (Domain->isComplexType()) {
1057 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001058 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001059 case FloatRank: return FloatComplexTy;
1060 case DoubleRank: return DoubleComplexTy;
1061 case LongDoubleRank: return LongDoubleComplexTy;
1062 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 }
Chris Lattner1361b112008-04-06 23:58:54 +00001064
1065 assert(Domain->isRealFloatingType() && "Unknown domain!");
1066 switch (EltRank) {
1067 default: assert(0 && "getFloatingRank(): illegal value for rank");
1068 case FloatRank: return FloatTy;
1069 case DoubleRank: return DoubleTy;
1070 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001071 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001072}
1073
Chris Lattner7cfeb082008-04-06 23:55:33 +00001074/// getFloatingTypeOrder - Compare the rank of the two specified floating
1075/// point types, ignoring the domain of the type (i.e. 'double' ==
1076/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1077/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001078int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1079 FloatingRank LHSR = getFloatingRank(LHS);
1080 FloatingRank RHSR = getFloatingRank(RHS);
1081
1082 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001083 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001084 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001085 return 1;
1086 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001087}
1088
Chris Lattnerf52ab252008-04-06 22:59:24 +00001089/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1090/// routine will assert if passed a built-in type that isn't an integer or enum,
1091/// or if it is not canonicalized.
1092static unsigned getIntegerRank(Type *T) {
1093 assert(T->isCanonical() && "T should be canonicalized");
1094 if (isa<EnumType>(T))
1095 return 4;
1096
1097 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001098 default: assert(0 && "getIntegerRank(): not a built-in integer");
1099 case BuiltinType::Bool:
1100 return 1;
1101 case BuiltinType::Char_S:
1102 case BuiltinType::Char_U:
1103 case BuiltinType::SChar:
1104 case BuiltinType::UChar:
1105 return 2;
1106 case BuiltinType::Short:
1107 case BuiltinType::UShort:
1108 return 3;
1109 case BuiltinType::Int:
1110 case BuiltinType::UInt:
1111 return 4;
1112 case BuiltinType::Long:
1113 case BuiltinType::ULong:
1114 return 5;
1115 case BuiltinType::LongLong:
1116 case BuiltinType::ULongLong:
1117 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001118 }
1119}
1120
Chris Lattner7cfeb082008-04-06 23:55:33 +00001121/// getIntegerTypeOrder - Returns the highest ranked integer type:
1122/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1123/// LHS < RHS, return -1.
1124int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001125 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1126 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001127 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001128
Chris Lattnerf52ab252008-04-06 22:59:24 +00001129 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1130 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001131
Chris Lattner7cfeb082008-04-06 23:55:33 +00001132 unsigned LHSRank = getIntegerRank(LHSC);
1133 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134
Chris Lattner7cfeb082008-04-06 23:55:33 +00001135 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1136 if (LHSRank == RHSRank) return 0;
1137 return LHSRank > RHSRank ? 1 : -1;
1138 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001139
Chris Lattner7cfeb082008-04-06 23:55:33 +00001140 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1141 if (LHSUnsigned) {
1142 // If the unsigned [LHS] type is larger, return it.
1143 if (LHSRank >= RHSRank)
1144 return 1;
1145
1146 // If the signed type can represent all values of the unsigned type, it
1147 // wins. Because we are dealing with 2's complement and types that are
1148 // powers of two larger than each other, this is always safe.
1149 return -1;
1150 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001151
Chris Lattner7cfeb082008-04-06 23:55:33 +00001152 // If the unsigned [RHS] type is larger, return it.
1153 if (RHSRank >= LHSRank)
1154 return -1;
1155
1156 // If the signed type can represent all values of the unsigned type, it
1157 // wins. Because we are dealing with 2's complement and types that are
1158 // powers of two larger than each other, this is always safe.
1159 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001160}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001161
1162// getCFConstantStringType - Return the type used for constant CFStrings.
1163QualType ASTContext::getCFConstantStringType() {
1164 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001165 CFConstantStringTypeDecl =
Chris Lattner0ed844b2008-04-04 06:12:32 +00001166 RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001167 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001168 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001169
1170 // const int *isa;
1171 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001172 // int flags;
1173 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001174 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001175 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001176 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001177 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001178 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001179 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001180
Anders Carlssonf06273f2007-11-19 00:25:30 +00001181 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001182 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001183 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001184
1185 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1186 }
1187
1188 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001189}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001190
Anders Carlssone8c49532007-10-29 06:33:42 +00001191// This returns true if a type has been typedefed to BOOL:
1192// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001193static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001194 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001195 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001196
1197 return false;
1198}
1199
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001200/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001201/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001202int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001203 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001204
1205 // Make all integer and enum types at least as large as an int
1206 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001207 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001208 // Treat arrays as pointers, since that's how they're passed in.
1209 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001210 sz = getTypeSize(VoidPtrTy);
1211 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001212}
1213
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001214/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001215/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001216void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001217 std::string& S)
1218{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001219 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001220 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001221 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001222 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001223 // Compute size of all parameters.
1224 // Start with computing size of a pointer in number of bytes.
1225 // FIXME: There might(should) be a better way of doing this computation!
1226 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001227 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001228 // The first two arguments (self and _cmd) are pointers; account for
1229 // their size.
1230 int ParmOffset = 2 * PtrSize;
1231 int NumOfParams = Decl->getNumParams();
1232 for (int i = 0; i < NumOfParams; i++) {
1233 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001234 int sz = getObjCEncodingTypeSize (PType);
1235 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001236 ParmOffset += sz;
1237 }
1238 S += llvm::utostr(ParmOffset);
1239 S += "@0:";
1240 S += llvm::utostr(PtrSize);
1241
1242 // Argument types.
1243 ParmOffset = 2 * PtrSize;
1244 for (int i = 0; i < NumOfParams; i++) {
1245 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001246 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001247 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001248 getObjCEncodingForTypeQualifier(
1249 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001250 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001251 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001252 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001253 }
1254}
1255
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001256void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1257 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001258{
Anders Carlssone8c49532007-10-29 06:33:42 +00001259 // FIXME: This currently doesn't encode:
1260 // @ An object (whether statically typed or typed id)
1261 // # A class object (Class)
1262 // : A method selector (SEL)
1263 // {name=type...} A structure
1264 // (name=type...) A union
1265 // bnum A bit field of num bits
1266
1267 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001268 char encoding;
1269 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001270 default: assert(0 && "Unhandled builtin type kind");
1271 case BuiltinType::Void: encoding = 'v'; break;
1272 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001273 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001274 case BuiltinType::UChar: encoding = 'C'; break;
1275 case BuiltinType::UShort: encoding = 'S'; break;
1276 case BuiltinType::UInt: encoding = 'I'; break;
1277 case BuiltinType::ULong: encoding = 'L'; break;
1278 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001279 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001280 case BuiltinType::SChar: encoding = 'c'; break;
1281 case BuiltinType::Short: encoding = 's'; break;
1282 case BuiltinType::Int: encoding = 'i'; break;
1283 case BuiltinType::Long: encoding = 'l'; break;
1284 case BuiltinType::LongLong: encoding = 'q'; break;
1285 case BuiltinType::Float: encoding = 'f'; break;
1286 case BuiltinType::Double: encoding = 'd'; break;
1287 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001288 }
1289
1290 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001291 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001293 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001294 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001295
1296 }
1297 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001298 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001299 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001300 S += '@';
1301 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001303 S += '#';
1304 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001305 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001306 S += ':';
1307 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001308 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001309
1310 if (PointeeTy->isCharType()) {
1311 // char pointer types should be encoded as '*' unless it is a
1312 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001313 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001314 S += '*';
1315 return;
1316 }
1317 }
1318
1319 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001320 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001321 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001322 S += '[';
1323
1324 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1325 S += llvm::utostr(CAT->getSize().getZExtValue());
1326 else
1327 assert(0 && "Unhandled array type!");
1328
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001329 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001330 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001331 } else if (T->getAsFunctionType()) {
1332 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001333 } else if (const RecordType *RTy = T->getAsRecordType()) {
1334 RecordDecl *RDecl= RTy->getDecl();
1335 S += '{';
1336 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001337 bool found = false;
1338 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1339 if (ERType[i] == RTy) {
1340 found = true;
1341 break;
1342 }
1343 if (!found) {
1344 ERType.push_back(RTy);
1345 S += '=';
1346 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1347 FieldDecl *field = RDecl->getMember(i);
1348 getObjCEncodingForType(field->getType(), S, ERType);
1349 }
1350 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1351 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001352 }
1353 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001354 } else if (T->isEnumeralType()) {
1355 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001356 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001357 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001358}
1359
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001360void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001361 std::string& S) const {
1362 if (QT & Decl::OBJC_TQ_In)
1363 S += 'n';
1364 if (QT & Decl::OBJC_TQ_Inout)
1365 S += 'N';
1366 if (QT & Decl::OBJC_TQ_Out)
1367 S += 'o';
1368 if (QT & Decl::OBJC_TQ_Bycopy)
1369 S += 'O';
1370 if (QT & Decl::OBJC_TQ_Byref)
1371 S += 'R';
1372 if (QT & Decl::OBJC_TQ_Oneway)
1373 S += 'V';
1374}
1375
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001376void ASTContext::setBuiltinVaListType(QualType T)
1377{
1378 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1379
1380 BuiltinVaListType = T;
1381}
1382
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001384{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001385 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001386
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001388
1389 // typedef struct objc_object *id;
1390 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1391 assert(ptr && "'id' incorrectly typed");
1392 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1393 assert(rec && "'id' incorrectly typed");
1394 IdStructType = rec;
1395}
1396
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001398{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001399 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001400
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001401 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001402
1403 // typedef struct objc_selector *SEL;
1404 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1405 assert(ptr && "'SEL' incorrectly typed");
1406 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1407 assert(rec && "'SEL' incorrectly typed");
1408 SelStructType = rec;
1409}
1410
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001412{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001413 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1414 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001415}
1416
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001417void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001418{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001419 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001420
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001421 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001422
1423 // typedef struct objc_class *Class;
1424 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1425 assert(ptr && "'Class' incorrectly typed");
1426 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1427 assert(rec && "'Class' incorrectly typed");
1428 ClassStructType = rec;
1429}
1430
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1432 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001433 "'NSConstantString' type already set!");
1434
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001435 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001436}
1437
Chris Lattner6ac46a42008-04-07 06:51:04 +00001438//===----------------------------------------------------------------------===//
1439// Type Compatibility Testing
1440//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001441
Chris Lattner78eca282008-04-07 06:49:41 +00001442/// C99 6.2.7p1: If both are complete types, then the following additional
1443/// requirements apply.
1444/// FIXME (handle compatibility across source files).
1445static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1446 const ASTContext &C) {
Steve Naroffab373092007-11-07 06:03:51 +00001447 // "Class" and "id" are compatible built-in structure types.
Chris Lattner78eca282008-04-07 06:49:41 +00001448 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1449 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroffab373092007-11-07 06:03:51 +00001450 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001451
Chris Lattner78eca282008-04-07 06:49:41 +00001452 // Within a translation unit a tag type is only compatible with itself. Self
1453 // equality is already handled by the time we get here.
1454 assert(LHS != RHS && "Self equality not handled!");
1455 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001456}
1457
1458bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1459 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1460 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001461 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1462 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001463 return false;
1464
1465 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1466 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1467
1468 return typesAreCompatible(ltype, rtype);
1469}
1470
Bill Wendling43d69752007-12-03 07:33:35 +00001471// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001472// reference to T, the operation assigns to the object of type T denoted by the
1473// reference.
1474bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1475 QualType ltype = lhs;
1476
1477 if (lhs->isReferenceType())
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001478 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001479
1480 QualType rtype = rhs;
1481
1482 if (rhs->isReferenceType())
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001483 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001484
1485 return typesAreCompatible(ltype, rtype);
1486}
1487
1488bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1489 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1490 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1491 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1492 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1493
1494 // first check the return types (common between C99 and K&R).
1495 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1496 return false;
1497
1498 if (lproto && rproto) { // two C99 style function prototypes
1499 unsigned lproto_nargs = lproto->getNumArgs();
1500 unsigned rproto_nargs = rproto->getNumArgs();
1501
1502 if (lproto_nargs != rproto_nargs)
1503 return false;
1504
1505 // both prototypes have the same number of arguments.
1506 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1507 (rproto->isVariadic() && !lproto->isVariadic()))
1508 return false;
1509
1510 // The use of ellipsis agree...now check the argument types.
1511 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001512 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1513 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001514 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001515 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001516 return false;
1517 return true;
1518 }
1519 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1520 return true;
1521
1522 // we have a mixture of K&R style with C99 prototypes
1523 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1524
1525 if (proto->isVariadic())
1526 return false;
1527
1528 // FIXME: Each parameter type T in the prototype must be compatible with the
1529 // type resulting from applying the usual argument conversions to T.
1530 return true;
1531}
1532
1533bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman4e92acf2008-02-06 04:53:22 +00001534 // Compatible arrays must have compatible element types
1535 QualType ltype = lhs->getAsArrayType()->getElementType();
1536 QualType rtype = rhs->getAsArrayType()->getElementType();
1537
Steve Naroffec0550f2007-10-15 20:41:53 +00001538 if (!typesAreCompatible(ltype, rtype))
1539 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001540
1541 // Compatible arrays must be the same size
1542 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1543 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1544 return RCAT->getSize() == LCAT->getSize();
1545
Steve Naroffec0550f2007-10-15 20:41:53 +00001546 return true;
1547}
1548
Chris Lattner6ac46a42008-04-07 06:51:04 +00001549/// areCompatVectorTypes - Return true if the two specified vector types are
1550/// compatible.
1551static bool areCompatVectorTypes(const VectorType *LHS,
1552 const VectorType *RHS) {
1553 assert(LHS->isCanonical() && RHS->isCanonical());
1554 return LHS->getElementType() == RHS->getElementType() &&
1555 LHS->getNumElements() == RHS->getNumElements();
1556}
1557
1558/// areCompatObjCInterfaces - Return true if the two interface types are
1559/// compatible for assignment from RHS to LHS. This handles validation of any
1560/// protocol qualifiers on the LHS or RHS.
1561///
1562static bool
1563areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1564 const ObjCInterfaceType *RHS) {
1565 // Verify that the base decls are compatible: the RHS must be a subclass of
1566 // the LHS.
1567 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1568 return false;
1569
1570 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1571 // protocol qualified at all, then we are good.
1572 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1573 return true;
1574
1575 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1576 // isn't a superset.
1577 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1578 return true; // FIXME: should return false!
1579
1580 // Finally, we must have two protocol-qualified interfaces.
1581 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1582 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1583 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1584 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1585 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1586 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1587
1588 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1589 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1590 // LHS in a single parallel scan until we run out of elements in LHS.
1591 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1592 ObjCProtocolDecl *LHSProto = *LHSPI;
1593
1594 while (RHSPI != RHSPE) {
1595 ObjCProtocolDecl *RHSProto = *RHSPI++;
1596 // If the RHS has a protocol that the LHS doesn't, ignore it.
1597 if (RHSProto != LHSProto)
1598 continue;
1599
1600 // Otherwise, the RHS does have this element.
1601 ++LHSPI;
1602 if (LHSPI == LHSPE)
1603 return true; // All protocols in LHS exist in RHS.
1604
1605 LHSProto = *LHSPI;
1606 }
1607
1608 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1609 return false;
1610}
1611
1612
Steve Naroffec0550f2007-10-15 20:41:53 +00001613/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1614/// both shall have the identically qualified version of a compatible type.
1615/// C99 6.2.7p1: Two types have compatible types if their types are the
1616/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001617bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1618 QualType LHS = LHS_NC.getCanonicalType();
1619 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner988ee6e2008-04-03 05:07:04 +00001620
Bill Wendling43d69752007-12-03 07:33:35 +00001621 // C++ [expr]: If an expression initially has the type "reference to T", the
1622 // type is adjusted to "T" prior to any further analysis, the expression
1623 // designates the object or function denoted by the reference, and the
1624 // expression is an lvalue.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001625 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1626 LHS = RT->getPointeeType();
1627 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1628 RHS = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001629
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001630 // If two types are identical, they are compatible.
1631 if (LHS == RHS)
1632 return true;
1633
1634 // If qualifiers differ, the types are different.
Chris Lattnera36a61f2008-04-07 05:43:21 +00001635 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1636 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001637 return false;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001638
1639 // Strip off ASQual's if present.
1640 if (LHSAS) {
1641 LHS = LHS.getUnqualifiedType();
1642 RHS = RHS.getUnqualifiedType();
1643 }
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001644
Chris Lattnerc4e40592008-04-07 04:07:56 +00001645 Type::TypeClass LHSClass = LHS->getTypeClass();
1646 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattner1adb8832008-01-14 05:45:46 +00001647
1648 // We want to consider the two function types to be the same for these
1649 // comparisons, just force one to the other.
1650 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1651 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001652
1653 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001654 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1655 LHSClass = Type::ConstantArray;
1656 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1657 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001658
Chris Lattnera36a61f2008-04-07 05:43:21 +00001659 // Canonicalize OCUVector -> Vector.
1660 if (LHSClass == Type::OCUVector) LHSClass = Type::Vector;
1661 if (RHSClass == Type::OCUVector) RHSClass = Type::Vector;
1662
Chris Lattnerb0489812008-04-07 06:38:24 +00001663 // Consider qualified interfaces and interfaces the same.
1664 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1665 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1666
Chris Lattnera36a61f2008-04-07 05:43:21 +00001667 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001668 if (LHSClass != RHSClass) {
Chris Lattnerb0489812008-04-07 06:38:24 +00001669 // ID is compatible with all interface types.
1670 if (isa<ObjCInterfaceType>(LHS))
1671 return isObjCIdType(RHS);
1672 if (isa<ObjCInterfaceType>(RHS))
1673 return isObjCIdType(LHS);
Chris Lattner6e26f5d2008-04-07 05:53:18 +00001674
Chris Lattner1adb8832008-01-14 05:45:46 +00001675 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1676 // a signed integer type, or an unsigned integer type.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001677 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1678 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1679 return EDecl->getIntegerType() == RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001680 }
Chris Lattnerc4e40592008-04-07 04:07:56 +00001681 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1682 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1683 return EDecl->getIntegerType() == LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001684 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001685
Steve Naroffec0550f2007-10-15 20:41:53 +00001686 return false;
1687 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00001688
Steve Naroff4a746782008-01-09 22:43:08 +00001689 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001690 switch (LHSClass) {
Chris Lattnera36a61f2008-04-07 05:43:21 +00001691 case Type::ASQual:
1692 case Type::FunctionProto:
1693 case Type::VariableArray:
1694 case Type::IncompleteArray:
1695 case Type::Reference:
Chris Lattnerb0489812008-04-07 06:38:24 +00001696 case Type::ObjCQualifiedInterface:
Chris Lattnera36a61f2008-04-07 05:43:21 +00001697 assert(0 && "Canonicalized away above");
Chris Lattner1adb8832008-01-14 05:45:46 +00001698 case Type::Pointer:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001699 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001700 case Type::ConstantArray:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001701 return arrayTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001702 case Type::FunctionNoProto:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001703 return functionTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001704 case Type::Tagged: // handle structures, unions
Chris Lattner78eca282008-04-07 06:49:41 +00001705 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001706 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00001707 // Only exactly equal builtin types are compatible, which is tested above.
1708 return false;
1709 case Type::Vector:
1710 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001711 case Type::ObjCInterface:
Chris Lattnerb0489812008-04-07 06:38:24 +00001712 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1713 cast<ObjCInterfaceType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001714 default:
1715 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001716 }
1717 return true; // should never get here...
1718}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001719
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001720/// Emit - Serialize an ASTContext object to Bitcode.
1721void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001722 S.EmitRef(SourceMgr);
1723 S.EmitRef(Target);
1724 S.EmitRef(Idents);
1725 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001726
Ted Kremenekfee04522007-10-31 22:44:07 +00001727 // Emit the size of the type vector so that we can reserve that size
1728 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001729 S.EmitInt(Types.size());
1730
Ted Kremenek03ed4402007-11-13 22:02:55 +00001731 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1732 I!=E;++I)
1733 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001734
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001735 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001736}
1737
Ted Kremenek0f84c002007-11-13 00:25:37 +00001738ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001739 SourceManager &SM = D.ReadRef<SourceManager>();
1740 TargetInfo &t = D.ReadRef<TargetInfo>();
1741 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1742 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001743
Ted Kremenekfee04522007-10-31 22:44:07 +00001744 unsigned size_reserve = D.ReadInt();
1745
1746 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1747
Ted Kremenek03ed4402007-11-13 22:02:55 +00001748 for (unsigned i = 0; i < size_reserve; ++i)
1749 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001750
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001751 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001752
1753 return A;
1754}