blob: 08a2bb1357ba732fcd19128747aa9ff01e2b333c [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
185/// getTypeSize - Return the size of the specified type, in bits. This method
186/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000187std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000188ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000189 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000190 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000191 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000192 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000193 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000194 case Type::FunctionNoProto:
195 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000196 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000197 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000198 case Type::VariableArray:
199 assert(0 && "VLAs not implemented yet!");
200 case Type::ConstantArray: {
201 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
202
Chris Lattner98be4942008-03-05 18:54:05 +0000203 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000204 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000205 Align = EltInfo.second;
206 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000207 }
Nate Begeman213541a2008-04-18 23:10:10 +0000208 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000209 case Type::Vector: {
210 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000211 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000212 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000213 // FIXME: This isn't right for unusual vectors
214 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000215 break;
216 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000217
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000218 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000219 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000220 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000221 case BuiltinType::Void:
222 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000223 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000224 Width = Target.getBoolWidth();
225 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000226 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000227 case BuiltinType::Char_S:
228 case BuiltinType::Char_U:
229 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000230 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000231 Width = Target.getCharWidth();
232 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000233 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000234 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000235 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000236 Width = Target.getShortWidth();
237 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000238 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000239 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000241 Width = Target.getIntWidth();
242 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000243 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000244 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000245 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000246 Width = Target.getLongWidth();
247 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000248 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000249 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000250 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000251 Width = Target.getLongLongWidth();
252 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000253 break;
254 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000255 Width = Target.getFloatWidth();
256 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000257 break;
258 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000259 Width = Target.getDoubleWidth();
260 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000261 break;
262 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000263 Width = Target.getLongDoubleWidth();
264 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000265 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000266 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000267 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000268 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000269 // FIXME: Pointers into different addr spaces could have different sizes and
270 // alignment requirements: getPointerInfo should take an AddrSpace.
271 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000273 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000274 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000275 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000276 case Type::Pointer: {
277 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000278 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000279 Align = Target.getPointerAlign(AS);
280 break;
281 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000282 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000283 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000284 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000285 // FIXME: This is wrong for struct layout: a reference in a struct has
286 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000287 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000288
289 case Type::Complex: {
290 // Complex types have the same alignment as their elements, but twice the
291 // size.
292 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000293 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000294 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000295 Align = EltInfo.second;
296 break;
297 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000298 case Type::ObjCInterface: {
299 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
300 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
301 Width = Layout.getSize();
302 Align = Layout.getAlignment();
303 break;
304 }
Chris Lattner71763312008-04-06 22:05:18 +0000305 case Type::Tagged: {
306 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
307 return getTypeInfo(ET->getDecl()->getIntegerType());
308
309 RecordType *RT = cast<RecordType>(T);
310 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
311 Width = Layout.getSize();
312 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000313 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000314 }
Chris Lattner71763312008-04-06 22:05:18 +0000315 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000316
Chris Lattner464175b2007-07-18 17:52:12 +0000317 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000318 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000319}
320
Devang Patel8b277042008-06-04 21:22:16 +0000321/// LayoutField - Field layout.
322void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
323 bool IsUnion, bool StructIsPacked,
324 ASTContext &Context) {
325 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
326 uint64_t FieldOffset = IsUnion ? 0 : Size;
327 uint64_t FieldSize;
328 unsigned FieldAlign;
329
330 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
331 // TODO: Need to check this algorithm on other targets!
332 // (tested on Linux-X86)
333 llvm::APSInt I(32);
334 bool BitWidthIsICE =
335 BitWidthExpr->isIntegerConstantExpr(I, Context);
336 assert (BitWidthIsICE && "Invalid BitField size expression");
337 FieldSize = I.getZExtValue();
338
339 std::pair<uint64_t, unsigned> FieldInfo =
340 Context.getTypeInfo(FD->getType());
341 uint64_t TypeSize = FieldInfo.first;
342
343 FieldAlign = FieldInfo.second;
344 if (FieldIsPacked)
345 FieldAlign = 1;
346 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
347 FieldAlign = std::max(FieldAlign, AA->getAlignment());
348
349 // Check if we need to add padding to give the field the correct
350 // alignment.
351 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
352 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
353
354 // Padding members don't affect overall alignment
355 if (!FD->getIdentifier())
356 FieldAlign = 1;
357 } else {
358 if (FD->getType()->isIncompleteType()) {
359 // This must be a flexible array member; we can't directly
360 // query getTypeInfo about these, so we figure it out here.
361 // Flexible array members don't have any size, but they
362 // have to be aligned appropriately for their element type.
363 FieldSize = 0;
364 const ArrayType* ATy = FD->getType()->getAsArrayType();
365 FieldAlign = Context.getTypeAlign(ATy->getElementType());
366 } else {
367 std::pair<uint64_t, unsigned> FieldInfo =
368 Context.getTypeInfo(FD->getType());
369 FieldSize = FieldInfo.first;
370 FieldAlign = FieldInfo.second;
371 }
372
373 if (FieldIsPacked)
374 FieldAlign = 8;
375 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
376 FieldAlign = std::max(FieldAlign, AA->getAlignment());
377
378 // Round up the current record size to the field's alignment boundary.
379 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
380 }
381
382 // Place this field at the current location.
383 FieldOffsets[FieldNo] = FieldOffset;
384
385 // Reserve space for this field.
386 if (IsUnion) {
387 Size = std::max(Size, FieldSize);
388 } else {
389 Size = FieldOffset + FieldSize;
390 }
391
392 // Remember max struct/class alignment.
393 Alignment = std::max(Alignment, FieldAlign);
394}
395
Devang Patel44a3dde2008-06-04 21:54:36 +0000396
397/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
398/// specified Objective C, which indicates its size and ivar
399/// position information.
400const ASTRecordLayout &
401ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
402 // Look up this layout, if already laid out, return what we have.
403 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
404 if (Entry) return *Entry;
405
406 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
407 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000408 ASTRecordLayout *NewEntry = NULL;
409 unsigned FieldCount = D->ivar_size();
410 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
411 FieldCount++;
412 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
413 unsigned Alignment = SL.getAlignment();
414 uint64_t Size = SL.getSize();
415 NewEntry = new ASTRecordLayout(Size, Alignment);
416 NewEntry->InitializeLayout(FieldCount);
417 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
418 } else {
419 NewEntry = new ASTRecordLayout();
420 NewEntry->InitializeLayout(FieldCount);
421 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000422 Entry = NewEntry;
423
Devang Patel44a3dde2008-06-04 21:54:36 +0000424 bool IsPacked = D->getAttr<PackedAttr>();
425
426 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
427 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
428 AA->getAlignment()));
429
430 // Layout each ivar sequentially.
431 unsigned i = 0;
432 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
433 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
434 const ObjCIvarDecl* Ivar = (*IVI);
435 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
436 }
437
438 // Finally, round the size of the total struct up to the alignment of the
439 // struct itself.
440 NewEntry->FinalizeLayout();
441 return *NewEntry;
442}
443
Devang Patel88a981b2007-11-01 19:11:01 +0000444/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000445/// specified record (struct/union/class), which indicates its size and field
446/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000447const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000448 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000449
Chris Lattner464175b2007-07-18 17:52:12 +0000450 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000451 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000452 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000453
Devang Patel88a981b2007-11-01 19:11:01 +0000454 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
455 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
456 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000457 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000458
Devang Patel8b277042008-06-04 21:22:16 +0000459 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000460 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000461 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000462
Eli Friedman4bd998b2008-05-30 09:31:38 +0000463 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000464 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
465 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000466
Eli Friedman4bd998b2008-05-30 09:31:38 +0000467 // Layout each field, for now, just sequentially, respecting alignment. In
468 // the future, this will need to be tweakable by targets.
469 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
470 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000471 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000472 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000473
474 // Finally, round the size of the total struct up to the alignment of the
475 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000476 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000477 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000478}
479
Chris Lattnera7674d82007-07-13 22:13:22 +0000480//===----------------------------------------------------------------------===//
481// Type creation/memoization methods
482//===----------------------------------------------------------------------===//
483
Christopher Lambebb97e92008-02-04 02:31:56 +0000484QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000485 QualType CanT = getCanonicalType(T);
486 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000487 return T;
488
489 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
490 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000491 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000492 "Type is already address space qualified");
493
494 // Check if we've already instantiated an address space qual'd type of this
495 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000496 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000497 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000498 void *InsertPos = 0;
499 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
500 return QualType(ASQy, 0);
501
502 // If the base type isn't canonical, this won't be a canonical type either,
503 // so fill in the canonical type field.
504 QualType Canonical;
505 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000506 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000507
508 // Get the new insert position for the node we care about.
509 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
510 assert(NewIP == 0 && "Shouldn't be in the map!");
511 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000512 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000513 ASQualTypes.InsertNode(New, InsertPos);
514 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000515 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000516}
517
Chris Lattnera7674d82007-07-13 22:13:22 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519/// getComplexType - Return the uniqued reference to the type for a complex
520/// number with the specified element type.
521QualType ASTContext::getComplexType(QualType T) {
522 // Unique pointers, to guarantee there is only one pointer of a particular
523 // structure.
524 llvm::FoldingSetNodeID ID;
525 ComplexType::Profile(ID, T);
526
527 void *InsertPos = 0;
528 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
529 return QualType(CT, 0);
530
531 // If the pointee type isn't canonical, this won't be a canonical type either,
532 // so fill in the canonical type field.
533 QualType Canonical;
534 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000535 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000536
537 // Get the new insert position for the node we care about.
538 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
539 assert(NewIP == 0 && "Shouldn't be in the map!");
540 }
541 ComplexType *New = new ComplexType(T, Canonical);
542 Types.push_back(New);
543 ComplexTypes.InsertNode(New, InsertPos);
544 return QualType(New, 0);
545}
546
547
548/// getPointerType - Return the uniqued reference to the type for a pointer to
549/// the specified type.
550QualType ASTContext::getPointerType(QualType T) {
551 // Unique pointers, to guarantee there is only one pointer of a particular
552 // structure.
553 llvm::FoldingSetNodeID ID;
554 PointerType::Profile(ID, T);
555
556 void *InsertPos = 0;
557 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
558 return QualType(PT, 0);
559
560 // If the pointee type isn't canonical, this won't be a canonical type either,
561 // so fill in the canonical type field.
562 QualType Canonical;
563 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000564 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000565
566 // Get the new insert position for the node we care about.
567 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
568 assert(NewIP == 0 && "Shouldn't be in the map!");
569 }
570 PointerType *New = new PointerType(T, Canonical);
571 Types.push_back(New);
572 PointerTypes.InsertNode(New, InsertPos);
573 return QualType(New, 0);
574}
575
576/// getReferenceType - Return the uniqued reference to the type for a reference
577/// to the specified type.
578QualType ASTContext::getReferenceType(QualType T) {
579 // Unique pointers, to guarantee there is only one pointer of a particular
580 // structure.
581 llvm::FoldingSetNodeID ID;
582 ReferenceType::Profile(ID, T);
583
584 void *InsertPos = 0;
585 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
586 return QualType(RT, 0);
587
588 // If the referencee type isn't canonical, this won't be a canonical type
589 // either, so fill in the canonical type field.
590 QualType Canonical;
591 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000592 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000593
594 // Get the new insert position for the node we care about.
595 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
596 assert(NewIP == 0 && "Shouldn't be in the map!");
597 }
598
599 ReferenceType *New = new ReferenceType(T, Canonical);
600 Types.push_back(New);
601 ReferenceTypes.InsertNode(New, InsertPos);
602 return QualType(New, 0);
603}
604
Steve Narofffb22d962007-08-30 01:06:46 +0000605/// getConstantArrayType - Return the unique reference to the type for an
606/// array of the specified element type.
607QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000608 const llvm::APInt &ArySize,
609 ArrayType::ArraySizeModifier ASM,
610 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000612 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613
614 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000615 if (ConstantArrayType *ATP =
616 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return QualType(ATP, 0);
618
619 // If the element type isn't canonical, this won't be a canonical type either,
620 // so fill in the canonical type field.
621 QualType Canonical;
622 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000623 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000624 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000626 ConstantArrayType *NewIP =
627 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
628
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 assert(NewIP == 0 && "Shouldn't be in the map!");
630 }
631
Steve Naroffc9406122007-08-30 18:10:14 +0000632 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
633 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000634 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Types.push_back(New);
636 return QualType(New, 0);
637}
638
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000639/// getVariableArrayType - Returns a non-unique reference to the type for a
640/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000641QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
642 ArrayType::ArraySizeModifier ASM,
643 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000644 // Since we don't unique expressions, it isn't possible to unique VLA's
645 // that have an expression provided for their size.
646
647 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
648 ASM, EltTypeQuals);
649
650 VariableArrayTypes.push_back(New);
651 Types.push_back(New);
652 return QualType(New, 0);
653}
654
655QualType ASTContext::getIncompleteArrayType(QualType EltTy,
656 ArrayType::ArraySizeModifier ASM,
657 unsigned EltTypeQuals) {
658 llvm::FoldingSetNodeID ID;
659 IncompleteArrayType::Profile(ID, EltTy);
660
661 void *InsertPos = 0;
662 if (IncompleteArrayType *ATP =
663 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
664 return QualType(ATP, 0);
665
666 // If the element type isn't canonical, this won't be a canonical type
667 // either, so fill in the canonical type field.
668 QualType Canonical;
669
670 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000671 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000672 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000673
674 // Get the new insert position for the node we care about.
675 IncompleteArrayType *NewIP =
676 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
677
678 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000679 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000680
681 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
682 ASM, EltTypeQuals);
683
684 IncompleteArrayTypes.InsertNode(New, InsertPos);
685 Types.push_back(New);
686 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000687}
688
Steve Naroff73322922007-07-18 18:00:27 +0000689/// getVectorType - Return the unique reference to a vector type of
690/// the specified element type and size. VectorType must be a built-in type.
691QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 BuiltinType *baseType;
693
Chris Lattnerf52ab252008-04-06 22:59:24 +0000694 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000695 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000696
697 // Check if we've already instantiated a vector of this type.
698 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000699 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 void *InsertPos = 0;
701 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
702 return QualType(VTP, 0);
703
704 // If the element type isn't canonical, this won't be a canonical type either,
705 // so fill in the canonical type field.
706 QualType Canonical;
707 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000708 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
710 // Get the new insert position for the node we care about.
711 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
712 assert(NewIP == 0 && "Shouldn't be in the map!");
713 }
714 VectorType *New = new VectorType(vecType, NumElts, Canonical);
715 VectorTypes.InsertNode(New, InsertPos);
716 Types.push_back(New);
717 return QualType(New, 0);
718}
719
Nate Begeman213541a2008-04-18 23:10:10 +0000720/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000721/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000722QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000723 BuiltinType *baseType;
724
Chris Lattnerf52ab252008-04-06 22:59:24 +0000725 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000726 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000727
728 // Check if we've already instantiated a vector of this type.
729 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000730 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000731 void *InsertPos = 0;
732 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
733 return QualType(VTP, 0);
734
735 // If the element type isn't canonical, this won't be a canonical type either,
736 // so fill in the canonical type field.
737 QualType Canonical;
738 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000739 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000740
741 // Get the new insert position for the node we care about.
742 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
743 assert(NewIP == 0 && "Shouldn't be in the map!");
744 }
Nate Begeman213541a2008-04-18 23:10:10 +0000745 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000746 VectorTypes.InsertNode(New, InsertPos);
747 Types.push_back(New);
748 return QualType(New, 0);
749}
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
752///
753QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
754 // Unique functions, to guarantee there is only one function of a particular
755 // structure.
756 llvm::FoldingSetNodeID ID;
757 FunctionTypeNoProto::Profile(ID, ResultTy);
758
759 void *InsertPos = 0;
760 if (FunctionTypeNoProto *FT =
761 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
762 return QualType(FT, 0);
763
764 QualType Canonical;
765 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000766 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
768 // Get the new insert position for the node we care about.
769 FunctionTypeNoProto *NewIP =
770 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
771 assert(NewIP == 0 && "Shouldn't be in the map!");
772 }
773
774 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
775 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000776 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 return QualType(New, 0);
778}
779
780/// getFunctionType - Return a normal function type with a typed argument
781/// list. isVariadic indicates whether the argument list includes '...'.
782QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
783 unsigned NumArgs, bool isVariadic) {
784 // Unique functions, to guarantee there is only one function of a particular
785 // structure.
786 llvm::FoldingSetNodeID ID;
787 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
788
789 void *InsertPos = 0;
790 if (FunctionTypeProto *FTP =
791 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
792 return QualType(FTP, 0);
793
794 // Determine whether the type being created is already canonical or not.
795 bool isCanonical = ResultTy->isCanonical();
796 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
797 if (!ArgArray[i]->isCanonical())
798 isCanonical = false;
799
800 // If this type isn't canonical, get the canonical version of it.
801 QualType Canonical;
802 if (!isCanonical) {
803 llvm::SmallVector<QualType, 16> CanonicalArgs;
804 CanonicalArgs.reserve(NumArgs);
805 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000806 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
Chris Lattnerf52ab252008-04-06 22:59:24 +0000808 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 &CanonicalArgs[0], NumArgs,
810 isVariadic);
811
812 // Get the new insert position for the node we care about.
813 FunctionTypeProto *NewIP =
814 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
815 assert(NewIP == 0 && "Shouldn't be in the map!");
816 }
817
818 // FunctionTypeProto objects are not allocated with new because they have a
819 // variable size array (for parameter types) at the end of them.
820 FunctionTypeProto *FTP =
821 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000822 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
824 Canonical);
825 Types.push_back(FTP);
826 FunctionTypeProtos.InsertNode(FTP, InsertPos);
827 return QualType(FTP, 0);
828}
829
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000830/// getTypeDeclType - Return the unique reference to the type for the
831/// specified type declaration.
832QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
833 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
834
835 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
836 return getTypedefType(Typedef);
837 else if (ObjCInterfaceDecl *ObjCInterface
838 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
839 return getObjCInterfaceType(ObjCInterface);
840 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
841 Decl->TypeForDecl = new RecordType(Record);
842 Types.push_back(Decl->TypeForDecl);
843 return QualType(Decl->TypeForDecl, 0);
844 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
845 Decl->TypeForDecl = new EnumType(Enum);
846 Types.push_back(Decl->TypeForDecl);
847 return QualType(Decl->TypeForDecl, 0);
848 } else
849 assert(false && "TypeDecl without a type?");
850}
851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852/// getTypedefType - Return the unique reference to the type for the
853/// specified typename decl.
854QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
855 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
856
Chris Lattnerf52ab252008-04-06 22:59:24 +0000857 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000858 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 Types.push_back(Decl->TypeForDecl);
860 return QualType(Decl->TypeForDecl, 0);
861}
862
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000863/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000864/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000866 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
867
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000869 Types.push_back(Decl->TypeForDecl);
870 return QualType(Decl->TypeForDecl, 0);
871}
872
Chris Lattner88cb27a2008-04-07 04:56:42 +0000873/// CmpProtocolNames - Comparison predicate for sorting protocols
874/// alphabetically.
875static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
876 const ObjCProtocolDecl *RHS) {
877 return strcmp(LHS->getName(), RHS->getName()) < 0;
878}
879
880static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
881 unsigned &NumProtocols) {
882 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
883
884 // Sort protocols, keyed by name.
885 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
886
887 // Remove duplicates.
888 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
889 NumProtocols = ProtocolsEnd-Protocols;
890}
891
892
Chris Lattner065f0d72008-04-07 04:44:08 +0000893/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
894/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
896 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000897 // Sort the protocol list alphabetically to canonicalize it.
898 SortAndUniqueProtocols(Protocols, NumProtocols);
899
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000900 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000901 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000902
903 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904 if (ObjCQualifiedInterfaceType *QT =
905 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000906 return QualType(QT, 0);
907
908 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909 ObjCQualifiedInterfaceType *QType =
910 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000911 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000913 return QualType(QType, 0);
914}
915
Chris Lattner88cb27a2008-04-07 04:56:42 +0000916/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
917/// and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918QualType ASTContext::getObjCQualifiedIdType(QualType idType,
919 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000920 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000921 // Sort the protocol list alphabetically to canonicalize it.
922 SortAndUniqueProtocols(Protocols, NumProtocols);
923
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000924 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000925 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000926
927 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000928 if (ObjCQualifiedIdType *QT =
929 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000930 return QualType(QT, 0);
931
932 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000933 QualType Canonical;
934 if (!idType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000935 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000936 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000937 ObjCQualifiedIdType *NewQT =
938 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000939 assert(NewQT == 0 && "Shouldn't be in the map!");
940 }
941
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942 ObjCQualifiedIdType *QType =
943 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000944 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000946 return QualType(QType, 0);
947}
948
Steve Naroff9752f252007-08-01 18:02:17 +0000949/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
950/// TypeOfExpr AST's (since expression's are never shared). For example,
951/// multiple declarations that refer to "typeof(x)" all contain different
952/// DeclRefExpr's. This doesn't effect the type checker, since it operates
953/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000954QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000955 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +0000956 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
957 Types.push_back(toe);
958 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000959}
960
Steve Naroff9752f252007-08-01 18:02:17 +0000961/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
962/// TypeOfType AST's. The only motivation to unique these nodes would be
963/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
964/// an issue. This doesn't effect the type checker, since it operates
965/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000966QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000967 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +0000968 TypeOfType *tot = new TypeOfType(tofType, Canonical);
969 Types.push_back(tot);
970 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000971}
972
Reid Spencer5f016e22007-07-11 17:01:13 +0000973/// getTagDeclType - Return the unique reference to the type for the
974/// specified TagDecl (struct/union/class/enum) decl.
975QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000976 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000977 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
979
980/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
981/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
982/// needs to agree with the definition in <stddef.h>.
983QualType ASTContext::getSizeType() const {
984 // On Darwin, size_t is defined as a "long unsigned int".
985 // FIXME: should derive from "Target".
986 return UnsignedLongTy;
987}
988
Eli Friedmanfd888a52008-02-12 08:29:21 +0000989/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
990/// width of characters in wide strings, The value is target dependent and
991/// needs to agree with the definition in <stddef.h>.
992QualType ASTContext::getWcharType() const {
993 // On Darwin, wchar_t is defined as a "int".
994 // FIXME: should derive from "Target".
995 return IntTy;
996}
997
Chris Lattner8b9023b2007-07-13 03:05:23 +0000998/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
999/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1000QualType ASTContext::getPointerDiffType() const {
1001 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1002 // FIXME: should derive from "Target".
1003 return IntTy;
1004}
1005
Chris Lattnere6327742008-04-02 05:18:44 +00001006//===----------------------------------------------------------------------===//
1007// Type Operators
1008//===----------------------------------------------------------------------===//
1009
Chris Lattner77c96472008-04-06 22:41:35 +00001010/// getCanonicalType - Return the canonical (structural) type corresponding to
1011/// the specified potentially non-canonical type. The non-canonical version
1012/// of a type may have many "decorated" versions of types. Decorators can
1013/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1014/// to be free of any of these, allowing two canonical types to be compared
1015/// for exact equality with a simple pointer comparison.
1016QualType ASTContext::getCanonicalType(QualType T) {
1017 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
1018 return QualType(CanType.getTypePtr(),
1019 T.getCVRQualifiers() | CanType.getCVRQualifiers());
1020}
1021
1022
Chris Lattnere6327742008-04-02 05:18:44 +00001023/// getArrayDecayedType - Return the properly qualified result of decaying the
1024/// specified array type to a pointer. This operation is non-trivial when
1025/// handling typedefs etc. The canonical type of "T" must be an array type,
1026/// this returns a pointer to a properly qualified element of the array.
1027///
1028/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1029QualType ASTContext::getArrayDecayedType(QualType Ty) {
1030 // Handle the common case where typedefs are not involved directly.
1031 QualType EltTy;
1032 unsigned ArrayQuals = 0;
1033 unsigned PointerQuals = 0;
1034 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1035 // Since T "isa" an array type, it could not have had an address space
1036 // qualifier, just CVR qualifiers. The properly qualified element pointer
1037 // gets the union of the CVR qualifiers from the element and the array, and
1038 // keeps any address space qualifier on the element type if present.
1039 EltTy = AT->getElementType();
1040 ArrayQuals = Ty.getCVRQualifiers();
1041 PointerQuals = AT->getIndexTypeQualifier();
1042 } else {
1043 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1044 // lose qualifiers when dealing with typedefs. Example:
1045 // typedef int arr[10];
1046 // void test2() {
1047 // const arr b;
1048 // b[4] = 1;
1049 // }
1050 //
1051 // The decayed type of b is "const int*" even though the element type of the
1052 // array is "int".
Chris Lattnerf52ab252008-04-06 22:59:24 +00001053 QualType CanTy = getCanonicalType(Ty);
Chris Lattnere6327742008-04-02 05:18:44 +00001054 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1055 assert(PrettyArrayType && "Not an array type!");
1056
1057 // Get the element type with 'getAsArrayType' so that we don't lose any
1058 // typedefs in the element type of the array.
1059 EltTy = PrettyArrayType->getElementType();
1060
1061 // If the array was address-space qualifier, make sure to ASQual the element
1062 // type. We can just grab the address space from the canonical type.
1063 if (unsigned AS = CanTy.getAddressSpace())
1064 EltTy = getASQualType(EltTy, AS);
1065
1066 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1067 // the CVR qualifiers directly from the canonical type, which is guaranteed
1068 // to have the full set unioned together.
1069 ArrayQuals = CanTy.getCVRQualifiers();
1070 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1071 }
1072
Chris Lattnerd9654552008-04-02 06:06:35 +00001073 // Apply any CVR qualifiers from the array type to the element type. This
1074 // implements C99 6.7.3p8: "If the specification of an array type includes
1075 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattnere6327742008-04-02 05:18:44 +00001076 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1077
1078 QualType PtrTy = getPointerType(EltTy);
1079
1080 // int x[restrict 4] -> int *restrict
1081 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1082
1083 return PtrTy;
1084}
1085
Reid Spencer5f016e22007-07-11 17:01:13 +00001086/// getFloatingRank - Return a relative rank for floating point types.
1087/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001088static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001089 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001091
Christopher Lambebb97e92008-02-04 02:31:56 +00001092 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001093 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 case BuiltinType::Float: return FloatRank;
1095 case BuiltinType::Double: return DoubleRank;
1096 case BuiltinType::LongDouble: return LongDoubleRank;
1097 }
1098}
1099
Steve Naroff716c7302007-08-27 01:41:48 +00001100/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1101/// point or a complex type (based on typeDomain/typeSize).
1102/// 'typeDomain' is a real floating point or complex type.
1103/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001104QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1105 QualType Domain) const {
1106 FloatingRank EltRank = getFloatingRank(Size);
1107 if (Domain->isComplexType()) {
1108 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001109 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001110 case FloatRank: return FloatComplexTy;
1111 case DoubleRank: return DoubleComplexTy;
1112 case LongDoubleRank: return LongDoubleComplexTy;
1113 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 }
Chris Lattner1361b112008-04-06 23:58:54 +00001115
1116 assert(Domain->isRealFloatingType() && "Unknown domain!");
1117 switch (EltRank) {
1118 default: assert(0 && "getFloatingRank(): illegal value for rank");
1119 case FloatRank: return FloatTy;
1120 case DoubleRank: return DoubleTy;
1121 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001122 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001123}
1124
Chris Lattner7cfeb082008-04-06 23:55:33 +00001125/// getFloatingTypeOrder - Compare the rank of the two specified floating
1126/// point types, ignoring the domain of the type (i.e. 'double' ==
1127/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1128/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001129int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1130 FloatingRank LHSR = getFloatingRank(LHS);
1131 FloatingRank RHSR = getFloatingRank(RHS);
1132
1133 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001134 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001135 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001136 return 1;
1137 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138}
1139
Chris Lattnerf52ab252008-04-06 22:59:24 +00001140/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1141/// routine will assert if passed a built-in type that isn't an integer or enum,
1142/// or if it is not canonicalized.
1143static unsigned getIntegerRank(Type *T) {
1144 assert(T->isCanonical() && "T should be canonicalized");
1145 if (isa<EnumType>(T))
1146 return 4;
1147
1148 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001149 default: assert(0 && "getIntegerRank(): not a built-in integer");
1150 case BuiltinType::Bool:
1151 return 1;
1152 case BuiltinType::Char_S:
1153 case BuiltinType::Char_U:
1154 case BuiltinType::SChar:
1155 case BuiltinType::UChar:
1156 return 2;
1157 case BuiltinType::Short:
1158 case BuiltinType::UShort:
1159 return 3;
1160 case BuiltinType::Int:
1161 case BuiltinType::UInt:
1162 return 4;
1163 case BuiltinType::Long:
1164 case BuiltinType::ULong:
1165 return 5;
1166 case BuiltinType::LongLong:
1167 case BuiltinType::ULongLong:
1168 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001169 }
1170}
1171
Chris Lattner7cfeb082008-04-06 23:55:33 +00001172/// getIntegerTypeOrder - Returns the highest ranked integer type:
1173/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1174/// LHS < RHS, return -1.
1175int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001176 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1177 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001178 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001179
Chris Lattnerf52ab252008-04-06 22:59:24 +00001180 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1181 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001182
Chris Lattner7cfeb082008-04-06 23:55:33 +00001183 unsigned LHSRank = getIntegerRank(LHSC);
1184 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
Chris Lattner7cfeb082008-04-06 23:55:33 +00001186 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1187 if (LHSRank == RHSRank) return 0;
1188 return LHSRank > RHSRank ? 1 : -1;
1189 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001190
Chris Lattner7cfeb082008-04-06 23:55:33 +00001191 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1192 if (LHSUnsigned) {
1193 // If the unsigned [LHS] type is larger, return it.
1194 if (LHSRank >= RHSRank)
1195 return 1;
1196
1197 // If the signed type can represent all values of the unsigned type, it
1198 // wins. Because we are dealing with 2's complement and types that are
1199 // powers of two larger than each other, this is always safe.
1200 return -1;
1201 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001202
Chris Lattner7cfeb082008-04-06 23:55:33 +00001203 // If the unsigned [RHS] type is larger, return it.
1204 if (RHSRank >= LHSRank)
1205 return -1;
1206
1207 // If the signed type can represent all values of the unsigned type, it
1208 // wins. Because we are dealing with 2's complement and types that are
1209 // powers of two larger than each other, this is always safe.
1210 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001212
1213// getCFConstantStringType - Return the type used for constant CFStrings.
1214QualType ASTContext::getCFConstantStringType() {
1215 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001216 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001217 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001218 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001219 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001220
1221 // const int *isa;
1222 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001223 // int flags;
1224 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001225 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001226 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001227 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001228 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001229 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001230 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001231
Anders Carlssonf06273f2007-11-19 00:25:30 +00001232 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001233 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001234 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001235
1236 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1237 }
1238
1239 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001240}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001241
Anders Carlssone8c49532007-10-29 06:33:42 +00001242// This returns true if a type has been typedefed to BOOL:
1243// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001244static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001245 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001246 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001247
1248 return false;
1249}
1250
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001251/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001252/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001254 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001255
1256 // Make all integer and enum types at least as large as an int
1257 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001258 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001259 // Treat arrays as pointers, since that's how they're passed in.
1260 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001261 sz = getTypeSize(VoidPtrTy);
1262 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001263}
1264
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001266/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001267void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001268 std::string& S)
1269{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001270 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001271 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001272 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001273 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001274 // Compute size of all parameters.
1275 // Start with computing size of a pointer in number of bytes.
1276 // FIXME: There might(should) be a better way of doing this computation!
1277 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001278 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001279 // The first two arguments (self and _cmd) are pointers; account for
1280 // their size.
1281 int ParmOffset = 2 * PtrSize;
1282 int NumOfParams = Decl->getNumParams();
1283 for (int i = 0; i < NumOfParams; i++) {
1284 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001285 int sz = getObjCEncodingTypeSize (PType);
1286 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001287 ParmOffset += sz;
1288 }
1289 S += llvm::utostr(ParmOffset);
1290 S += "@0:";
1291 S += llvm::utostr(PtrSize);
1292
1293 // Argument types.
1294 ParmOffset = 2 * PtrSize;
1295 for (int i = 0; i < NumOfParams; i++) {
1296 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001297 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001298 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001299 getObjCEncodingForTypeQualifier(
1300 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001301 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001302 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001303 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001304 }
1305}
1306
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001307void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1308 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001309{
Anders Carlssone8c49532007-10-29 06:33:42 +00001310 // FIXME: This currently doesn't encode:
1311 // @ An object (whether statically typed or typed id)
1312 // # A class object (Class)
1313 // : A method selector (SEL)
1314 // {name=type...} A structure
1315 // (name=type...) A union
1316 // bnum A bit field of num bits
1317
1318 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001319 char encoding;
1320 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001321 default: assert(0 && "Unhandled builtin type kind");
1322 case BuiltinType::Void: encoding = 'v'; break;
1323 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001324 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001325 case BuiltinType::UChar: encoding = 'C'; break;
1326 case BuiltinType::UShort: encoding = 'S'; break;
1327 case BuiltinType::UInt: encoding = 'I'; break;
1328 case BuiltinType::ULong: encoding = 'L'; break;
1329 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001330 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001331 case BuiltinType::SChar: encoding = 'c'; break;
1332 case BuiltinType::Short: encoding = 's'; break;
1333 case BuiltinType::Int: encoding = 'i'; break;
1334 case BuiltinType::Long: encoding = 'l'; break;
1335 case BuiltinType::LongLong: encoding = 'q'; break;
1336 case BuiltinType::Float: encoding = 'f'; break;
1337 case BuiltinType::Double: encoding = 'd'; break;
1338 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001339 }
1340
1341 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001342 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001343 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001344 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001345 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001346
1347 }
1348 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001349 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001350 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001351 S += '@';
1352 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001353 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001354 S += '#';
1355 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001356 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001357 S += ':';
1358 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001359 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001360
1361 if (PointeeTy->isCharType()) {
1362 // char pointer types should be encoded as '*' unless it is a
1363 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001364 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001365 S += '*';
1366 return;
1367 }
1368 }
1369
1370 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001371 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001372 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001373 S += '[';
1374
1375 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1376 S += llvm::utostr(CAT->getSize().getZExtValue());
1377 else
1378 assert(0 && "Unhandled array type!");
1379
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001380 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001381 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001382 } else if (T->getAsFunctionType()) {
1383 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001384 } else if (const RecordType *RTy = T->getAsRecordType()) {
1385 RecordDecl *RDecl= RTy->getDecl();
1386 S += '{';
1387 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001388 bool found = false;
1389 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1390 if (ERType[i] == RTy) {
1391 found = true;
1392 break;
1393 }
1394 if (!found) {
1395 ERType.push_back(RTy);
1396 S += '=';
1397 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1398 FieldDecl *field = RDecl->getMember(i);
1399 getObjCEncodingForType(field->getType(), S, ERType);
1400 }
1401 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1402 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001403 }
1404 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001405 } else if (T->isEnumeralType()) {
1406 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001407 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001408 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001409}
1410
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001412 std::string& S) const {
1413 if (QT & Decl::OBJC_TQ_In)
1414 S += 'n';
1415 if (QT & Decl::OBJC_TQ_Inout)
1416 S += 'N';
1417 if (QT & Decl::OBJC_TQ_Out)
1418 S += 'o';
1419 if (QT & Decl::OBJC_TQ_Bycopy)
1420 S += 'O';
1421 if (QT & Decl::OBJC_TQ_Byref)
1422 S += 'R';
1423 if (QT & Decl::OBJC_TQ_Oneway)
1424 S += 'V';
1425}
1426
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001427void ASTContext::setBuiltinVaListType(QualType T)
1428{
1429 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1430
1431 BuiltinVaListType = T;
1432}
1433
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001435{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001436 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001437
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001438 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001439
1440 // typedef struct objc_object *id;
1441 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1442 assert(ptr && "'id' incorrectly typed");
1443 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1444 assert(rec && "'id' incorrectly typed");
1445 IdStructType = rec;
1446}
1447
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001449{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001450 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001451
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001452 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001453
1454 // typedef struct objc_selector *SEL;
1455 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1456 assert(ptr && "'SEL' incorrectly typed");
1457 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1458 assert(rec && "'SEL' incorrectly typed");
1459 SelStructType = rec;
1460}
1461
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001463{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001464 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1465 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001466}
1467
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001468void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001469{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001470 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001471
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001472 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001473
1474 // typedef struct objc_class *Class;
1475 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1476 assert(ptr && "'Class' incorrectly typed");
1477 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1478 assert(rec && "'Class' incorrectly typed");
1479 ClassStructType = rec;
1480}
1481
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001482void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1483 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001484 "'NSConstantString' type already set!");
1485
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001486 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001487}
1488
Chris Lattner6ac46a42008-04-07 06:51:04 +00001489//===----------------------------------------------------------------------===//
1490// Type Compatibility Testing
1491//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001492
Chris Lattner78eca282008-04-07 06:49:41 +00001493/// C99 6.2.7p1: If both are complete types, then the following additional
1494/// requirements apply.
1495/// FIXME (handle compatibility across source files).
1496static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1497 const ASTContext &C) {
Steve Naroffab373092007-11-07 06:03:51 +00001498 // "Class" and "id" are compatible built-in structure types.
Chris Lattner78eca282008-04-07 06:49:41 +00001499 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1500 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroffab373092007-11-07 06:03:51 +00001501 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001502
Chris Lattner78eca282008-04-07 06:49:41 +00001503 // Within a translation unit a tag type is only compatible with itself. Self
1504 // equality is already handled by the time we get here.
1505 assert(LHS != RHS && "Self equality not handled!");
1506 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001507}
1508
1509bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1510 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1511 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001512 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1513 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001514 return false;
1515
1516 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1517 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1518
1519 return typesAreCompatible(ltype, rtype);
1520}
1521
Steve Naroffec0550f2007-10-15 20:41:53 +00001522bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1523 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1524 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1525 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1526 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1527
1528 // first check the return types (common between C99 and K&R).
1529 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1530 return false;
1531
1532 if (lproto && rproto) { // two C99 style function prototypes
1533 unsigned lproto_nargs = lproto->getNumArgs();
1534 unsigned rproto_nargs = rproto->getNumArgs();
1535
1536 if (lproto_nargs != rproto_nargs)
1537 return false;
1538
1539 // both prototypes have the same number of arguments.
1540 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1541 (rproto->isVariadic() && !lproto->isVariadic()))
1542 return false;
1543
1544 // The use of ellipsis agree...now check the argument types.
1545 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001546 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1547 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001548 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001549 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001550 return false;
1551 return true;
1552 }
Chris Lattner5426bf62008-04-07 07:01:58 +00001553
Steve Naroffec0550f2007-10-15 20:41:53 +00001554 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1555 return true;
1556
1557 // we have a mixture of K&R style with C99 prototypes
1558 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroffec0550f2007-10-15 20:41:53 +00001559 if (proto->isVariadic())
1560 return false;
1561
1562 // FIXME: Each parameter type T in the prototype must be compatible with the
1563 // type resulting from applying the usual argument conversions to T.
1564 return true;
1565}
1566
Chris Lattneracc99722008-04-07 06:56:55 +00001567// C99 6.7.5.2p6
1568static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattneracc99722008-04-07 06:56:55 +00001569 // Constant arrays must be the same size to be compatible.
1570 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1571 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1572 if (RCAT->getSize() != LCAT->getSize())
1573 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001574
Chris Lattner8c7bbb52008-04-07 06:58:21 +00001575 // Compatible arrays must have compatible element types
1576 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroffec0550f2007-10-15 20:41:53 +00001577}
1578
Chris Lattner6ac46a42008-04-07 06:51:04 +00001579/// areCompatVectorTypes - Return true if the two specified vector types are
1580/// compatible.
1581static bool areCompatVectorTypes(const VectorType *LHS,
1582 const VectorType *RHS) {
1583 assert(LHS->isCanonical() && RHS->isCanonical());
1584 return LHS->getElementType() == RHS->getElementType() &&
1585 LHS->getNumElements() == RHS->getNumElements();
1586}
1587
1588/// areCompatObjCInterfaces - Return true if the two interface types are
1589/// compatible for assignment from RHS to LHS. This handles validation of any
1590/// protocol qualifiers on the LHS or RHS.
1591///
Chris Lattner5426bf62008-04-07 07:01:58 +00001592static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1593 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001594 // Verify that the base decls are compatible: the RHS must be a subclass of
1595 // the LHS.
1596 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1597 return false;
1598
1599 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1600 // protocol qualified at all, then we are good.
1601 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1602 return true;
1603
1604 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1605 // isn't a superset.
1606 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1607 return true; // FIXME: should return false!
1608
1609 // Finally, we must have two protocol-qualified interfaces.
1610 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1611 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1612 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1613 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1614 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1615 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1616
1617 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1618 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1619 // LHS in a single parallel scan until we run out of elements in LHS.
1620 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1621 ObjCProtocolDecl *LHSProto = *LHSPI;
1622
1623 while (RHSPI != RHSPE) {
1624 ObjCProtocolDecl *RHSProto = *RHSPI++;
1625 // If the RHS has a protocol that the LHS doesn't, ignore it.
1626 if (RHSProto != LHSProto)
1627 continue;
1628
1629 // Otherwise, the RHS does have this element.
1630 ++LHSPI;
1631 if (LHSPI == LHSPE)
1632 return true; // All protocols in LHS exist in RHS.
1633
1634 LHSProto = *LHSPI;
1635 }
1636
1637 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1638 return false;
1639}
1640
1641
Steve Naroffec0550f2007-10-15 20:41:53 +00001642/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1643/// both shall have the identically qualified version of a compatible type.
1644/// C99 6.2.7p1: Two types have compatible types if their types are the
1645/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001646bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1647 QualType LHS = LHS_NC.getCanonicalType();
1648 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner988ee6e2008-04-03 05:07:04 +00001649
Bill Wendling43d69752007-12-03 07:33:35 +00001650 // C++ [expr]: If an expression initially has the type "reference to T", the
1651 // type is adjusted to "T" prior to any further analysis, the expression
1652 // designates the object or function denoted by the reference, and the
1653 // expression is an lvalue.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001654 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1655 LHS = RT->getPointeeType();
1656 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1657 RHS = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001658
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001659 // If two types are identical, they are compatible.
1660 if (LHS == RHS)
1661 return true;
1662
1663 // If qualifiers differ, the types are different.
Chris Lattnera36a61f2008-04-07 05:43:21 +00001664 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1665 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001666 return false;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001667
1668 // Strip off ASQual's if present.
1669 if (LHSAS) {
1670 LHS = LHS.getUnqualifiedType();
1671 RHS = RHS.getUnqualifiedType();
1672 }
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001673
Chris Lattnerc4e40592008-04-07 04:07:56 +00001674 Type::TypeClass LHSClass = LHS->getTypeClass();
1675 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattner1adb8832008-01-14 05:45:46 +00001676
1677 // We want to consider the two function types to be the same for these
1678 // comparisons, just force one to the other.
1679 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1680 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001681
1682 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001683 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1684 LHSClass = Type::ConstantArray;
1685 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1686 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001687
Nate Begeman213541a2008-04-18 23:10:10 +00001688 // Canonicalize ExtVector -> Vector.
1689 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1690 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001691
Chris Lattnerb0489812008-04-07 06:38:24 +00001692 // Consider qualified interfaces and interfaces the same.
1693 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1694 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1695
Chris Lattnera36a61f2008-04-07 05:43:21 +00001696 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001697 if (LHSClass != RHSClass) {
Chris Lattnerb0489812008-04-07 06:38:24 +00001698 // ID is compatible with all interface types.
1699 if (isa<ObjCInterfaceType>(LHS))
1700 return isObjCIdType(RHS);
1701 if (isa<ObjCInterfaceType>(RHS))
1702 return isObjCIdType(LHS);
Steve Naroff97341622008-06-04 15:07:33 +00001703
1704 // ID is compatible with all qualified id types.
1705 if (isa<ObjCQualifiedIdType>(LHS)) {
1706 if (const PointerType *PT = RHS->getAsPointerType())
1707 return isObjCIdType(PT->getPointeeType());
1708 }
1709 if (isa<ObjCQualifiedIdType>(RHS)) {
1710 if (const PointerType *PT = LHS->getAsPointerType())
1711 return isObjCIdType(PT->getPointeeType());
1712 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001713 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1714 // a signed integer type, or an unsigned integer type.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001715 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1716 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1717 return EDecl->getIntegerType() == RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001718 }
Chris Lattnerc4e40592008-04-07 04:07:56 +00001719 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1720 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1721 return EDecl->getIntegerType() == LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001722 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001723
Steve Naroffec0550f2007-10-15 20:41:53 +00001724 return false;
1725 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00001726
Steve Naroff4a746782008-01-09 22:43:08 +00001727 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001728 switch (LHSClass) {
Chris Lattnera36a61f2008-04-07 05:43:21 +00001729 case Type::ASQual:
1730 case Type::FunctionProto:
1731 case Type::VariableArray:
1732 case Type::IncompleteArray:
1733 case Type::Reference:
Chris Lattnerb0489812008-04-07 06:38:24 +00001734 case Type::ObjCQualifiedInterface:
Chris Lattnera36a61f2008-04-07 05:43:21 +00001735 assert(0 && "Canonicalized away above");
Chris Lattner1adb8832008-01-14 05:45:46 +00001736 case Type::Pointer:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001737 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001738 case Type::ConstantArray:
Chris Lattneracc99722008-04-07 06:56:55 +00001739 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1740 *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001741 case Type::FunctionNoProto:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001742 return functionTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001743 case Type::Tagged: // handle structures, unions
Chris Lattner78eca282008-04-07 06:49:41 +00001744 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001745 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00001746 // Only exactly equal builtin types are compatible, which is tested above.
1747 return false;
1748 case Type::Vector:
1749 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001750 case Type::ObjCInterface:
Chris Lattnerb0489812008-04-07 06:38:24 +00001751 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1752 cast<ObjCInterfaceType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001753 default:
1754 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001755 }
1756 return true; // should never get here...
1757}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001758
Chris Lattner5426bf62008-04-07 07:01:58 +00001759//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00001760// Integer Predicates
1761//===----------------------------------------------------------------------===//
1762unsigned ASTContext::getIntWidth(QualType T) {
1763 if (T == BoolTy)
1764 return 1;
1765 // At the moment, only bool has padding bits
1766 return (unsigned)getTypeSize(T);
1767}
1768
1769QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1770 assert(T->isSignedIntegerType() && "Unexpected type");
1771 if (const EnumType* ETy = T->getAsEnumType())
1772 T = ETy->getDecl()->getIntegerType();
1773 const BuiltinType* BTy = T->getAsBuiltinType();
1774 assert (BTy && "Unexpected signed integer type");
1775 switch (BTy->getKind()) {
1776 case BuiltinType::Char_S:
1777 case BuiltinType::SChar:
1778 return UnsignedCharTy;
1779 case BuiltinType::Short:
1780 return UnsignedShortTy;
1781 case BuiltinType::Int:
1782 return UnsignedIntTy;
1783 case BuiltinType::Long:
1784 return UnsignedLongTy;
1785 case BuiltinType::LongLong:
1786 return UnsignedLongLongTy;
1787 default:
1788 assert(0 && "Unexpected signed integer type");
1789 return QualType();
1790 }
1791}
1792
1793
1794//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00001795// Serialization Support
1796//===----------------------------------------------------------------------===//
1797
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001798/// Emit - Serialize an ASTContext object to Bitcode.
1799void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001800 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00001801 S.EmitRef(SourceMgr);
1802 S.EmitRef(Target);
1803 S.EmitRef(Idents);
1804 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001805
Ted Kremenekfee04522007-10-31 22:44:07 +00001806 // Emit the size of the type vector so that we can reserve that size
1807 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001808 S.EmitInt(Types.size());
1809
Ted Kremenek03ed4402007-11-13 22:02:55 +00001810 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1811 I!=E;++I)
1812 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001813
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001814 S.EmitOwnedPtr(TUDecl);
1815
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001816 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001817}
1818
Ted Kremenek0f84c002007-11-13 00:25:37 +00001819ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001820
1821 // Read the language options.
1822 LangOptions LOpts;
1823 LOpts.Read(D);
1824
Ted Kremenekfee04522007-10-31 22:44:07 +00001825 SourceManager &SM = D.ReadRef<SourceManager>();
1826 TargetInfo &t = D.ReadRef<TargetInfo>();
1827 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1828 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001829
Ted Kremenekfee04522007-10-31 22:44:07 +00001830 unsigned size_reserve = D.ReadInt();
1831
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001832 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00001833
Ted Kremenek03ed4402007-11-13 22:02:55 +00001834 for (unsigned i = 0; i < size_reserve; ++i)
1835 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001836
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001837 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1838
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001839 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001840
1841 return A;
1842}