blob: 47a3e3ac67f1446f8053cb6701b45bfc42379a02 [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()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000032 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000033 Types.pop_back();
34 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000035
36 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000037}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000047 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
48 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000049 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000059 else if (isa<ComplexType>(T))
60 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 else if (isa<ArrayType>(T))
62 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000063 else if (isa<VectorType>(T))
64 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000073 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000074 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000075 case TagDecl::TK_struct: ++NumTagStruct; break;
76 case TagDecl::TK_union: ++NumTagUnion; break;
77 case TagDecl::TK_class: ++NumTagClass; break;
78 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000080 } else if (isa<ObjCInterfaceType>(T))
81 ++NumObjCInterfaces;
82 else if (isa<ObjCQualifiedInterfaceType>(T))
83 ++NumObjCQualifiedInterfaces;
84 else if (isa<ObjCQualifiedIdType>(T))
85 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +000086 else if (isa<TypeOfType>(T))
87 ++NumTypeOfTypes;
88 else if (isa<TypeOfExpr>(T))
89 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +000090 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000091 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +000092 assert(0 && "Unknown type!");
93 }
94 }
95
96 fprintf(stderr, " %d builtin types\n", NumBuiltin);
97 fprintf(stderr, " %d pointer types\n", NumPointer);
98 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000099 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000101 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
103 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
104 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
105 fprintf(stderr, " %d tagged types\n", NumTagged);
106 fprintf(stderr, " %d struct types\n", NumTagStruct);
107 fprintf(stderr, " %d union types\n", NumTagUnion);
108 fprintf(stderr, " %d class types\n", NumTagClass);
109 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000111 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000113 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000114 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000115 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
116 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
119 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000120 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 NumFunctionP*sizeof(FunctionTypeProto)+
122 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000123 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
124 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
129 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000141 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
163
164 // C99 6.2.5p11.
165 FloatComplexTy = getComplexType(FloatTy);
166 DoubleComplexTy = getComplexType(DoubleTy);
167 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000168
169 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000171 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000172 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000173 ClassStructType = 0;
174
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000176
177 // void * type
178 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Chris Lattner464175b2007-07-18 17:52:12 +0000181//===----------------------------------------------------------------------===//
182// Type Sizing and Analysis
183//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000184
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000185/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
186/// scalar floating point type.
187const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
188 const BuiltinType *BT = T->getAsBuiltinType();
189 assert(BT && "Not a floating point type!");
190 switch (BT->getKind()) {
191 default: assert(0 && "Not a floating point type!");
192 case BuiltinType::Float: return Target.getFloatFormat();
193 case BuiltinType::Double: return Target.getDoubleFormat();
194 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
195 }
196}
197
198
Chris Lattnera7674d82007-07-13 22:13:22 +0000199/// getTypeSize - Return the size of the specified type, in bits. This method
200/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000201std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000202ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000203 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000204 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000205 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000206 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000207 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000208 case Type::FunctionNoProto:
209 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000210 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000211 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000212 case Type::VariableArray:
213 assert(0 && "VLAs not implemented yet!");
214 case Type::ConstantArray: {
215 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
216
Chris Lattner98be4942008-03-05 18:54:05 +0000217 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000218 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000219 Align = EltInfo.second;
220 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000221 }
Nate Begeman213541a2008-04-18 23:10:10 +0000222 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000223 case Type::Vector: {
224 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000225 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000226 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000227 // FIXME: This isn't right for unusual vectors
228 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000229 break;
230 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000231
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000232 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000233 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000234 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000235 case BuiltinType::Void:
236 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000237 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000238 Width = Target.getBoolWidth();
239 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000241 case BuiltinType::Char_S:
242 case BuiltinType::Char_U:
243 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000244 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000245 Width = Target.getCharWidth();
246 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000247 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000248 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000249 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000250 Width = Target.getShortWidth();
251 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000252 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000253 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000254 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000255 Width = Target.getIntWidth();
256 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000257 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000258 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000259 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000260 Width = Target.getLongWidth();
261 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000262 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000263 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000264 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000265 Width = Target.getLongLongWidth();
266 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000267 break;
268 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000269 Width = Target.getFloatWidth();
270 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000271 break;
272 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000273 Width = Target.getDoubleWidth();
274 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000275 break;
276 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000277 Width = Target.getLongDoubleWidth();
278 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000279 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000280 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000281 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000282 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000283 // FIXME: Pointers into different addr spaces could have different sizes and
284 // alignment requirements: getPointerInfo should take an AddrSpace.
285 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000286 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000287 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000288 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000289 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000290 case Type::Pointer: {
291 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000292 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000293 Align = Target.getPointerAlign(AS);
294 break;
295 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000296 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000297 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000298 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000299 // FIXME: This is wrong for struct layout: a reference in a struct has
300 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000301 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000302
303 case Type::Complex: {
304 // Complex types have the same alignment as their elements, but twice the
305 // size.
306 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000307 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000308 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000309 Align = EltInfo.second;
310 break;
311 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000312 case Type::ObjCInterface: {
313 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
314 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
315 Width = Layout.getSize();
316 Align = Layout.getAlignment();
317 break;
318 }
Chris Lattner71763312008-04-06 22:05:18 +0000319 case Type::Tagged: {
320 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
321 return getTypeInfo(ET->getDecl()->getIntegerType());
322
323 RecordType *RT = cast<RecordType>(T);
324 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
325 Width = Layout.getSize();
326 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000327 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000328 }
Chris Lattner71763312008-04-06 22:05:18 +0000329 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000330
Chris Lattner464175b2007-07-18 17:52:12 +0000331 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000332 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000333}
334
Devang Patel8b277042008-06-04 21:22:16 +0000335/// LayoutField - Field layout.
336void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
337 bool IsUnion, bool StructIsPacked,
338 ASTContext &Context) {
339 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
340 uint64_t FieldOffset = IsUnion ? 0 : Size;
341 uint64_t FieldSize;
342 unsigned FieldAlign;
343
344 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
345 // TODO: Need to check this algorithm on other targets!
346 // (tested on Linux-X86)
347 llvm::APSInt I(32);
348 bool BitWidthIsICE =
349 BitWidthExpr->isIntegerConstantExpr(I, Context);
350 assert (BitWidthIsICE && "Invalid BitField size expression");
351 FieldSize = I.getZExtValue();
352
353 std::pair<uint64_t, unsigned> FieldInfo =
354 Context.getTypeInfo(FD->getType());
355 uint64_t TypeSize = FieldInfo.first;
356
357 FieldAlign = FieldInfo.second;
358 if (FieldIsPacked)
359 FieldAlign = 1;
360 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
361 FieldAlign = std::max(FieldAlign, AA->getAlignment());
362
363 // Check if we need to add padding to give the field the correct
364 // alignment.
365 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
366 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
367
368 // Padding members don't affect overall alignment
369 if (!FD->getIdentifier())
370 FieldAlign = 1;
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 FieldSize = 0;
378 const ArrayType* ATy = FD->getType()->getAsArrayType();
379 FieldAlign = Context.getTypeAlign(ATy->getElementType());
380 } else {
381 std::pair<uint64_t, unsigned> FieldInfo =
382 Context.getTypeInfo(FD->getType());
383 FieldSize = FieldInfo.first;
384 FieldAlign = FieldInfo.second;
385 }
386
387 if (FieldIsPacked)
388 FieldAlign = 8;
389 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
390 FieldAlign = std::max(FieldAlign, AA->getAlignment());
391
392 // Round up the current record size to the field's alignment boundary.
393 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
394 }
395
396 // Place this field at the current location.
397 FieldOffsets[FieldNo] = FieldOffset;
398
399 // Reserve space for this field.
400 if (IsUnion) {
401 Size = std::max(Size, FieldSize);
402 } else {
403 Size = FieldOffset + FieldSize;
404 }
405
406 // Remember max struct/class alignment.
407 Alignment = std::max(Alignment, FieldAlign);
408}
409
Devang Patel44a3dde2008-06-04 21:54:36 +0000410
411/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
412/// specified Objective C, which indicates its size and ivar
413/// position information.
414const ASTRecordLayout &
415ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
416 // Look up this layout, if already laid out, return what we have.
417 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
418 if (Entry) return *Entry;
419
420 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
421 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000422 ASTRecordLayout *NewEntry = NULL;
423 unsigned FieldCount = D->ivar_size();
424 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
425 FieldCount++;
426 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
427 unsigned Alignment = SL.getAlignment();
428 uint64_t Size = SL.getSize();
429 NewEntry = new ASTRecordLayout(Size, Alignment);
430 NewEntry->InitializeLayout(FieldCount);
431 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
432 } else {
433 NewEntry = new ASTRecordLayout();
434 NewEntry->InitializeLayout(FieldCount);
435 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000436 Entry = NewEntry;
437
Devang Patel44a3dde2008-06-04 21:54:36 +0000438 bool IsPacked = D->getAttr<PackedAttr>();
439
440 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
441 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
442 AA->getAlignment()));
443
444 // Layout each ivar sequentially.
445 unsigned i = 0;
446 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
447 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
448 const ObjCIvarDecl* Ivar = (*IVI);
449 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
450 }
451
452 // Finally, round the size of the total struct up to the alignment of the
453 // struct itself.
454 NewEntry->FinalizeLayout();
455 return *NewEntry;
456}
457
Devang Patel88a981b2007-11-01 19:11:01 +0000458/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000459/// specified record (struct/union/class), which indicates its size and field
460/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000461const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000462 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000463
Chris Lattner464175b2007-07-18 17:52:12 +0000464 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000465 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000466 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000467
Devang Patel88a981b2007-11-01 19:11:01 +0000468 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
469 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
470 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000471 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000472
Devang Patel8b277042008-06-04 21:22:16 +0000473 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000474 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000475 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000476
Eli Friedman4bd998b2008-05-30 09:31:38 +0000477 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000478 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
479 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000480
Eli Friedman4bd998b2008-05-30 09:31:38 +0000481 // Layout each field, for now, just sequentially, respecting alignment. In
482 // the future, this will need to be tweakable by targets.
483 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
484 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000485 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000486 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000487
488 // Finally, round the size of the total struct up to the alignment of the
489 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000490 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000492}
493
Chris Lattnera7674d82007-07-13 22:13:22 +0000494//===----------------------------------------------------------------------===//
495// Type creation/memoization methods
496//===----------------------------------------------------------------------===//
497
Christopher Lambebb97e92008-02-04 02:31:56 +0000498QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000499 QualType CanT = getCanonicalType(T);
500 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000501 return T;
502
503 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
504 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000505 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000506 "Type is already address space qualified");
507
508 // Check if we've already instantiated an address space qual'd type of this
509 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000510 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000511 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000512 void *InsertPos = 0;
513 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
514 return QualType(ASQy, 0);
515
516 // If the base type isn't canonical, this won't be a canonical type either,
517 // so fill in the canonical type field.
518 QualType Canonical;
519 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000520 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000521
522 // Get the new insert position for the node we care about.
523 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
524 assert(NewIP == 0 && "Shouldn't be in the map!");
525 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000526 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000527 ASQualTypes.InsertNode(New, InsertPos);
528 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000529 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000530}
531
Chris Lattnera7674d82007-07-13 22:13:22 +0000532
Reid Spencer5f016e22007-07-11 17:01:13 +0000533/// getComplexType - Return the uniqued reference to the type for a complex
534/// number with the specified element type.
535QualType ASTContext::getComplexType(QualType T) {
536 // Unique pointers, to guarantee there is only one pointer of a particular
537 // structure.
538 llvm::FoldingSetNodeID ID;
539 ComplexType::Profile(ID, T);
540
541 void *InsertPos = 0;
542 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
543 return QualType(CT, 0);
544
545 // If the pointee type isn't canonical, this won't be a canonical type either,
546 // so fill in the canonical type field.
547 QualType Canonical;
548 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000549 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
551 // Get the new insert position for the node we care about.
552 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
553 assert(NewIP == 0 && "Shouldn't be in the map!");
554 }
555 ComplexType *New = new ComplexType(T, Canonical);
556 Types.push_back(New);
557 ComplexTypes.InsertNode(New, InsertPos);
558 return QualType(New, 0);
559}
560
561
562/// getPointerType - Return the uniqued reference to the type for a pointer to
563/// the specified type.
564QualType ASTContext::getPointerType(QualType T) {
565 // Unique pointers, to guarantee there is only one pointer of a particular
566 // structure.
567 llvm::FoldingSetNodeID ID;
568 PointerType::Profile(ID, T);
569
570 void *InsertPos = 0;
571 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
572 return QualType(PT, 0);
573
574 // If the pointee type isn't canonical, this won't be a canonical type either,
575 // so fill in the canonical type field.
576 QualType Canonical;
577 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000578 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
580 // Get the new insert position for the node we care about.
581 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
582 assert(NewIP == 0 && "Shouldn't be in the map!");
583 }
584 PointerType *New = new PointerType(T, Canonical);
585 Types.push_back(New);
586 PointerTypes.InsertNode(New, InsertPos);
587 return QualType(New, 0);
588}
589
590/// getReferenceType - Return the uniqued reference to the type for a reference
591/// to the specified type.
592QualType ASTContext::getReferenceType(QualType T) {
593 // Unique pointers, to guarantee there is only one pointer of a particular
594 // structure.
595 llvm::FoldingSetNodeID ID;
596 ReferenceType::Profile(ID, T);
597
598 void *InsertPos = 0;
599 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
600 return QualType(RT, 0);
601
602 // If the referencee type isn't canonical, this won't be a canonical type
603 // either, so fill in the canonical type field.
604 QualType Canonical;
605 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000606 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608 // Get the new insert position for the node we care about.
609 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
610 assert(NewIP == 0 && "Shouldn't be in the map!");
611 }
612
613 ReferenceType *New = new ReferenceType(T, Canonical);
614 Types.push_back(New);
615 ReferenceTypes.InsertNode(New, InsertPos);
616 return QualType(New, 0);
617}
618
Steve Narofffb22d962007-08-30 01:06:46 +0000619/// getConstantArrayType - Return the unique reference to the type for an
620/// array of the specified element type.
621QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000622 const llvm::APInt &ArySize,
623 ArrayType::ArraySizeModifier ASM,
624 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000626 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000627
628 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000629 if (ConstantArrayType *ATP =
630 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 return QualType(ATP, 0);
632
633 // If the element type isn't canonical, this won't be a canonical type either,
634 // so fill in the canonical type field.
635 QualType Canonical;
636 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000637 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000638 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000640 ConstantArrayType *NewIP =
641 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 assert(NewIP == 0 && "Shouldn't be in the map!");
644 }
645
Steve Naroffc9406122007-08-30 18:10:14 +0000646 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
647 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000648 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 Types.push_back(New);
650 return QualType(New, 0);
651}
652
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000653/// getVariableArrayType - Returns a non-unique reference to the type for a
654/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000655QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
656 ArrayType::ArraySizeModifier ASM,
657 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000658 // Since we don't unique expressions, it isn't possible to unique VLA's
659 // that have an expression provided for their size.
660
661 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
662 ASM, EltTypeQuals);
663
664 VariableArrayTypes.push_back(New);
665 Types.push_back(New);
666 return QualType(New, 0);
667}
668
669QualType ASTContext::getIncompleteArrayType(QualType EltTy,
670 ArrayType::ArraySizeModifier ASM,
671 unsigned EltTypeQuals) {
672 llvm::FoldingSetNodeID ID;
673 IncompleteArrayType::Profile(ID, EltTy);
674
675 void *InsertPos = 0;
676 if (IncompleteArrayType *ATP =
677 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
678 return QualType(ATP, 0);
679
680 // If the element type isn't canonical, this won't be a canonical type
681 // either, so fill in the canonical type field.
682 QualType Canonical;
683
684 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000685 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000686 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000687
688 // Get the new insert position for the node we care about.
689 IncompleteArrayType *NewIP =
690 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
691
692 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000693 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000694
695 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
696 ASM, EltTypeQuals);
697
698 IncompleteArrayTypes.InsertNode(New, InsertPos);
699 Types.push_back(New);
700 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000701}
702
Steve Naroff73322922007-07-18 18:00:27 +0000703/// getVectorType - Return the unique reference to a vector type of
704/// the specified element type and size. VectorType must be a built-in type.
705QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 BuiltinType *baseType;
707
Chris Lattnerf52ab252008-04-06 22:59:24 +0000708 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000709 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000710
711 // Check if we've already instantiated a vector of this type.
712 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000713 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 void *InsertPos = 0;
715 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
716 return QualType(VTP, 0);
717
718 // If the element type isn't canonical, this won't be a canonical type either,
719 // so fill in the canonical type field.
720 QualType Canonical;
721 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000722 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723
724 // Get the new insert position for the node we care about.
725 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
726 assert(NewIP == 0 && "Shouldn't be in the map!");
727 }
728 VectorType *New = new VectorType(vecType, NumElts, Canonical);
729 VectorTypes.InsertNode(New, InsertPos);
730 Types.push_back(New);
731 return QualType(New, 0);
732}
733
Nate Begeman213541a2008-04-18 23:10:10 +0000734/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000735/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000736QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000737 BuiltinType *baseType;
738
Chris Lattnerf52ab252008-04-06 22:59:24 +0000739 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000740 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000741
742 // Check if we've already instantiated a vector of this type.
743 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000744 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000745 void *InsertPos = 0;
746 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
747 return QualType(VTP, 0);
748
749 // If the element type isn't canonical, this won't be a canonical type either,
750 // so fill in the canonical type field.
751 QualType Canonical;
752 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000753 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000754
755 // Get the new insert position for the node we care about.
756 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
757 assert(NewIP == 0 && "Shouldn't be in the map!");
758 }
Nate Begeman213541a2008-04-18 23:10:10 +0000759 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000760 VectorTypes.InsertNode(New, InsertPos);
761 Types.push_back(New);
762 return QualType(New, 0);
763}
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
766///
767QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
768 // Unique functions, to guarantee there is only one function of a particular
769 // structure.
770 llvm::FoldingSetNodeID ID;
771 FunctionTypeNoProto::Profile(ID, ResultTy);
772
773 void *InsertPos = 0;
774 if (FunctionTypeNoProto *FT =
775 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
776 return QualType(FT, 0);
777
778 QualType Canonical;
779 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000780 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000781
782 // Get the new insert position for the node we care about.
783 FunctionTypeNoProto *NewIP =
784 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
785 assert(NewIP == 0 && "Shouldn't be in the map!");
786 }
787
788 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
789 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000790 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 return QualType(New, 0);
792}
793
794/// getFunctionType - Return a normal function type with a typed argument
795/// list. isVariadic indicates whether the argument list includes '...'.
796QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
797 unsigned NumArgs, bool isVariadic) {
798 // Unique functions, to guarantee there is only one function of a particular
799 // structure.
800 llvm::FoldingSetNodeID ID;
801 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
802
803 void *InsertPos = 0;
804 if (FunctionTypeProto *FTP =
805 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
806 return QualType(FTP, 0);
807
808 // Determine whether the type being created is already canonical or not.
809 bool isCanonical = ResultTy->isCanonical();
810 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
811 if (!ArgArray[i]->isCanonical())
812 isCanonical = false;
813
814 // If this type isn't canonical, get the canonical version of it.
815 QualType Canonical;
816 if (!isCanonical) {
817 llvm::SmallVector<QualType, 16> CanonicalArgs;
818 CanonicalArgs.reserve(NumArgs);
819 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000820 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000821
Chris Lattnerf52ab252008-04-06 22:59:24 +0000822 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 &CanonicalArgs[0], NumArgs,
824 isVariadic);
825
826 // Get the new insert position for the node we care about.
827 FunctionTypeProto *NewIP =
828 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
829 assert(NewIP == 0 && "Shouldn't be in the map!");
830 }
831
832 // FunctionTypeProto objects are not allocated with new because they have a
833 // variable size array (for parameter types) at the end of them.
834 FunctionTypeProto *FTP =
835 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000836 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
838 Canonical);
839 Types.push_back(FTP);
840 FunctionTypeProtos.InsertNode(FTP, InsertPos);
841 return QualType(FTP, 0);
842}
843
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000844/// getTypeDeclType - Return the unique reference to the type for the
845/// specified type declaration.
846QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
847 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
848
849 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
850 return getTypedefType(Typedef);
851 else if (ObjCInterfaceDecl *ObjCInterface
852 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
853 return getObjCInterfaceType(ObjCInterface);
854 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
855 Decl->TypeForDecl = new RecordType(Record);
856 Types.push_back(Decl->TypeForDecl);
857 return QualType(Decl->TypeForDecl, 0);
858 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
859 Decl->TypeForDecl = new EnumType(Enum);
860 Types.push_back(Decl->TypeForDecl);
861 return QualType(Decl->TypeForDecl, 0);
862 } else
863 assert(false && "TypeDecl without a type?");
864}
865
Reid Spencer5f016e22007-07-11 17:01:13 +0000866/// getTypedefType - Return the unique reference to the type for the
867/// specified typename decl.
868QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
869 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
870
Chris Lattnerf52ab252008-04-06 22:59:24 +0000871 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000872 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 Types.push_back(Decl->TypeForDecl);
874 return QualType(Decl->TypeForDecl, 0);
875}
876
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000877/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000878/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000879QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000880 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
881
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000883 Types.push_back(Decl->TypeForDecl);
884 return QualType(Decl->TypeForDecl, 0);
885}
886
Chris Lattner88cb27a2008-04-07 04:56:42 +0000887/// CmpProtocolNames - Comparison predicate for sorting protocols
888/// alphabetically.
889static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
890 const ObjCProtocolDecl *RHS) {
891 return strcmp(LHS->getName(), RHS->getName()) < 0;
892}
893
894static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
895 unsigned &NumProtocols) {
896 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
897
898 // Sort protocols, keyed by name.
899 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
900
901 // Remove duplicates.
902 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
903 NumProtocols = ProtocolsEnd-Protocols;
904}
905
906
Chris Lattner065f0d72008-04-07 04:44:08 +0000907/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
908/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
910 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000911 // Sort the protocol list alphabetically to canonicalize it.
912 SortAndUniqueProtocols(Protocols, NumProtocols);
913
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000914 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000915 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000916
917 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 if (ObjCQualifiedInterfaceType *QT =
919 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000920 return QualType(QT, 0);
921
922 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923 ObjCQualifiedInterfaceType *QType =
924 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000925 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000927 return QualType(QType, 0);
928}
929
Chris Lattner88cb27a2008-04-07 04:56:42 +0000930/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
931/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000932QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000933 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000934 // Sort the protocol list alphabetically to canonicalize it.
935 SortAndUniqueProtocols(Protocols, NumProtocols);
936
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000937 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000939
940 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000941 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000942 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000943 return QualType(QT, 0);
944
945 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000946 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000947 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000949 return QualType(QType, 0);
950}
951
Steve Naroff9752f252007-08-01 18:02:17 +0000952/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
953/// TypeOfExpr AST's (since expression's are never shared). For example,
954/// multiple declarations that refer to "typeof(x)" all contain different
955/// DeclRefExpr's. This doesn't effect the type checker, since it operates
956/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000957QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000958 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +0000959 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
960 Types.push_back(toe);
961 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000962}
963
Steve Naroff9752f252007-08-01 18:02:17 +0000964/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
965/// TypeOfType AST's. The only motivation to unique these nodes would be
966/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
967/// an issue. This doesn't effect the type checker, since it operates
968/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000969QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000970 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +0000971 TypeOfType *tot = new TypeOfType(tofType, Canonical);
972 Types.push_back(tot);
973 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000974}
975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976/// getTagDeclType - Return the unique reference to the type for the
977/// specified TagDecl (struct/union/class/enum) decl.
978QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000979 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000980 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000981}
982
983/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
984/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
985/// needs to agree with the definition in <stddef.h>.
986QualType ASTContext::getSizeType() const {
987 // On Darwin, size_t is defined as a "long unsigned int".
988 // FIXME: should derive from "Target".
989 return UnsignedLongTy;
990}
991
Eli Friedmanfd888a52008-02-12 08:29:21 +0000992/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
993/// width of characters in wide strings, The value is target dependent and
994/// needs to agree with the definition in <stddef.h>.
995QualType ASTContext::getWcharType() const {
996 // On Darwin, wchar_t is defined as a "int".
997 // FIXME: should derive from "Target".
998 return IntTy;
999}
1000
Chris Lattner8b9023b2007-07-13 03:05:23 +00001001/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1002/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1003QualType ASTContext::getPointerDiffType() const {
1004 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1005 // FIXME: should derive from "Target".
1006 return IntTy;
1007}
1008
Chris Lattnere6327742008-04-02 05:18:44 +00001009//===----------------------------------------------------------------------===//
1010// Type Operators
1011//===----------------------------------------------------------------------===//
1012
Chris Lattner77c96472008-04-06 22:41:35 +00001013/// getCanonicalType - Return the canonical (structural) type corresponding to
1014/// the specified potentially non-canonical type. The non-canonical version
1015/// of a type may have many "decorated" versions of types. Decorators can
1016/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1017/// to be free of any of these, allowing two canonical types to be compared
1018/// for exact equality with a simple pointer comparison.
1019QualType ASTContext::getCanonicalType(QualType T) {
1020 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
1021 return QualType(CanType.getTypePtr(),
1022 T.getCVRQualifiers() | CanType.getCVRQualifiers());
1023}
1024
1025
Chris Lattnere6327742008-04-02 05:18:44 +00001026/// getArrayDecayedType - Return the properly qualified result of decaying the
1027/// specified array type to a pointer. This operation is non-trivial when
1028/// handling typedefs etc. The canonical type of "T" must be an array type,
1029/// this returns a pointer to a properly qualified element of the array.
1030///
1031/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1032QualType ASTContext::getArrayDecayedType(QualType Ty) {
1033 // Handle the common case where typedefs are not involved directly.
1034 QualType EltTy;
1035 unsigned ArrayQuals = 0;
1036 unsigned PointerQuals = 0;
1037 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1038 // Since T "isa" an array type, it could not have had an address space
1039 // qualifier, just CVR qualifiers. The properly qualified element pointer
1040 // gets the union of the CVR qualifiers from the element and the array, and
1041 // keeps any address space qualifier on the element type if present.
1042 EltTy = AT->getElementType();
1043 ArrayQuals = Ty.getCVRQualifiers();
1044 PointerQuals = AT->getIndexTypeQualifier();
1045 } else {
1046 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1047 // lose qualifiers when dealing with typedefs. Example:
1048 // typedef int arr[10];
1049 // void test2() {
1050 // const arr b;
1051 // b[4] = 1;
1052 // }
1053 //
1054 // The decayed type of b is "const int*" even though the element type of the
1055 // array is "int".
Chris Lattnerf52ab252008-04-06 22:59:24 +00001056 QualType CanTy = getCanonicalType(Ty);
Chris Lattnere6327742008-04-02 05:18:44 +00001057 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1058 assert(PrettyArrayType && "Not an array type!");
1059
1060 // Get the element type with 'getAsArrayType' so that we don't lose any
1061 // typedefs in the element type of the array.
1062 EltTy = PrettyArrayType->getElementType();
1063
1064 // If the array was address-space qualifier, make sure to ASQual the element
1065 // type. We can just grab the address space from the canonical type.
1066 if (unsigned AS = CanTy.getAddressSpace())
1067 EltTy = getASQualType(EltTy, AS);
1068
1069 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1070 // the CVR qualifiers directly from the canonical type, which is guaranteed
1071 // to have the full set unioned together.
1072 ArrayQuals = CanTy.getCVRQualifiers();
1073 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1074 }
1075
Chris Lattnerd9654552008-04-02 06:06:35 +00001076 // Apply any CVR qualifiers from the array type to the element type. This
1077 // implements C99 6.7.3p8: "If the specification of an array type includes
1078 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattnere6327742008-04-02 05:18:44 +00001079 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1080
1081 QualType PtrTy = getPointerType(EltTy);
1082
1083 // int x[restrict 4] -> int *restrict
1084 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1085
1086 return PtrTy;
1087}
1088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089/// getFloatingRank - Return a relative rank for floating point types.
1090/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001091static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001092 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001094
Christopher Lambebb97e92008-02-04 02:31:56 +00001095 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001096 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 case BuiltinType::Float: return FloatRank;
1098 case BuiltinType::Double: return DoubleRank;
1099 case BuiltinType::LongDouble: return LongDoubleRank;
1100 }
1101}
1102
Steve Naroff716c7302007-08-27 01:41:48 +00001103/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1104/// point or a complex type (based on typeDomain/typeSize).
1105/// 'typeDomain' is a real floating point or complex type.
1106/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001107QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1108 QualType Domain) const {
1109 FloatingRank EltRank = getFloatingRank(Size);
1110 if (Domain->isComplexType()) {
1111 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001112 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001113 case FloatRank: return FloatComplexTy;
1114 case DoubleRank: return DoubleComplexTy;
1115 case LongDoubleRank: return LongDoubleComplexTy;
1116 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 }
Chris Lattner1361b112008-04-06 23:58:54 +00001118
1119 assert(Domain->isRealFloatingType() && "Unknown domain!");
1120 switch (EltRank) {
1121 default: assert(0 && "getFloatingRank(): illegal value for rank");
1122 case FloatRank: return FloatTy;
1123 case DoubleRank: return DoubleTy;
1124 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001125 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001126}
1127
Chris Lattner7cfeb082008-04-06 23:55:33 +00001128/// getFloatingTypeOrder - Compare the rank of the two specified floating
1129/// point types, ignoring the domain of the type (i.e. 'double' ==
1130/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1131/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001132int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1133 FloatingRank LHSR = getFloatingRank(LHS);
1134 FloatingRank RHSR = getFloatingRank(RHS);
1135
1136 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001137 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001138 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001139 return 1;
1140 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001141}
1142
Chris Lattnerf52ab252008-04-06 22:59:24 +00001143/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1144/// routine will assert if passed a built-in type that isn't an integer or enum,
1145/// or if it is not canonicalized.
1146static unsigned getIntegerRank(Type *T) {
1147 assert(T->isCanonical() && "T should be canonicalized");
1148 if (isa<EnumType>(T))
1149 return 4;
1150
1151 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001152 default: assert(0 && "getIntegerRank(): not a built-in integer");
1153 case BuiltinType::Bool:
1154 return 1;
1155 case BuiltinType::Char_S:
1156 case BuiltinType::Char_U:
1157 case BuiltinType::SChar:
1158 case BuiltinType::UChar:
1159 return 2;
1160 case BuiltinType::Short:
1161 case BuiltinType::UShort:
1162 return 3;
1163 case BuiltinType::Int:
1164 case BuiltinType::UInt:
1165 return 4;
1166 case BuiltinType::Long:
1167 case BuiltinType::ULong:
1168 return 5;
1169 case BuiltinType::LongLong:
1170 case BuiltinType::ULongLong:
1171 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001172 }
1173}
1174
Chris Lattner7cfeb082008-04-06 23:55:33 +00001175/// getIntegerTypeOrder - Returns the highest ranked integer type:
1176/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1177/// LHS < RHS, return -1.
1178int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001179 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1180 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001181 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001182
Chris Lattnerf52ab252008-04-06 22:59:24 +00001183 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1184 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
Chris Lattner7cfeb082008-04-06 23:55:33 +00001186 unsigned LHSRank = getIntegerRank(LHSC);
1187 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188
Chris Lattner7cfeb082008-04-06 23:55:33 +00001189 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1190 if (LHSRank == RHSRank) return 0;
1191 return LHSRank > RHSRank ? 1 : -1;
1192 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
Chris Lattner7cfeb082008-04-06 23:55:33 +00001194 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1195 if (LHSUnsigned) {
1196 // If the unsigned [LHS] type is larger, return it.
1197 if (LHSRank >= RHSRank)
1198 return 1;
1199
1200 // If the signed type can represent all values of the unsigned type, it
1201 // wins. Because we are dealing with 2's complement and types that are
1202 // powers of two larger than each other, this is always safe.
1203 return -1;
1204 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001205
Chris Lattner7cfeb082008-04-06 23:55:33 +00001206 // If the unsigned [RHS] type is larger, return it.
1207 if (RHSRank >= LHSRank)
1208 return -1;
1209
1210 // If the signed type can represent all values of the unsigned type, it
1211 // wins. Because we are dealing with 2's complement and types that are
1212 // powers of two larger than each other, this is always safe.
1213 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001214}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001215
1216// getCFConstantStringType - Return the type used for constant CFStrings.
1217QualType ASTContext::getCFConstantStringType() {
1218 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001219 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001220 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001221 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001222 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001223
1224 // const int *isa;
1225 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001226 // int flags;
1227 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001228 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001229 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001230 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001231 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001232 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001233 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001234
Anders Carlssonf06273f2007-11-19 00:25:30 +00001235 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001236 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001237 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001238
1239 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1240 }
1241
1242 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001243}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001244
Anders Carlssone8c49532007-10-29 06:33:42 +00001245// This returns true if a type has been typedefed to BOOL:
1246// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001247static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001248 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001249 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001250
1251 return false;
1252}
1253
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001254/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001255/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001256int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001257 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001258
1259 // Make all integer and enum types at least as large as an int
1260 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001261 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001262 // Treat arrays as pointers, since that's how they're passed in.
1263 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001264 sz = getTypeSize(VoidPtrTy);
1265 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001266}
1267
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001268/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001269/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001270void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001271 std::string& S)
1272{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001273 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001274 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001275 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001276 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001277 // Compute size of all parameters.
1278 // Start with computing size of a pointer in number of bytes.
1279 // FIXME: There might(should) be a better way of doing this computation!
1280 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001281 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001282 // The first two arguments (self and _cmd) are pointers; account for
1283 // their size.
1284 int ParmOffset = 2 * PtrSize;
1285 int NumOfParams = Decl->getNumParams();
1286 for (int i = 0; i < NumOfParams; i++) {
1287 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001288 int sz = getObjCEncodingTypeSize (PType);
1289 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001290 ParmOffset += sz;
1291 }
1292 S += llvm::utostr(ParmOffset);
1293 S += "@0:";
1294 S += llvm::utostr(PtrSize);
1295
1296 // Argument types.
1297 ParmOffset = 2 * PtrSize;
1298 for (int i = 0; i < NumOfParams; i++) {
1299 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001300 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001301 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 getObjCEncodingForTypeQualifier(
1303 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001304 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001305 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001306 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001307 }
1308}
1309
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001310void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1311 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001312{
Anders Carlssone8c49532007-10-29 06:33:42 +00001313 // FIXME: This currently doesn't encode:
1314 // @ An object (whether statically typed or typed id)
1315 // # A class object (Class)
1316 // : A method selector (SEL)
1317 // {name=type...} A structure
1318 // (name=type...) A union
1319 // bnum A bit field of num bits
1320
1321 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001322 char encoding;
1323 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001324 default: assert(0 && "Unhandled builtin type kind");
1325 case BuiltinType::Void: encoding = 'v'; break;
1326 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001327 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001328 case BuiltinType::UChar: encoding = 'C'; break;
1329 case BuiltinType::UShort: encoding = 'S'; break;
1330 case BuiltinType::UInt: encoding = 'I'; break;
1331 case BuiltinType::ULong: encoding = 'L'; break;
1332 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001333 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001334 case BuiltinType::SChar: encoding = 'c'; break;
1335 case BuiltinType::Short: encoding = 's'; break;
1336 case BuiltinType::Int: encoding = 'i'; break;
1337 case BuiltinType::Long: encoding = 'l'; break;
1338 case BuiltinType::LongLong: encoding = 'q'; break;
1339 case BuiltinType::Float: encoding = 'f'; break;
1340 case BuiltinType::Double: encoding = 'd'; break;
1341 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001342 }
1343
1344 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001345 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001347 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001348 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001349
1350 }
1351 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001352 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001353 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001354 S += '@';
1355 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001356 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001357 S += '#';
1358 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001360 S += ':';
1361 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001362 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001363
1364 if (PointeeTy->isCharType()) {
1365 // char pointer types should be encoded as '*' unless it is a
1366 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001367 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001368 S += '*';
1369 return;
1370 }
1371 }
1372
1373 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001374 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001375 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001376 S += '[';
1377
1378 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1379 S += llvm::utostr(CAT->getSize().getZExtValue());
1380 else
1381 assert(0 && "Unhandled array type!");
1382
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001383 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001384 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001385 } else if (T->getAsFunctionType()) {
1386 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001387 } else if (const RecordType *RTy = T->getAsRecordType()) {
1388 RecordDecl *RDecl= RTy->getDecl();
1389 S += '{';
1390 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001391 bool found = false;
1392 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1393 if (ERType[i] == RTy) {
1394 found = true;
1395 break;
1396 }
1397 if (!found) {
1398 ERType.push_back(RTy);
1399 S += '=';
1400 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1401 FieldDecl *field = RDecl->getMember(i);
1402 getObjCEncodingForType(field->getType(), S, ERType);
1403 }
1404 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1405 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001406 }
1407 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001408 } else if (T->isEnumeralType()) {
1409 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001410 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001411 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001412}
1413
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001415 std::string& S) const {
1416 if (QT & Decl::OBJC_TQ_In)
1417 S += 'n';
1418 if (QT & Decl::OBJC_TQ_Inout)
1419 S += 'N';
1420 if (QT & Decl::OBJC_TQ_Out)
1421 S += 'o';
1422 if (QT & Decl::OBJC_TQ_Bycopy)
1423 S += 'O';
1424 if (QT & Decl::OBJC_TQ_Byref)
1425 S += 'R';
1426 if (QT & Decl::OBJC_TQ_Oneway)
1427 S += 'V';
1428}
1429
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001430void ASTContext::setBuiltinVaListType(QualType T)
1431{
1432 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1433
1434 BuiltinVaListType = T;
1435}
1436
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001438{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001439 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001440
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001442
1443 // typedef struct objc_object *id;
1444 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1445 assert(ptr && "'id' incorrectly typed");
1446 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1447 assert(rec && "'id' incorrectly typed");
1448 IdStructType = rec;
1449}
1450
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001451void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001452{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001453 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001454
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001455 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001456
1457 // typedef struct objc_selector *SEL;
1458 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1459 assert(ptr && "'SEL' incorrectly typed");
1460 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1461 assert(rec && "'SEL' incorrectly typed");
1462 SelStructType = rec;
1463}
1464
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001466{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001467 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1468 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001469}
1470
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001472{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001474
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001476
1477 // typedef struct objc_class *Class;
1478 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1479 assert(ptr && "'Class' incorrectly typed");
1480 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1481 assert(rec && "'Class' incorrectly typed");
1482 ClassStructType = rec;
1483}
1484
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1486 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001487 "'NSConstantString' type already set!");
1488
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001489 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001490}
1491
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001492
1493//===----------------------------------------------------------------------===//
1494// Type Predicates.
1495//===----------------------------------------------------------------------===//
1496
1497/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1498/// to an object type. This includes "id" and "Class" (two 'special' pointers
1499/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1500/// ID type).
1501bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1502 if (Ty->isObjCQualifiedIdType())
1503 return true;
1504
1505 if (!Ty->isPointerType())
1506 return false;
1507
1508 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1509 // pointer types. This looks for the typedef specifically, not for the
1510 // underlying type.
1511 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1512 return true;
1513
1514 // If this a pointer to an interface (e.g. NSString*), it is ok.
1515 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1516}
1517
Chris Lattner6ac46a42008-04-07 06:51:04 +00001518//===----------------------------------------------------------------------===//
1519// Type Compatibility Testing
1520//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001521
Chris Lattner78eca282008-04-07 06:49:41 +00001522/// C99 6.2.7p1: If both are complete types, then the following additional
1523/// requirements apply.
1524/// FIXME (handle compatibility across source files).
1525static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1526 const ASTContext &C) {
Steve Naroffab373092007-11-07 06:03:51 +00001527 // "Class" and "id" are compatible built-in structure types.
Chris Lattner78eca282008-04-07 06:49:41 +00001528 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1529 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroffab373092007-11-07 06:03:51 +00001530 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001531
Chris Lattner78eca282008-04-07 06:49:41 +00001532 // Within a translation unit a tag type is only compatible with itself. Self
1533 // equality is already handled by the time we get here.
1534 assert(LHS != RHS && "Self equality not handled!");
1535 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001536}
1537
1538bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1539 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1540 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001541 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1542 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001543 return false;
1544
Chris Lattner28be73f2008-07-26 21:30:36 +00001545 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1546 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001547
1548 return typesAreCompatible(ltype, rtype);
1549}
1550
Steve Naroffec0550f2007-10-15 20:41:53 +00001551bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001552 const FunctionType *lbase = lhs->getAsFunctionType();
1553 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001554 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1555 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1556
1557 // first check the return types (common between C99 and K&R).
1558 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1559 return false;
1560
1561 if (lproto && rproto) { // two C99 style function prototypes
1562 unsigned lproto_nargs = lproto->getNumArgs();
1563 unsigned rproto_nargs = rproto->getNumArgs();
1564
1565 if (lproto_nargs != rproto_nargs)
1566 return false;
1567
1568 // both prototypes have the same number of arguments.
1569 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1570 (rproto->isVariadic() && !lproto->isVariadic()))
1571 return false;
1572
1573 // The use of ellipsis agree...now check the argument types.
1574 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001575 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1576 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001577 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001578 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001579 return false;
1580 return true;
1581 }
Chris Lattner5426bf62008-04-07 07:01:58 +00001582
Steve Naroffec0550f2007-10-15 20:41:53 +00001583 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1584 return true;
1585
1586 // we have a mixture of K&R style with C99 prototypes
1587 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroffec0550f2007-10-15 20:41:53 +00001588 if (proto->isVariadic())
1589 return false;
1590
1591 // FIXME: Each parameter type T in the prototype must be compatible with the
1592 // type resulting from applying the usual argument conversions to T.
1593 return true;
1594}
1595
Chris Lattneracc99722008-04-07 06:56:55 +00001596// C99 6.7.5.2p6
1597static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattneracc99722008-04-07 06:56:55 +00001598 // Constant arrays must be the same size to be compatible.
1599 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1600 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1601 if (RCAT->getSize() != LCAT->getSize())
1602 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001603
Chris Lattner8c7bbb52008-04-07 06:58:21 +00001604 // Compatible arrays must have compatible element types
1605 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroffec0550f2007-10-15 20:41:53 +00001606}
1607
Chris Lattner6ac46a42008-04-07 06:51:04 +00001608/// areCompatVectorTypes - Return true if the two specified vector types are
1609/// compatible.
1610static bool areCompatVectorTypes(const VectorType *LHS,
1611 const VectorType *RHS) {
1612 assert(LHS->isCanonical() && RHS->isCanonical());
1613 return LHS->getElementType() == RHS->getElementType() &&
1614 LHS->getNumElements() == RHS->getNumElements();
1615}
1616
1617/// areCompatObjCInterfaces - Return true if the two interface types are
1618/// compatible for assignment from RHS to LHS. This handles validation of any
1619/// protocol qualifiers on the LHS or RHS.
1620///
Chris Lattner5426bf62008-04-07 07:01:58 +00001621static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1622 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001623 // Verify that the base decls are compatible: the RHS must be a subclass of
1624 // the LHS.
1625 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1626 return false;
1627
1628 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1629 // protocol qualified at all, then we are good.
1630 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1631 return true;
1632
1633 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1634 // isn't a superset.
1635 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1636 return true; // FIXME: should return false!
1637
1638 // Finally, we must have two protocol-qualified interfaces.
1639 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1640 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1641 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1642 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1643 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1644 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1645
1646 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1647 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1648 // LHS in a single parallel scan until we run out of elements in LHS.
1649 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1650 ObjCProtocolDecl *LHSProto = *LHSPI;
1651
1652 while (RHSPI != RHSPE) {
1653 ObjCProtocolDecl *RHSProto = *RHSPI++;
1654 // If the RHS has a protocol that the LHS doesn't, ignore it.
1655 if (RHSProto != LHSProto)
1656 continue;
1657
1658 // Otherwise, the RHS does have this element.
1659 ++LHSPI;
1660 if (LHSPI == LHSPE)
1661 return true; // All protocols in LHS exist in RHS.
1662
1663 LHSProto = *LHSPI;
1664 }
1665
1666 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1667 return false;
1668}
1669
1670
Steve Naroffec0550f2007-10-15 20:41:53 +00001671/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1672/// both shall have the identically qualified version of a compatible type.
1673/// C99 6.2.7p1: Two types have compatible types if their types are the
1674/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001675bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001676 QualType LHS = getCanonicalType(LHS_NC);
1677 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner988ee6e2008-04-03 05:07:04 +00001678
Bill Wendling43d69752007-12-03 07:33:35 +00001679 // C++ [expr]: If an expression initially has the type "reference to T", the
1680 // type is adjusted to "T" prior to any further analysis, the expression
1681 // designates the object or function denoted by the reference, and the
1682 // expression is an lvalue.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001683 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1684 LHS = RT->getPointeeType();
1685 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1686 RHS = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001687
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001688 // If two types are identical, they are compatible.
1689 if (LHS == RHS)
1690 return true;
1691
1692 // If qualifiers differ, the types are different.
Chris Lattnera36a61f2008-04-07 05:43:21 +00001693 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1694 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001695 return false;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001696
1697 // Strip off ASQual's if present.
1698 if (LHSAS) {
1699 LHS = LHS.getUnqualifiedType();
1700 RHS = RHS.getUnqualifiedType();
1701 }
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001702
Chris Lattnerc4e40592008-04-07 04:07:56 +00001703 Type::TypeClass LHSClass = LHS->getTypeClass();
1704 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattner1adb8832008-01-14 05:45:46 +00001705
1706 // We want to consider the two function types to be the same for these
1707 // comparisons, just force one to the other.
1708 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1709 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001710
1711 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001712 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1713 LHSClass = Type::ConstantArray;
1714 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1715 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001716
Nate Begeman213541a2008-04-18 23:10:10 +00001717 // Canonicalize ExtVector -> Vector.
1718 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1719 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001720
Chris Lattnerb0489812008-04-07 06:38:24 +00001721 // Consider qualified interfaces and interfaces the same.
1722 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1723 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1724
Chris Lattnera36a61f2008-04-07 05:43:21 +00001725 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001726 if (LHSClass != RHSClass) {
Chris Lattnerb0489812008-04-07 06:38:24 +00001727 // ID is compatible with all interface types.
1728 if (isa<ObjCInterfaceType>(LHS))
1729 return isObjCIdType(RHS);
1730 if (isa<ObjCInterfaceType>(RHS))
1731 return isObjCIdType(LHS);
Steve Naroff97341622008-06-04 15:07:33 +00001732
1733 // ID is compatible with all qualified id types.
1734 if (isa<ObjCQualifiedIdType>(LHS)) {
1735 if (const PointerType *PT = RHS->getAsPointerType())
1736 return isObjCIdType(PT->getPointeeType());
1737 }
1738 if (isa<ObjCQualifiedIdType>(RHS)) {
1739 if (const PointerType *PT = LHS->getAsPointerType())
1740 return isObjCIdType(PT->getPointeeType());
1741 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001742 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1743 // a signed integer type, or an unsigned integer type.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001744 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1745 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1746 return EDecl->getIntegerType() == RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001747 }
Chris Lattnerc4e40592008-04-07 04:07:56 +00001748 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1749 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1750 return EDecl->getIntegerType() == LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001751 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001752
Steve Naroffec0550f2007-10-15 20:41:53 +00001753 return false;
1754 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00001755
Steve Naroff4a746782008-01-09 22:43:08 +00001756 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001757 switch (LHSClass) {
Chris Lattnera36a61f2008-04-07 05:43:21 +00001758 case Type::ASQual:
1759 case Type::FunctionProto:
1760 case Type::VariableArray:
1761 case Type::IncompleteArray:
1762 case Type::Reference:
Chris Lattnerb0489812008-04-07 06:38:24 +00001763 case Type::ObjCQualifiedInterface:
Chris Lattnera36a61f2008-04-07 05:43:21 +00001764 assert(0 && "Canonicalized away above");
Chris Lattner1adb8832008-01-14 05:45:46 +00001765 case Type::Pointer:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001766 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001767 case Type::ConstantArray:
Chris Lattneracc99722008-04-07 06:56:55 +00001768 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1769 *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001770 case Type::FunctionNoProto:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001771 return functionTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001772 case Type::Tagged: // handle structures, unions
Chris Lattner78eca282008-04-07 06:49:41 +00001773 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001774 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00001775 // Only exactly equal builtin types are compatible, which is tested above.
1776 return false;
1777 case Type::Vector:
1778 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001779 case Type::ObjCInterface:
Chris Lattnerb0489812008-04-07 06:38:24 +00001780 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1781 cast<ObjCInterfaceType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001782 default:
1783 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001784 }
1785 return true; // should never get here...
1786}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001787
Chris Lattner5426bf62008-04-07 07:01:58 +00001788//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00001789// Integer Predicates
1790//===----------------------------------------------------------------------===//
1791unsigned ASTContext::getIntWidth(QualType T) {
1792 if (T == BoolTy)
1793 return 1;
1794 // At the moment, only bool has padding bits
1795 return (unsigned)getTypeSize(T);
1796}
1797
1798QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1799 assert(T->isSignedIntegerType() && "Unexpected type");
1800 if (const EnumType* ETy = T->getAsEnumType())
1801 T = ETy->getDecl()->getIntegerType();
1802 const BuiltinType* BTy = T->getAsBuiltinType();
1803 assert (BTy && "Unexpected signed integer type");
1804 switch (BTy->getKind()) {
1805 case BuiltinType::Char_S:
1806 case BuiltinType::SChar:
1807 return UnsignedCharTy;
1808 case BuiltinType::Short:
1809 return UnsignedShortTy;
1810 case BuiltinType::Int:
1811 return UnsignedIntTy;
1812 case BuiltinType::Long:
1813 return UnsignedLongTy;
1814 case BuiltinType::LongLong:
1815 return UnsignedLongLongTy;
1816 default:
1817 assert(0 && "Unexpected signed integer type");
1818 return QualType();
1819 }
1820}
1821
1822
1823//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00001824// Serialization Support
1825//===----------------------------------------------------------------------===//
1826
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001827/// Emit - Serialize an ASTContext object to Bitcode.
1828void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001829 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00001830 S.EmitRef(SourceMgr);
1831 S.EmitRef(Target);
1832 S.EmitRef(Idents);
1833 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001834
Ted Kremenekfee04522007-10-31 22:44:07 +00001835 // Emit the size of the type vector so that we can reserve that size
1836 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001837 S.EmitInt(Types.size());
1838
Ted Kremenek03ed4402007-11-13 22:02:55 +00001839 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1840 I!=E;++I)
1841 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001842
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001843 S.EmitOwnedPtr(TUDecl);
1844
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001845 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001846}
1847
Ted Kremenek0f84c002007-11-13 00:25:37 +00001848ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001849
1850 // Read the language options.
1851 LangOptions LOpts;
1852 LOpts.Read(D);
1853
Ted Kremenekfee04522007-10-31 22:44:07 +00001854 SourceManager &SM = D.ReadRef<SourceManager>();
1855 TargetInfo &t = D.ReadRef<TargetInfo>();
1856 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1857 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001858
Ted Kremenekfee04522007-10-31 22:44:07 +00001859 unsigned size_reserve = D.ReadInt();
1860
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001861 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00001862
Ted Kremenek03ed4402007-11-13 22:02:55 +00001863 for (unsigned i = 0; i < size_reserve; ++i)
1864 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001865
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001866 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1867
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001868 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001869
1870 return A;
1871}