blob: fc24f5fe31097b633f20086a367cb10bd5ad0a02 [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
Chris Lattner61710852008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Daniel Dunbare91593e2008-08-11 04:54:23 +000036 Idents(idents), Selectors(sels)
37{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 Types.pop_back();
49 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000050
51 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000052}
53
54void ASTContext::PrintStats() const {
55 fprintf(stderr, "*** AST Context Stats:\n");
56 fprintf(stderr, " %d types total.\n", (int)Types.size());
57 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000058 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
60
61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000062 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
63 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000064 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
67 Type *T = Types[i];
68 if (isa<BuiltinType>(T))
69 ++NumBuiltin;
70 else if (isa<PointerType>(T))
71 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000072 else if (isa<BlockPointerType>(T))
73 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 else if (isa<ReferenceType>(T))
75 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000076 else if (isa<ComplexType>(T))
77 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 else if (isa<ArrayType>(T))
79 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000080 else if (isa<VectorType>(T))
81 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 else if (isa<FunctionTypeNoProto>(T))
83 ++NumFunctionNP;
84 else if (isa<FunctionTypeProto>(T))
85 ++NumFunctionP;
86 else if (isa<TypedefType>(T))
87 ++NumTypeName;
88 else if (TagType *TT = dyn_cast<TagType>(T)) {
89 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000090 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000092 case TagDecl::TK_struct: ++NumTagStruct; break;
93 case TagDecl::TK_union: ++NumTagUnion; break;
94 case TagDecl::TK_class: ++NumTagClass; break;
95 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 } else if (isa<ObjCInterfaceType>(T))
98 ++NumObjCInterfaces;
99 else if (isa<ObjCQualifiedInterfaceType>(T))
100 ++NumObjCQualifiedInterfaces;
101 else if (isa<ObjCQualifiedIdType>(T))
102 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000103 else if (isa<TypeOfType>(T))
104 ++NumTypeOfTypes;
105 else if (isa<TypeOfExpr>(T))
106 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000107 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000108 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 assert(0 && "Unknown type!");
110 }
111 }
112
113 fprintf(stderr, " %d builtin types\n", NumBuiltin);
114 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000115 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000117 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000119 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
121 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
122 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
123 fprintf(stderr, " %d tagged types\n", NumTagged);
124 fprintf(stderr, " %d struct types\n", NumTagStruct);
125 fprintf(stderr, " %d union types\n", NumTagUnion);
126 fprintf(stderr, " %d class types\n", NumTagClass);
127 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000128 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000129 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000130 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000131 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000132 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000133 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
134 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
137 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000138 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 NumFunctionP*sizeof(FunctionTypeProto)+
140 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000141 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
142 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000143}
144
145
146void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
147 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
148}
149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150void ASTContext::InitBuiltinTypes() {
151 assert(VoidTy.isNull() && "Context reinitialized?");
152
153 // C99 6.2.5p19.
154 InitBuiltinType(VoidTy, BuiltinType::Void);
155
156 // C99 6.2.5p2.
157 InitBuiltinType(BoolTy, BuiltinType::Bool);
158 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000159 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 InitBuiltinType(CharTy, BuiltinType::Char_S);
161 else
162 InitBuiltinType(CharTy, BuiltinType::Char_U);
163 // C99 6.2.5p4.
164 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
165 InitBuiltinType(ShortTy, BuiltinType::Short);
166 InitBuiltinType(IntTy, BuiltinType::Int);
167 InitBuiltinType(LongTy, BuiltinType::Long);
168 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
169
170 // C99 6.2.5p6.
171 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
172 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
173 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
174 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
175 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
176
177 // C99 6.2.5p10.
178 InitBuiltinType(FloatTy, BuiltinType::Float);
179 InitBuiltinType(DoubleTy, BuiltinType::Double);
180 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000181
182 // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // C99 6.2.5p11.
186 FloatComplexTy = getComplexType(FloatTy);
187 DoubleComplexTy = getComplexType(DoubleTy);
188 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000189
190 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000191 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000192 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000193 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000194 ClassStructType = 0;
195
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000197
198 // void * type
199 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200}
201
Chris Lattner464175b2007-07-18 17:52:12 +0000202//===----------------------------------------------------------------------===//
203// Type Sizing and Analysis
204//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000205
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000206/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
207/// scalar floating point type.
208const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
209 const BuiltinType *BT = T->getAsBuiltinType();
210 assert(BT && "Not a floating point type!");
211 switch (BT->getKind()) {
212 default: assert(0 && "Not a floating point type!");
213 case BuiltinType::Float: return Target.getFloatFormat();
214 case BuiltinType::Double: return Target.getDoubleFormat();
215 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
216 }
217}
218
219
Chris Lattnera7674d82007-07-13 22:13:22 +0000220/// getTypeSize - Return the size of the specified type, in bits. This method
221/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000222std::pair<uint64_t, unsigned>
Chris Lattner98be4942008-03-05 18:54:05 +0000223ASTContext::getTypeInfo(QualType T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000224 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000225 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000226 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000227 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000228 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000229 case Type::FunctionNoProto:
230 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000231 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000232 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000233 case Type::VariableArray:
234 assert(0 && "VLAs not implemented yet!");
235 case Type::ConstantArray: {
236 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
237
Chris Lattner98be4942008-03-05 18:54:05 +0000238 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000239 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000240 Align = EltInfo.second;
241 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000242 }
Nate Begeman213541a2008-04-18 23:10:10 +0000243 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000244 case Type::Vector: {
245 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000246 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000247 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000248 // FIXME: This isn't right for unusual vectors
249 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000250 break;
251 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000252
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000253 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000254 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000255 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000256 case BuiltinType::Void:
257 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000258 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000259 Width = Target.getBoolWidth();
260 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000261 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000262 case BuiltinType::Char_S:
263 case BuiltinType::Char_U:
264 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000265 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000266 Width = Target.getCharWidth();
267 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000268 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000269 case BuiltinType::WChar:
270 Width = Target.getWCharWidth();
271 Align = Target.getWCharAlign();
272 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000273 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000274 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000275 Width = Target.getShortWidth();
276 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000277 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000278 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000279 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000280 Width = Target.getIntWidth();
281 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000282 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000283 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000284 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000285 Width = Target.getLongWidth();
286 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000287 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000288 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000289 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000290 Width = Target.getLongLongWidth();
291 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000292 break;
293 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000294 Width = Target.getFloatWidth();
295 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000296 break;
297 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000298 Width = Target.getDoubleWidth();
299 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000300 break;
301 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000302 Width = Target.getLongDoubleWidth();
303 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000304 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000305 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000306 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000307 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000308 // FIXME: Pointers into different addr spaces could have different sizes and
309 // alignment requirements: getPointerInfo should take an AddrSpace.
310 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000312 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000313 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000314 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000315 case Type::BlockPointer: {
316 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
317 Width = Target.getPointerWidth(AS);
318 Align = Target.getPointerAlign(AS);
319 break;
320 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000321 case Type::Pointer: {
322 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000323 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000324 Align = Target.getPointerAlign(AS);
325 break;
326 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000327 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000328 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000329 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000330 // FIXME: This is wrong for struct layout: a reference in a struct has
331 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000332 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000333
334 case Type::Complex: {
335 // Complex types have the same alignment as their elements, but twice the
336 // size.
337 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000338 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000339 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000340 Align = EltInfo.second;
341 break;
342 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000343 case Type::ObjCInterface: {
344 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
345 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
346 Width = Layout.getSize();
347 Align = Layout.getAlignment();
348 break;
349 }
Chris Lattner71763312008-04-06 22:05:18 +0000350 case Type::Tagged: {
Chris Lattner8389eab2008-08-09 21:35:13 +0000351 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
352 Width = 1;
353 Align = 1;
354 break;
355 }
356
Chris Lattner71763312008-04-06 22:05:18 +0000357 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
358 return getTypeInfo(ET->getDecl()->getIntegerType());
359
360 RecordType *RT = cast<RecordType>(T);
361 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
362 Width = Layout.getSize();
363 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000364 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000365 }
Chris Lattner71763312008-04-06 22:05:18 +0000366 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000367
Chris Lattner464175b2007-07-18 17:52:12 +0000368 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000369 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000370}
371
Devang Patel8b277042008-06-04 21:22:16 +0000372/// LayoutField - Field layout.
373void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
374 bool IsUnion, bool StructIsPacked,
375 ASTContext &Context) {
376 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
377 uint64_t FieldOffset = IsUnion ? 0 : Size;
378 uint64_t FieldSize;
379 unsigned FieldAlign;
380
381 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
382 // TODO: Need to check this algorithm on other targets!
383 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000384 FieldSize =
385 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000386
387 std::pair<uint64_t, unsigned> FieldInfo =
388 Context.getTypeInfo(FD->getType());
389 uint64_t TypeSize = FieldInfo.first;
390
391 FieldAlign = FieldInfo.second;
392 if (FieldIsPacked)
393 FieldAlign = 1;
394 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
395 FieldAlign = std::max(FieldAlign, AA->getAlignment());
396
397 // Check if we need to add padding to give the field the correct
398 // alignment.
399 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
400 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
401
402 // Padding members don't affect overall alignment
403 if (!FD->getIdentifier())
404 FieldAlign = 1;
405 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000406 if (FD->getType()->isIncompleteArrayType()) {
407 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000408 // query getTypeInfo about these, so we figure it out here.
409 // Flexible array members don't have any size, but they
410 // have to be aligned appropriately for their element type.
411 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000412 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000413 FieldAlign = Context.getTypeAlign(ATy->getElementType());
414 } else {
415 std::pair<uint64_t, unsigned> FieldInfo =
416 Context.getTypeInfo(FD->getType());
417 FieldSize = FieldInfo.first;
418 FieldAlign = FieldInfo.second;
419 }
420
421 if (FieldIsPacked)
422 FieldAlign = 8;
423 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
424 FieldAlign = std::max(FieldAlign, AA->getAlignment());
425
426 // Round up the current record size to the field's alignment boundary.
427 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
428 }
429
430 // Place this field at the current location.
431 FieldOffsets[FieldNo] = FieldOffset;
432
433 // Reserve space for this field.
434 if (IsUnion) {
435 Size = std::max(Size, FieldSize);
436 } else {
437 Size = FieldOffset + FieldSize;
438 }
439
440 // Remember max struct/class alignment.
441 Alignment = std::max(Alignment, FieldAlign);
442}
443
Devang Patel44a3dde2008-06-04 21:54:36 +0000444
Chris Lattner61710852008-10-05 17:34:18 +0000445/// getASTObjcInterfaceLayout - Get or compute information about the layout of
446/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000447/// position information.
448const ASTRecordLayout &
449ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
450 // Look up this layout, if already laid out, return what we have.
451 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
452 if (Entry) return *Entry;
453
454 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
455 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000456 ASTRecordLayout *NewEntry = NULL;
457 unsigned FieldCount = D->ivar_size();
458 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
459 FieldCount++;
460 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
461 unsigned Alignment = SL.getAlignment();
462 uint64_t Size = SL.getSize();
463 NewEntry = new ASTRecordLayout(Size, Alignment);
464 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000465 // Super class is at the beginning of the layout.
466 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000467 } else {
468 NewEntry = new ASTRecordLayout();
469 NewEntry->InitializeLayout(FieldCount);
470 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000471 Entry = NewEntry;
472
Devang Patel44a3dde2008-06-04 21:54:36 +0000473 bool IsPacked = D->getAttr<PackedAttr>();
474
475 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
476 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
477 AA->getAlignment()));
478
479 // Layout each ivar sequentially.
480 unsigned i = 0;
481 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
482 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
483 const ObjCIvarDecl* Ivar = (*IVI);
484 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
485 }
486
487 // Finally, round the size of the total struct up to the alignment of the
488 // struct itself.
489 NewEntry->FinalizeLayout();
490 return *NewEntry;
491}
492
Devang Patel88a981b2007-11-01 19:11:01 +0000493/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000494/// specified record (struct/union/class), which indicates its size and field
495/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000496const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000497 D = D->getDefinition(*this);
498 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000499
Chris Lattner464175b2007-07-18 17:52:12 +0000500 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000501 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000502 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000503
Devang Patel88a981b2007-11-01 19:11:01 +0000504 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
505 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
506 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000507 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000508
Devang Patel8b277042008-06-04 21:22:16 +0000509 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman4bd998b2008-05-30 09:31:38 +0000510 bool StructIsPacked = D->getAttr<PackedAttr>();
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000511 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000512
Eli Friedman4bd998b2008-05-30 09:31:38 +0000513 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000514 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
515 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000516
Eli Friedman4bd998b2008-05-30 09:31:38 +0000517 // Layout each field, for now, just sequentially, respecting alignment. In
518 // the future, this will need to be tweakable by targets.
519 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
520 const FieldDecl *FD = D->getMember(i);
Devang Patel8b277042008-06-04 21:22:16 +0000521 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000522 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000523
524 // Finally, round the size of the total struct up to the alignment of the
525 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000526 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000527 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000528}
529
Chris Lattnera7674d82007-07-13 22:13:22 +0000530//===----------------------------------------------------------------------===//
531// Type creation/memoization methods
532//===----------------------------------------------------------------------===//
533
Christopher Lambebb97e92008-02-04 02:31:56 +0000534QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000535 QualType CanT = getCanonicalType(T);
536 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000537 return T;
538
539 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
540 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000541 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000542 "Type is already address space qualified");
543
544 // Check if we've already instantiated an address space qual'd type of this
545 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000546 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000547 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000548 void *InsertPos = 0;
549 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
550 return QualType(ASQy, 0);
551
552 // If the base type isn't canonical, this won't be a canonical type either,
553 // so fill in the canonical type field.
554 QualType Canonical;
555 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000556 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000557
558 // Get the new insert position for the node we care about.
559 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
560 assert(NewIP == 0 && "Shouldn't be in the map!");
561 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000562 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000563 ASQualTypes.InsertNode(New, InsertPos);
564 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000565 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000566}
567
Chris Lattnera7674d82007-07-13 22:13:22 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569/// getComplexType - Return the uniqued reference to the type for a complex
570/// number with the specified element type.
571QualType ASTContext::getComplexType(QualType T) {
572 // Unique pointers, to guarantee there is only one pointer of a particular
573 // structure.
574 llvm::FoldingSetNodeID ID;
575 ComplexType::Profile(ID, T);
576
577 void *InsertPos = 0;
578 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
579 return QualType(CT, 0);
580
581 // If the pointee type isn't canonical, this won't be a canonical type either,
582 // so fill in the canonical type field.
583 QualType Canonical;
584 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000585 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000586
587 // Get the new insert position for the node we care about.
588 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
589 assert(NewIP == 0 && "Shouldn't be in the map!");
590 }
591 ComplexType *New = new ComplexType(T, Canonical);
592 Types.push_back(New);
593 ComplexTypes.InsertNode(New, InsertPos);
594 return QualType(New, 0);
595}
596
597
598/// getPointerType - Return the uniqued reference to the type for a pointer to
599/// the specified type.
600QualType ASTContext::getPointerType(QualType T) {
601 // Unique pointers, to guarantee there is only one pointer of a particular
602 // structure.
603 llvm::FoldingSetNodeID ID;
604 PointerType::Profile(ID, T);
605
606 void *InsertPos = 0;
607 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
608 return QualType(PT, 0);
609
610 // If the pointee type isn't canonical, this won't be a canonical type either,
611 // so fill in the canonical type field.
612 QualType Canonical;
613 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000614 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000615
616 // Get the new insert position for the node we care about.
617 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
618 assert(NewIP == 0 && "Shouldn't be in the map!");
619 }
620 PointerType *New = new PointerType(T, Canonical);
621 Types.push_back(New);
622 PointerTypes.InsertNode(New, InsertPos);
623 return QualType(New, 0);
624}
625
Steve Naroff5618bd42008-08-27 16:04:49 +0000626/// getBlockPointerType - Return the uniqued reference to the type for
627/// a pointer to the specified block.
628QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000629 assert(T->isFunctionType() && "block of function types only");
630 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000631 // structure.
632 llvm::FoldingSetNodeID ID;
633 BlockPointerType::Profile(ID, T);
634
635 void *InsertPos = 0;
636 if (BlockPointerType *PT =
637 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
638 return QualType(PT, 0);
639
Steve Naroff296e8d52008-08-28 19:20:44 +0000640 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000641 // type either so fill in the canonical type field.
642 QualType Canonical;
643 if (!T->isCanonical()) {
644 Canonical = getBlockPointerType(getCanonicalType(T));
645
646 // Get the new insert position for the node we care about.
647 BlockPointerType *NewIP =
648 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
649 assert(NewIP == 0 && "Shouldn't be in the map!");
650 }
651 BlockPointerType *New = new BlockPointerType(T, Canonical);
652 Types.push_back(New);
653 BlockPointerTypes.InsertNode(New, InsertPos);
654 return QualType(New, 0);
655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657/// getReferenceType - Return the uniqued reference to the type for a reference
658/// to the specified type.
659QualType ASTContext::getReferenceType(QualType T) {
660 // Unique pointers, to guarantee there is only one pointer of a particular
661 // structure.
662 llvm::FoldingSetNodeID ID;
663 ReferenceType::Profile(ID, T);
664
665 void *InsertPos = 0;
666 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
667 return QualType(RT, 0);
668
669 // If the referencee type isn't canonical, this won't be a canonical type
670 // either, so fill in the canonical type field.
671 QualType Canonical;
672 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000673 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000674
675 // Get the new insert position for the node we care about.
676 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
677 assert(NewIP == 0 && "Shouldn't be in the map!");
678 }
679
680 ReferenceType *New = new ReferenceType(T, Canonical);
681 Types.push_back(New);
682 ReferenceTypes.InsertNode(New, InsertPos);
683 return QualType(New, 0);
684}
685
Steve Narofffb22d962007-08-30 01:06:46 +0000686/// getConstantArrayType - Return the unique reference to the type for an
687/// array of the specified element type.
688QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000689 const llvm::APInt &ArySize,
690 ArrayType::ArraySizeModifier ASM,
691 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000693 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694
695 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000696 if (ConstantArrayType *ATP =
697 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 return QualType(ATP, 0);
699
700 // If the element type isn't canonical, this won't be a canonical type either,
701 // so fill in the canonical type field.
702 QualType Canonical;
703 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000704 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000705 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000707 ConstantArrayType *NewIP =
708 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 assert(NewIP == 0 && "Shouldn't be in the map!");
711 }
712
Steve Naroffc9406122007-08-30 18:10:14 +0000713 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
714 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000715 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 Types.push_back(New);
717 return QualType(New, 0);
718}
719
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000720/// getVariableArrayType - Returns a non-unique reference to the type for a
721/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000722QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
723 ArrayType::ArraySizeModifier ASM,
724 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000725 // Since we don't unique expressions, it isn't possible to unique VLA's
726 // that have an expression provided for their size.
727
728 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
729 ASM, EltTypeQuals);
730
731 VariableArrayTypes.push_back(New);
732 Types.push_back(New);
733 return QualType(New, 0);
734}
735
736QualType ASTContext::getIncompleteArrayType(QualType EltTy,
737 ArrayType::ArraySizeModifier ASM,
738 unsigned EltTypeQuals) {
739 llvm::FoldingSetNodeID ID;
740 IncompleteArrayType::Profile(ID, EltTy);
741
742 void *InsertPos = 0;
743 if (IncompleteArrayType *ATP =
744 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
745 return QualType(ATP, 0);
746
747 // If the element type isn't canonical, this won't be a canonical type
748 // either, so fill in the canonical type field.
749 QualType Canonical;
750
751 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000752 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000753 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000754
755 // Get the new insert position for the node we care about.
756 IncompleteArrayType *NewIP =
757 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
758
759 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000760 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000761
762 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
763 ASM, EltTypeQuals);
764
765 IncompleteArrayTypes.InsertNode(New, InsertPos);
766 Types.push_back(New);
767 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000768}
769
Steve Naroff73322922007-07-18 18:00:27 +0000770/// getVectorType - Return the unique reference to a vector type of
771/// the specified element type and size. VectorType must be a built-in type.
772QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 BuiltinType *baseType;
774
Chris Lattnerf52ab252008-04-06 22:59:24 +0000775 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000776 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000777
778 // Check if we've already instantiated a vector of this type.
779 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000780 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 void *InsertPos = 0;
782 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
783 return QualType(VTP, 0);
784
785 // If the element type isn't canonical, this won't be a canonical type either,
786 // so fill in the canonical type field.
787 QualType Canonical;
788 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000789 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000790
791 // Get the new insert position for the node we care about.
792 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
793 assert(NewIP == 0 && "Shouldn't be in the map!");
794 }
795 VectorType *New = new VectorType(vecType, NumElts, Canonical);
796 VectorTypes.InsertNode(New, InsertPos);
797 Types.push_back(New);
798 return QualType(New, 0);
799}
800
Nate Begeman213541a2008-04-18 23:10:10 +0000801/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000802/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000803QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000804 BuiltinType *baseType;
805
Chris Lattnerf52ab252008-04-06 22:59:24 +0000806 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000807 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000808
809 // Check if we've already instantiated a vector of this type.
810 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000811 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000812 void *InsertPos = 0;
813 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
814 return QualType(VTP, 0);
815
816 // If the element type isn't canonical, this won't be a canonical type either,
817 // so fill in the canonical type field.
818 QualType Canonical;
819 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000820 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000821
822 // Get the new insert position for the node we care about.
823 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
824 assert(NewIP == 0 && "Shouldn't be in the map!");
825 }
Nate Begeman213541a2008-04-18 23:10:10 +0000826 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000827 VectorTypes.InsertNode(New, InsertPos);
828 Types.push_back(New);
829 return QualType(New, 0);
830}
831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
833///
834QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
835 // Unique functions, to guarantee there is only one function of a particular
836 // structure.
837 llvm::FoldingSetNodeID ID;
838 FunctionTypeNoProto::Profile(ID, ResultTy);
839
840 void *InsertPos = 0;
841 if (FunctionTypeNoProto *FT =
842 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
843 return QualType(FT, 0);
844
845 QualType Canonical;
846 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000847 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000848
849 // Get the new insert position for the node we care about.
850 FunctionTypeNoProto *NewIP =
851 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
852 assert(NewIP == 0 && "Shouldn't be in the map!");
853 }
854
855 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
856 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000857 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 return QualType(New, 0);
859}
860
861/// getFunctionType - Return a normal function type with a typed argument
862/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +0000863QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 unsigned NumArgs, bool isVariadic) {
865 // Unique functions, to guarantee there is only one function of a particular
866 // structure.
867 llvm::FoldingSetNodeID ID;
868 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
869
870 void *InsertPos = 0;
871 if (FunctionTypeProto *FTP =
872 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
873 return QualType(FTP, 0);
874
875 // Determine whether the type being created is already canonical or not.
876 bool isCanonical = ResultTy->isCanonical();
877 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
878 if (!ArgArray[i]->isCanonical())
879 isCanonical = false;
880
881 // If this type isn't canonical, get the canonical version of it.
882 QualType Canonical;
883 if (!isCanonical) {
884 llvm::SmallVector<QualType, 16> CanonicalArgs;
885 CanonicalArgs.reserve(NumArgs);
886 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000887 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000888
Chris Lattnerf52ab252008-04-06 22:59:24 +0000889 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 &CanonicalArgs[0], NumArgs,
891 isVariadic);
892
893 // Get the new insert position for the node we care about.
894 FunctionTypeProto *NewIP =
895 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
896 assert(NewIP == 0 && "Shouldn't be in the map!");
897 }
898
899 // FunctionTypeProto objects are not allocated with new because they have a
900 // variable size array (for parameter types) at the end of them.
901 FunctionTypeProto *FTP =
902 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000903 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
905 Canonical);
906 Types.push_back(FTP);
907 FunctionTypeProtos.InsertNode(FTP, InsertPos);
908 return QualType(FTP, 0);
909}
910
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000911/// getTypeDeclType - Return the unique reference to the type for the
912/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000913QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000914 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
915
916 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
917 return getTypedefType(Typedef);
918 else if (ObjCInterfaceDecl *ObjCInterface
919 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
920 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000921
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000922 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl)) {
923 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
924 : new CXXRecordType(CXXRecord);
925 }
926 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
927 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
928 : new RecordType(Record);
929 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000930 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000931 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000932 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000933 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000934
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000935 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000936 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000937}
938
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000939/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
940/// about which RecordDecl serves as the definition of a particular
941/// struct/union/class. This will eventually be used by enums as well.
942void ASTContext::setTagDefinition(TagDecl* D) {
943 assert (D->isDefinition());
944 cast<TagType>(D->TypeForDecl)->decl = D;
945}
946
Reid Spencer5f016e22007-07-11 17:01:13 +0000947/// getTypedefType - Return the unique reference to the type for the
948/// specified typename decl.
949QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
950 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
951
Chris Lattnerf52ab252008-04-06 22:59:24 +0000952 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000953 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 Types.push_back(Decl->TypeForDecl);
955 return QualType(Decl->TypeForDecl, 0);
956}
957
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000958/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000959/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000961 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
962
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000963 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000964 Types.push_back(Decl->TypeForDecl);
965 return QualType(Decl->TypeForDecl, 0);
966}
967
Chris Lattner88cb27a2008-04-07 04:56:42 +0000968/// CmpProtocolNames - Comparison predicate for sorting protocols
969/// alphabetically.
970static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
971 const ObjCProtocolDecl *RHS) {
972 return strcmp(LHS->getName(), RHS->getName()) < 0;
973}
974
975static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
976 unsigned &NumProtocols) {
977 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
978
979 // Sort protocols, keyed by name.
980 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
981
982 // Remove duplicates.
983 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
984 NumProtocols = ProtocolsEnd-Protocols;
985}
986
987
Chris Lattner065f0d72008-04-07 04:44:08 +0000988/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
989/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
991 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +0000992 // Sort the protocol list alphabetically to canonicalize it.
993 SortAndUniqueProtocols(Protocols, NumProtocols);
994
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000995 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +0000996 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000997
998 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000999 if (ObjCQualifiedInterfaceType *QT =
1000 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001001 return QualType(QT, 0);
1002
1003 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 ObjCQualifiedInterfaceType *QType =
1005 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001006 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001007 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001008 return QualType(QType, 0);
1009}
1010
Chris Lattner88cb27a2008-04-07 04:56:42 +00001011/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1012/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001013QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001014 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001015 // Sort the protocol list alphabetically to canonicalize it.
1016 SortAndUniqueProtocols(Protocols, NumProtocols);
1017
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001018 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001020
1021 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001023 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001024 return QualType(QT, 0);
1025
1026 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001027 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001028 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001029 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001030 return QualType(QType, 0);
1031}
1032
Steve Naroff9752f252007-08-01 18:02:17 +00001033/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1034/// TypeOfExpr AST's (since expression's are never shared). For example,
1035/// multiple declarations that refer to "typeof(x)" all contain different
1036/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1037/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001038QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001039 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001040 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1041 Types.push_back(toe);
1042 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001043}
1044
Steve Naroff9752f252007-08-01 18:02:17 +00001045/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1046/// TypeOfType AST's. The only motivation to unique these nodes would be
1047/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1048/// an issue. This doesn't effect the type checker, since it operates
1049/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001050QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001051 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001052 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1053 Types.push_back(tot);
1054 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001055}
1056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057/// getTagDeclType - Return the unique reference to the type for the
1058/// specified TagDecl (struct/union/class/enum) decl.
1059QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001060 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001061 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001062}
1063
1064/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1065/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1066/// needs to agree with the definition in <stddef.h>.
1067QualType ASTContext::getSizeType() const {
1068 // On Darwin, size_t is defined as a "long unsigned int".
1069 // FIXME: should derive from "Target".
1070 return UnsignedLongTy;
1071}
1072
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001073/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001074/// width of characters in wide strings, The value is target dependent and
1075/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001076QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001077 if (LangOpts.CPlusPlus)
1078 return WCharTy;
1079
Eli Friedmanfd888a52008-02-12 08:29:21 +00001080 // On Darwin, wchar_t is defined as a "int".
1081 // FIXME: should derive from "Target".
1082 return IntTy;
1083}
1084
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001085/// getSignedWCharType - Return the type of "signed wchar_t".
1086/// Used when in C++, as a GCC extension.
1087QualType ASTContext::getSignedWCharType() const {
1088 // FIXME: derive from "Target" ?
1089 return WCharTy;
1090}
1091
1092/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1093/// Used when in C++, as a GCC extension.
1094QualType ASTContext::getUnsignedWCharType() const {
1095 // FIXME: derive from "Target" ?
1096 return UnsignedIntTy;
1097}
1098
Chris Lattner8b9023b2007-07-13 03:05:23 +00001099/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1100/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1101QualType ASTContext::getPointerDiffType() const {
1102 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1103 // FIXME: should derive from "Target".
1104 return IntTy;
1105}
1106
Chris Lattnere6327742008-04-02 05:18:44 +00001107//===----------------------------------------------------------------------===//
1108// Type Operators
1109//===----------------------------------------------------------------------===//
1110
Chris Lattner77c96472008-04-06 22:41:35 +00001111/// getCanonicalType - Return the canonical (structural) type corresponding to
1112/// the specified potentially non-canonical type. The non-canonical version
1113/// of a type may have many "decorated" versions of types. Decorators can
1114/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1115/// to be free of any of these, allowing two canonical types to be compared
1116/// for exact equality with a simple pointer comparison.
1117QualType ASTContext::getCanonicalType(QualType T) {
1118 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001119
1120 // If the result has type qualifiers, make sure to canonicalize them as well.
1121 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1122 if (TypeQuals == 0) return CanType;
1123
1124 // If the type qualifiers are on an array type, get the canonical type of the
1125 // array with the qualifiers applied to the element type.
1126 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1127 if (!AT)
1128 return CanType.getQualifiedType(TypeQuals);
1129
1130 // Get the canonical version of the element with the extra qualifiers on it.
1131 // This can recursively sink qualifiers through multiple levels of arrays.
1132 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1133 NewEltTy = getCanonicalType(NewEltTy);
1134
1135 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1136 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1137 CAT->getIndexTypeQualifier());
1138 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1139 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1140 IAT->getIndexTypeQualifier());
1141
1142 // FIXME: What is the ownership of size expressions in VLAs?
1143 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1144 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1145 VAT->getSizeModifier(),
1146 VAT->getIndexTypeQualifier());
1147}
1148
1149
1150const ArrayType *ASTContext::getAsArrayType(QualType T) {
1151 // Handle the non-qualified case efficiently.
1152 if (T.getCVRQualifiers() == 0) {
1153 // Handle the common positive case fast.
1154 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1155 return AT;
1156 }
1157
1158 // Handle the common negative case fast, ignoring CVR qualifiers.
1159 QualType CType = T->getCanonicalTypeInternal();
1160
1161 // Make sure to look through type qualifiers (like ASQuals) for the negative
1162 // test.
1163 if (!isa<ArrayType>(CType) &&
1164 !isa<ArrayType>(CType.getUnqualifiedType()))
1165 return 0;
1166
1167 // Apply any CVR qualifiers from the array type to the element type. This
1168 // implements C99 6.7.3p8: "If the specification of an array type includes
1169 // any type qualifiers, the element type is so qualified, not the array type."
1170
1171 // If we get here, we either have type qualifiers on the type, or we have
1172 // sugar such as a typedef in the way. If we have type qualifiers on the type
1173 // we must propagate them down into the elemeng type.
1174 unsigned CVRQuals = T.getCVRQualifiers();
1175 unsigned AddrSpace = 0;
1176 Type *Ty = T.getTypePtr();
1177
1178 // Rip through ASQualType's and typedefs to get to a concrete type.
1179 while (1) {
1180 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1181 AddrSpace = ASQT->getAddressSpace();
1182 Ty = ASQT->getBaseType();
1183 } else {
1184 T = Ty->getDesugaredType();
1185 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1186 break;
1187 CVRQuals |= T.getCVRQualifiers();
1188 Ty = T.getTypePtr();
1189 }
1190 }
1191
1192 // If we have a simple case, just return now.
1193 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1194 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1195 return ATy;
1196
1197 // Otherwise, we have an array and we have qualifiers on it. Push the
1198 // qualifiers into the array element type and return a new array type.
1199 // Get the canonical version of the element with the extra qualifiers on it.
1200 // This can recursively sink qualifiers through multiple levels of arrays.
1201 QualType NewEltTy = ATy->getElementType();
1202 if (AddrSpace)
1203 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1204 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1205
1206 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1207 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1208 CAT->getSizeModifier(),
1209 CAT->getIndexTypeQualifier()));
1210 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1211 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1212 IAT->getSizeModifier(),
1213 IAT->getIndexTypeQualifier()));
1214
1215 // FIXME: What is the ownership of size expressions in VLAs?
1216 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1217 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1218 VAT->getSizeModifier(),
1219 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001220}
1221
1222
Chris Lattnere6327742008-04-02 05:18:44 +00001223/// getArrayDecayedType - Return the properly qualified result of decaying the
1224/// specified array type to a pointer. This operation is non-trivial when
1225/// handling typedefs etc. The canonical type of "T" must be an array type,
1226/// this returns a pointer to a properly qualified element of the array.
1227///
1228/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1229QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001230 // Get the element type with 'getAsArrayType' so that we don't lose any
1231 // typedefs in the element type of the array. This also handles propagation
1232 // of type qualifiers from the array type into the element type if present
1233 // (C99 6.7.3p8).
1234 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1235 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001236
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001237 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001238
1239 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001240 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001241}
1242
Reid Spencer5f016e22007-07-11 17:01:13 +00001243/// getFloatingRank - Return a relative rank for floating point types.
1244/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001245static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001246 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001248
Christopher Lambebb97e92008-02-04 02:31:56 +00001249 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001250 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 case BuiltinType::Float: return FloatRank;
1252 case BuiltinType::Double: return DoubleRank;
1253 case BuiltinType::LongDouble: return LongDoubleRank;
1254 }
1255}
1256
Steve Naroff716c7302007-08-27 01:41:48 +00001257/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1258/// point or a complex type (based on typeDomain/typeSize).
1259/// 'typeDomain' is a real floating point or complex type.
1260/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001261QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1262 QualType Domain) const {
1263 FloatingRank EltRank = getFloatingRank(Size);
1264 if (Domain->isComplexType()) {
1265 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001266 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001267 case FloatRank: return FloatComplexTy;
1268 case DoubleRank: return DoubleComplexTy;
1269 case LongDoubleRank: return LongDoubleComplexTy;
1270 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 }
Chris Lattner1361b112008-04-06 23:58:54 +00001272
1273 assert(Domain->isRealFloatingType() && "Unknown domain!");
1274 switch (EltRank) {
1275 default: assert(0 && "getFloatingRank(): illegal value for rank");
1276 case FloatRank: return FloatTy;
1277 case DoubleRank: return DoubleTy;
1278 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001279 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001280}
1281
Chris Lattner7cfeb082008-04-06 23:55:33 +00001282/// getFloatingTypeOrder - Compare the rank of the two specified floating
1283/// point types, ignoring the domain of the type (i.e. 'double' ==
1284/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1285/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001286int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1287 FloatingRank LHSR = getFloatingRank(LHS);
1288 FloatingRank RHSR = getFloatingRank(RHS);
1289
1290 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001291 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001292 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001293 return 1;
1294 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001295}
1296
Chris Lattnerf52ab252008-04-06 22:59:24 +00001297/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1298/// routine will assert if passed a built-in type that isn't an integer or enum,
1299/// or if it is not canonicalized.
1300static unsigned getIntegerRank(Type *T) {
1301 assert(T->isCanonical() && "T should be canonicalized");
1302 if (isa<EnumType>(T))
1303 return 4;
1304
1305 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001306 default: assert(0 && "getIntegerRank(): not a built-in integer");
1307 case BuiltinType::Bool:
1308 return 1;
1309 case BuiltinType::Char_S:
1310 case BuiltinType::Char_U:
1311 case BuiltinType::SChar:
1312 case BuiltinType::UChar:
1313 return 2;
1314 case BuiltinType::Short:
1315 case BuiltinType::UShort:
1316 return 3;
1317 case BuiltinType::Int:
1318 case BuiltinType::UInt:
1319 return 4;
1320 case BuiltinType::Long:
1321 case BuiltinType::ULong:
1322 return 5;
1323 case BuiltinType::LongLong:
1324 case BuiltinType::ULongLong:
1325 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001326 }
1327}
1328
Chris Lattner7cfeb082008-04-06 23:55:33 +00001329/// getIntegerTypeOrder - Returns the highest ranked integer type:
1330/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1331/// LHS < RHS, return -1.
1332int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001333 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1334 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001335 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336
Chris Lattnerf52ab252008-04-06 22:59:24 +00001337 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1338 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001339
Chris Lattner7cfeb082008-04-06 23:55:33 +00001340 unsigned LHSRank = getIntegerRank(LHSC);
1341 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001342
Chris Lattner7cfeb082008-04-06 23:55:33 +00001343 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1344 if (LHSRank == RHSRank) return 0;
1345 return LHSRank > RHSRank ? 1 : -1;
1346 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001347
Chris Lattner7cfeb082008-04-06 23:55:33 +00001348 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1349 if (LHSUnsigned) {
1350 // If the unsigned [LHS] type is larger, return it.
1351 if (LHSRank >= RHSRank)
1352 return 1;
1353
1354 // If the signed type can represent all values of the unsigned type, it
1355 // wins. Because we are dealing with 2's complement and types that are
1356 // powers of two larger than each other, this is always safe.
1357 return -1;
1358 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001359
Chris Lattner7cfeb082008-04-06 23:55:33 +00001360 // If the unsigned [RHS] type is larger, return it.
1361 if (RHSRank >= LHSRank)
1362 return -1;
1363
1364 // If the signed type can represent all values of the unsigned type, it
1365 // wins. Because we are dealing with 2's complement and types that are
1366 // powers of two larger than each other, this is always safe.
1367 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001368}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001369
1370// getCFConstantStringType - Return the type used for constant CFStrings.
1371QualType ASTContext::getCFConstantStringType() {
1372 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001373 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001374 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001375 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001376 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001377
1378 // const int *isa;
1379 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001380 // int flags;
1381 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001382 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001383 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001384 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001385 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001386 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001387 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001388
Anders Carlssonf06273f2007-11-19 00:25:30 +00001389 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001390 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001391 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001392
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001393 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001394 }
1395
1396 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001397}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001398
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001399QualType ASTContext::getObjCFastEnumerationStateType()
1400{
1401 if (!ObjCFastEnumerationStateTypeDecl) {
1402 QualType FieldTypes[] = {
1403 UnsignedLongTy,
1404 getPointerType(ObjCIdType),
1405 getPointerType(UnsignedLongTy),
1406 getConstantArrayType(UnsignedLongTy,
1407 llvm::APInt(32, 5), ArrayType::Normal, 0)
1408 };
1409
1410 FieldDecl *FieldDecls[4];
1411 for (size_t i = 0; i < 4; ++i)
1412 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1413 FieldTypes[i]);
1414
1415 ObjCFastEnumerationStateTypeDecl =
1416 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001417 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001418
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001419 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001420 }
1421
1422 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1423}
1424
Anders Carlssone8c49532007-10-29 06:33:42 +00001425// This returns true if a type has been typedefed to BOOL:
1426// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001427static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001428 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001429 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001430
1431 return false;
1432}
1433
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001435/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001436int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001437 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001438
1439 // Make all integer and enum types at least as large as an int
1440 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001441 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001442 // Treat arrays as pointers, since that's how they're passed in.
1443 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001444 sz = getTypeSize(VoidPtrTy);
1445 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001446}
1447
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001449/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001450void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001451 std::string& S)
1452{
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001453 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001454 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001455 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001456 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001457 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001458 // Compute size of all parameters.
1459 // Start with computing size of a pointer in number of bytes.
1460 // FIXME: There might(should) be a better way of doing this computation!
1461 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001462 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001463 // The first two arguments (self and _cmd) are pointers; account for
1464 // their size.
1465 int ParmOffset = 2 * PtrSize;
1466 int NumOfParams = Decl->getNumParams();
1467 for (int i = 0; i < NumOfParams; i++) {
1468 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 int sz = getObjCEncodingTypeSize (PType);
1470 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001471 ParmOffset += sz;
1472 }
1473 S += llvm::utostr(ParmOffset);
1474 S += "@0:";
1475 S += llvm::utostr(PtrSize);
1476
1477 // Argument types.
1478 ParmOffset = 2 * PtrSize;
1479 for (int i = 0; i < NumOfParams; i++) {
1480 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001481 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001482 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001483 getObjCEncodingForTypeQualifier(
1484 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001485 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001486 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001488 }
1489}
1490
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001491/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1492/// method declaration. If non-NULL, Container must be either an
1493/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1494/// NULL when getting encodings for protocol properties.
1495void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1496 const Decl *Container,
1497 std::string& S)
1498{
1499 // Collect information from the property implementation decl(s).
1500 bool Dynamic = false;
1501 ObjCPropertyImplDecl *SynthesizePID = 0;
1502
1503 // FIXME: Duplicated code due to poor abstraction.
1504 if (Container) {
1505 if (const ObjCCategoryImplDecl *CID =
1506 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1507 for (ObjCCategoryImplDecl::propimpl_iterator
1508 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1509 ObjCPropertyImplDecl *PID = *i;
1510 if (PID->getPropertyDecl() == PD) {
1511 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1512 Dynamic = true;
1513 } else {
1514 SynthesizePID = PID;
1515 }
1516 }
1517 }
1518 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001519 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001520 for (ObjCCategoryImplDecl::propimpl_iterator
1521 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1522 ObjCPropertyImplDecl *PID = *i;
1523 if (PID->getPropertyDecl() == PD) {
1524 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1525 Dynamic = true;
1526 } else {
1527 SynthesizePID = PID;
1528 }
1529 }
1530 }
1531 }
1532 }
1533
1534 // FIXME: This is not very efficient.
1535 S = "T";
1536
1537 // Encode result type.
1538 // FIXME: GCC uses a generating_property_type_encoding mode during
1539 // this part. Investigate.
1540 getObjCEncodingForType(PD->getType(), S, EncodingRecordTypes);
1541
1542 if (PD->isReadOnly()) {
1543 S += ",R";
1544 } else {
1545 switch (PD->getSetterKind()) {
1546 case ObjCPropertyDecl::Assign: break;
1547 case ObjCPropertyDecl::Copy: S += ",C"; break;
1548 case ObjCPropertyDecl::Retain: S += ",&"; break;
1549 }
1550 }
1551
1552 // It really isn't clear at all what this means, since properties
1553 // are "dynamic by default".
1554 if (Dynamic)
1555 S += ",D";
1556
1557 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1558 S += ",G";
1559 S += PD->getGetterName().getName();
1560 }
1561
1562 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1563 S += ",S";
1564 S += PD->getSetterName().getName();
1565 }
1566
1567 if (SynthesizePID) {
1568 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1569 S += ",V";
1570 S += OID->getName();
1571 }
1572
1573 // FIXME: OBJCGC: weak & strong
1574}
1575
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001576void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001577 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001578 // FIXME: This currently doesn't encode:
1579 // @ An object (whether statically typed or typed id)
1580 // # A class object (Class)
1581 // : A method selector (SEL)
1582 // {name=type...} A structure
1583 // (name=type...) A union
1584 // bnum A bit field of num bits
1585
1586 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001587 char encoding;
1588 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001589 default: assert(0 && "Unhandled builtin type kind");
1590 case BuiltinType::Void: encoding = 'v'; break;
1591 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001592 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001593 case BuiltinType::UChar: encoding = 'C'; break;
1594 case BuiltinType::UShort: encoding = 'S'; break;
1595 case BuiltinType::UInt: encoding = 'I'; break;
1596 case BuiltinType::ULong: encoding = 'L'; break;
1597 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001598 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001599 case BuiltinType::SChar: encoding = 'c'; break;
1600 case BuiltinType::Short: encoding = 's'; break;
1601 case BuiltinType::Int: encoding = 'i'; break;
1602 case BuiltinType::Long: encoding = 'l'; break;
1603 case BuiltinType::LongLong: encoding = 'q'; break;
1604 case BuiltinType::Float: encoding = 'f'; break;
1605 case BuiltinType::Double: encoding = 'd'; break;
1606 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001607 }
1608
1609 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001610 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001612 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001613 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001614
1615 }
1616 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001617 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001618 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001619 S += '@';
1620 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001621 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001622 S += '#';
1623 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001624 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001625 S += ':';
1626 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001627 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001628
1629 if (PointeeTy->isCharType()) {
1630 // char pointer types should be encoded as '*' unless it is a
1631 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001632 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001633 S += '*';
1634 return;
1635 }
1636 }
1637
1638 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001639 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001640 } else if (const ArrayType *AT =
1641 // Ignore type qualifiers etc.
1642 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001643 S += '[';
1644
1645 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1646 S += llvm::utostr(CAT->getSize().getZExtValue());
1647 else
1648 assert(0 && "Unhandled array type!");
1649
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001650 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001651 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001652 } else if (T->getAsFunctionType()) {
1653 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001654 } else if (const RecordType *RTy = T->getAsRecordType()) {
1655 RecordDecl *RDecl= RTy->getDecl();
Steve Narofffaf37e72008-08-14 15:00:38 +00001656 // This mimics the behavior in gcc's encode_aggregate_within().
1657 // The idea is to only inline structure definitions for top level pointers
1658 // to structures and embedded structures.
1659 bool inlining = (S.size() == 1 && S[0] == '^' ||
1660 S.size() > 1 && S[S.size()-1] != '^');
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001661 S += '{';
1662 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001663 bool found = false;
1664 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1665 if (ERType[i] == RTy) {
1666 found = true;
1667 break;
1668 }
Steve Narofffaf37e72008-08-14 15:00:38 +00001669 if (!found && inlining) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001670 ERType.push_back(RTy);
1671 S += '=';
1672 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1673 FieldDecl *field = RDecl->getMember(i);
1674 getObjCEncodingForType(field->getType(), S, ERType);
1675 }
1676 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1677 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001678 }
1679 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001680 } else if (T->isEnumeralType()) {
1681 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001682 } else if (T->isBlockPointerType()) {
1683 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001684 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001685 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001686}
1687
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001688void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001689 std::string& S) const {
1690 if (QT & Decl::OBJC_TQ_In)
1691 S += 'n';
1692 if (QT & Decl::OBJC_TQ_Inout)
1693 S += 'N';
1694 if (QT & Decl::OBJC_TQ_Out)
1695 S += 'o';
1696 if (QT & Decl::OBJC_TQ_Bycopy)
1697 S += 'O';
1698 if (QT & Decl::OBJC_TQ_Byref)
1699 S += 'R';
1700 if (QT & Decl::OBJC_TQ_Oneway)
1701 S += 'V';
1702}
1703
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001704void ASTContext::setBuiltinVaListType(QualType T)
1705{
1706 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1707
1708 BuiltinVaListType = T;
1709}
1710
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001711void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001712{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001713 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001714
1715 // typedef struct objc_object *id;
1716 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1717 assert(ptr && "'id' incorrectly typed");
1718 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1719 assert(rec && "'id' incorrectly typed");
1720 IdStructType = rec;
1721}
1722
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001723void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001724{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001725 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001726
1727 // typedef struct objc_selector *SEL;
1728 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1729 assert(ptr && "'SEL' incorrectly typed");
1730 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1731 assert(rec && "'SEL' incorrectly typed");
1732 SelStructType = rec;
1733}
1734
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001735void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001736{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001738}
1739
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001740void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001741{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001742 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001743
1744 // typedef struct objc_class *Class;
1745 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1746 assert(ptr && "'Class' incorrectly typed");
1747 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1748 assert(rec && "'Class' incorrectly typed");
1749 ClassStructType = rec;
1750}
1751
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001752void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1753 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001754 "'NSConstantString' type already set!");
1755
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001756 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001757}
1758
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001759
1760//===----------------------------------------------------------------------===//
1761// Type Predicates.
1762//===----------------------------------------------------------------------===//
1763
1764/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1765/// to an object type. This includes "id" and "Class" (two 'special' pointers
1766/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1767/// ID type).
1768bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1769 if (Ty->isObjCQualifiedIdType())
1770 return true;
1771
1772 if (!Ty->isPointerType())
1773 return false;
1774
1775 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1776 // pointer types. This looks for the typedef specifically, not for the
1777 // underlying type.
1778 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1779 return true;
1780
1781 // If this a pointer to an interface (e.g. NSString*), it is ok.
1782 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1783}
1784
Chris Lattner6ac46a42008-04-07 06:51:04 +00001785//===----------------------------------------------------------------------===//
1786// Type Compatibility Testing
1787//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001788
Steve Naroff1c7d0672008-09-04 15:10:53 +00001789/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001790/// block types. Types must be strictly compatible here. For example,
1791/// C unfortunately doesn't produce an error for the following:
1792///
1793/// int (*emptyArgFunc)();
1794/// int (*intArgList)(int) = emptyArgFunc;
1795///
1796/// For blocks, we will produce an error for the following (similar to C++):
1797///
1798/// int (^emptyArgBlock)();
1799/// int (^intArgBlock)(int) = emptyArgBlock;
1800///
1801/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1802///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001803bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff3c1b9122008-09-09 13:47:19 +00001804 return getCanonicalType(lhs) == getCanonicalType(rhs);
Steve Naroff1c7d0672008-09-04 15:10:53 +00001805}
1806
Chris Lattner6ac46a42008-04-07 06:51:04 +00001807/// areCompatVectorTypes - Return true if the two specified vector types are
1808/// compatible.
1809static bool areCompatVectorTypes(const VectorType *LHS,
1810 const VectorType *RHS) {
1811 assert(LHS->isCanonical() && RHS->isCanonical());
1812 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00001813 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00001814}
1815
Eli Friedman3d815e72008-08-22 00:56:42 +00001816/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001817/// compatible for assignment from RHS to LHS. This handles validation of any
1818/// protocol qualifiers on the LHS or RHS.
1819///
Eli Friedman3d815e72008-08-22 00:56:42 +00001820bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1821 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001822 // Verify that the base decls are compatible: the RHS must be a subclass of
1823 // the LHS.
1824 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1825 return false;
1826
1827 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1828 // protocol qualified at all, then we are good.
1829 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1830 return true;
1831
1832 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1833 // isn't a superset.
1834 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1835 return true; // FIXME: should return false!
1836
1837 // Finally, we must have two protocol-qualified interfaces.
1838 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1839 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1840 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1841 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1842 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1843 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1844
1845 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1846 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1847 // LHS in a single parallel scan until we run out of elements in LHS.
1848 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1849 ObjCProtocolDecl *LHSProto = *LHSPI;
1850
1851 while (RHSPI != RHSPE) {
1852 ObjCProtocolDecl *RHSProto = *RHSPI++;
1853 // If the RHS has a protocol that the LHS doesn't, ignore it.
1854 if (RHSProto != LHSProto)
1855 continue;
1856
1857 // Otherwise, the RHS does have this element.
1858 ++LHSPI;
1859 if (LHSPI == LHSPE)
1860 return true; // All protocols in LHS exist in RHS.
1861
1862 LHSProto = *LHSPI;
1863 }
1864
1865 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1866 return false;
1867}
1868
Steve Naroffec0550f2007-10-15 20:41:53 +00001869/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1870/// both shall have the identically qualified version of a compatible type.
1871/// C99 6.2.7p1: Two types have compatible types if their types are the
1872/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001873bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1874 return !mergeTypes(LHS, RHS).isNull();
1875}
1876
1877QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1878 const FunctionType *lbase = lhs->getAsFunctionType();
1879 const FunctionType *rbase = rhs->getAsFunctionType();
1880 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1881 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1882 bool allLTypes = true;
1883 bool allRTypes = true;
1884
1885 // Check return type
1886 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
1887 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00001888 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
1889 allLTypes = false;
1890 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
1891 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00001892
1893 if (lproto && rproto) { // two C99 style function prototypes
1894 unsigned lproto_nargs = lproto->getNumArgs();
1895 unsigned rproto_nargs = rproto->getNumArgs();
1896
1897 // Compatible functions must have the same number of arguments
1898 if (lproto_nargs != rproto_nargs)
1899 return QualType();
1900
1901 // Variadic and non-variadic functions aren't compatible
1902 if (lproto->isVariadic() != rproto->isVariadic())
1903 return QualType();
1904
1905 // Check argument compatibility
1906 llvm::SmallVector<QualType, 10> types;
1907 for (unsigned i = 0; i < lproto_nargs; i++) {
1908 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
1909 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
1910 QualType argtype = mergeTypes(largtype, rargtype);
1911 if (argtype.isNull()) return QualType();
1912 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00001913 if (getCanonicalType(argtype) != getCanonicalType(largtype))
1914 allLTypes = false;
1915 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
1916 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00001917 }
1918 if (allLTypes) return lhs;
1919 if (allRTypes) return rhs;
1920 return getFunctionType(retType, types.begin(), types.size(),
1921 lproto->isVariadic());
1922 }
1923
1924 if (lproto) allRTypes = false;
1925 if (rproto) allLTypes = false;
1926
1927 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1928 if (proto) {
1929 if (proto->isVariadic()) return QualType();
1930 // Check that the types are compatible with the types that
1931 // would result from default argument promotions (C99 6.7.5.3p15).
1932 // The only types actually affected are promotable integer
1933 // types and floats, which would be passed as a different
1934 // type depending on whether the prototype is visible.
1935 unsigned proto_nargs = proto->getNumArgs();
1936 for (unsigned i = 0; i < proto_nargs; ++i) {
1937 QualType argTy = proto->getArgType(i);
1938 if (argTy->isPromotableIntegerType() ||
1939 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
1940 return QualType();
1941 }
1942
1943 if (allLTypes) return lhs;
1944 if (allRTypes) return rhs;
1945 return getFunctionType(retType, proto->arg_type_begin(),
1946 proto->getNumArgs(), lproto->isVariadic());
1947 }
1948
1949 if (allLTypes) return lhs;
1950 if (allRTypes) return rhs;
1951 return getFunctionTypeNoProto(retType);
1952}
1953
1954QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00001955 // C++ [expr]: If an expression initially has the type "reference to T", the
1956 // type is adjusted to "T" prior to any further analysis, the expression
1957 // designates the object or function denoted by the reference, and the
1958 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00001959 // FIXME: C++ shouldn't be going through here! The rules are different
1960 // enough that they should be handled separately.
1961 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001962 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00001963 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00001964 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00001965
Eli Friedman3d815e72008-08-22 00:56:42 +00001966 QualType LHSCan = getCanonicalType(LHS),
1967 RHSCan = getCanonicalType(RHS);
1968
1969 // If two types are identical, they are compatible.
1970 if (LHSCan == RHSCan)
1971 return LHS;
1972
1973 // If the qualifiers are different, the types aren't compatible
1974 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
1975 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
1976 return QualType();
1977
1978 Type::TypeClass LHSClass = LHSCan->getTypeClass();
1979 Type::TypeClass RHSClass = RHSCan->getTypeClass();
1980
Chris Lattner1adb8832008-01-14 05:45:46 +00001981 // We want to consider the two function types to be the same for these
1982 // comparisons, just force one to the other.
1983 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1984 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001985
1986 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00001987 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1988 LHSClass = Type::ConstantArray;
1989 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1990 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001991
Nate Begeman213541a2008-04-18 23:10:10 +00001992 // Canonicalize ExtVector -> Vector.
1993 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1994 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00001995
Chris Lattnerb0489812008-04-07 06:38:24 +00001996 // Consider qualified interfaces and interfaces the same.
1997 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1998 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00001999
Chris Lattnera36a61f2008-04-07 05:43:21 +00002000 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002001 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00002002 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00002003 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00002004 if (const PointerType *PT = RHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00002005 if (isObjCIdType(PT->getPointeeType()))
2006 return LHS;
Steve Naroff97341622008-06-04 15:07:33 +00002007 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002008 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff97341622008-06-04 15:07:33 +00002009 if (const PointerType *PT = LHS->getAsPointerType())
Eli Friedman3d815e72008-08-22 00:56:42 +00002010 if (isObjCIdType(PT->getPointeeType()))
2011 return RHS;
2012 }
2013
Chris Lattner1adb8832008-01-14 05:45:46 +00002014 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2015 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002016 if (const EnumType* ETy = LHS->getAsEnumType()) {
2017 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2018 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002019 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002020 if (const EnumType* ETy = RHS->getAsEnumType()) {
2021 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2022 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002023 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002024
Eli Friedman3d815e72008-08-22 00:56:42 +00002025 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002026 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002027
Steve Naroff4a746782008-01-09 22:43:08 +00002028 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002029 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002030 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002031 {
2032 // Merge two pointer types, while trying to preserve typedef info
2033 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2034 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2035 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2036 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002037 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2038 return LHS;
2039 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2040 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002041 return getPointerType(ResultType);
2042 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002043 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002044 {
2045 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2046 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2047 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2048 return QualType();
2049
2050 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2051 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2052 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2053 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002054 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2055 return LHS;
2056 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2057 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002058 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2059 ArrayType::ArraySizeModifier(), 0);
2060 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2061 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002062 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2063 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002064 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2065 return LHS;
2066 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2067 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002068 if (LVAT) {
2069 // FIXME: This isn't correct! But tricky to implement because
2070 // the array's size has to be the size of LHS, but the type
2071 // has to be different.
2072 return LHS;
2073 }
2074 if (RVAT) {
2075 // FIXME: This isn't correct! But tricky to implement because
2076 // the array's size has to be the size of RHS, but the type
2077 // has to be different.
2078 return RHS;
2079 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002080 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2081 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002082 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002083 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002084 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002085 return mergeFunctionTypes(LHS, RHS);
2086 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002087 // FIXME: Why are these compatible?
2088 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2089 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2090 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002091 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002092 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002093 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002094 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002095 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2096 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002097 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002098 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002099 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2100 // for checking assignment/comparison safety
2101 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002102 default:
2103 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002104 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002105 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002106}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002107
Chris Lattner5426bf62008-04-07 07:01:58 +00002108//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002109// Integer Predicates
2110//===----------------------------------------------------------------------===//
2111unsigned ASTContext::getIntWidth(QualType T) {
2112 if (T == BoolTy)
2113 return 1;
2114 // At the moment, only bool has padding bits
2115 return (unsigned)getTypeSize(T);
2116}
2117
2118QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2119 assert(T->isSignedIntegerType() && "Unexpected type");
2120 if (const EnumType* ETy = T->getAsEnumType())
2121 T = ETy->getDecl()->getIntegerType();
2122 const BuiltinType* BTy = T->getAsBuiltinType();
2123 assert (BTy && "Unexpected signed integer type");
2124 switch (BTy->getKind()) {
2125 case BuiltinType::Char_S:
2126 case BuiltinType::SChar:
2127 return UnsignedCharTy;
2128 case BuiltinType::Short:
2129 return UnsignedShortTy;
2130 case BuiltinType::Int:
2131 return UnsignedIntTy;
2132 case BuiltinType::Long:
2133 return UnsignedLongTy;
2134 case BuiltinType::LongLong:
2135 return UnsignedLongLongTy;
2136 default:
2137 assert(0 && "Unexpected signed integer type");
2138 return QualType();
2139 }
2140}
2141
2142
2143//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002144// Serialization Support
2145//===----------------------------------------------------------------------===//
2146
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002147/// Emit - Serialize an ASTContext object to Bitcode.
2148void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002149 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002150 S.EmitRef(SourceMgr);
2151 S.EmitRef(Target);
2152 S.EmitRef(Idents);
2153 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002154
Ted Kremenekfee04522007-10-31 22:44:07 +00002155 // Emit the size of the type vector so that we can reserve that size
2156 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002157 S.EmitInt(Types.size());
2158
Ted Kremenek03ed4402007-11-13 22:02:55 +00002159 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2160 I!=E;++I)
2161 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002162
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002163 S.EmitOwnedPtr(TUDecl);
2164
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002165 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002166}
2167
Ted Kremenek0f84c002007-11-13 00:25:37 +00002168ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002169
2170 // Read the language options.
2171 LangOptions LOpts;
2172 LOpts.Read(D);
2173
Ted Kremenekfee04522007-10-31 22:44:07 +00002174 SourceManager &SM = D.ReadRef<SourceManager>();
2175 TargetInfo &t = D.ReadRef<TargetInfo>();
2176 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2177 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002178
Ted Kremenekfee04522007-10-31 22:44:07 +00002179 unsigned size_reserve = D.ReadInt();
2180
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002181 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002182
Ted Kremenek03ed4402007-11-13 22:02:55 +00002183 for (unsigned i = 0; i < size_reserve; ++i)
2184 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002185
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002186 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2187
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002188 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002189
2190 return A;
2191}