blob: a4b54f25e061a5e5629a9ff3f582068d846a94f9 [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"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Daniel Dunbare91593e2008-08-11 04:54:23 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
31 IdentifierTable &idents, SelectorTable &sels,
32 unsigned size_reserve) :
33 CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t),
34 Idents(idents), Selectors(sels)
35{
36 if (size_reserve > 0) Types.reserve(size_reserve);
37 InitBuiltinTypes();
38 BuiltinInfo.InitializeBuiltins(idents, Target);
39 TUDecl = TranslationUnitDecl::Create(*this);
40}
41
Reid Spencer5f016e22007-07-11 17:01:13 +000042ASTContext::~ASTContext() {
43 // Deallocate all the types.
44 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000045 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 Types.pop_back();
47 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000048
49 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
52void ASTContext::PrintStats() const {
53 fprintf(stderr, "*** AST Context Stats:\n");
54 fprintf(stderr, " %d types total.\n", (int)Types.size());
55 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000056 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000057 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
58
59 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
61 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000062 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000063
64 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
65 Type *T = Types[i];
66 if (isa<BuiltinType>(T))
67 ++NumBuiltin;
68 else if (isa<PointerType>(T))
69 ++NumPointer;
70 else if (isa<ReferenceType>(T))
71 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000072 else if (isa<ComplexType>(T))
73 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 else if (isa<ArrayType>(T))
75 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000076 else if (isa<VectorType>(T))
77 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 else if (isa<FunctionTypeNoProto>(T))
79 ++NumFunctionNP;
80 else if (isa<FunctionTypeProto>(T))
81 ++NumFunctionP;
82 else if (isa<TypedefType>(T))
83 ++NumTypeName;
84 else if (TagType *TT = dyn_cast<TagType>(T)) {
85 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000086 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000087 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000088 case TagDecl::TK_struct: ++NumTagStruct; break;
89 case TagDecl::TK_union: ++NumTagUnion; break;
90 case TagDecl::TK_class: ++NumTagClass; break;
91 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000092 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000093 } else if (isa<ObjCInterfaceType>(T))
94 ++NumObjCInterfaces;
95 else if (isa<ObjCQualifiedInterfaceType>(T))
96 ++NumObjCQualifiedInterfaces;
97 else if (isa<ObjCQualifiedIdType>(T))
98 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +000099 else if (isa<TypeOfType>(T))
100 ++NumTypeOfTypes;
101 else if (isa<TypeOfExpr>(T))
102 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000103 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000104 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 assert(0 && "Unknown type!");
106 }
107 }
108
109 fprintf(stderr, " %d builtin types\n", NumBuiltin);
110 fprintf(stderr, " %d pointer types\n", NumPointer);
111 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000112 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000114 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
116 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
117 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
118 fprintf(stderr, " %d tagged types\n", NumTagged);
119 fprintf(stderr, " %d struct types\n", NumTagStruct);
120 fprintf(stderr, " %d union types\n", NumTagUnion);
121 fprintf(stderr, " %d class types\n", NumTagClass);
122 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000123 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000124 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000125 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000126 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000127 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000128 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
129 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
132 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000133 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 NumFunctionP*sizeof(FunctionTypeProto)+
135 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000136 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
137 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
140
141void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
142 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
143}
144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145void ASTContext::InitBuiltinTypes() {
146 assert(VoidTy.isNull() && "Context reinitialized?");
147
148 // C99 6.2.5p19.
149 InitBuiltinType(VoidTy, BuiltinType::Void);
150
151 // C99 6.2.5p2.
152 InitBuiltinType(BoolTy, BuiltinType::Bool);
153 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000154 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 InitBuiltinType(CharTy, BuiltinType::Char_S);
156 else
157 InitBuiltinType(CharTy, BuiltinType::Char_U);
158 // C99 6.2.5p4.
159 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
160 InitBuiltinType(ShortTy, BuiltinType::Short);
161 InitBuiltinType(IntTy, BuiltinType::Int);
162 InitBuiltinType(LongTy, BuiltinType::Long);
163 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
164
165 // C99 6.2.5p6.
166 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
167 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
168 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
169 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
170 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
171
172 // C99 6.2.5p10.
173 InitBuiltinType(FloatTy, BuiltinType::Float);
174 InitBuiltinType(DoubleTy, BuiltinType::Double);
175 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000176
177 // C++ 3.9.1p5
178 InitBuiltinType(WCharTy, BuiltinType::WChar);
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 // C99 6.2.5p11.
181 FloatComplexTy = getComplexType(FloatTy);
182 DoubleComplexTy = getComplexType(DoubleTy);
183 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000184
185 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000186 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000187 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000189 ClassStructType = 0;
190
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000191 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000192
193 // void * type
194 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195}
196
Chris Lattner464175b2007-07-18 17:52:12 +0000197//===----------------------------------------------------------------------===//
198// Type Sizing and Analysis
199//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000200
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000201/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
202/// scalar floating point type.
203const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
204 const BuiltinType *BT = T->getAsBuiltinType();
205 assert(BT && "Not a floating point type!");
206 switch (BT->getKind()) {
207 default: assert(0 && "Not a floating point type!");
208 case BuiltinType::Float: return Target.getFloatFormat();
209 case BuiltinType::Double: return Target.getDoubleFormat();
210 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
211 }
212}
213
214
Chris Lattnera7674d82007-07-13 22:13:22 +0000215/// getTypeSize - Return the size of the specified type, in bits. This method
216/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000217std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000218ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000219 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000220 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000221 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000222 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000223 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000224 case Type::FunctionNoProto:
225 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000226 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000227 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000228 case Type::VariableArray:
229 assert(0 && "VLAs not implemented yet!");
230 case Type::ConstantArray: {
231 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
232
Chris Lattner98be4942008-03-05 18:54:05 +0000233 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000234 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000235 Align = EltInfo.second;
236 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000237 }
Nate Begeman213541a2008-04-18 23:10:10 +0000238 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000239 case Type::Vector: {
240 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000241 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000242 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000243 // FIXME: This isn't right for unusual vectors
244 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000245 break;
246 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000247
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000248 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000249 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000250 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000251 case BuiltinType::Void:
252 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000253 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000254 Width = Target.getBoolWidth();
255 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000256 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000257 case BuiltinType::Char_S:
258 case BuiltinType::Char_U:
259 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000260 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000261 Width = Target.getCharWidth();
262 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000263 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000264 case BuiltinType::WChar:
265 Width = Target.getWCharWidth();
266 Align = Target.getWCharAlign();
267 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000268 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000269 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000270 Width = Target.getShortWidth();
271 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000272 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000273 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000274 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000275 Width = Target.getIntWidth();
276 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000277 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000278 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000279 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000280 Width = Target.getLongWidth();
281 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000282 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000283 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000284 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000285 Width = Target.getLongLongWidth();
286 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000287 break;
288 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000289 Width = Target.getFloatWidth();
290 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000291 break;
292 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000293 Width = Target.getDoubleWidth();
294 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000295 break;
296 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000297 Width = Target.getLongDoubleWidth();
298 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000299 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000300 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000301 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000302 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000303 // FIXME: Pointers into different addr spaces could have different sizes and
304 // alignment requirements: getPointerInfo should take an AddrSpace.
305 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000306 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000307 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000308 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000309 break;
Chris Lattnerf72a4432008-03-08 08:34:58 +0000310 case Type::Pointer: {
311 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000312 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000313 Align = Target.getPointerAlign(AS);
314 break;
315 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000316 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000317 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000318 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000319 // FIXME: This is wrong for struct layout: a reference in a struct has
320 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000321 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000322
323 case Type::Complex: {
324 // Complex types have the same alignment as their elements, but twice the
325 // size.
326 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000327 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000328 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000329 Align = EltInfo.second;
330 break;
331 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000332 case Type::ObjCInterface: {
333 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
334 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
335 Width = Layout.getSize();
336 Align = Layout.getAlignment();
337 break;
338 }
Chris Lattner71763312008-04-06 22:05:18 +0000339 case Type::Tagged: {
Chris Lattner8389eab2008-08-09 21:35:13 +0000340 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
341 Width = 1;
342 Align = 1;
343 break;
344 }
345
Chris Lattner71763312008-04-06 22:05:18 +0000346 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
347 return getTypeInfo(ET->getDecl()->getIntegerType());
348
349 RecordType *RT = cast<RecordType>(T);
350 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
351 Width = Layout.getSize();
352 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000353 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000354 }
Chris Lattner71763312008-04-06 22:05:18 +0000355 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000356
Chris Lattner464175b2007-07-18 17:52:12 +0000357 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000359}
360
Devang Patel8b277042008-06-04 21:22:16 +0000361/// LayoutField - Field layout.
362void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
363 bool IsUnion, bool StructIsPacked,
364 ASTContext &Context) {
365 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
366 uint64_t FieldOffset = IsUnion ? 0 : Size;
367 uint64_t FieldSize;
368 unsigned FieldAlign;
369
370 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
371 // TODO: Need to check this algorithm on other targets!
372 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000373 FieldSize =
374 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000375
376 std::pair<uint64_t, unsigned> FieldInfo =
377 Context.getTypeInfo(FD->getType());
378 uint64_t TypeSize = FieldInfo.first;
379
380 FieldAlign = FieldInfo.second;
381 if (FieldIsPacked)
382 FieldAlign = 1;
383 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
384 FieldAlign = std::max(FieldAlign, AA->getAlignment());
385
386 // Check if we need to add padding to give the field the correct
387 // alignment.
388 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
389 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
390
391 // Padding members don't affect overall alignment
392 if (!FD->getIdentifier())
393 FieldAlign = 1;
394 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000395 if (FD->getType()->isIncompleteArrayType()) {
396 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000397 // query getTypeInfo about these, so we figure it out here.
398 // Flexible array members don't have any size, but they
399 // have to be aligned appropriately for their element type.
400 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000401 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000402 FieldAlign = Context.getTypeAlign(ATy->getElementType());
403 } else {
404 std::pair<uint64_t, unsigned> FieldInfo =
405 Context.getTypeInfo(FD->getType());
406 FieldSize = FieldInfo.first;
407 FieldAlign = FieldInfo.second;
408 }
409
410 if (FieldIsPacked)
411 FieldAlign = 8;
412 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
413 FieldAlign = std::max(FieldAlign, AA->getAlignment());
414
415 // Round up the current record size to the field's alignment boundary.
416 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
417 }
418
419 // Place this field at the current location.
420 FieldOffsets[FieldNo] = FieldOffset;
421
422 // Reserve space for this field.
423 if (IsUnion) {
424 Size = std::max(Size, FieldSize);
425 } else {
426 Size = FieldOffset + FieldSize;
427 }
428
429 // Remember max struct/class alignment.
430 Alignment = std::max(Alignment, FieldAlign);
431}
432
Devang Patel44a3dde2008-06-04 21:54:36 +0000433
434/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
435/// specified Objective C, which indicates its size and ivar
436/// position information.
437const ASTRecordLayout &
438ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
439 // Look up this layout, if already laid out, return what we have.
440 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
441 if (Entry) return *Entry;
442
443 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
444 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000445 ASTRecordLayout *NewEntry = NULL;
446 unsigned FieldCount = D->ivar_size();
447 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
448 FieldCount++;
449 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
450 unsigned Alignment = SL.getAlignment();
451 uint64_t Size = SL.getSize();
452 NewEntry = new ASTRecordLayout(Size, Alignment);
453 NewEntry->InitializeLayout(FieldCount);
454 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
455 } else {
456 NewEntry = new ASTRecordLayout();
457 NewEntry->InitializeLayout(FieldCount);
458 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000459 Entry = NewEntry;
460
Devang Patel44a3dde2008-06-04 21:54:36 +0000461 bool IsPacked = D->getAttr<PackedAttr>();
462
463 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
464 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
465 AA->getAlignment()));
466
467 // Layout each ivar sequentially.
468 unsigned i = 0;
469 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
470 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
471 const ObjCIvarDecl* Ivar = (*IVI);
472 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
473 }
474
475 // Finally, round the size of the total struct up to the alignment of the
476 // struct itself.
477 NewEntry->FinalizeLayout();
478 return *NewEntry;
479}
480
Devang Patel88a981b2007-11-01 19:11:01 +0000481/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000482/// specified record (struct/union/class), which indicates its size and field
483/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000484const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner464175b2007-07-18 17:52:12 +0000485 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000486
Chris Lattner464175b2007-07-18 17:52:12 +0000487 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000488 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000489 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000490
Devang Patel88a981b2007-11-01 19:11:01 +0000491 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
492 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
493 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000494 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000495
Devang Patel8b277042008-06-04 21:22:16 +0000496 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000497 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000498 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000499
Eli Friedman4bd998b2008-05-30 09:31:38 +0000500 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000501 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
502 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000503
Eli Friedman4bd998b2008-05-30 09:31:38 +0000504 // Layout each field, for now, just sequentially, respecting alignment. In
505 // the future, this will need to be tweakable by targets.
506 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
507 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000508 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000509 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000510
511 // Finally, round the size of the total struct up to the alignment of the
512 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000513 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000514 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000515}
516
Chris Lattnera7674d82007-07-13 22:13:22 +0000517//===----------------------------------------------------------------------===//
518// Type creation/memoization methods
519//===----------------------------------------------------------------------===//
520
Christopher Lambebb97e92008-02-04 02:31:56 +0000521QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000522 QualType CanT = getCanonicalType(T);
523 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000524 return T;
525
526 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
527 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000528 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000529 "Type is already address space qualified");
530
531 // Check if we've already instantiated an address space qual'd type of this
532 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000533 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000534 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000535 void *InsertPos = 0;
536 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
537 return QualType(ASQy, 0);
538
539 // If the base type isn't canonical, this won't be a canonical type either,
540 // so fill in the canonical type field.
541 QualType Canonical;
542 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000543 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000544
545 // Get the new insert position for the node we care about.
546 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
547 assert(NewIP == 0 && "Shouldn't be in the map!");
548 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000549 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000550 ASQualTypes.InsertNode(New, InsertPos);
551 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000552 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000553}
554
Chris Lattnera7674d82007-07-13 22:13:22 +0000555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556/// getComplexType - Return the uniqued reference to the type for a complex
557/// number with the specified element type.
558QualType ASTContext::getComplexType(QualType T) {
559 // Unique pointers, to guarantee there is only one pointer of a particular
560 // structure.
561 llvm::FoldingSetNodeID ID;
562 ComplexType::Profile(ID, T);
563
564 void *InsertPos = 0;
565 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
566 return QualType(CT, 0);
567
568 // If the pointee type isn't canonical, this won't be a canonical type either,
569 // so fill in the canonical type field.
570 QualType Canonical;
571 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000572 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000573
574 // Get the new insert position for the node we care about.
575 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
576 assert(NewIP == 0 && "Shouldn't be in the map!");
577 }
578 ComplexType *New = new ComplexType(T, Canonical);
579 Types.push_back(New);
580 ComplexTypes.InsertNode(New, InsertPos);
581 return QualType(New, 0);
582}
583
584
585/// getPointerType - Return the uniqued reference to the type for a pointer to
586/// the specified type.
587QualType ASTContext::getPointerType(QualType T) {
588 // Unique pointers, to guarantee there is only one pointer of a particular
589 // structure.
590 llvm::FoldingSetNodeID ID;
591 PointerType::Profile(ID, T);
592
593 void *InsertPos = 0;
594 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
595 return QualType(PT, 0);
596
597 // If the pointee type isn't canonical, this won't be a canonical type either,
598 // so fill in the canonical type field.
599 QualType Canonical;
600 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000601 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000602
603 // Get the new insert position for the node we care about.
604 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
605 assert(NewIP == 0 && "Shouldn't be in the map!");
606 }
607 PointerType *New = new PointerType(T, Canonical);
608 Types.push_back(New);
609 PointerTypes.InsertNode(New, InsertPos);
610 return QualType(New, 0);
611}
612
Steve Naroff5618bd42008-08-27 16:04:49 +0000613/// getBlockPointerType - Return the uniqued reference to the type for
614/// a pointer to the specified block.
615QualType ASTContext::getBlockPointerType(QualType T) {
616 assert(T->isFunctionType() && "closure of function types only");
617 // Unique pointers, to guarantee there is only one closure of a particular
618 // structure.
619 llvm::FoldingSetNodeID ID;
620 BlockPointerType::Profile(ID, T);
621
622 void *InsertPos = 0;
623 if (BlockPointerType *PT =
624 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
625 return QualType(PT, 0);
626
627 // If the closure pointee type isn't canonical, this won't be a canonical
628 // type either so fill in the canonical type field.
629 QualType Canonical;
630 if (!T->isCanonical()) {
631 Canonical = getBlockPointerType(getCanonicalType(T));
632
633 // Get the new insert position for the node we care about.
634 BlockPointerType *NewIP =
635 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
636 assert(NewIP == 0 && "Shouldn't be in the map!");
637 }
638 BlockPointerType *New = new BlockPointerType(T, Canonical);
639 Types.push_back(New);
640 BlockPointerTypes.InsertNode(New, InsertPos);
641 return QualType(New, 0);
642}
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644/// getReferenceType - Return the uniqued reference to the type for a reference
645/// to the specified type.
646QualType ASTContext::getReferenceType(QualType T) {
647 // Unique pointers, to guarantee there is only one pointer of a particular
648 // structure.
649 llvm::FoldingSetNodeID ID;
650 ReferenceType::Profile(ID, T);
651
652 void *InsertPos = 0;
653 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
654 return QualType(RT, 0);
655
656 // If the referencee type isn't canonical, this won't be a canonical type
657 // either, so fill in the canonical type field.
658 QualType Canonical;
659 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000660 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
662 // Get the new insert position for the node we care about.
663 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
664 assert(NewIP == 0 && "Shouldn't be in the map!");
665 }
666
667 ReferenceType *New = new ReferenceType(T, Canonical);
668 Types.push_back(New);
669 ReferenceTypes.InsertNode(New, InsertPos);
670 return QualType(New, 0);
671}
672
Steve Narofffb22d962007-08-30 01:06:46 +0000673/// getConstantArrayType - Return the unique reference to the type for an
674/// array of the specified element type.
675QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000676 const llvm::APInt &ArySize,
677 ArrayType::ArraySizeModifier ASM,
678 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000680 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000681
682 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000683 if (ConstantArrayType *ATP =
684 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return QualType(ATP, 0);
686
687 // If the element type isn't canonical, this won't be a canonical type either,
688 // so fill in the canonical type field.
689 QualType Canonical;
690 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000691 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000692 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000694 ConstantArrayType *NewIP =
695 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 assert(NewIP == 0 && "Shouldn't be in the map!");
698 }
699
Steve Naroffc9406122007-08-30 18:10:14 +0000700 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
701 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000702 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 Types.push_back(New);
704 return QualType(New, 0);
705}
706
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000707/// getVariableArrayType - Returns a non-unique reference to the type for a
708/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000709QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
710 ArrayType::ArraySizeModifier ASM,
711 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000712 // Since we don't unique expressions, it isn't possible to unique VLA's
713 // that have an expression provided for their size.
714
715 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
716 ASM, EltTypeQuals);
717
718 VariableArrayTypes.push_back(New);
719 Types.push_back(New);
720 return QualType(New, 0);
721}
722
723QualType ASTContext::getIncompleteArrayType(QualType EltTy,
724 ArrayType::ArraySizeModifier ASM,
725 unsigned EltTypeQuals) {
726 llvm::FoldingSetNodeID ID;
727 IncompleteArrayType::Profile(ID, EltTy);
728
729 void *InsertPos = 0;
730 if (IncompleteArrayType *ATP =
731 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
732 return QualType(ATP, 0);
733
734 // If the element type isn't canonical, this won't be a canonical type
735 // either, so fill in the canonical type field.
736 QualType Canonical;
737
738 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000739 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000740 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000741
742 // Get the new insert position for the node we care about.
743 IncompleteArrayType *NewIP =
744 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
745
746 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000747 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000748
749 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
750 ASM, EltTypeQuals);
751
752 IncompleteArrayTypes.InsertNode(New, InsertPos);
753 Types.push_back(New);
754 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000755}
756
Steve Naroff73322922007-07-18 18:00:27 +0000757/// getVectorType - Return the unique reference to a vector type of
758/// the specified element type and size. VectorType must be a built-in type.
759QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 BuiltinType *baseType;
761
Chris Lattnerf52ab252008-04-06 22:59:24 +0000762 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000763 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000764
765 // Check if we've already instantiated a vector of this type.
766 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000767 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 void *InsertPos = 0;
769 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
770 return QualType(VTP, 0);
771
772 // If the element type isn't canonical, this won't be a canonical type either,
773 // so fill in the canonical type field.
774 QualType Canonical;
775 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000776 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000777
778 // Get the new insert position for the node we care about.
779 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
780 assert(NewIP == 0 && "Shouldn't be in the map!");
781 }
782 VectorType *New = new VectorType(vecType, NumElts, Canonical);
783 VectorTypes.InsertNode(New, InsertPos);
784 Types.push_back(New);
785 return QualType(New, 0);
786}
787
Nate Begeman213541a2008-04-18 23:10:10 +0000788/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000789/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000790QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000791 BuiltinType *baseType;
792
Chris Lattnerf52ab252008-04-06 22:59:24 +0000793 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000794 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000795
796 // Check if we've already instantiated a vector of this type.
797 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000798 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000799 void *InsertPos = 0;
800 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
801 return QualType(VTP, 0);
802
803 // If the element type isn't canonical, this won't be a canonical type either,
804 // so fill in the canonical type field.
805 QualType Canonical;
806 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000807 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000808
809 // Get the new insert position for the node we care about.
810 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
811 assert(NewIP == 0 && "Shouldn't be in the map!");
812 }
Nate Begeman213541a2008-04-18 23:10:10 +0000813 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000814 VectorTypes.InsertNode(New, InsertPos);
815 Types.push_back(New);
816 return QualType(New, 0);
817}
818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
820///
821QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
822 // Unique functions, to guarantee there is only one function of a particular
823 // structure.
824 llvm::FoldingSetNodeID ID;
825 FunctionTypeNoProto::Profile(ID, ResultTy);
826
827 void *InsertPos = 0;
828 if (FunctionTypeNoProto *FT =
829 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
830 return QualType(FT, 0);
831
832 QualType Canonical;
833 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000834 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
836 // Get the new insert position for the node we care about.
837 FunctionTypeNoProto *NewIP =
838 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
839 assert(NewIP == 0 && "Shouldn't be in the map!");
840 }
841
842 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
843 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000844 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 return QualType(New, 0);
846}
847
848/// getFunctionType - Return a normal function type with a typed argument
849/// list. isVariadic indicates whether the argument list includes '...'.
Eli Friedman86da77f2008-08-22 00:59:49 +0000850QualType ASTContext::getFunctionType(QualType ResultTy, const QualType *ArgArray,
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 unsigned NumArgs, bool isVariadic) {
852 // Unique functions, to guarantee there is only one function of a particular
853 // structure.
854 llvm::FoldingSetNodeID ID;
855 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
856
857 void *InsertPos = 0;
858 if (FunctionTypeProto *FTP =
859 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
860 return QualType(FTP, 0);
861
862 // Determine whether the type being created is already canonical or not.
863 bool isCanonical = ResultTy->isCanonical();
864 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
865 if (!ArgArray[i]->isCanonical())
866 isCanonical = false;
867
868 // If this type isn't canonical, get the canonical version of it.
869 QualType Canonical;
870 if (!isCanonical) {
871 llvm::SmallVector<QualType, 16> CanonicalArgs;
872 CanonicalArgs.reserve(NumArgs);
873 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000874 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000875
Chris Lattnerf52ab252008-04-06 22:59:24 +0000876 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 &CanonicalArgs[0], NumArgs,
878 isVariadic);
879
880 // Get the new insert position for the node we care about.
881 FunctionTypeProto *NewIP =
882 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
883 assert(NewIP == 0 && "Shouldn't be in the map!");
884 }
885
886 // FunctionTypeProto objects are not allocated with new because they have a
887 // variable size array (for parameter types) at the end of them.
888 FunctionTypeProto *FTP =
889 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000890 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
892 Canonical);
893 Types.push_back(FTP);
894 FunctionTypeProtos.InsertNode(FTP, InsertPos);
895 return QualType(FTP, 0);
896}
897
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000898/// getTypeDeclType - Return the unique reference to the type for the
899/// specified type declaration.
900QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
901 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
902
903 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
904 return getTypedefType(Typedef);
905 else if (ObjCInterfaceDecl *ObjCInterface
906 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
907 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000908
909 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
910 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
911 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000912 Decl->TypeForDecl = new RecordType(Record);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000913 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000914 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000915 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000916 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000917
918 Types.push_back(Decl->TypeForDecl);
919 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000920}
921
Reid Spencer5f016e22007-07-11 17:01:13 +0000922/// getTypedefType - Return the unique reference to the type for the
923/// specified typename decl.
924QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
925 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
926
Chris Lattnerf52ab252008-04-06 22:59:24 +0000927 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000928 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 Types.push_back(Decl->TypeForDecl);
930 return QualType(Decl->TypeForDecl, 0);
931}
932
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000934/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000935QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000936 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
937
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000939 Types.push_back(Decl->TypeForDecl);
940 return QualType(Decl->TypeForDecl, 0);
941}
942
Chris Lattner88cb27a2008-04-07 04:56:42 +0000943/// CmpProtocolNames - Comparison predicate for sorting protocols
944/// alphabetically.
945static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
946 const ObjCProtocolDecl *RHS) {
947 return strcmp(LHS->getName(), RHS->getName()) < 0;
948}
949
950static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
951 unsigned &NumProtocols) {
952 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
953
954 // Sort protocols, keyed by name.
955 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
956
957 // Remove duplicates.
958 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
959 NumProtocols = ProtocolsEnd-Protocols;
960}
961
962
Chris Lattner065f0d72008-04-07 04:44:08 +0000963/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
964/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000965QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
966 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000967 // Sort the protocol list alphabetically to canonicalize it.
968 SortAndUniqueProtocols(Protocols, NumProtocols);
969
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000970 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000971 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000972
973 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 if (ObjCQualifiedInterfaceType *QT =
975 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000976 return QualType(QT, 0);
977
978 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979 ObjCQualifiedInterfaceType *QType =
980 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000981 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000983 return QualType(QType, 0);
984}
985
Chris Lattner88cb27a2008-04-07 04:56:42 +0000986/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
987/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000988QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000989 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000990 // Sort the protocol list alphabetically to canonicalize it.
991 SortAndUniqueProtocols(Protocols, NumProtocols);
992
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000993 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000995
996 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000998 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000999 return QualType(QT, 0);
1000
1001 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001002 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001003 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001005 return QualType(QType, 0);
1006}
1007
Steve Naroff9752f252007-08-01 18:02:17 +00001008/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1009/// TypeOfExpr AST's (since expression's are never shared). For example,
1010/// multiple declarations that refer to "typeof(x)" all contain different
1011/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1012/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001013QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001014 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001015 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1016 Types.push_back(toe);
1017 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001018}
1019
Steve Naroff9752f252007-08-01 18:02:17 +00001020/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1021/// TypeOfType AST's. The only motivation to unique these nodes would be
1022/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1023/// an issue. This doesn't effect the type checker, since it operates
1024/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001025QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001026 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001027 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1028 Types.push_back(tot);
1029 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001030}
1031
Reid Spencer5f016e22007-07-11 17:01:13 +00001032/// getTagDeclType - Return the unique reference to the type for the
1033/// specified TagDecl (struct/union/class/enum) decl.
1034QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001035 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001036 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
1039/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1040/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1041/// needs to agree with the definition in <stddef.h>.
1042QualType ASTContext::getSizeType() const {
1043 // On Darwin, size_t is defined as a "long unsigned int".
1044 // FIXME: should derive from "Target".
1045 return UnsignedLongTy;
1046}
1047
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001048/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001049/// width of characters in wide strings, The value is target dependent and
1050/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001051QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001052 if (LangOpts.CPlusPlus)
1053 return WCharTy;
1054
Eli Friedmanfd888a52008-02-12 08:29:21 +00001055 // On Darwin, wchar_t is defined as a "int".
1056 // FIXME: should derive from "Target".
1057 return IntTy;
1058}
1059
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001060/// getSignedWCharType - Return the type of "signed wchar_t".
1061/// Used when in C++, as a GCC extension.
1062QualType ASTContext::getSignedWCharType() const {
1063 // FIXME: derive from "Target" ?
1064 return WCharTy;
1065}
1066
1067/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1068/// Used when in C++, as a GCC extension.
1069QualType ASTContext::getUnsignedWCharType() const {
1070 // FIXME: derive from "Target" ?
1071 return UnsignedIntTy;
1072}
1073
Chris Lattner8b9023b2007-07-13 03:05:23 +00001074/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1075/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1076QualType ASTContext::getPointerDiffType() const {
1077 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1078 // FIXME: should derive from "Target".
1079 return IntTy;
1080}
1081
Chris Lattnere6327742008-04-02 05:18:44 +00001082//===----------------------------------------------------------------------===//
1083// Type Operators
1084//===----------------------------------------------------------------------===//
1085
Chris Lattner77c96472008-04-06 22:41:35 +00001086/// getCanonicalType - Return the canonical (structural) type corresponding to
1087/// the specified potentially non-canonical type. The non-canonical version
1088/// of a type may have many "decorated" versions of types. Decorators can
1089/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1090/// to be free of any of these, allowing two canonical types to be compared
1091/// for exact equality with a simple pointer comparison.
1092QualType ASTContext::getCanonicalType(QualType T) {
1093 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001094
1095 // If the result has type qualifiers, make sure to canonicalize them as well.
1096 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1097 if (TypeQuals == 0) return CanType;
1098
1099 // If the type qualifiers are on an array type, get the canonical type of the
1100 // array with the qualifiers applied to the element type.
1101 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1102 if (!AT)
1103 return CanType.getQualifiedType(TypeQuals);
1104
1105 // Get the canonical version of the element with the extra qualifiers on it.
1106 // This can recursively sink qualifiers through multiple levels of arrays.
1107 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1108 NewEltTy = getCanonicalType(NewEltTy);
1109
1110 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1111 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1112 CAT->getIndexTypeQualifier());
1113 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1114 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1115 IAT->getIndexTypeQualifier());
1116
1117 // FIXME: What is the ownership of size expressions in VLAs?
1118 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1119 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1120 VAT->getSizeModifier(),
1121 VAT->getIndexTypeQualifier());
1122}
1123
1124
1125const ArrayType *ASTContext::getAsArrayType(QualType T) {
1126 // Handle the non-qualified case efficiently.
1127 if (T.getCVRQualifiers() == 0) {
1128 // Handle the common positive case fast.
1129 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1130 return AT;
1131 }
1132
1133 // Handle the common negative case fast, ignoring CVR qualifiers.
1134 QualType CType = T->getCanonicalTypeInternal();
1135
1136 // Make sure to look through type qualifiers (like ASQuals) for the negative
1137 // test.
1138 if (!isa<ArrayType>(CType) &&
1139 !isa<ArrayType>(CType.getUnqualifiedType()))
1140 return 0;
1141
1142 // Apply any CVR qualifiers from the array type to the element type. This
1143 // implements C99 6.7.3p8: "If the specification of an array type includes
1144 // any type qualifiers, the element type is so qualified, not the array type."
1145
1146 // If we get here, we either have type qualifiers on the type, or we have
1147 // sugar such as a typedef in the way. If we have type qualifiers on the type
1148 // we must propagate them down into the elemeng type.
1149 unsigned CVRQuals = T.getCVRQualifiers();
1150 unsigned AddrSpace = 0;
1151 Type *Ty = T.getTypePtr();
1152
1153 // Rip through ASQualType's and typedefs to get to a concrete type.
1154 while (1) {
1155 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1156 AddrSpace = ASQT->getAddressSpace();
1157 Ty = ASQT->getBaseType();
1158 } else {
1159 T = Ty->getDesugaredType();
1160 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1161 break;
1162 CVRQuals |= T.getCVRQualifiers();
1163 Ty = T.getTypePtr();
1164 }
1165 }
1166
1167 // If we have a simple case, just return now.
1168 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1169 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1170 return ATy;
1171
1172 // Otherwise, we have an array and we have qualifiers on it. Push the
1173 // qualifiers into the array element type and return a new array type.
1174 // Get the canonical version of the element with the extra qualifiers on it.
1175 // This can recursively sink qualifiers through multiple levels of arrays.
1176 QualType NewEltTy = ATy->getElementType();
1177 if (AddrSpace)
1178 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1179 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1180
1181 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1182 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1183 CAT->getSizeModifier(),
1184 CAT->getIndexTypeQualifier()));
1185 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1186 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1187 IAT->getSizeModifier(),
1188 IAT->getIndexTypeQualifier()));
1189
1190 // FIXME: What is the ownership of size expressions in VLAs?
1191 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1192 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1193 VAT->getSizeModifier(),
1194 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001195}
1196
1197
Chris Lattnere6327742008-04-02 05:18:44 +00001198/// getArrayDecayedType - Return the properly qualified result of decaying the
1199/// specified array type to a pointer. This operation is non-trivial when
1200/// handling typedefs etc. The canonical type of "T" must be an array type,
1201/// this returns a pointer to a properly qualified element of the array.
1202///
1203/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1204QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001205 // Get the element type with 'getAsArrayType' so that we don't lose any
1206 // typedefs in the element type of the array. This also handles propagation
1207 // of type qualifiers from the array type into the element type if present
1208 // (C99 6.7.3p8).
1209 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1210 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001211
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001212 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001213
1214 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001215 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001216}
1217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218/// getFloatingRank - Return a relative rank for floating point types.
1219/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001220static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001221 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001223
Christopher Lambebb97e92008-02-04 02:31:56 +00001224 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001225 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 case BuiltinType::Float: return FloatRank;
1227 case BuiltinType::Double: return DoubleRank;
1228 case BuiltinType::LongDouble: return LongDoubleRank;
1229 }
1230}
1231
Steve Naroff716c7302007-08-27 01:41:48 +00001232/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1233/// point or a complex type (based on typeDomain/typeSize).
1234/// 'typeDomain' is a real floating point or complex type.
1235/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001236QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1237 QualType Domain) const {
1238 FloatingRank EltRank = getFloatingRank(Size);
1239 if (Domain->isComplexType()) {
1240 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001241 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001242 case FloatRank: return FloatComplexTy;
1243 case DoubleRank: return DoubleComplexTy;
1244 case LongDoubleRank: return LongDoubleComplexTy;
1245 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 }
Chris Lattner1361b112008-04-06 23:58:54 +00001247
1248 assert(Domain->isRealFloatingType() && "Unknown domain!");
1249 switch (EltRank) {
1250 default: assert(0 && "getFloatingRank(): illegal value for rank");
1251 case FloatRank: return FloatTy;
1252 case DoubleRank: return DoubleTy;
1253 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001254 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001255}
1256
Chris Lattner7cfeb082008-04-06 23:55:33 +00001257/// getFloatingTypeOrder - Compare the rank of the two specified floating
1258/// point types, ignoring the domain of the type (i.e. 'double' ==
1259/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1260/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001261int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1262 FloatingRank LHSR = getFloatingRank(LHS);
1263 FloatingRank RHSR = getFloatingRank(RHS);
1264
1265 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001266 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001267 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001268 return 1;
1269 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001270}
1271
Chris Lattnerf52ab252008-04-06 22:59:24 +00001272/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1273/// routine will assert if passed a built-in type that isn't an integer or enum,
1274/// or if it is not canonicalized.
1275static unsigned getIntegerRank(Type *T) {
1276 assert(T->isCanonical() && "T should be canonicalized");
1277 if (isa<EnumType>(T))
1278 return 4;
1279
1280 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001281 default: assert(0 && "getIntegerRank(): not a built-in integer");
1282 case BuiltinType::Bool:
1283 return 1;
1284 case BuiltinType::Char_S:
1285 case BuiltinType::Char_U:
1286 case BuiltinType::SChar:
1287 case BuiltinType::UChar:
1288 return 2;
1289 case BuiltinType::Short:
1290 case BuiltinType::UShort:
1291 return 3;
1292 case BuiltinType::Int:
1293 case BuiltinType::UInt:
1294 return 4;
1295 case BuiltinType::Long:
1296 case BuiltinType::ULong:
1297 return 5;
1298 case BuiltinType::LongLong:
1299 case BuiltinType::ULongLong:
1300 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001301 }
1302}
1303
Chris Lattner7cfeb082008-04-06 23:55:33 +00001304/// getIntegerTypeOrder - Returns the highest ranked integer type:
1305/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1306/// LHS < RHS, return -1.
1307int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001308 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1309 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001310 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001311
Chris Lattnerf52ab252008-04-06 22:59:24 +00001312 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1313 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001314
Chris Lattner7cfeb082008-04-06 23:55:33 +00001315 unsigned LHSRank = getIntegerRank(LHSC);
1316 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001317
Chris Lattner7cfeb082008-04-06 23:55:33 +00001318 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1319 if (LHSRank == RHSRank) return 0;
1320 return LHSRank > RHSRank ? 1 : -1;
1321 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001322
Chris Lattner7cfeb082008-04-06 23:55:33 +00001323 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1324 if (LHSUnsigned) {
1325 // If the unsigned [LHS] type is larger, return it.
1326 if (LHSRank >= RHSRank)
1327 return 1;
1328
1329 // If the signed type can represent all values of the unsigned type, it
1330 // wins. Because we are dealing with 2's complement and types that are
1331 // powers of two larger than each other, this is always safe.
1332 return -1;
1333 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001334
Chris Lattner7cfeb082008-04-06 23:55:33 +00001335 // If the unsigned [RHS] type is larger, return it.
1336 if (RHSRank >= LHSRank)
1337 return -1;
1338
1339 // If the signed type can represent all values of the unsigned type, it
1340 // wins. Because we are dealing with 2's complement and types that are
1341 // powers of two larger than each other, this is always safe.
1342 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001343}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001344
1345// getCFConstantStringType - Return the type used for constant CFStrings.
1346QualType ASTContext::getCFConstantStringType() {
1347 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001348 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001349 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattnerc63e6602008-03-15 21:32:50 +00001350 &Idents.get("NSConstantString"), 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +00001351 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001352
1353 // const int *isa;
1354 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001355 // int flags;
1356 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001357 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001358 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001359 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001360 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001361 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001362 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001363
Anders Carlssonf06273f2007-11-19 00:25:30 +00001364 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001365 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001366 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001367
1368 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1369 }
1370
1371 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001372}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001373
Anders Carlssone8c49532007-10-29 06:33:42 +00001374// This returns true if a type has been typedefed to BOOL:
1375// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001376static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001377 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001378 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001379
1380 return false;
1381}
1382
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001384/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001385int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001386 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001387
1388 // Make all integer and enum types at least as large as an int
1389 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001390 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001391 // Treat arrays as pointers, since that's how they're passed in.
1392 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001393 sz = getTypeSize(VoidPtrTy);
1394 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001395}
1396
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001398/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001399void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001400 std::string& S)
1401{
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001402 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001403 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001404 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001405 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001406 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001407 // Compute size of all parameters.
1408 // Start with computing size of a pointer in number of bytes.
1409 // FIXME: There might(should) be a better way of doing this computation!
1410 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001411 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001412 // The first two arguments (self and _cmd) are pointers; account for
1413 // their size.
1414 int ParmOffset = 2 * PtrSize;
1415 int NumOfParams = Decl->getNumParams();
1416 for (int i = 0; i < NumOfParams; i++) {
1417 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 int sz = getObjCEncodingTypeSize (PType);
1419 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001420 ParmOffset += sz;
1421 }
1422 S += llvm::utostr(ParmOffset);
1423 S += "@0:";
1424 S += llvm::utostr(PtrSize);
1425
1426 // Argument types.
1427 ParmOffset = 2 * PtrSize;
1428 for (int i = 0; i < NumOfParams; i++) {
1429 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001430 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001431 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001432 getObjCEncodingForTypeQualifier(
1433 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001434 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001435 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001436 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001437 }
1438}
1439
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001440/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1441/// method declaration. If non-NULL, Container must be either an
1442/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1443/// NULL when getting encodings for protocol properties.
1444void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1445 const Decl *Container,
1446 std::string& S)
1447{
1448 // Collect information from the property implementation decl(s).
1449 bool Dynamic = false;
1450 ObjCPropertyImplDecl *SynthesizePID = 0;
1451
1452 // FIXME: Duplicated code due to poor abstraction.
1453 if (Container) {
1454 if (const ObjCCategoryImplDecl *CID =
1455 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1456 for (ObjCCategoryImplDecl::propimpl_iterator
1457 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1458 ObjCPropertyImplDecl *PID = *i;
1459 if (PID->getPropertyDecl() == PD) {
1460 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1461 Dynamic = true;
1462 } else {
1463 SynthesizePID = PID;
1464 }
1465 }
1466 }
1467 } else {
1468 const ObjCImplementationDecl *OID = cast<ObjCImplementationDecl>(Container);
1469 for (ObjCCategoryImplDecl::propimpl_iterator
1470 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1471 ObjCPropertyImplDecl *PID = *i;
1472 if (PID->getPropertyDecl() == PD) {
1473 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1474 Dynamic = true;
1475 } else {
1476 SynthesizePID = PID;
1477 }
1478 }
1479 }
1480 }
1481 }
1482
1483 // FIXME: This is not very efficient.
1484 S = "T";
1485
1486 // Encode result type.
1487 // FIXME: GCC uses a generating_property_type_encoding mode during
1488 // this part. Investigate.
1489 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1490
1491 if (PD->isReadOnly()) {
1492 S += ",R";
1493 } else {
1494 switch (PD->getSetterKind()) {
1495 case ObjCPropertyDecl::Assign: break;
1496 case ObjCPropertyDecl::Copy: S += ",C"; break;
1497 case ObjCPropertyDecl::Retain: S += ",&"; break;
1498 }
1499 }
1500
1501 // It really isn't clear at all what this means, since properties
1502 // are "dynamic by default".
1503 if (Dynamic)
1504 S += ",D";
1505
1506 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1507 S += ",G";
1508 S += PD->getGetterName().getName();
1509 }
1510
1511 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1512 S += ",S";
1513 S += PD->getSetterName().getName();
1514 }
1515
1516 if (SynthesizePID) {
1517 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1518 S += ",V";
1519 S += OID->getName();
1520 }
1521
1522 // FIXME: OBJCGC: weak & strong
1523}
1524
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001525void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001526 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001527 // FIXME: This currently doesn't encode:
1528 // @ An object (whether statically typed or typed id)
1529 // # A class object (Class)
1530 // : A method selector (SEL)
1531 // {name=type...} A structure
1532 // (name=type...) A union
1533 // bnum A bit field of num bits
1534
1535 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001536 char encoding;
1537 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001538 default: assert(0 && "Unhandled builtin type kind");
1539 case BuiltinType::Void: encoding = 'v'; break;
1540 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001541 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001542 case BuiltinType::UChar: encoding = 'C'; break;
1543 case BuiltinType::UShort: encoding = 'S'; break;
1544 case BuiltinType::UInt: encoding = 'I'; break;
1545 case BuiltinType::ULong: encoding = 'L'; break;
1546 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001547 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001548 case BuiltinType::SChar: encoding = 'c'; break;
1549 case BuiltinType::Short: encoding = 's'; break;
1550 case BuiltinType::Int: encoding = 'i'; break;
1551 case BuiltinType::Long: encoding = 'l'; break;
1552 case BuiltinType::LongLong: encoding = 'q'; break;
1553 case BuiltinType::Float: encoding = 'f'; break;
1554 case BuiltinType::Double: encoding = 'd'; break;
1555 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001556 }
1557
1558 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001559 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001560 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001561 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001562 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001563
1564 }
1565 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001566 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001567 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001568 S += '@';
1569 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001570 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001571 S += '#';
1572 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001574 S += ':';
1575 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001576 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001577
1578 if (PointeeTy->isCharType()) {
1579 // char pointer types should be encoded as '*' unless it is a
1580 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001581 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001582 S += '*';
1583 return;
1584 }
1585 }
1586
1587 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001588 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001589 } else if (const ArrayType *AT =
1590 // Ignore type qualifiers etc.
1591 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001592 S += '[';
1593
1594 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1595 S += llvm::utostr(CAT->getSize().getZExtValue());
1596 else
1597 assert(0 && "Unhandled array type!");
1598
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001599 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001600 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001601 } else if (T->getAsFunctionType()) {
1602 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001603 } else if (const RecordType *RTy = T->getAsRecordType()) {
1604 RecordDecl *RDecl= RTy->getDecl();
Steve Narofffaf37e72008-08-14 15:00:38 +00001605 // This mimics the behavior in gcc's encode_aggregate_within().
1606 // The idea is to only inline structure definitions for top level pointers
1607 // to structures and embedded structures.
1608 bool inlining = (S.size() == 1 && S[0] == '^' ||
1609 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001610 S += '{';
1611 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001612 bool found = false;
1613 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1614 if (ERType[i] == RTy) {
1615 found = true;
1616 break;
1617 }
Steve Narofffaf37e72008-08-14 15:00:38 +00001618 if (!found && inlining) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001619 ERType.push_back(RTy);
1620 S += '=';
1621 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1622 FieldDecl *field = RDecl->getMember(i);
1623 getObjCEncodingForType(field->getType(), S, ERType);
1624 }
1625 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1626 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001627 }
1628 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001629 } else if (T->isEnumeralType()) {
1630 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001631 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001632 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001633}
1634
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001635void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001636 std::string& S) const {
1637 if (QT & Decl::OBJC_TQ_In)
1638 S += 'n';
1639 if (QT & Decl::OBJC_TQ_Inout)
1640 S += 'N';
1641 if (QT & Decl::OBJC_TQ_Out)
1642 S += 'o';
1643 if (QT & Decl::OBJC_TQ_Bycopy)
1644 S += 'O';
1645 if (QT & Decl::OBJC_TQ_Byref)
1646 S += 'R';
1647 if (QT & Decl::OBJC_TQ_Oneway)
1648 S += 'V';
1649}
1650
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001651void ASTContext::setBuiltinVaListType(QualType T)
1652{
1653 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1654
1655 BuiltinVaListType = T;
1656}
1657
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001658void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001659{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001660 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001661
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001662 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001663
1664 // typedef struct objc_object *id;
1665 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1666 assert(ptr && "'id' incorrectly typed");
1667 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1668 assert(rec && "'id' incorrectly typed");
1669 IdStructType = rec;
1670}
1671
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001672void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001673{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001674 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001675
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001676 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001677
1678 // typedef struct objc_selector *SEL;
1679 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1680 assert(ptr && "'SEL' incorrectly typed");
1681 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1682 assert(rec && "'SEL' incorrectly typed");
1683 SelStructType = rec;
1684}
1685
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001686void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001687{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001688 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1689 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001690}
1691
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001692void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001693{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001694 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001695
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001696 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001697
1698 // typedef struct objc_class *Class;
1699 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1700 assert(ptr && "'Class' incorrectly typed");
1701 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1702 assert(rec && "'Class' incorrectly typed");
1703 ClassStructType = rec;
1704}
1705
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001706void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1707 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001708 "'NSConstantString' type already set!");
1709
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001710 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001711}
1712
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001713
1714//===----------------------------------------------------------------------===//
1715// Type Predicates.
1716//===----------------------------------------------------------------------===//
1717
1718/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1719/// to an object type. This includes "id" and "Class" (two 'special' pointers
1720/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1721/// ID type).
1722bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1723 if (Ty->isObjCQualifiedIdType())
1724 return true;
1725
1726 if (!Ty->isPointerType())
1727 return false;
1728
1729 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1730 // pointer types. This looks for the typedef specifically, not for the
1731 // underlying type.
1732 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1733 return true;
1734
1735 // If this a pointer to an interface (e.g. NSString*), it is ok.
1736 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1737}
1738
Chris Lattner6ac46a42008-04-07 06:51:04 +00001739//===----------------------------------------------------------------------===//
1740// Type Compatibility Testing
1741//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001742
Chris Lattner6ac46a42008-04-07 06:51:04 +00001743/// areCompatVectorTypes - Return true if the two specified vector types are
1744/// compatible.
1745static bool areCompatVectorTypes(const VectorType *LHS,
1746 const VectorType *RHS) {
1747 assert(LHS->isCanonical() && RHS->isCanonical());
1748 return LHS->getElementType() == RHS->getElementType() &&
1749 LHS->getNumElements() == RHS->getNumElements();
1750}
1751
Eli Friedman3d815e72008-08-22 00:56:42 +00001752/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001753/// compatible for assignment from RHS to LHS. This handles validation of any
1754/// protocol qualifiers on the LHS or RHS.
1755///
Eli Friedman3d815e72008-08-22 00:56:42 +00001756bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1757 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001758 // Verify that the base decls are compatible: the RHS must be a subclass of
1759 // the LHS.
1760 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1761 return false;
1762
1763 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1764 // protocol qualified at all, then we are good.
1765 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1766 return true;
1767
1768 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1769 // isn't a superset.
1770 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1771 return true; // FIXME: should return false!
1772
1773 // Finally, we must have two protocol-qualified interfaces.
1774 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1775 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1776 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1777 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1778 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1779 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1780
1781 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1782 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1783 // LHS in a single parallel scan until we run out of elements in LHS.
1784 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1785 ObjCProtocolDecl *LHSProto = *LHSPI;
1786
1787 while (RHSPI != RHSPE) {
1788 ObjCProtocolDecl *RHSProto = *RHSPI++;
1789 // If the RHS has a protocol that the LHS doesn't, ignore it.
1790 if (RHSProto != LHSProto)
1791 continue;
1792
1793 // Otherwise, the RHS does have this element.
1794 ++LHSPI;
1795 if (LHSPI == LHSPE)
1796 return true; // All protocols in LHS exist in RHS.
1797
1798 LHSProto = *LHSPI;
1799 }
1800
1801 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1802 return false;
1803}
1804
Steve Naroffec0550f2007-10-15 20:41:53 +00001805/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1806/// both shall have the identically qualified version of a compatible type.
1807/// C99 6.2.7p1: Two types have compatible types if their types are the
1808/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001809bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1810 return !mergeTypes(LHS, RHS).isNull();
1811}
1812
1813QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1814 const FunctionType *lbase = lhs->getAsFunctionType();
1815 const FunctionType *rbase = rhs->getAsFunctionType();
1816 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1817 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1818 bool allLTypes = true;
1819 bool allRTypes = true;
1820
1821 // Check return type
1822 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1823 if (retType.isNull()) return QualType();
1824 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) allLTypes = false;
1825 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) allRTypes = false;
1826
1827 if (lproto && rproto) { // two C99 style function prototypes
1828 unsigned lproto_nargs = lproto->getNumArgs();
1829 unsigned rproto_nargs = rproto->getNumArgs();
1830
1831 // Compatible functions must have the same number of arguments
1832 if (lproto_nargs != rproto_nargs)
1833 return QualType();
1834
1835 // Variadic and non-variadic functions aren't compatible
1836 if (lproto->isVariadic() != rproto->isVariadic())
1837 return QualType();
1838
1839 // Check argument compatibility
1840 llvm::SmallVector<QualType, 10> types;
1841 for (unsigned i = 0; i < lproto_nargs; i++) {
1842 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1843 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1844 QualType argtype = mergeTypes(largtype, rargtype);
1845 if (argtype.isNull()) return QualType();
1846 types.push_back(argtype);
1847 if (getCanonicalType(argtype) != getCanonicalType(largtype)) allLTypes = false;
1848 if (getCanonicalType(argtype) != getCanonicalType(rargtype)) allRTypes = false;
1849 }
1850 if (allLTypes) return lhs;
1851 if (allRTypes) return rhs;
1852 return getFunctionType(retType, types.begin(), types.size(),
1853 lproto->isVariadic());
1854 }
1855
1856 if (lproto) allRTypes = false;
1857 if (rproto) allLTypes = false;
1858
1859 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1860 if (proto) {
1861 if (proto->isVariadic()) return QualType();
1862 // Check that the types are compatible with the types that
1863 // would result from default argument promotions (C99 6.7.5.3p15).
1864 // The only types actually affected are promotable integer
1865 // types and floats, which would be passed as a different
1866 // type depending on whether the prototype is visible.
1867 unsigned proto_nargs = proto->getNumArgs();
1868 for (unsigned i = 0; i < proto_nargs; ++i) {
1869 QualType argTy = proto->getArgType(i);
1870 if (argTy->isPromotableIntegerType() ||
1871 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1872 return QualType();
1873 }
1874
1875 if (allLTypes) return lhs;
1876 if (allRTypes) return rhs;
1877 return getFunctionType(retType, proto->arg_type_begin(),
1878 proto->getNumArgs(), lproto->isVariadic());
1879 }
1880
1881 if (allLTypes) return lhs;
1882 if (allRTypes) return rhs;
1883 return getFunctionTypeNoProto(retType);
1884}
1885
1886QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00001887 // C++ [expr]: If an expression initially has the type "reference to T", the
1888 // type is adjusted to "T" prior to any further analysis, the expression
1889 // designates the object or function denoted by the reference, and the
1890 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00001891 // FIXME: C++ shouldn't be going through here! The rules are different
1892 // enough that they should be handled separately.
1893 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001894 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00001895 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001896 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001897
Eli Friedman3d815e72008-08-22 00:56:42 +00001898 QualType LHSCan = getCanonicalType(LHS),
1899 RHSCan = getCanonicalType(RHS);
1900
1901 // If two types are identical, they are compatible.
1902 if (LHSCan == RHSCan)
1903 return LHS;
1904
1905 // If the qualifiers are different, the types aren't compatible
1906 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
1907 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
1908 return QualType();
1909
1910 Type::TypeClass LHSClass = LHSCan->getTypeClass();
1911 Type::TypeClass RHSClass = RHSCan->getTypeClass();
1912
Chris Lattner1adb8832008-01-14 05:45:46 +00001913 // We want to consider the two function types to be the same for these
1914 // comparisons, just force one to the other.
1915 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1916 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001917
1918 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001919 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1920 LHSClass = Type::ConstantArray;
1921 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1922 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001923
Nate Begeman213541a2008-04-18 23:10:10 +00001924 // Canonicalize ExtVector -> Vector.
1925 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1926 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001927
Chris Lattnerb0489812008-04-07 06:38:24 +00001928 // Consider qualified interfaces and interfaces the same.
1929 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1930 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00001931
Chris Lattnera36a61f2008-04-07 05:43:21 +00001932 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001933 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00001934 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00001935 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00001936 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00001937 if (isObjCIdType(PT->getPointeeType()))
1938 return LHS;
Steve Naroff97341622008-06-04 15:07:33 +00001939 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001940 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00001941 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00001942 if (isObjCIdType(PT->getPointeeType()))
1943 return RHS;
1944 }
1945
Chris Lattner1adb8832008-01-14 05:45:46 +00001946 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1947 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00001948 if (const EnumType* ETy = LHS->getAsEnumType()) {
1949 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
1950 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001951 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001952 if (const EnumType* ETy = RHS->getAsEnumType()) {
1953 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
1954 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00001955 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001956
Eli Friedman3d815e72008-08-22 00:56:42 +00001957 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001958 }
Eli Friedman3d815e72008-08-22 00:56:42 +00001959
Steve Naroff4a746782008-01-09 22:43:08 +00001960 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001961 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00001962 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00001963 {
1964 // Merge two pointer types, while trying to preserve typedef info
1965 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
1966 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
1967 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
1968 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00001969 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) return LHS;
1970 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00001971 return getPointerType(ResultType);
1972 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001973 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00001974 {
1975 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
1976 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
1977 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
1978 return QualType();
1979
1980 QualType LHSElem = getAsArrayType(LHS)->getElementType();
1981 QualType RHSElem = getAsArrayType(RHS)->getElementType();
1982 QualType ResultType = mergeTypes(LHSElem, RHSElem);
1983 if (ResultType.isNull()) return QualType();
Eli Friedman3bc0f452008-08-22 01:48:21 +00001984 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
1985 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
1986 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
1987 ArrayType::ArraySizeModifier(), 0);
1988 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
1989 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00001990 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
1991 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Eli Friedman3bc0f452008-08-22 01:48:21 +00001992 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
1993 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00001994 if (LVAT) {
1995 // FIXME: This isn't correct! But tricky to implement because
1996 // the array's size has to be the size of LHS, but the type
1997 // has to be different.
1998 return LHS;
1999 }
2000 if (RVAT) {
2001 // FIXME: This isn't correct! But tricky to implement because
2002 // the array's size has to be the size of RHS, but the type
2003 // has to be different.
2004 return RHS;
2005 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002006 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2007 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002008 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(), 0);
2009 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002010 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002011 return mergeFunctionTypes(LHS, RHS);
2012 case Type::Tagged:
2013 {
2014 // FIXME: Why are these compatible?
2015 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2016 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2017 return QualType();
2018 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002019 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002020 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002021 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002022 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002023 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2024 return LHS;
Chris Lattner1adb8832008-01-14 05:45:46 +00002025 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002026 {
2027 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2028 // for checking assignment/comparison safety
2029 return QualType();
2030 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002031 default:
2032 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002033 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002034 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002035}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002036
Chris Lattner5426bf62008-04-07 07:01:58 +00002037//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002038// Integer Predicates
2039//===----------------------------------------------------------------------===//
2040unsigned ASTContext::getIntWidth(QualType T) {
2041 if (T == BoolTy)
2042 return 1;
2043 // At the moment, only bool has padding bits
2044 return (unsigned)getTypeSize(T);
2045}
2046
2047QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2048 assert(T->isSignedIntegerType() && "Unexpected type");
2049 if (const EnumType* ETy = T->getAsEnumType())
2050 T = ETy->getDecl()->getIntegerType();
2051 const BuiltinType* BTy = T->getAsBuiltinType();
2052 assert (BTy && "Unexpected signed integer type");
2053 switch (BTy->getKind()) {
2054 case BuiltinType::Char_S:
2055 case BuiltinType::SChar:
2056 return UnsignedCharTy;
2057 case BuiltinType::Short:
2058 return UnsignedShortTy;
2059 case BuiltinType::Int:
2060 return UnsignedIntTy;
2061 case BuiltinType::Long:
2062 return UnsignedLongTy;
2063 case BuiltinType::LongLong:
2064 return UnsignedLongLongTy;
2065 default:
2066 assert(0 && "Unexpected signed integer type");
2067 return QualType();
2068 }
2069}
2070
2071
2072//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002073// Serialization Support
2074//===----------------------------------------------------------------------===//
2075
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002076/// Emit - Serialize an ASTContext object to Bitcode.
2077void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002078 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002079 S.EmitRef(SourceMgr);
2080 S.EmitRef(Target);
2081 S.EmitRef(Idents);
2082 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002083
Ted Kremenekfee04522007-10-31 22:44:07 +00002084 // Emit the size of the type vector so that we can reserve that size
2085 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002086 S.EmitInt(Types.size());
2087
Ted Kremenek03ed4402007-11-13 22:02:55 +00002088 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2089 I!=E;++I)
2090 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002091
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002092 S.EmitOwnedPtr(TUDecl);
2093
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002094 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002095}
2096
Ted Kremenek0f84c002007-11-13 00:25:37 +00002097ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002098
2099 // Read the language options.
2100 LangOptions LOpts;
2101 LOpts.Read(D);
2102
Ted Kremenekfee04522007-10-31 22:44:07 +00002103 SourceManager &SM = D.ReadRef<SourceManager>();
2104 TargetInfo &t = D.ReadRef<TargetInfo>();
2105 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2106 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002107
Ted Kremenekfee04522007-10-31 22:44:07 +00002108 unsigned size_reserve = D.ReadInt();
2109
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002110 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002111
Ted Kremenek03ed4402007-11-13 22:02:55 +00002112 for (unsigned i = 0; i < size_reserve; ++i)
2113 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002114
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002115 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2116
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002117 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002118
2119 return A;
2120}