blob: 5c39c2b539947ab02f387b6f8cdb1996bf2f637d [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"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
21#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Daniel Dunbare91593e2008-08-11 04:54:23 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
33 IdentifierTable &idents, SelectorTable &sels,
34 unsigned size_reserve) :
35 CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t),
36 Idents(idents), Selectors(sels)
37{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 Types.pop_back();
49 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000050
51 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000052}
53
54void ASTContext::PrintStats() const {
55 fprintf(stderr, "*** AST Context Stats:\n");
56 fprintf(stderr, " %d types total.\n", (int)Types.size());
57 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000058 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
60
61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
63 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000064 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
67 Type *T = Types[i];
68 if (isa<BuiltinType>(T))
69 ++NumBuiltin;
70 else if (isa<PointerType>(T))
71 ++NumPointer;
72 else if (isa<ReferenceType>(T))
73 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000074 else if (isa<ComplexType>(T))
75 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 else if (isa<ArrayType>(T))
77 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000078 else if (isa<VectorType>(T))
79 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000080 else if (isa<FunctionTypeNoProto>(T))
81 ++NumFunctionNP;
82 else if (isa<FunctionTypeProto>(T))
83 ++NumFunctionP;
84 else if (isa<TypedefType>(T))
85 ++NumTypeName;
86 else if (TagType *TT = dyn_cast<TagType>(T)) {
87 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000088 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000090 case TagDecl::TK_struct: ++NumTagStruct; break;
91 case TagDecl::TK_union: ++NumTagUnion; break;
92 case TagDecl::TK_class: ++NumTagClass; break;
93 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000094 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000095 } else if (isa<ObjCInterfaceType>(T))
96 ++NumObjCInterfaces;
97 else if (isa<ObjCQualifiedInterfaceType>(T))
98 ++NumObjCQualifiedInterfaces;
99 else if (isa<ObjCQualifiedIdType>(T))
100 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000101 else if (isa<TypeOfType>(T))
102 ++NumTypeOfTypes;
103 else if (isa<TypeOfExpr>(T))
104 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000105 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000106 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 assert(0 && "Unknown type!");
108 }
109 }
110
111 fprintf(stderr, " %d builtin types\n", NumBuiltin);
112 fprintf(stderr, " %d pointer types\n", NumPointer);
113 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000114 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000116 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
118 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
119 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
120 fprintf(stderr, " %d tagged types\n", NumTagged);
121 fprintf(stderr, " %d struct types\n", NumTagStruct);
122 fprintf(stderr, " %d union types\n", NumTagUnion);
123 fprintf(stderr, " %d class types\n", NumTagClass);
124 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000125 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000126 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000127 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000128 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000130 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
131 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
134 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000135 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 NumFunctionP*sizeof(FunctionTypeProto)+
137 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000138 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
139 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
142
143void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
144 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
145}
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147void ASTContext::InitBuiltinTypes() {
148 assert(VoidTy.isNull() && "Context reinitialized?");
149
150 // C99 6.2.5p19.
151 InitBuiltinType(VoidTy, BuiltinType::Void);
152
153 // C99 6.2.5p2.
154 InitBuiltinType(BoolTy, BuiltinType::Bool);
155 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000156 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 InitBuiltinType(CharTy, BuiltinType::Char_S);
158 else
159 InitBuiltinType(CharTy, BuiltinType::Char_U);
160 // C99 6.2.5p4.
161 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
162 InitBuiltinType(ShortTy, BuiltinType::Short);
163 InitBuiltinType(IntTy, BuiltinType::Int);
164 InitBuiltinType(LongTy, BuiltinType::Long);
165 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
166
167 // C99 6.2.5p6.
168 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
169 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
170 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
171 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
172 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
173
174 // C99 6.2.5p10.
175 InitBuiltinType(FloatTy, BuiltinType::Float);
176 InitBuiltinType(DoubleTy, BuiltinType::Double);
177 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000178
179 // C++ 3.9.1p5
180 InitBuiltinType(WCharTy, BuiltinType::WChar);
181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // C99 6.2.5p11.
183 FloatComplexTy = getComplexType(FloatTy);
184 DoubleComplexTy = getComplexType(DoubleTy);
185 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000186
187 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000189 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000191 ClassStructType = 0;
192
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000193 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000194
195 // void * type
196 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Chris Lattner464175b2007-07-18 17:52:12 +0000199//===----------------------------------------------------------------------===//
200// Type Sizing and Analysis
201//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000202
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000203/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
204/// scalar floating point type.
205const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
206 const BuiltinType *BT = T->getAsBuiltinType();
207 assert(BT && "Not a floating point type!");
208 switch (BT->getKind()) {
209 default: assert(0 && "Not a floating point type!");
210 case BuiltinType::Float: return Target.getFloatFormat();
211 case BuiltinType::Double: return Target.getDoubleFormat();
212 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
213 }
214}
215
216
Chris Lattnera7674d82007-07-13 22:13:22 +0000217/// getTypeSize - Return the size of the specified type, in bits. This method
218/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000219std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000220ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000221 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000222 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000223 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000224 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000225 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000226 case Type::FunctionNoProto:
227 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000228 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000229 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000230 case Type::VariableArray:
231 assert(0 && "VLAs not implemented yet!");
232 case Type::ConstantArray: {
233 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
234
Chris Lattner98be4942008-03-05 18:54:05 +0000235 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000236 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000237 Align = EltInfo.second;
238 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000239 }
Nate Begeman213541a2008-04-18 23:10:10 +0000240 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000241 case Type::Vector: {
242 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000243 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000244 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000245 // FIXME: This isn't right for unusual vectors
246 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000247 break;
248 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000249
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000250 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000251 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000252 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000253 case BuiltinType::Void:
254 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000255 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000256 Width = Target.getBoolWidth();
257 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000258 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000259 case BuiltinType::Char_S:
260 case BuiltinType::Char_U:
261 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000262 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000263 Width = Target.getCharWidth();
264 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000265 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000266 case BuiltinType::WChar:
267 Width = Target.getWCharWidth();
268 Align = Target.getWCharAlign();
269 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000270 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000271 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000272 Width = Target.getShortWidth();
273 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000274 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000275 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000276 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000277 Width = Target.getIntWidth();
278 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000279 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000280 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000281 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000282 Width = Target.getLongWidth();
283 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000284 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000285 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000286 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000287 Width = Target.getLongLongWidth();
288 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000289 break;
290 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = Target.getFloatWidth();
292 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000293 break;
294 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000295 Width = Target.getDoubleWidth();
296 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000297 break;
298 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000299 Width = Target.getLongDoubleWidth();
300 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000301 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000302 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000303 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000304 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000305 // FIXME: Pointers into different addr spaces could have different sizes and
306 // alignment requirements: getPointerInfo should take an AddrSpace.
307 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000308 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000309 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000310 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000311 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000312 case Type::Pointer: {
313 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000314 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000315 Align = Target.getPointerAlign(AS);
316 break;
317 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000318 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000319 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000320 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000321 // FIXME: This is wrong for struct layout: a reference in a struct has
322 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000323 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000324
325 case Type::Complex: {
326 // Complex types have the same alignment as their elements, but twice the
327 // size.
328 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000329 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000331 Align = EltInfo.second;
332 break;
333 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000334 case Type::ObjCInterface: {
335 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
336 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
337 Width = Layout.getSize();
338 Align = Layout.getAlignment();
339 break;
340 }
Chris Lattner71763312008-04-06 22:05:18 +0000341 case Type::Tagged: {
Chris Lattner8389eab2008-08-09 21:35:13 +0000342 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
343 Width = 1;
344 Align = 1;
345 break;
346 }
347
Chris Lattner71763312008-04-06 22:05:18 +0000348 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
349 return getTypeInfo(ET->getDecl()->getIntegerType());
350
351 RecordType *RT = cast<RecordType>(T);
352 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
353 Width = Layout.getSize();
354 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000355 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000356 }
Chris Lattner71763312008-04-06 22:05:18 +0000357 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000358
Chris Lattner464175b2007-07-18 17:52:12 +0000359 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000360 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000361}
362
Devang Patel8b277042008-06-04 21:22:16 +0000363/// LayoutField - Field layout.
364void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
365 bool IsUnion, bool StructIsPacked,
366 ASTContext &Context) {
367 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
368 uint64_t FieldOffset = IsUnion ? 0 : Size;
369 uint64_t FieldSize;
370 unsigned FieldAlign;
371
372 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
373 // TODO: Need to check this algorithm on other targets!
374 // (tested on Linux-X86)
375 llvm::APSInt I(32);
376 bool BitWidthIsICE =
377 BitWidthExpr->isIntegerConstantExpr(I, Context);
378 assert (BitWidthIsICE && "Invalid BitField size expression");
379 FieldSize = I.getZExtValue();
380
381 std::pair<uint64_t, unsigned> FieldInfo =
382 Context.getTypeInfo(FD->getType());
383 uint64_t TypeSize = FieldInfo.first;
384
385 FieldAlign = FieldInfo.second;
386 if (FieldIsPacked)
387 FieldAlign = 1;
388 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
389 FieldAlign = std::max(FieldAlign, AA->getAlignment());
390
391 // Check if we need to add padding to give the field the correct
392 // alignment.
393 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
394 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
395
396 // Padding members don't affect overall alignment
397 if (!FD->getIdentifier())
398 FieldAlign = 1;
399 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000400 if (FD->getType()->isIncompleteArrayType()) {
401 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000402 // query getTypeInfo about these, so we figure it out here.
403 // Flexible array members don't have any size, but they
404 // have to be aligned appropriately for their element type.
405 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000406 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000407 FieldAlign = Context.getTypeAlign(ATy->getElementType());
408 } else {
409 std::pair<uint64_t, unsigned> FieldInfo =
410 Context.getTypeInfo(FD->getType());
411 FieldSize = FieldInfo.first;
412 FieldAlign = FieldInfo.second;
413 }
414
415 if (FieldIsPacked)
416 FieldAlign = 8;
417 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
418 FieldAlign = std::max(FieldAlign, AA->getAlignment());
419
420 // Round up the current record size to the field's alignment boundary.
421 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
422 }
423
424 // Place this field at the current location.
425 FieldOffsets[FieldNo] = FieldOffset;
426
427 // Reserve space for this field.
428 if (IsUnion) {
429 Size = std::max(Size, FieldSize);
430 } else {
431 Size = FieldOffset + FieldSize;
432 }
433
434 // Remember max struct/class alignment.
435 Alignment = std::max(Alignment, FieldAlign);
436}
437
Devang Patel44a3dde2008-06-04 21:54:36 +0000438
439/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
440/// specified Objective C, which indicates its size and ivar
441/// position information.
442const ASTRecordLayout &
443ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
444 // Look up this layout, if already laid out, return what we have.
445 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
446 if (Entry) return *Entry;
447
448 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
449 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000450 ASTRecordLayout *NewEntry = NULL;
451 unsigned FieldCount = D->ivar_size();
452 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
453 FieldCount++;
454 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
455 unsigned Alignment = SL.getAlignment();
456 uint64_t Size = SL.getSize();
457 NewEntry = new ASTRecordLayout(Size, Alignment);
458 NewEntry->InitializeLayout(FieldCount);
459 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
460 } else {
461 NewEntry = new ASTRecordLayout();
462 NewEntry->InitializeLayout(FieldCount);
463 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000464 Entry = NewEntry;
465
Devang Patel44a3dde2008-06-04 21:54:36 +0000466 bool IsPacked = D->getAttr<PackedAttr>();
467
468 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
469 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
470 AA->getAlignment()));
471
472 // Layout each ivar sequentially.
473 unsigned i = 0;
474 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
475 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
476 const ObjCIvarDecl* Ivar = (*IVI);
477 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
478 }
479
480 // Finally, round the size of the total struct up to the alignment of the
481 // struct itself.
482 NewEntry->FinalizeLayout();
483 return *NewEntry;
484}
485
Devang Patel88a981b2007-11-01 19:11:01 +0000486/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000487/// specified record (struct/union/class), which indicates its size and field
488/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000489const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000490 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000491
Chris Lattner464175b2007-07-18 17:52:12 +0000492 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000493 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000494 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000495
Devang Patel88a981b2007-11-01 19:11:01 +0000496 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
497 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
498 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000499 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000500
Devang Patel8b277042008-06-04 21:22:16 +0000501 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000502 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000503 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000504
Eli Friedman4bd998b2008-05-30 09:31:38 +0000505 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000506 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
507 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000508
Eli Friedman4bd998b2008-05-30 09:31:38 +0000509 // Layout each field, for now, just sequentially, respecting alignment. In
510 // the future, this will need to be tweakable by targets.
511 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
512 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000513 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000514 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000515
516 // Finally, round the size of the total struct up to the alignment of the
517 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000518 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000519 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000520}
521
Chris Lattnera7674d82007-07-13 22:13:22 +0000522//===----------------------------------------------------------------------===//
523// Type creation/memoization methods
524//===----------------------------------------------------------------------===//
525
Christopher Lambebb97e92008-02-04 02:31:56 +0000526QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000527 QualType CanT = getCanonicalType(T);
528 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000529 return T;
530
531 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
532 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000533 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000534 "Type is already address space qualified");
535
536 // Check if we've already instantiated an address space qual'd type of this
537 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000538 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000539 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000540 void *InsertPos = 0;
541 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
542 return QualType(ASQy, 0);
543
544 // If the base type isn't canonical, this won't be a canonical type either,
545 // so fill in the canonical type field.
546 QualType Canonical;
547 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000548 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000549
550 // Get the new insert position for the node we care about.
551 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
552 assert(NewIP == 0 && "Shouldn't be in the map!");
553 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000554 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000555 ASQualTypes.InsertNode(New, InsertPos);
556 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000557 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000558}
559
Chris Lattnera7674d82007-07-13 22:13:22 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561/// getComplexType - Return the uniqued reference to the type for a complex
562/// number with the specified element type.
563QualType ASTContext::getComplexType(QualType T) {
564 // Unique pointers, to guarantee there is only one pointer of a particular
565 // structure.
566 llvm::FoldingSetNodeID ID;
567 ComplexType::Profile(ID, T);
568
569 void *InsertPos = 0;
570 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
571 return QualType(CT, 0);
572
573 // If the pointee type isn't canonical, this won't be a canonical type either,
574 // so fill in the canonical type field.
575 QualType Canonical;
576 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000577 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
579 // Get the new insert position for the node we care about.
580 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
581 assert(NewIP == 0 && "Shouldn't be in the map!");
582 }
583 ComplexType *New = new ComplexType(T, Canonical);
584 Types.push_back(New);
585 ComplexTypes.InsertNode(New, InsertPos);
586 return QualType(New, 0);
587}
588
589
590/// getPointerType - Return the uniqued reference to the type for a pointer to
591/// the specified type.
592QualType ASTContext::getPointerType(QualType T) {
593 // Unique pointers, to guarantee there is only one pointer of a particular
594 // structure.
595 llvm::FoldingSetNodeID ID;
596 PointerType::Profile(ID, T);
597
598 void *InsertPos = 0;
599 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
600 return QualType(PT, 0);
601
602 // If the pointee type isn't canonical, this won't be a canonical type either,
603 // so fill in the canonical type field.
604 QualType Canonical;
605 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000606 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608 // Get the new insert position for the node we care about.
609 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
610 assert(NewIP == 0 && "Shouldn't be in the map!");
611 }
612 PointerType *New = new PointerType(T, Canonical);
613 Types.push_back(New);
614 PointerTypes.InsertNode(New, InsertPos);
615 return QualType(New, 0);
616}
617
618/// getReferenceType - Return the uniqued reference to the type for a reference
619/// to the specified type.
620QualType ASTContext::getReferenceType(QualType T) {
621 // Unique pointers, to guarantee there is only one pointer of a particular
622 // structure.
623 llvm::FoldingSetNodeID ID;
624 ReferenceType::Profile(ID, T);
625
626 void *InsertPos = 0;
627 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
628 return QualType(RT, 0);
629
630 // If the referencee type isn't canonical, this won't be a canonical type
631 // either, so fill in the canonical type field.
632 QualType Canonical;
633 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000634 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000635
636 // Get the new insert position for the node we care about.
637 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
638 assert(NewIP == 0 && "Shouldn't be in the map!");
639 }
640
641 ReferenceType *New = new ReferenceType(T, Canonical);
642 Types.push_back(New);
643 ReferenceTypes.InsertNode(New, InsertPos);
644 return QualType(New, 0);
645}
646
Steve Narofffb22d962007-08-30 01:06:46 +0000647/// getConstantArrayType - Return the unique reference to the type for an
648/// array of the specified element type.
649QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000650 const llvm::APInt &ArySize,
651 ArrayType::ArraySizeModifier ASM,
652 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000654 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000655
656 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000657 if (ConstantArrayType *ATP =
658 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 return QualType(ATP, 0);
660
661 // If the element type isn't canonical, this won't be a canonical type either,
662 // so fill in the canonical type field.
663 QualType Canonical;
664 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000665 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000666 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000668 ConstantArrayType *NewIP =
669 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 assert(NewIP == 0 && "Shouldn't be in the map!");
672 }
673
Steve Naroffc9406122007-08-30 18:10:14 +0000674 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
675 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000676 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 Types.push_back(New);
678 return QualType(New, 0);
679}
680
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000681/// getVariableArrayType - Returns a non-unique reference to the type for a
682/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000683QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
684 ArrayType::ArraySizeModifier ASM,
685 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000686 // Since we don't unique expressions, it isn't possible to unique VLA's
687 // that have an expression provided for their size.
688
689 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
690 ASM, EltTypeQuals);
691
692 VariableArrayTypes.push_back(New);
693 Types.push_back(New);
694 return QualType(New, 0);
695}
696
697QualType ASTContext::getIncompleteArrayType(QualType EltTy,
698 ArrayType::ArraySizeModifier ASM,
699 unsigned EltTypeQuals) {
700 llvm::FoldingSetNodeID ID;
701 IncompleteArrayType::Profile(ID, EltTy);
702
703 void *InsertPos = 0;
704 if (IncompleteArrayType *ATP =
705 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
706 return QualType(ATP, 0);
707
708 // If the element type isn't canonical, this won't be a canonical type
709 // either, so fill in the canonical type field.
710 QualType Canonical;
711
712 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000713 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000714 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000715
716 // Get the new insert position for the node we care about.
717 IncompleteArrayType *NewIP =
718 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
719
720 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000721 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000722
723 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
724 ASM, EltTypeQuals);
725
726 IncompleteArrayTypes.InsertNode(New, InsertPos);
727 Types.push_back(New);
728 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000729}
730
Steve Naroff73322922007-07-18 18:00:27 +0000731/// getVectorType - Return the unique reference to a vector type of
732/// the specified element type and size. VectorType must be a built-in type.
733QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 BuiltinType *baseType;
735
Chris Lattnerf52ab252008-04-06 22:59:24 +0000736 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000737 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000738
739 // Check if we've already instantiated a vector of this type.
740 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000741 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 void *InsertPos = 0;
743 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
744 return QualType(VTP, 0);
745
746 // If the element type isn't canonical, this won't be a canonical type either,
747 // so fill in the canonical type field.
748 QualType Canonical;
749 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000750 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751
752 // Get the new insert position for the node we care about.
753 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
754 assert(NewIP == 0 && "Shouldn't be in the map!");
755 }
756 VectorType *New = new VectorType(vecType, NumElts, Canonical);
757 VectorTypes.InsertNode(New, InsertPos);
758 Types.push_back(New);
759 return QualType(New, 0);
760}
761
Nate Begeman213541a2008-04-18 23:10:10 +0000762/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000763/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000764QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000765 BuiltinType *baseType;
766
Chris Lattnerf52ab252008-04-06 22:59:24 +0000767 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000768 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000769
770 // Check if we've already instantiated a vector of this type.
771 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000772 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000773 void *InsertPos = 0;
774 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
775 return QualType(VTP, 0);
776
777 // If the element type isn't canonical, this won't be a canonical type either,
778 // so fill in the canonical type field.
779 QualType Canonical;
780 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000781 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000782
783 // Get the new insert position for the node we care about.
784 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
785 assert(NewIP == 0 && "Shouldn't be in the map!");
786 }
Nate Begeman213541a2008-04-18 23:10:10 +0000787 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000788 VectorTypes.InsertNode(New, InsertPos);
789 Types.push_back(New);
790 return QualType(New, 0);
791}
792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
794///
795QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
796 // Unique functions, to guarantee there is only one function of a particular
797 // structure.
798 llvm::FoldingSetNodeID ID;
799 FunctionTypeNoProto::Profile(ID, ResultTy);
800
801 void *InsertPos = 0;
802 if (FunctionTypeNoProto *FT =
803 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
804 return QualType(FT, 0);
805
806 QualType Canonical;
807 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000808 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000809
810 // Get the new insert position for the node we care about.
811 FunctionTypeNoProto *NewIP =
812 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
813 assert(NewIP == 0 && "Shouldn't be in the map!");
814 }
815
816 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
817 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000818 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 return QualType(New, 0);
820}
821
822/// getFunctionType - Return a normal function type with a typed argument
823/// list. isVariadic indicates whether the argument list includes '...'.
824QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
825 unsigned NumArgs, bool isVariadic) {
826 // Unique functions, to guarantee there is only one function of a particular
827 // structure.
828 llvm::FoldingSetNodeID ID;
829 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
830
831 void *InsertPos = 0;
832 if (FunctionTypeProto *FTP =
833 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
834 return QualType(FTP, 0);
835
836 // Determine whether the type being created is already canonical or not.
837 bool isCanonical = ResultTy->isCanonical();
838 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
839 if (!ArgArray[i]->isCanonical())
840 isCanonical = false;
841
842 // If this type isn't canonical, get the canonical version of it.
843 QualType Canonical;
844 if (!isCanonical) {
845 llvm::SmallVector<QualType, 16> CanonicalArgs;
846 CanonicalArgs.reserve(NumArgs);
847 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000848 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000849
Chris Lattnerf52ab252008-04-06 22:59:24 +0000850 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 &CanonicalArgs[0], NumArgs,
852 isVariadic);
853
854 // Get the new insert position for the node we care about.
855 FunctionTypeProto *NewIP =
856 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
857 assert(NewIP == 0 && "Shouldn't be in the map!");
858 }
859
860 // FunctionTypeProto objects are not allocated with new because they have a
861 // variable size array (for parameter types) at the end of them.
862 FunctionTypeProto *FTP =
863 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000864 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
866 Canonical);
867 Types.push_back(FTP);
868 FunctionTypeProtos.InsertNode(FTP, InsertPos);
869 return QualType(FTP, 0);
870}
871
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000872/// getTypeDeclType - Return the unique reference to the type for the
873/// specified type declaration.
874QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
875 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
876
877 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
878 return getTypedefType(Typedef);
879 else if (ObjCInterfaceDecl *ObjCInterface
880 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
881 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000882
883 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
884 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
885 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000886 Decl->TypeForDecl = new RecordType(Record);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000887 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000888 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000889 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000890 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000891
892 Types.push_back(Decl->TypeForDecl);
893 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000894}
895
Reid Spencer5f016e22007-07-11 17:01:13 +0000896/// getTypedefType - Return the unique reference to the type for the
897/// specified typename decl.
898QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
899 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
900
Chris Lattnerf52ab252008-04-06 22:59:24 +0000901 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000902 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 Types.push_back(Decl->TypeForDecl);
904 return QualType(Decl->TypeForDecl, 0);
905}
906
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000907/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000908/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000910 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
911
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000913 Types.push_back(Decl->TypeForDecl);
914 return QualType(Decl->TypeForDecl, 0);
915}
916
Chris Lattner88cb27a2008-04-07 04:56:42 +0000917/// CmpProtocolNames - Comparison predicate for sorting protocols
918/// alphabetically.
919static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
920 const ObjCProtocolDecl *RHS) {
921 return strcmp(LHS->getName(), RHS->getName()) < 0;
922}
923
924static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
925 unsigned &NumProtocols) {
926 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
927
928 // Sort protocols, keyed by name.
929 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
930
931 // Remove duplicates.
932 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
933 NumProtocols = ProtocolsEnd-Protocols;
934}
935
936
Chris Lattner065f0d72008-04-07 04:44:08 +0000937/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
938/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
940 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000941 // Sort the protocol list alphabetically to canonicalize it.
942 SortAndUniqueProtocols(Protocols, NumProtocols);
943
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000944 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000945 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000946
947 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000948 if (ObjCQualifiedInterfaceType *QT =
949 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000950 return QualType(QT, 0);
951
952 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953 ObjCQualifiedInterfaceType *QType =
954 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000955 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000956 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000957 return QualType(QType, 0);
958}
959
Chris Lattner88cb27a2008-04-07 04:56:42 +0000960/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
961/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000962QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000963 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000964 // Sort the protocol list alphabetically to canonicalize it.
965 SortAndUniqueProtocols(Protocols, NumProtocols);
966
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000967 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000968 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000969
970 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000972 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000973 return QualType(QT, 0);
974
975 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000976 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000977 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000979 return QualType(QType, 0);
980}
981
Steve Naroff9752f252007-08-01 18:02:17 +0000982/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
983/// TypeOfExpr AST's (since expression's are never shared). For example,
984/// multiple declarations that refer to "typeof(x)" all contain different
985/// DeclRefExpr's. This doesn't effect the type checker, since it operates
986/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000987QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000988 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +0000989 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
990 Types.push_back(toe);
991 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000992}
993
Steve Naroff9752f252007-08-01 18:02:17 +0000994/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
995/// TypeOfType AST's. The only motivation to unique these nodes would be
996/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
997/// an issue. This doesn't effect the type checker, since it operates
998/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000999QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001000 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001001 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1002 Types.push_back(tot);
1003 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001004}
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006/// getTagDeclType - Return the unique reference to the type for the
1007/// specified TagDecl (struct/union/class/enum) decl.
1008QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001009 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001010 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001011}
1012
1013/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1014/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1015/// needs to agree with the definition in <stddef.h>.
1016QualType ASTContext::getSizeType() const {
1017 // On Darwin, size_t is defined as a "long unsigned int".
1018 // FIXME: should derive from "Target".
1019 return UnsignedLongTy;
1020}
1021
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001022/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001023/// width of characters in wide strings, The value is target dependent and
1024/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001025QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001026 if (LangOpts.CPlusPlus)
1027 return WCharTy;
1028
Eli Friedmanfd888a52008-02-12 08:29:21 +00001029 // On Darwin, wchar_t is defined as a "int".
1030 // FIXME: should derive from "Target".
1031 return IntTy;
1032}
1033
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001034/// getSignedWCharType - Return the type of "signed wchar_t".
1035/// Used when in C++, as a GCC extension.
1036QualType ASTContext::getSignedWCharType() const {
1037 // FIXME: derive from "Target" ?
1038 return WCharTy;
1039}
1040
1041/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1042/// Used when in C++, as a GCC extension.
1043QualType ASTContext::getUnsignedWCharType() const {
1044 // FIXME: derive from "Target" ?
1045 return UnsignedIntTy;
1046}
1047
Chris Lattner8b9023b2007-07-13 03:05:23 +00001048/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1049/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1050QualType ASTContext::getPointerDiffType() const {
1051 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1052 // FIXME: should derive from "Target".
1053 return IntTy;
1054}
1055
Chris Lattnere6327742008-04-02 05:18:44 +00001056//===----------------------------------------------------------------------===//
1057// Type Operators
1058//===----------------------------------------------------------------------===//
1059
Chris Lattner77c96472008-04-06 22:41:35 +00001060/// getCanonicalType - Return the canonical (structural) type corresponding to
1061/// the specified potentially non-canonical type. The non-canonical version
1062/// of a type may have many "decorated" versions of types. Decorators can
1063/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1064/// to be free of any of these, allowing two canonical types to be compared
1065/// for exact equality with a simple pointer comparison.
1066QualType ASTContext::getCanonicalType(QualType T) {
1067 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001068
1069 // If the result has type qualifiers, make sure to canonicalize them as well.
1070 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1071 if (TypeQuals == 0) return CanType;
1072
1073 // If the type qualifiers are on an array type, get the canonical type of the
1074 // array with the qualifiers applied to the element type.
1075 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1076 if (!AT)
1077 return CanType.getQualifiedType(TypeQuals);
1078
1079 // Get the canonical version of the element with the extra qualifiers on it.
1080 // This can recursively sink qualifiers through multiple levels of arrays.
1081 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1082 NewEltTy = getCanonicalType(NewEltTy);
1083
1084 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1085 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1086 CAT->getIndexTypeQualifier());
1087 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1088 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1089 IAT->getIndexTypeQualifier());
1090
1091 // FIXME: What is the ownership of size expressions in VLAs?
1092 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1093 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1094 VAT->getSizeModifier(),
1095 VAT->getIndexTypeQualifier());
1096}
1097
1098
1099const ArrayType *ASTContext::getAsArrayType(QualType T) {
1100 // Handle the non-qualified case efficiently.
1101 if (T.getCVRQualifiers() == 0) {
1102 // Handle the common positive case fast.
1103 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1104 return AT;
1105 }
1106
1107 // Handle the common negative case fast, ignoring CVR qualifiers.
1108 QualType CType = T->getCanonicalTypeInternal();
1109
1110 // Make sure to look through type qualifiers (like ASQuals) for the negative
1111 // test.
1112 if (!isa<ArrayType>(CType) &&
1113 !isa<ArrayType>(CType.getUnqualifiedType()))
1114 return 0;
1115
1116 // Apply any CVR qualifiers from the array type to the element type. This
1117 // implements C99 6.7.3p8: "If the specification of an array type includes
1118 // any type qualifiers, the element type is so qualified, not the array type."
1119
1120 // If we get here, we either have type qualifiers on the type, or we have
1121 // sugar such as a typedef in the way. If we have type qualifiers on the type
1122 // we must propagate them down into the elemeng type.
1123 unsigned CVRQuals = T.getCVRQualifiers();
1124 unsigned AddrSpace = 0;
1125 Type *Ty = T.getTypePtr();
1126
1127 // Rip through ASQualType's and typedefs to get to a concrete type.
1128 while (1) {
1129 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1130 AddrSpace = ASQT->getAddressSpace();
1131 Ty = ASQT->getBaseType();
1132 } else {
1133 T = Ty->getDesugaredType();
1134 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1135 break;
1136 CVRQuals |= T.getCVRQualifiers();
1137 Ty = T.getTypePtr();
1138 }
1139 }
1140
1141 // If we have a simple case, just return now.
1142 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1143 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1144 return ATy;
1145
1146 // Otherwise, we have an array and we have qualifiers on it. Push the
1147 // qualifiers into the array element type and return a new array type.
1148 // Get the canonical version of the element with the extra qualifiers on it.
1149 // This can recursively sink qualifiers through multiple levels of arrays.
1150 QualType NewEltTy = ATy->getElementType();
1151 if (AddrSpace)
1152 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1153 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1154
1155 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1156 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1157 CAT->getSizeModifier(),
1158 CAT->getIndexTypeQualifier()));
1159 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1160 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1161 IAT->getSizeModifier(),
1162 IAT->getIndexTypeQualifier()));
1163
1164 // FIXME: What is the ownership of size expressions in VLAs?
1165 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1166 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1167 VAT->getSizeModifier(),
1168 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001169}
1170
1171
Chris Lattnere6327742008-04-02 05:18:44 +00001172/// getArrayDecayedType - Return the properly qualified result of decaying the
1173/// specified array type to a pointer. This operation is non-trivial when
1174/// handling typedefs etc. The canonical type of "T" must be an array type,
1175/// this returns a pointer to a properly qualified element of the array.
1176///
1177/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1178QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001179 // Get the element type with 'getAsArrayType' so that we don't lose any
1180 // typedefs in the element type of the array. This also handles propagation
1181 // of type qualifiers from the array type into the element type if present
1182 // (C99 6.7.3p8).
1183 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1184 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001185
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001186 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001187
1188 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001189 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001190}
1191
Reid Spencer5f016e22007-07-11 17:01:13 +00001192/// getFloatingRank - Return a relative rank for floating point types.
1193/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001194static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001195 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001197
Christopher Lambebb97e92008-02-04 02:31:56 +00001198 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001199 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 case BuiltinType::Float: return FloatRank;
1201 case BuiltinType::Double: return DoubleRank;
1202 case BuiltinType::LongDouble: return LongDoubleRank;
1203 }
1204}
1205
Steve Naroff716c7302007-08-27 01:41:48 +00001206/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1207/// point or a complex type (based on typeDomain/typeSize).
1208/// 'typeDomain' is a real floating point or complex type.
1209/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001210QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1211 QualType Domain) const {
1212 FloatingRank EltRank = getFloatingRank(Size);
1213 if (Domain->isComplexType()) {
1214 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001215 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001216 case FloatRank: return FloatComplexTy;
1217 case DoubleRank: return DoubleComplexTy;
1218 case LongDoubleRank: return LongDoubleComplexTy;
1219 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 }
Chris Lattner1361b112008-04-06 23:58:54 +00001221
1222 assert(Domain->isRealFloatingType() && "Unknown domain!");
1223 switch (EltRank) {
1224 default: assert(0 && "getFloatingRank(): illegal value for rank");
1225 case FloatRank: return FloatTy;
1226 case DoubleRank: return DoubleTy;
1227 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001228 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001229}
1230
Chris Lattner7cfeb082008-04-06 23:55:33 +00001231/// getFloatingTypeOrder - Compare the rank of the two specified floating
1232/// point types, ignoring the domain of the type (i.e. 'double' ==
1233/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1234/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001235int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1236 FloatingRank LHSR = getFloatingRank(LHS);
1237 FloatingRank RHSR = getFloatingRank(RHS);
1238
1239 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001240 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001241 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001242 return 1;
1243 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001244}
1245
Chris Lattnerf52ab252008-04-06 22:59:24 +00001246/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1247/// routine will assert if passed a built-in type that isn't an integer or enum,
1248/// or if it is not canonicalized.
1249static unsigned getIntegerRank(Type *T) {
1250 assert(T->isCanonical() && "T should be canonicalized");
1251 if (isa<EnumType>(T))
1252 return 4;
1253
1254 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001255 default: assert(0 && "getIntegerRank(): not a built-in integer");
1256 case BuiltinType::Bool:
1257 return 1;
1258 case BuiltinType::Char_S:
1259 case BuiltinType::Char_U:
1260 case BuiltinType::SChar:
1261 case BuiltinType::UChar:
1262 return 2;
1263 case BuiltinType::Short:
1264 case BuiltinType::UShort:
1265 return 3;
1266 case BuiltinType::Int:
1267 case BuiltinType::UInt:
1268 return 4;
1269 case BuiltinType::Long:
1270 case BuiltinType::ULong:
1271 return 5;
1272 case BuiltinType::LongLong:
1273 case BuiltinType::ULongLong:
1274 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001275 }
1276}
1277
Chris Lattner7cfeb082008-04-06 23:55:33 +00001278/// getIntegerTypeOrder - Returns the highest ranked integer type:
1279/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1280/// LHS < RHS, return -1.
1281int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001282 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1283 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001284 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285
Chris Lattnerf52ab252008-04-06 22:59:24 +00001286 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1287 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001288
Chris Lattner7cfeb082008-04-06 23:55:33 +00001289 unsigned LHSRank = getIntegerRank(LHSC);
1290 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291
Chris Lattner7cfeb082008-04-06 23:55:33 +00001292 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1293 if (LHSRank == RHSRank) return 0;
1294 return LHSRank > RHSRank ? 1 : -1;
1295 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
Chris Lattner7cfeb082008-04-06 23:55:33 +00001297 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1298 if (LHSUnsigned) {
1299 // If the unsigned [LHS] type is larger, return it.
1300 if (LHSRank >= RHSRank)
1301 return 1;
1302
1303 // If the signed type can represent all values of the unsigned type, it
1304 // wins. Because we are dealing with 2's complement and types that are
1305 // powers of two larger than each other, this is always safe.
1306 return -1;
1307 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001308
Chris Lattner7cfeb082008-04-06 23:55:33 +00001309 // If the unsigned [RHS] type is larger, return it.
1310 if (RHSRank >= LHSRank)
1311 return -1;
1312
1313 // If the signed type can represent all values of the unsigned type, it
1314 // wins. Because we are dealing with 2's complement and types that are
1315 // powers of two larger than each other, this is always safe.
1316 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001317}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001318
1319// getCFConstantStringType - Return the type used for constant CFStrings.
1320QualType ASTContext::getCFConstantStringType() {
1321 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001322 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001323 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001324 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001325 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001326
1327 // const int *isa;
1328 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001329 // int flags;
1330 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001331 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001332 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001333 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001334 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001335 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001336 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001337
Anders Carlssonf06273f2007-11-19 00:25:30 +00001338 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001339 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001340 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001341
1342 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1343 }
1344
1345 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001346}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001347
Anders Carlssone8c49532007-10-29 06:33:42 +00001348// This returns true if a type has been typedefed to BOOL:
1349// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001350static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001351 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001352 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001353
1354 return false;
1355}
1356
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001357/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001358/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001360 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001361
1362 // Make all integer and enum types at least as large as an int
1363 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001364 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001365 // Treat arrays as pointers, since that's how they're passed in.
1366 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001367 sz = getTypeSize(VoidPtrTy);
1368 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001369}
1370
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001372/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001373void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001374 std::string& S)
1375{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001376 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001377 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001378 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001379 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001380 // Compute size of all parameters.
1381 // Start with computing size of a pointer in number of bytes.
1382 // FIXME: There might(should) be a better way of doing this computation!
1383 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001384 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001385 // The first two arguments (self and _cmd) are pointers; account for
1386 // their size.
1387 int ParmOffset = 2 * PtrSize;
1388 int NumOfParams = Decl->getNumParams();
1389 for (int i = 0; i < NumOfParams; i++) {
1390 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001391 int sz = getObjCEncodingTypeSize (PType);
1392 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001393 ParmOffset += sz;
1394 }
1395 S += llvm::utostr(ParmOffset);
1396 S += "@0:";
1397 S += llvm::utostr(PtrSize);
1398
1399 // Argument types.
1400 ParmOffset = 2 * PtrSize;
1401 for (int i = 0; i < NumOfParams; i++) {
1402 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001403 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001404 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 getObjCEncodingForTypeQualifier(
1406 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001407 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001408 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001410 }
1411}
1412
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001413void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001414 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001415 // FIXME: This currently doesn't encode:
1416 // @ An object (whether statically typed or typed id)
1417 // # A class object (Class)
1418 // : A method selector (SEL)
1419 // {name=type...} A structure
1420 // (name=type...) A union
1421 // bnum A bit field of num bits
1422
1423 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001424 char encoding;
1425 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001426 default: assert(0 && "Unhandled builtin type kind");
1427 case BuiltinType::Void: encoding = 'v'; break;
1428 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001429 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001430 case BuiltinType::UChar: encoding = 'C'; break;
1431 case BuiltinType::UShort: encoding = 'S'; break;
1432 case BuiltinType::UInt: encoding = 'I'; break;
1433 case BuiltinType::ULong: encoding = 'L'; break;
1434 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001435 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001436 case BuiltinType::SChar: encoding = 'c'; break;
1437 case BuiltinType::Short: encoding = 's'; break;
1438 case BuiltinType::Int: encoding = 'i'; break;
1439 case BuiltinType::Long: encoding = 'l'; break;
1440 case BuiltinType::LongLong: encoding = 'q'; break;
1441 case BuiltinType::Float: encoding = 'f'; break;
1442 case BuiltinType::Double: encoding = 'd'; break;
1443 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001444 }
1445
1446 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001447 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001449 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001450 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001451
1452 }
1453 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001454 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001455 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001456 S += '@';
1457 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001458 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001459 S += '#';
1460 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001462 S += ':';
1463 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001464 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001465
1466 if (PointeeTy->isCharType()) {
1467 // char pointer types should be encoded as '*' unless it is a
1468 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001469 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001470 S += '*';
1471 return;
1472 }
1473 }
1474
1475 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001476 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001477 } else if (const ArrayType *AT =
1478 // Ignore type qualifiers etc.
1479 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001480 S += '[';
1481
1482 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1483 S += llvm::utostr(CAT->getSize().getZExtValue());
1484 else
1485 assert(0 && "Unhandled array type!");
1486
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001487 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001488 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001489 } else if (T->getAsFunctionType()) {
1490 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001491 } else if (const RecordType *RTy = T->getAsRecordType()) {
1492 RecordDecl *RDecl= RTy->getDecl();
1493 S += '{';
1494 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001495 bool found = false;
1496 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1497 if (ERType[i] == RTy) {
1498 found = true;
1499 break;
1500 }
1501 if (!found) {
1502 ERType.push_back(RTy);
1503 S += '=';
1504 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1505 FieldDecl *field = RDecl->getMember(i);
1506 getObjCEncodingForType(field->getType(), S, ERType);
1507 }
1508 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1509 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001510 }
1511 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001512 } else if (T->isEnumeralType()) {
1513 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001514 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001515 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001516}
1517
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001519 std::string& S) const {
1520 if (QT & Decl::OBJC_TQ_In)
1521 S += 'n';
1522 if (QT & Decl::OBJC_TQ_Inout)
1523 S += 'N';
1524 if (QT & Decl::OBJC_TQ_Out)
1525 S += 'o';
1526 if (QT & Decl::OBJC_TQ_Bycopy)
1527 S += 'O';
1528 if (QT & Decl::OBJC_TQ_Byref)
1529 S += 'R';
1530 if (QT & Decl::OBJC_TQ_Oneway)
1531 S += 'V';
1532}
1533
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001534void ASTContext::setBuiltinVaListType(QualType T)
1535{
1536 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1537
1538 BuiltinVaListType = T;
1539}
1540
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001541void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001542{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001543 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001544
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001546
1547 // typedef struct objc_object *id;
1548 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1549 assert(ptr && "'id' incorrectly typed");
1550 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1551 assert(rec && "'id' incorrectly typed");
1552 IdStructType = rec;
1553}
1554
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001555void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001556{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001557 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001558
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001560
1561 // typedef struct objc_selector *SEL;
1562 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1563 assert(ptr && "'SEL' incorrectly typed");
1564 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1565 assert(rec && "'SEL' incorrectly typed");
1566 SelStructType = rec;
1567}
1568
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001570{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001571 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1572 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001573}
1574
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001576{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001577 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001578
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001579 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001580
1581 // typedef struct objc_class *Class;
1582 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1583 assert(ptr && "'Class' incorrectly typed");
1584 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1585 assert(rec && "'Class' incorrectly typed");
1586 ClassStructType = rec;
1587}
1588
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1590 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001591 "'NSConstantString' type already set!");
1592
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001593 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001594}
1595
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001596
1597//===----------------------------------------------------------------------===//
1598// Type Predicates.
1599//===----------------------------------------------------------------------===//
1600
1601/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1602/// to an object type. This includes "id" and "Class" (two 'special' pointers
1603/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1604/// ID type).
1605bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1606 if (Ty->isObjCQualifiedIdType())
1607 return true;
1608
1609 if (!Ty->isPointerType())
1610 return false;
1611
1612 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1613 // pointer types. This looks for the typedef specifically, not for the
1614 // underlying type.
1615 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1616 return true;
1617
1618 // If this a pointer to an interface (e.g. NSString*), it is ok.
1619 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1620}
1621
Chris Lattner6ac46a42008-04-07 06:51:04 +00001622//===----------------------------------------------------------------------===//
1623// Type Compatibility Testing
1624//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001625
Chris Lattner78eca282008-04-07 06:49:41 +00001626/// C99 6.2.7p1: If both are complete types, then the following additional
1627/// requirements apply.
1628/// FIXME (handle compatibility across source files).
1629static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1630 const ASTContext &C) {
Steve Naroffab373092007-11-07 06:03:51 +00001631 // "Class" and "id" are compatible built-in structure types.
Chris Lattner78eca282008-04-07 06:49:41 +00001632 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1633 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroffab373092007-11-07 06:03:51 +00001634 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001635
Chris Lattner78eca282008-04-07 06:49:41 +00001636 // Within a translation unit a tag type is only compatible with itself. Self
1637 // equality is already handled by the time we get here.
1638 assert(LHS != RHS && "Self equality not handled!");
1639 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001640}
1641
1642bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1643 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1644 // identically qualified and both shall be pointers to compatible types.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001645 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1646 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroffec0550f2007-10-15 20:41:53 +00001647 return false;
1648
Chris Lattner28be73f2008-07-26 21:30:36 +00001649 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1650 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001651
1652 return typesAreCompatible(ltype, rtype);
1653}
1654
Steve Naroffec0550f2007-10-15 20:41:53 +00001655bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001656 const FunctionType *lbase = lhs->getAsFunctionType();
1657 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001658 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1659 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1660
1661 // first check the return types (common between C99 and K&R).
1662 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1663 return false;
1664
1665 if (lproto && rproto) { // two C99 style function prototypes
1666 unsigned lproto_nargs = lproto->getNumArgs();
1667 unsigned rproto_nargs = rproto->getNumArgs();
1668
1669 if (lproto_nargs != rproto_nargs)
1670 return false;
1671
1672 // both prototypes have the same number of arguments.
1673 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1674 (rproto->isVariadic() && !lproto->isVariadic()))
1675 return false;
1676
1677 // The use of ellipsis agree...now check the argument types.
1678 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001679 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1680 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001681 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001682 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001683 return false;
1684 return true;
1685 }
Chris Lattner5426bf62008-04-07 07:01:58 +00001686
Steve Naroffec0550f2007-10-15 20:41:53 +00001687 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1688 return true;
1689
1690 // we have a mixture of K&R style with C99 prototypes
1691 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroffec0550f2007-10-15 20:41:53 +00001692 if (proto->isVariadic())
1693 return false;
1694
1695 // FIXME: Each parameter type T in the prototype must be compatible with the
1696 // type resulting from applying the usual argument conversions to T.
1697 return true;
1698}
1699
Chris Lattneracc99722008-04-07 06:56:55 +00001700// C99 6.7.5.2p6
1701static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattneracc99722008-04-07 06:56:55 +00001702 // Constant arrays must be the same size to be compatible.
1703 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1704 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1705 if (RCAT->getSize() != LCAT->getSize())
1706 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001707
Chris Lattner8c7bbb52008-04-07 06:58:21 +00001708 // Compatible arrays must have compatible element types
1709 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroffec0550f2007-10-15 20:41:53 +00001710}
1711
Chris Lattner6ac46a42008-04-07 06:51:04 +00001712/// areCompatVectorTypes - Return true if the two specified vector types are
1713/// compatible.
1714static bool areCompatVectorTypes(const VectorType *LHS,
1715 const VectorType *RHS) {
1716 assert(LHS->isCanonical() && RHS->isCanonical());
1717 return LHS->getElementType() == RHS->getElementType() &&
1718 LHS->getNumElements() == RHS->getNumElements();
1719}
1720
1721/// areCompatObjCInterfaces - Return true if the two interface types are
1722/// compatible for assignment from RHS to LHS. This handles validation of any
1723/// protocol qualifiers on the LHS or RHS.
1724///
Chris Lattner5426bf62008-04-07 07:01:58 +00001725static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1726 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001727 // Verify that the base decls are compatible: the RHS must be a subclass of
1728 // the LHS.
1729 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1730 return false;
1731
1732 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1733 // protocol qualified at all, then we are good.
1734 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1735 return true;
1736
1737 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1738 // isn't a superset.
1739 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1740 return true; // FIXME: should return false!
1741
1742 // Finally, we must have two protocol-qualified interfaces.
1743 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1744 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1745 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1746 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1747 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1748 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1749
1750 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1751 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1752 // LHS in a single parallel scan until we run out of elements in LHS.
1753 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1754 ObjCProtocolDecl *LHSProto = *LHSPI;
1755
1756 while (RHSPI != RHSPE) {
1757 ObjCProtocolDecl *RHSProto = *RHSPI++;
1758 // If the RHS has a protocol that the LHS doesn't, ignore it.
1759 if (RHSProto != LHSProto)
1760 continue;
1761
1762 // Otherwise, the RHS does have this element.
1763 ++LHSPI;
1764 if (LHSPI == LHSPE)
1765 return true; // All protocols in LHS exist in RHS.
1766
1767 LHSProto = *LHSPI;
1768 }
1769
1770 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1771 return false;
1772}
1773
1774
Steve Naroffec0550f2007-10-15 20:41:53 +00001775/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1776/// both shall have the identically qualified version of a compatible type.
1777/// C99 6.2.7p1: Two types have compatible types if their types are the
1778/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001779bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner28be73f2008-07-26 21:30:36 +00001780 QualType LHS = getCanonicalType(LHS_NC);
1781 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner988ee6e2008-04-03 05:07:04 +00001782
Bill Wendling43d69752007-12-03 07:33:35 +00001783 // C++ [expr]: If an expression initially has the type "reference to T", the
1784 // type is adjusted to "T" prior to any further analysis, the expression
1785 // designates the object or function denoted by the reference, and the
1786 // expression is an lvalue.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001787 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1788 LHS = RT->getPointeeType();
1789 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1790 RHS = RT->getPointeeType();
Chris Lattner1adb8832008-01-14 05:45:46 +00001791
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001792 // If two types are identical, they are compatible.
1793 if (LHS == RHS)
1794 return true;
1795
1796 // If qualifiers differ, the types are different.
Chris Lattnera36a61f2008-04-07 05:43:21 +00001797 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1798 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001799 return false;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001800
1801 // Strip off ASQual's if present.
1802 if (LHSAS) {
1803 LHS = LHS.getUnqualifiedType();
1804 RHS = RHS.getUnqualifiedType();
1805 }
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001806
Chris Lattnerc4e40592008-04-07 04:07:56 +00001807 Type::TypeClass LHSClass = LHS->getTypeClass();
1808 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattner1adb8832008-01-14 05:45:46 +00001809
1810 // We want to consider the two function types to be the same for these
1811 // comparisons, just force one to the other.
1812 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1813 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001814
1815 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001816 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1817 LHSClass = Type::ConstantArray;
1818 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1819 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001820
Nate Begeman213541a2008-04-18 23:10:10 +00001821 // Canonicalize ExtVector -> Vector.
1822 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1823 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001824
Chris Lattnerb0489812008-04-07 06:38:24 +00001825 // Consider qualified interfaces and interfaces the same.
1826 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1827 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1828
Chris Lattnera36a61f2008-04-07 05:43:21 +00001829 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001830 if (LHSClass != RHSClass) {
Chris Lattnerb0489812008-04-07 06:38:24 +00001831 // ID is compatible with all interface types.
1832 if (isa<ObjCInterfaceType>(LHS))
1833 return isObjCIdType(RHS);
1834 if (isa<ObjCInterfaceType>(RHS))
1835 return isObjCIdType(LHS);
Steve Naroff97341622008-06-04 15:07:33 +00001836
1837 // ID is compatible with all qualified id types.
1838 if (isa<ObjCQualifiedIdType>(LHS)) {
1839 if (const PointerType *PT = RHS->getAsPointerType())
1840 return isObjCIdType(PT->getPointeeType());
1841 }
1842 if (isa<ObjCQualifiedIdType>(RHS)) {
1843 if (const PointerType *PT = LHS->getAsPointerType())
1844 return isObjCIdType(PT->getPointeeType());
1845 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001846 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1847 // a signed integer type, or an unsigned integer type.
Chris Lattnerc4e40592008-04-07 04:07:56 +00001848 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1849 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1850 return EDecl->getIntegerType() == RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001851 }
Chris Lattnerc4e40592008-04-07 04:07:56 +00001852 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1853 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1854 return EDecl->getIntegerType() == LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001855 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001856
Steve Naroffec0550f2007-10-15 20:41:53 +00001857 return false;
1858 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00001859
Steve Naroff4a746782008-01-09 22:43:08 +00001860 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001861 switch (LHSClass) {
Chris Lattnera36a61f2008-04-07 05:43:21 +00001862 case Type::ASQual:
1863 case Type::FunctionProto:
1864 case Type::VariableArray:
1865 case Type::IncompleteArray:
1866 case Type::Reference:
Chris Lattnerb0489812008-04-07 06:38:24 +00001867 case Type::ObjCQualifiedInterface:
Chris Lattnera36a61f2008-04-07 05:43:21 +00001868 assert(0 && "Canonicalized away above");
Chris Lattner1adb8832008-01-14 05:45:46 +00001869 case Type::Pointer:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001870 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001871 case Type::ConstantArray:
Chris Lattneracc99722008-04-07 06:56:55 +00001872 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1873 *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001874 case Type::FunctionNoProto:
Chris Lattnerc4e40592008-04-07 04:07:56 +00001875 return functionTypesAreCompatible(LHS, RHS);
Chris Lattner1adb8832008-01-14 05:45:46 +00001876 case Type::Tagged: // handle structures, unions
Chris Lattner78eca282008-04-07 06:49:41 +00001877 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattner1adb8832008-01-14 05:45:46 +00001878 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00001879 // Only exactly equal builtin types are compatible, which is tested above.
1880 return false;
1881 case Type::Vector:
1882 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001883 case Type::ObjCInterface:
Chris Lattnerb0489812008-04-07 06:38:24 +00001884 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1885 cast<ObjCInterfaceType>(RHS));
Chris Lattner1adb8832008-01-14 05:45:46 +00001886 default:
1887 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001888 }
1889 return true; // should never get here...
1890}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001891
Chris Lattner5426bf62008-04-07 07:01:58 +00001892//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00001893// Integer Predicates
1894//===----------------------------------------------------------------------===//
1895unsigned ASTContext::getIntWidth(QualType T) {
1896 if (T == BoolTy)
1897 return 1;
1898 // At the moment, only bool has padding bits
1899 return (unsigned)getTypeSize(T);
1900}
1901
1902QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1903 assert(T->isSignedIntegerType() && "Unexpected type");
1904 if (const EnumType* ETy = T->getAsEnumType())
1905 T = ETy->getDecl()->getIntegerType();
1906 const BuiltinType* BTy = T->getAsBuiltinType();
1907 assert (BTy && "Unexpected signed integer type");
1908 switch (BTy->getKind()) {
1909 case BuiltinType::Char_S:
1910 case BuiltinType::SChar:
1911 return UnsignedCharTy;
1912 case BuiltinType::Short:
1913 return UnsignedShortTy;
1914 case BuiltinType::Int:
1915 return UnsignedIntTy;
1916 case BuiltinType::Long:
1917 return UnsignedLongTy;
1918 case BuiltinType::LongLong:
1919 return UnsignedLongLongTy;
1920 default:
1921 assert(0 && "Unexpected signed integer type");
1922 return QualType();
1923 }
1924}
1925
1926
1927//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00001928// Serialization Support
1929//===----------------------------------------------------------------------===//
1930
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001931/// Emit - Serialize an ASTContext object to Bitcode.
1932void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001933 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00001934 S.EmitRef(SourceMgr);
1935 S.EmitRef(Target);
1936 S.EmitRef(Idents);
1937 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001938
Ted Kremenekfee04522007-10-31 22:44:07 +00001939 // Emit the size of the type vector so that we can reserve that size
1940 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001941 S.EmitInt(Types.size());
1942
Ted Kremenek03ed4402007-11-13 22:02:55 +00001943 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1944 I!=E;++I)
1945 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001946
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001947 S.EmitOwnedPtr(TUDecl);
1948
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001949 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001950}
1951
Ted Kremenek0f84c002007-11-13 00:25:37 +00001952ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001953
1954 // Read the language options.
1955 LangOptions LOpts;
1956 LOpts.Read(D);
1957
Ted Kremenekfee04522007-10-31 22:44:07 +00001958 SourceManager &SM = D.ReadRef<SourceManager>();
1959 TargetInfo &t = D.ReadRef<TargetInfo>();
1960 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1961 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00001962
Ted Kremenekfee04522007-10-31 22:44:07 +00001963 unsigned size_reserve = D.ReadInt();
1964
Ted Kremeneke7d07d12008-06-04 15:55:15 +00001965 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00001966
Ted Kremenek03ed4402007-11-13 22:02:55 +00001967 for (unsigned i = 0; i < size_reserve; ++i)
1968 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001969
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001970 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1971
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001972 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001973
1974 return A;
1975}