blob: 4983d217c3e549fb64e10b71bb0385c77524608a [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),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000036 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000037{
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
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000185 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000186 InitBuiltinType(OverloadTy, BuiltinType::Overload);
187
188 // Placeholder type for type-dependent expressions whose type is
189 // completely unknown. No code should ever check a type against
190 // DependentTy and users should never see it; however, it is here to
191 // help diagnose failures to properly check for type-dependent
192 // expressions.
193 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // C99 6.2.5p11.
196 FloatComplexTy = getComplexType(FloatTy);
197 DoubleComplexTy = getComplexType(DoubleTy);
198 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000199
Steve Naroff7e219e42007-10-15 14:41:52 +0000200 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000202 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000203 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000204 ClassStructType = 0;
205
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000206 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000207
208 // void * type
209 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Chris Lattner464175b2007-07-18 17:52:12 +0000212//===----------------------------------------------------------------------===//
213// Type Sizing and Analysis
214//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000215
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000216/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
217/// scalar floating point type.
218const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
219 const BuiltinType *BT = T->getAsBuiltinType();
220 assert(BT && "Not a floating point type!");
221 switch (BT->getKind()) {
222 default: assert(0 && "Not a floating point type!");
223 case BuiltinType::Float: return Target.getFloatFormat();
224 case BuiltinType::Double: return Target.getDoubleFormat();
225 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
226 }
227}
228
229
Chris Lattnera7674d82007-07-13 22:13:22 +0000230/// getTypeSize - Return the size of the specified type, in bits. This method
231/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000232std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000233ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000234 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000235 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000236 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000237 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000238 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000239 case Type::FunctionNoProto:
240 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000241 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000242 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000243 case Type::VariableArray:
244 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000245 case Type::DependentSizedArray:
246 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000247 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000248 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000249
Chris Lattner98be4942008-03-05 18:54:05 +0000250 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000251 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000252 Align = EltInfo.second;
253 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000254 }
Nate Begeman213541a2008-04-18 23:10:10 +0000255 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000256 case Type::Vector: {
257 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000258 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000259 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000260 // FIXME: This isn't right for unusual vectors
261 Align = Width;
Chris Lattner030d8842007-07-19 22:06:24 +0000262 break;
263 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000264
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000265 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000266 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000267 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000268 case BuiltinType::Void:
269 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000270 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000271 Width = Target.getBoolWidth();
272 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000273 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000274 case BuiltinType::Char_S:
275 case BuiltinType::Char_U:
276 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000277 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000278 Width = Target.getCharWidth();
279 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000280 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000281 case BuiltinType::WChar:
282 Width = Target.getWCharWidth();
283 Align = Target.getWCharAlign();
284 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000285 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000286 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000287 Width = Target.getShortWidth();
288 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000289 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000290 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000291 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000292 Width = Target.getIntWidth();
293 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000294 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000295 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000296 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000297 Width = Target.getLongWidth();
298 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000299 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000300 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000301 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000302 Width = Target.getLongLongWidth();
303 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000304 break;
305 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000306 Width = Target.getFloatWidth();
307 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000308 break;
309 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000310 Width = Target.getDoubleWidth();
311 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000312 break;
313 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000314 Width = Target.getLongDoubleWidth();
315 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000316 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000317 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000318 break;
Christopher Lambebb97e92008-02-04 02:31:56 +0000319 case Type::ASQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000320 // FIXME: Pointers into different addr spaces could have different sizes and
321 // alignment requirements: getPointerInfo should take an AddrSpace.
322 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000323 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000324 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000325 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000326 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000327 case Type::BlockPointer: {
328 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
329 Width = Target.getPointerWidth(AS);
330 Align = Target.getPointerAlign(AS);
331 break;
332 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000333 case Type::Pointer: {
334 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000335 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000336 Align = Target.getPointerAlign(AS);
337 break;
338 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000339 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000340 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000341 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000342 // FIXME: This is wrong for struct layout: a reference in a struct has
343 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000344 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner5d2a6302007-07-18 18:26:58 +0000345
346 case Type::Complex: {
347 // Complex types have the same alignment as their elements, but twice the
348 // size.
349 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000350 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000351 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000352 Align = EltInfo.second;
353 break;
354 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000355 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000356 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000357 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
358 Width = Layout.getSize();
359 Align = Layout.getAlignment();
360 break;
361 }
Chris Lattner71763312008-04-06 22:05:18 +0000362 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000363 const TagType *TT = cast<TagType>(T);
364
365 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000366 Width = 1;
367 Align = 1;
368 break;
369 }
370
Daniel Dunbar1d751182008-11-08 05:48:37 +0000371 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000372 return getTypeInfo(ET->getDecl()->getIntegerType());
373
Daniel Dunbar1d751182008-11-08 05:48:37 +0000374 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000375 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
376 Width = Layout.getSize();
377 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000378 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000379 }
Chris Lattner71763312008-04-06 22:05:18 +0000380 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000381
Chris Lattner464175b2007-07-18 17:52:12 +0000382 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000383 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000384}
385
Devang Patel8b277042008-06-04 21:22:16 +0000386/// LayoutField - Field layout.
387void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000388 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000389 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000390 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000391 uint64_t FieldOffset = IsUnion ? 0 : Size;
392 uint64_t FieldSize;
393 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000394
395 // FIXME: Should this override struct packing? Probably we want to
396 // take the minimum?
397 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
398 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000399
400 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
401 // TODO: Need to check this algorithm on other targets!
402 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000403 FieldSize =
404 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000405
406 std::pair<uint64_t, unsigned> FieldInfo =
407 Context.getTypeInfo(FD->getType());
408 uint64_t TypeSize = FieldInfo.first;
409
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000410 // Determine the alignment of this bitfield. The packing
411 // attributes define a maximum and the alignment attribute defines
412 // a minimum.
413 // FIXME: What is the right behavior when the specified alignment
414 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000415 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000416 if (FieldPacking)
417 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000418 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
419 FieldAlign = std::max(FieldAlign, AA->getAlignment());
420
421 // Check if we need to add padding to give the field the correct
422 // alignment.
423 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
424 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
425
426 // Padding members don't affect overall alignment
427 if (!FD->getIdentifier())
428 FieldAlign = 1;
429 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000430 if (FD->getType()->isIncompleteArrayType()) {
431 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000432 // query getTypeInfo about these, so we figure it out here.
433 // Flexible array members don't have any size, but they
434 // have to be aligned appropriately for their element type.
435 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000436 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000437 FieldAlign = Context.getTypeAlign(ATy->getElementType());
438 } else {
439 std::pair<uint64_t, unsigned> FieldInfo =
440 Context.getTypeInfo(FD->getType());
441 FieldSize = FieldInfo.first;
442 FieldAlign = FieldInfo.second;
443 }
444
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000445 // Determine the alignment of this bitfield. The packing
446 // attributes define a maximum and the alignment attribute defines
447 // a minimum. Additionally, the packing alignment must be at least
448 // a byte for non-bitfields.
449 //
450 // FIXME: What is the right behavior when the specified alignment
451 // is smaller than the specified packing?
452 if (FieldPacking)
453 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000454 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
455 FieldAlign = std::max(FieldAlign, AA->getAlignment());
456
457 // Round up the current record size to the field's alignment boundary.
458 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
459 }
460
461 // Place this field at the current location.
462 FieldOffsets[FieldNo] = FieldOffset;
463
464 // Reserve space for this field.
465 if (IsUnion) {
466 Size = std::max(Size, FieldSize);
467 } else {
468 Size = FieldOffset + FieldSize;
469 }
470
471 // Remember max struct/class alignment.
472 Alignment = std::max(Alignment, FieldAlign);
473}
474
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000475static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
476 std::vector<FieldDecl*> &Fields) {
477 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
478 if (SuperClass)
479 CollectObjCIvars(SuperClass, Fields);
480 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
481 E = OI->ivar_end(); I != E; ++I) {
482 ObjCIvarDecl *IVDecl = (*I);
483 if (!IVDecl->isInvalidDecl())
484 Fields.push_back(cast<FieldDecl>(IVDecl));
485 }
486}
487
488/// addRecordToClass - produces record info. for the class for its
489/// ivars and all those inherited.
490///
491const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
492{
493 const RecordDecl *&RD = ASTRecordForInterface[D];
494 if (RD)
495 return RD;
496 std::vector<FieldDecl*> RecFields;
497 CollectObjCIvars(D, RecFields);
498 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
499 D->getLocation(),
500 D->getIdentifier());
501 /// FIXME! Can do collection of ivars and adding to the record while
502 /// doing it.
503 for (unsigned int i = 0; i != RecFields.size(); i++) {
504 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
505 RecFields[i]->getLocation(),
506 RecFields[i]->getIdentifier(),
507 RecFields[i]->getType(),
508 RecFields[i]->getBitWidth(), false, 0);
509 NewRD->addDecl(*this, Field);
510 }
511 NewRD->completeDefinition(*this);
512 RD = NewRD;
513 return RD;
514}
Devang Patel44a3dde2008-06-04 21:54:36 +0000515
Chris Lattner61710852008-10-05 17:34:18 +0000516/// getASTObjcInterfaceLayout - Get or compute information about the layout of
517/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000518/// position information.
519const ASTRecordLayout &
520ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
521 // Look up this layout, if already laid out, return what we have.
522 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
523 if (Entry) return *Entry;
524
525 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
526 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000527 ASTRecordLayout *NewEntry = NULL;
528 unsigned FieldCount = D->ivar_size();
529 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
530 FieldCount++;
531 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
532 unsigned Alignment = SL.getAlignment();
533 uint64_t Size = SL.getSize();
534 NewEntry = new ASTRecordLayout(Size, Alignment);
535 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000536 // Super class is at the beginning of the layout.
537 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000538 } else {
539 NewEntry = new ASTRecordLayout();
540 NewEntry->InitializeLayout(FieldCount);
541 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000542 Entry = NewEntry;
543
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000544 unsigned StructPacking = 0;
545 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
546 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000547
548 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
549 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
550 AA->getAlignment()));
551
552 // Layout each ivar sequentially.
553 unsigned i = 0;
554 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
555 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
556 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000557 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000558 }
559
560 // Finally, round the size of the total struct up to the alignment of the
561 // struct itself.
562 NewEntry->FinalizeLayout();
563 return *NewEntry;
564}
565
Devang Patel88a981b2007-11-01 19:11:01 +0000566/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000567/// specified record (struct/union/class), which indicates its size and field
568/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000569const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000570 D = D->getDefinition(*this);
571 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000572
Chris Lattner464175b2007-07-18 17:52:12 +0000573 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000574 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000575 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000576
Devang Patel88a981b2007-11-01 19:11:01 +0000577 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
578 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
579 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000580 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000581
Douglas Gregore267ff32008-12-11 20:41:00 +0000582 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000583 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000584 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000585
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000586 unsigned StructPacking = 0;
587 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
588 StructPacking = PA->getAlignment();
589
Eli Friedman4bd998b2008-05-30 09:31:38 +0000590 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000591 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
592 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000593
Eli Friedman4bd998b2008-05-30 09:31:38 +0000594 // Layout each field, for now, just sequentially, respecting alignment. In
595 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000596 unsigned FieldIdx = 0;
Douglas Gregora4c46df2008-12-11 17:59:21 +0000597 for (RecordDecl::field_const_iterator Field = D->field_begin(),
598 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000599 Field != FieldEnd; (void)++Field, ++FieldIdx)
600 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000601
602 // Finally, round the size of the total struct up to the alignment of the
603 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000604 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000605 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000606}
607
Chris Lattnera7674d82007-07-13 22:13:22 +0000608//===----------------------------------------------------------------------===//
609// Type creation/memoization methods
610//===----------------------------------------------------------------------===//
611
Christopher Lambebb97e92008-02-04 02:31:56 +0000612QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000613 QualType CanT = getCanonicalType(T);
614 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000615 return T;
616
617 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
618 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000619 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000620 "Type is already address space qualified");
621
622 // Check if we've already instantiated an address space qual'd type of this
623 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000624 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000625 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000626 void *InsertPos = 0;
627 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
628 return QualType(ASQy, 0);
629
630 // If the base type isn't canonical, this won't be a canonical type either,
631 // so fill in the canonical type field.
632 QualType Canonical;
633 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000634 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000635
636 // Get the new insert position for the node we care about.
637 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000638 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000639 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000640 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000641 ASQualTypes.InsertNode(New, InsertPos);
642 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000643 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000644}
645
Chris Lattnera7674d82007-07-13 22:13:22 +0000646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647/// getComplexType - Return the uniqued reference to the type for a complex
648/// number with the specified element type.
649QualType ASTContext::getComplexType(QualType T) {
650 // Unique pointers, to guarantee there is only one pointer of a particular
651 // structure.
652 llvm::FoldingSetNodeID ID;
653 ComplexType::Profile(ID, T);
654
655 void *InsertPos = 0;
656 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
657 return QualType(CT, 0);
658
659 // If the pointee type isn't canonical, this won't be a canonical type either,
660 // so fill in the canonical type field.
661 QualType Canonical;
662 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000663 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000664
665 // Get the new insert position for the node we care about.
666 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000667 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 }
669 ComplexType *New = new ComplexType(T, Canonical);
670 Types.push_back(New);
671 ComplexTypes.InsertNode(New, InsertPos);
672 return QualType(New, 0);
673}
674
675
676/// getPointerType - Return the uniqued reference to the type for a pointer to
677/// the specified type.
678QualType ASTContext::getPointerType(QualType T) {
679 // Unique pointers, to guarantee there is only one pointer of a particular
680 // structure.
681 llvm::FoldingSetNodeID ID;
682 PointerType::Profile(ID, T);
683
684 void *InsertPos = 0;
685 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
686 return QualType(PT, 0);
687
688 // If the pointee type isn't canonical, this won't be a canonical type either,
689 // so fill in the canonical type field.
690 QualType Canonical;
691 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000692 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
694 // Get the new insert position for the node we care about.
695 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000696 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 }
698 PointerType *New = new PointerType(T, Canonical);
699 Types.push_back(New);
700 PointerTypes.InsertNode(New, InsertPos);
701 return QualType(New, 0);
702}
703
Steve Naroff5618bd42008-08-27 16:04:49 +0000704/// getBlockPointerType - Return the uniqued reference to the type for
705/// a pointer to the specified block.
706QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000707 assert(T->isFunctionType() && "block of function types only");
708 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000709 // structure.
710 llvm::FoldingSetNodeID ID;
711 BlockPointerType::Profile(ID, T);
712
713 void *InsertPos = 0;
714 if (BlockPointerType *PT =
715 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
716 return QualType(PT, 0);
717
Steve Naroff296e8d52008-08-28 19:20:44 +0000718 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000719 // type either so fill in the canonical type field.
720 QualType Canonical;
721 if (!T->isCanonical()) {
722 Canonical = getBlockPointerType(getCanonicalType(T));
723
724 // Get the new insert position for the node we care about.
725 BlockPointerType *NewIP =
726 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000727 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000728 }
729 BlockPointerType *New = new BlockPointerType(T, Canonical);
730 Types.push_back(New);
731 BlockPointerTypes.InsertNode(New, InsertPos);
732 return QualType(New, 0);
733}
734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735/// getReferenceType - Return the uniqued reference to the type for a reference
736/// to the specified type.
737QualType ASTContext::getReferenceType(QualType T) {
738 // Unique pointers, to guarantee there is only one pointer of a particular
739 // structure.
740 llvm::FoldingSetNodeID ID;
741 ReferenceType::Profile(ID, T);
742
743 void *InsertPos = 0;
744 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
745 return QualType(RT, 0);
746
747 // If the referencee type isn't canonical, this won't be a canonical type
748 // either, so fill in the canonical type field.
749 QualType Canonical;
750 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000751 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
753 // Get the new insert position for the node we care about.
754 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000755 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 }
757
758 ReferenceType *New = new ReferenceType(T, Canonical);
759 Types.push_back(New);
760 ReferenceTypes.InsertNode(New, InsertPos);
761 return QualType(New, 0);
762}
763
Steve Narofffb22d962007-08-30 01:06:46 +0000764/// getConstantArrayType - Return the unique reference to the type for an
765/// array of the specified element type.
766QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000767 const llvm::APInt &ArySize,
768 ArrayType::ArraySizeModifier ASM,
769 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000771 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000772
773 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000774 if (ConstantArrayType *ATP =
775 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 return QualType(ATP, 0);
777
778 // If the element type isn't canonical, this won't be a canonical type either,
779 // so fill in the canonical type field.
780 QualType Canonical;
781 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000782 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000783 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000785 ConstantArrayType *NewIP =
786 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000787 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 }
789
Steve Naroffc9406122007-08-30 18:10:14 +0000790 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
791 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000792 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 Types.push_back(New);
794 return QualType(New, 0);
795}
796
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000797/// getVariableArrayType - Returns a non-unique reference to the type for a
798/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000799QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
800 ArrayType::ArraySizeModifier ASM,
801 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000802 // Since we don't unique expressions, it isn't possible to unique VLA's
803 // that have an expression provided for their size.
804
805 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
806 ASM, EltTypeQuals);
807
808 VariableArrayTypes.push_back(New);
809 Types.push_back(New);
810 return QualType(New, 0);
811}
812
Douglas Gregor898574e2008-12-05 23:32:09 +0000813/// getDependentSizedArrayType - Returns a non-unique reference to
814/// the type for a dependently-sized array of the specified element
815/// type. FIXME: We will need these to be uniqued, or at least
816/// comparable, at some point.
817QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
818 ArrayType::ArraySizeModifier ASM,
819 unsigned EltTypeQuals) {
820 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
821 "Size must be type- or value-dependent!");
822
823 // Since we don't unique expressions, it isn't possible to unique
824 // dependently-sized array types.
825
826 DependentSizedArrayType *New
827 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
828 ASM, EltTypeQuals);
829
830 DependentSizedArrayTypes.push_back(New);
831 Types.push_back(New);
832 return QualType(New, 0);
833}
834
Eli Friedmanc5773c42008-02-15 18:16:39 +0000835QualType ASTContext::getIncompleteArrayType(QualType EltTy,
836 ArrayType::ArraySizeModifier ASM,
837 unsigned EltTypeQuals) {
838 llvm::FoldingSetNodeID ID;
839 IncompleteArrayType::Profile(ID, EltTy);
840
841 void *InsertPos = 0;
842 if (IncompleteArrayType *ATP =
843 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
844 return QualType(ATP, 0);
845
846 // If the element type isn't canonical, this won't be a canonical type
847 // either, so fill in the canonical type field.
848 QualType Canonical;
849
850 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000851 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000852 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000853
854 // Get the new insert position for the node we care about.
855 IncompleteArrayType *NewIP =
856 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000857 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000858 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000859
860 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
861 ASM, EltTypeQuals);
862
863 IncompleteArrayTypes.InsertNode(New, InsertPos);
864 Types.push_back(New);
865 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000866}
867
Steve Naroff73322922007-07-18 18:00:27 +0000868/// getVectorType - Return the unique reference to a vector type of
869/// the specified element type and size. VectorType must be a built-in type.
870QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 BuiltinType *baseType;
872
Chris Lattnerf52ab252008-04-06 22:59:24 +0000873 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000874 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000875
876 // Check if we've already instantiated a vector of this type.
877 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000878 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 void *InsertPos = 0;
880 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
881 return QualType(VTP, 0);
882
883 // If the element type isn't canonical, this won't be a canonical type either,
884 // so fill in the canonical type field.
885 QualType Canonical;
886 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000887 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000888
889 // Get the new insert position for the node we care about.
890 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000891 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 }
893 VectorType *New = new VectorType(vecType, NumElts, Canonical);
894 VectorTypes.InsertNode(New, InsertPos);
895 Types.push_back(New);
896 return QualType(New, 0);
897}
898
Nate Begeman213541a2008-04-18 23:10:10 +0000899/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000900/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000901QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000902 BuiltinType *baseType;
903
Chris Lattnerf52ab252008-04-06 22:59:24 +0000904 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000905 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000906
907 // Check if we've already instantiated a vector of this type.
908 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000909 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000910 void *InsertPos = 0;
911 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
912 return QualType(VTP, 0);
913
914 // If the element type isn't canonical, this won't be a canonical type either,
915 // so fill in the canonical type field.
916 QualType Canonical;
917 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000918 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000919
920 // Get the new insert position for the node we care about.
921 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000922 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +0000923 }
Nate Begeman213541a2008-04-18 23:10:10 +0000924 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000925 VectorTypes.InsertNode(New, InsertPos);
926 Types.push_back(New);
927 return QualType(New, 0);
928}
929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
931///
932QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
933 // Unique functions, to guarantee there is only one function of a particular
934 // structure.
935 llvm::FoldingSetNodeID ID;
936 FunctionTypeNoProto::Profile(ID, ResultTy);
937
938 void *InsertPos = 0;
939 if (FunctionTypeNoProto *FT =
940 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
941 return QualType(FT, 0);
942
943 QualType Canonical;
944 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000945 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000946
947 // Get the new insert position for the node we care about.
948 FunctionTypeNoProto *NewIP =
949 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000950 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 }
952
953 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
954 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000955 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 return QualType(New, 0);
957}
958
959/// getFunctionType - Return a normal function type with a typed argument
960/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +0000961QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000962 unsigned NumArgs, bool isVariadic,
963 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 // Unique functions, to guarantee there is only one function of a particular
965 // structure.
966 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000967 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
968 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000969
970 void *InsertPos = 0;
971 if (FunctionTypeProto *FTP =
972 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
973 return QualType(FTP, 0);
974
975 // Determine whether the type being created is already canonical or not.
976 bool isCanonical = ResultTy->isCanonical();
977 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
978 if (!ArgArray[i]->isCanonical())
979 isCanonical = false;
980
981 // If this type isn't canonical, get the canonical version of it.
982 QualType Canonical;
983 if (!isCanonical) {
984 llvm::SmallVector<QualType, 16> CanonicalArgs;
985 CanonicalArgs.reserve(NumArgs);
986 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000987 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000988
Chris Lattnerf52ab252008-04-06 22:59:24 +0000989 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000991 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000992
993 // Get the new insert position for the node we care about.
994 FunctionTypeProto *NewIP =
995 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000996 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 }
998
999 // FunctionTypeProto objects are not allocated with new because they have a
1000 // variable size array (for parameter types) at the end of them.
1001 FunctionTypeProto *FTP =
1002 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +00001003 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001005 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 Types.push_back(FTP);
1007 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1008 return QualType(FTP, 0);
1009}
1010
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001011/// getTypeDeclType - Return the unique reference to the type for the
1012/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001013QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001014 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001015 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1016
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001017 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001018 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +00001019 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1020 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001021 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001022 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001023
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001024 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001025 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1026 : new CXXRecordType(CXXRecord);
1027 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001028 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001029 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1030 : new RecordType(Record);
1031 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001032 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor7df7b6b2008-12-15 16:32:14 +00001033 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
1034 : new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001035 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001036 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001037
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001038 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001039 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001040}
1041
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001042void ASTContext::setTagDefinition(TagDecl* D) {
1043 assert (D->isDefinition());
Douglas Gregor7df7b6b2008-12-15 16:32:14 +00001044 cast<TagType>(D->TypeForDecl)->decl = D;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001045}
1046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047/// getTypedefType - Return the unique reference to the type for the
1048/// specified typename decl.
1049QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1050 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1051
Chris Lattnerf52ab252008-04-06 22:59:24 +00001052 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001053 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 Types.push_back(Decl->TypeForDecl);
1055 return QualType(Decl->TypeForDecl, 0);
1056}
1057
Douglas Gregor72c3f312008-12-05 18:15:24 +00001058/// getTemplateTypeParmType - Return the unique reference to the type
1059/// for the specified template type parameter declaration.
1060QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1061 if (!Decl->TypeForDecl) {
1062 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1063 Types.push_back(Decl->TypeForDecl);
1064 }
1065 return QualType(Decl->TypeForDecl, 0);
1066}
1067
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001068/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001069/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001071 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1072
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001073 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001074 Types.push_back(Decl->TypeForDecl);
1075 return QualType(Decl->TypeForDecl, 0);
1076}
1077
Chris Lattner88cb27a2008-04-07 04:56:42 +00001078/// CmpProtocolNames - Comparison predicate for sorting protocols
1079/// alphabetically.
1080static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1081 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001082 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001083}
1084
1085static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1086 unsigned &NumProtocols) {
1087 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1088
1089 // Sort protocols, keyed by name.
1090 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1091
1092 // Remove duplicates.
1093 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1094 NumProtocols = ProtocolsEnd-Protocols;
1095}
1096
1097
Chris Lattner065f0d72008-04-07 04:44:08 +00001098/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1099/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001100QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1101 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001102 // Sort the protocol list alphabetically to canonicalize it.
1103 SortAndUniqueProtocols(Protocols, NumProtocols);
1104
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001105 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001106 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001107
1108 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001109 if (ObjCQualifiedInterfaceType *QT =
1110 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001111 return QualType(QT, 0);
1112
1113 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 ObjCQualifiedInterfaceType *QType =
1115 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001116 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001118 return QualType(QType, 0);
1119}
1120
Chris Lattner88cb27a2008-04-07 04:56:42 +00001121/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1122/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001123QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001124 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001125 // Sort the protocol list alphabetically to canonicalize it.
1126 SortAndUniqueProtocols(Protocols, NumProtocols);
1127
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001128 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001129 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001130
1131 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001133 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001134 return QualType(QT, 0);
1135
1136 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001137 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001138 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001139 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001140 return QualType(QType, 0);
1141}
1142
Steve Naroff9752f252007-08-01 18:02:17 +00001143/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1144/// TypeOfExpr AST's (since expression's are never shared). For example,
1145/// multiple declarations that refer to "typeof(x)" all contain different
1146/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1147/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001148QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001149 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001150 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1151 Types.push_back(toe);
1152 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001153}
1154
Steve Naroff9752f252007-08-01 18:02:17 +00001155/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1156/// TypeOfType AST's. The only motivation to unique these nodes would be
1157/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1158/// an issue. This doesn't effect the type checker, since it operates
1159/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001160QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001161 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001162 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1163 Types.push_back(tot);
1164 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001165}
1166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167/// getTagDeclType - Return the unique reference to the type for the
1168/// specified TagDecl (struct/union/class/enum) decl.
1169QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001170 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001171 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001172}
1173
1174/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1175/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1176/// needs to agree with the definition in <stddef.h>.
1177QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001178 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001179}
1180
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001181/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001182/// width of characters in wide strings, The value is target dependent and
1183/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001184QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001185 if (LangOpts.CPlusPlus)
1186 return WCharTy;
1187
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001188 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1189 // wide-character type?
1190 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001191}
1192
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001193/// getSignedWCharType - Return the type of "signed wchar_t".
1194/// Used when in C++, as a GCC extension.
1195QualType ASTContext::getSignedWCharType() const {
1196 // FIXME: derive from "Target" ?
1197 return WCharTy;
1198}
1199
1200/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1201/// Used when in C++, as a GCC extension.
1202QualType ASTContext::getUnsignedWCharType() const {
1203 // FIXME: derive from "Target" ?
1204 return UnsignedIntTy;
1205}
1206
Chris Lattner8b9023b2007-07-13 03:05:23 +00001207/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1208/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1209QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001210 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001211}
1212
Chris Lattnere6327742008-04-02 05:18:44 +00001213//===----------------------------------------------------------------------===//
1214// Type Operators
1215//===----------------------------------------------------------------------===//
1216
Chris Lattner77c96472008-04-06 22:41:35 +00001217/// getCanonicalType - Return the canonical (structural) type corresponding to
1218/// the specified potentially non-canonical type. The non-canonical version
1219/// of a type may have many "decorated" versions of types. Decorators can
1220/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1221/// to be free of any of these, allowing two canonical types to be compared
1222/// for exact equality with a simple pointer comparison.
1223QualType ASTContext::getCanonicalType(QualType T) {
1224 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001225
1226 // If the result has type qualifiers, make sure to canonicalize them as well.
1227 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1228 if (TypeQuals == 0) return CanType;
1229
1230 // If the type qualifiers are on an array type, get the canonical type of the
1231 // array with the qualifiers applied to the element type.
1232 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1233 if (!AT)
1234 return CanType.getQualifiedType(TypeQuals);
1235
1236 // Get the canonical version of the element with the extra qualifiers on it.
1237 // This can recursively sink qualifiers through multiple levels of arrays.
1238 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1239 NewEltTy = getCanonicalType(NewEltTy);
1240
1241 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1242 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1243 CAT->getIndexTypeQualifier());
1244 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1245 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1246 IAT->getIndexTypeQualifier());
1247
Douglas Gregor898574e2008-12-05 23:32:09 +00001248 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1249 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1250 DSAT->getSizeModifier(),
1251 DSAT->getIndexTypeQualifier());
1252
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001253 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1254 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1255 VAT->getSizeModifier(),
1256 VAT->getIndexTypeQualifier());
1257}
1258
1259
1260const ArrayType *ASTContext::getAsArrayType(QualType T) {
1261 // Handle the non-qualified case efficiently.
1262 if (T.getCVRQualifiers() == 0) {
1263 // Handle the common positive case fast.
1264 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1265 return AT;
1266 }
1267
1268 // Handle the common negative case fast, ignoring CVR qualifiers.
1269 QualType CType = T->getCanonicalTypeInternal();
1270
1271 // Make sure to look through type qualifiers (like ASQuals) for the negative
1272 // test.
1273 if (!isa<ArrayType>(CType) &&
1274 !isa<ArrayType>(CType.getUnqualifiedType()))
1275 return 0;
1276
1277 // Apply any CVR qualifiers from the array type to the element type. This
1278 // implements C99 6.7.3p8: "If the specification of an array type includes
1279 // any type qualifiers, the element type is so qualified, not the array type."
1280
1281 // If we get here, we either have type qualifiers on the type, or we have
1282 // sugar such as a typedef in the way. If we have type qualifiers on the type
1283 // we must propagate them down into the elemeng type.
1284 unsigned CVRQuals = T.getCVRQualifiers();
1285 unsigned AddrSpace = 0;
1286 Type *Ty = T.getTypePtr();
1287
1288 // Rip through ASQualType's and typedefs to get to a concrete type.
1289 while (1) {
1290 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1291 AddrSpace = ASQT->getAddressSpace();
1292 Ty = ASQT->getBaseType();
1293 } else {
1294 T = Ty->getDesugaredType();
1295 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1296 break;
1297 CVRQuals |= T.getCVRQualifiers();
1298 Ty = T.getTypePtr();
1299 }
1300 }
1301
1302 // If we have a simple case, just return now.
1303 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1304 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1305 return ATy;
1306
1307 // Otherwise, we have an array and we have qualifiers on it. Push the
1308 // qualifiers into the array element type and return a new array type.
1309 // Get the canonical version of the element with the extra qualifiers on it.
1310 // This can recursively sink qualifiers through multiple levels of arrays.
1311 QualType NewEltTy = ATy->getElementType();
1312 if (AddrSpace)
1313 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1314 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1315
1316 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1317 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1318 CAT->getSizeModifier(),
1319 CAT->getIndexTypeQualifier()));
1320 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1321 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1322 IAT->getSizeModifier(),
1323 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001324
Douglas Gregor898574e2008-12-05 23:32:09 +00001325 if (const DependentSizedArrayType *DSAT
1326 = dyn_cast<DependentSizedArrayType>(ATy))
1327 return cast<ArrayType>(
1328 getDependentSizedArrayType(NewEltTy,
1329 DSAT->getSizeExpr(),
1330 DSAT->getSizeModifier(),
1331 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001332
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001333 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1334 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1335 VAT->getSizeModifier(),
1336 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001337}
1338
1339
Chris Lattnere6327742008-04-02 05:18:44 +00001340/// getArrayDecayedType - Return the properly qualified result of decaying the
1341/// specified array type to a pointer. This operation is non-trivial when
1342/// handling typedefs etc. The canonical type of "T" must be an array type,
1343/// this returns a pointer to a properly qualified element of the array.
1344///
1345/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1346QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001347 // Get the element type with 'getAsArrayType' so that we don't lose any
1348 // typedefs in the element type of the array. This also handles propagation
1349 // of type qualifiers from the array type into the element type if present
1350 // (C99 6.7.3p8).
1351 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1352 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001353
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001354 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001355
1356 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001357 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001358}
1359
Reid Spencer5f016e22007-07-11 17:01:13 +00001360/// getFloatingRank - Return a relative rank for floating point types.
1361/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001362static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001363 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001365
Christopher Lambebb97e92008-02-04 02:31:56 +00001366 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001367 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001368 case BuiltinType::Float: return FloatRank;
1369 case BuiltinType::Double: return DoubleRank;
1370 case BuiltinType::LongDouble: return LongDoubleRank;
1371 }
1372}
1373
Steve Naroff716c7302007-08-27 01:41:48 +00001374/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1375/// point or a complex type (based on typeDomain/typeSize).
1376/// 'typeDomain' is a real floating point or complex type.
1377/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001378QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1379 QualType Domain) const {
1380 FloatingRank EltRank = getFloatingRank(Size);
1381 if (Domain->isComplexType()) {
1382 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001383 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001384 case FloatRank: return FloatComplexTy;
1385 case DoubleRank: return DoubleComplexTy;
1386 case LongDoubleRank: return LongDoubleComplexTy;
1387 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 }
Chris Lattner1361b112008-04-06 23:58:54 +00001389
1390 assert(Domain->isRealFloatingType() && "Unknown domain!");
1391 switch (EltRank) {
1392 default: assert(0 && "getFloatingRank(): illegal value for rank");
1393 case FloatRank: return FloatTy;
1394 case DoubleRank: return DoubleTy;
1395 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001396 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001397}
1398
Chris Lattner7cfeb082008-04-06 23:55:33 +00001399/// getFloatingTypeOrder - Compare the rank of the two specified floating
1400/// point types, ignoring the domain of the type (i.e. 'double' ==
1401/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1402/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001403int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1404 FloatingRank LHSR = getFloatingRank(LHS);
1405 FloatingRank RHSR = getFloatingRank(RHS);
1406
1407 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001408 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001409 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001410 return 1;
1411 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001412}
1413
Chris Lattnerf52ab252008-04-06 22:59:24 +00001414/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1415/// routine will assert if passed a built-in type that isn't an integer or enum,
1416/// or if it is not canonicalized.
1417static unsigned getIntegerRank(Type *T) {
1418 assert(T->isCanonical() && "T should be canonicalized");
1419 if (isa<EnumType>(T))
1420 return 4;
1421
1422 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001423 default: assert(0 && "getIntegerRank(): not a built-in integer");
1424 case BuiltinType::Bool:
1425 return 1;
1426 case BuiltinType::Char_S:
1427 case BuiltinType::Char_U:
1428 case BuiltinType::SChar:
1429 case BuiltinType::UChar:
1430 return 2;
1431 case BuiltinType::Short:
1432 case BuiltinType::UShort:
1433 return 3;
1434 case BuiltinType::Int:
1435 case BuiltinType::UInt:
1436 return 4;
1437 case BuiltinType::Long:
1438 case BuiltinType::ULong:
1439 return 5;
1440 case BuiltinType::LongLong:
1441 case BuiltinType::ULongLong:
1442 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001443 }
1444}
1445
Chris Lattner7cfeb082008-04-06 23:55:33 +00001446/// getIntegerTypeOrder - Returns the highest ranked integer type:
1447/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1448/// LHS < RHS, return -1.
1449int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001450 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1451 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001452 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001453
Chris Lattnerf52ab252008-04-06 22:59:24 +00001454 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1455 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001456
Chris Lattner7cfeb082008-04-06 23:55:33 +00001457 unsigned LHSRank = getIntegerRank(LHSC);
1458 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001459
Chris Lattner7cfeb082008-04-06 23:55:33 +00001460 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1461 if (LHSRank == RHSRank) return 0;
1462 return LHSRank > RHSRank ? 1 : -1;
1463 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001464
Chris Lattner7cfeb082008-04-06 23:55:33 +00001465 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1466 if (LHSUnsigned) {
1467 // If the unsigned [LHS] type is larger, return it.
1468 if (LHSRank >= RHSRank)
1469 return 1;
1470
1471 // If the signed type can represent all values of the unsigned type, it
1472 // wins. Because we are dealing with 2's complement and types that are
1473 // powers of two larger than each other, this is always safe.
1474 return -1;
1475 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001476
Chris Lattner7cfeb082008-04-06 23:55:33 +00001477 // If the unsigned [RHS] type is larger, return it.
1478 if (RHSRank >= LHSRank)
1479 return -1;
1480
1481 // If the signed type can represent all values of the unsigned type, it
1482 // wins. Because we are dealing with 2's complement and types that are
1483 // powers of two larger than each other, this is always safe.
1484 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001485}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001486
1487// getCFConstantStringType - Return the type used for constant CFStrings.
1488QualType ASTContext::getCFConstantStringType() {
1489 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001490 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001491 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001492 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001493 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001494
1495 // const int *isa;
1496 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001497 // int flags;
1498 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001499 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001500 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001501 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001502 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001503
Anders Carlsson71993dd2007-08-17 05:31:46 +00001504 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001505 for (unsigned i = 0; i < 4; ++i) {
1506 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1507 SourceLocation(), 0,
1508 FieldTypes[i], /*BitWidth=*/0,
1509 /*Mutable=*/false, /*PrevDecl=*/0);
1510 CFConstantStringTypeDecl->addDecl(*this, Field, true);
1511 }
1512
1513 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001514 }
1515
1516 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001517}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001518
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001519QualType ASTContext::getObjCFastEnumerationStateType()
1520{
1521 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001522 ObjCFastEnumerationStateTypeDecl =
1523 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1524 &Idents.get("__objcFastEnumerationState"));
1525
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001526 QualType FieldTypes[] = {
1527 UnsignedLongTy,
1528 getPointerType(ObjCIdType),
1529 getPointerType(UnsignedLongTy),
1530 getConstantArrayType(UnsignedLongTy,
1531 llvm::APInt(32, 5), ArrayType::Normal, 0)
1532 };
1533
Douglas Gregor44b43212008-12-11 16:49:14 +00001534 for (size_t i = 0; i < 4; ++i) {
1535 FieldDecl *Field = FieldDecl::Create(*this,
1536 ObjCFastEnumerationStateTypeDecl,
1537 SourceLocation(), 0,
1538 FieldTypes[i], /*BitWidth=*/0,
1539 /*Mutable=*/false, /*PrevDecl=*/0);
1540 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field, true);
1541 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001542
Douglas Gregor44b43212008-12-11 16:49:14 +00001543 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001544 }
1545
1546 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1547}
1548
Anders Carlssone8c49532007-10-29 06:33:42 +00001549// This returns true if a type has been typedefed to BOOL:
1550// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001551static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001552 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001553 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1554 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001555
1556 return false;
1557}
1558
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001560/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001562 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001563
1564 // Make all integer and enum types at least as large as an int
1565 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001566 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001567 // Treat arrays as pointers, since that's how they're passed in.
1568 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001569 sz = getTypeSize(VoidPtrTy);
1570 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001571}
1572
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001574/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001575void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001576 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001577 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001578 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001579 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001580 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001581 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001582 // Compute size of all parameters.
1583 // Start with computing size of a pointer in number of bytes.
1584 // FIXME: There might(should) be a better way of doing this computation!
1585 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001586 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001587 // The first two arguments (self and _cmd) are pointers; account for
1588 // their size.
1589 int ParmOffset = 2 * PtrSize;
1590 int NumOfParams = Decl->getNumParams();
1591 for (int i = 0; i < NumOfParams; i++) {
1592 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001593 int sz = getObjCEncodingTypeSize (PType);
1594 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001595 ParmOffset += sz;
1596 }
1597 S += llvm::utostr(ParmOffset);
1598 S += "@0:";
1599 S += llvm::utostr(PtrSize);
1600
1601 // Argument types.
1602 ParmOffset = 2 * PtrSize;
1603 for (int i = 0; i < NumOfParams; i++) {
1604 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001605 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001606 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001607 getObjCEncodingForTypeQualifier(
1608 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001609 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001610 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001612 }
1613}
1614
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001615/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1616/// method declaration. If non-NULL, Container must be either an
1617/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1618/// NULL when getting encodings for protocol properties.
1619void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1620 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001621 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001622 // Collect information from the property implementation decl(s).
1623 bool Dynamic = false;
1624 ObjCPropertyImplDecl *SynthesizePID = 0;
1625
1626 // FIXME: Duplicated code due to poor abstraction.
1627 if (Container) {
1628 if (const ObjCCategoryImplDecl *CID =
1629 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1630 for (ObjCCategoryImplDecl::propimpl_iterator
1631 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1632 ObjCPropertyImplDecl *PID = *i;
1633 if (PID->getPropertyDecl() == PD) {
1634 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1635 Dynamic = true;
1636 } else {
1637 SynthesizePID = PID;
1638 }
1639 }
1640 }
1641 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001642 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001643 for (ObjCCategoryImplDecl::propimpl_iterator
1644 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1645 ObjCPropertyImplDecl *PID = *i;
1646 if (PID->getPropertyDecl() == PD) {
1647 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1648 Dynamic = true;
1649 } else {
1650 SynthesizePID = PID;
1651 }
1652 }
1653 }
1654 }
1655 }
1656
1657 // FIXME: This is not very efficient.
1658 S = "T";
1659
1660 // Encode result type.
1661 // FIXME: GCC uses a generating_property_type_encoding mode during
1662 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001663 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001664
1665 if (PD->isReadOnly()) {
1666 S += ",R";
1667 } else {
1668 switch (PD->getSetterKind()) {
1669 case ObjCPropertyDecl::Assign: break;
1670 case ObjCPropertyDecl::Copy: S += ",C"; break;
1671 case ObjCPropertyDecl::Retain: S += ",&"; break;
1672 }
1673 }
1674
1675 // It really isn't clear at all what this means, since properties
1676 // are "dynamic by default".
1677 if (Dynamic)
1678 S += ",D";
1679
1680 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1681 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001682 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001683 }
1684
1685 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1686 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001687 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001688 }
1689
1690 if (SynthesizePID) {
1691 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1692 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001693 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001694 }
1695
1696 // FIXME: OBJCGC: weak & strong
1697}
1698
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001699void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001700 bool NameFields) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001701 // We follow the behavior of gcc, expanding structures which are
1702 // directly pointed to, and expanding embedded structures. Note that
1703 // these rules are sufficient to prevent recursive encoding of the
1704 // same type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001705 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001706}
1707
1708void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1709 bool ExpandPointedToStructures,
1710 bool ExpandStructures,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001711 bool NameFields) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001712 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001713 char encoding;
1714 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001715 default: assert(0 && "Unhandled builtin type kind");
1716 case BuiltinType::Void: encoding = 'v'; break;
1717 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001718 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001719 case BuiltinType::UChar: encoding = 'C'; break;
1720 case BuiltinType::UShort: encoding = 'S'; break;
1721 case BuiltinType::UInt: encoding = 'I'; break;
1722 case BuiltinType::ULong: encoding = 'L'; break;
1723 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001724 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001725 case BuiltinType::SChar: encoding = 'c'; break;
1726 case BuiltinType::Short: encoding = 's'; break;
1727 case BuiltinType::Int: encoding = 'i'; break;
1728 case BuiltinType::Long: encoding = 'l'; break;
1729 case BuiltinType::LongLong: encoding = 'q'; break;
1730 case BuiltinType::Float: encoding = 'f'; break;
1731 case BuiltinType::Double: encoding = 'd'; break;
1732 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001733 }
1734
1735 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001736 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001737 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001738 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001739 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1740 ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001741 ExpandStructures, NameFields);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001742 }
1743 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001744 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001745 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001746 S += '@';
1747 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001748 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001749 S += '#';
1750 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001751 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001752 S += ':';
1753 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001754 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001755
1756 if (PointeeTy->isCharType()) {
1757 // char pointer types should be encoded as '*' unless it is a
1758 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001759 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001760 S += '*';
1761 return;
1762 }
1763 }
1764
1765 S += '^';
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001766 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001767 false, ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001768 NameFields);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001769 } else if (const ArrayType *AT =
1770 // Ignore type qualifiers etc.
1771 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001772 S += '[';
1773
1774 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1775 S += llvm::utostr(CAT->getSize().getZExtValue());
1776 else
1777 assert(0 && "Unhandled array type!");
1778
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001779 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001780 false, ExpandStructures, NameFields);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001781 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001782 } else if (T->getAsFunctionType()) {
1783 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001784 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001785 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001786 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001787 // Anonymous structures print as '?'
1788 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1789 S += II->getName();
1790 } else {
1791 S += '?';
1792 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001793 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001794 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00001795 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1796 FieldEnd = RDecl->field_end();
1797 Field != FieldEnd; ++Field) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001798 if (NameFields) {
1799 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00001800 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001801 S += '"';
1802 }
1803
1804 // Special case bit-fields.
Douglas Gregor44b43212008-12-11 16:49:14 +00001805 if (const Expr *E = Field->getBitWidth()) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001806 // FIXME: Fix constness.
1807 ASTContext *Ctx = const_cast<ASTContext*>(this);
1808 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1809 // FIXME: Obj-C is losing information about the type size
1810 // here. Investigate if this is a problem.
1811 S += 'b';
1812 S += llvm::utostr(N);
1813 } else {
Douglas Gregor44b43212008-12-11 16:49:14 +00001814 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1815 NameFields);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001816 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001817 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001818 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001819 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001820 } else if (T->isEnumeralType()) {
1821 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001822 } else if (T->isBlockPointerType()) {
1823 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001824 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001825 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001826}
1827
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001828void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001829 std::string& S) const {
1830 if (QT & Decl::OBJC_TQ_In)
1831 S += 'n';
1832 if (QT & Decl::OBJC_TQ_Inout)
1833 S += 'N';
1834 if (QT & Decl::OBJC_TQ_Out)
1835 S += 'o';
1836 if (QT & Decl::OBJC_TQ_Bycopy)
1837 S += 'O';
1838 if (QT & Decl::OBJC_TQ_Byref)
1839 S += 'R';
1840 if (QT & Decl::OBJC_TQ_Oneway)
1841 S += 'V';
1842}
1843
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001844void ASTContext::setBuiltinVaListType(QualType T)
1845{
1846 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1847
1848 BuiltinVaListType = T;
1849}
1850
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001851void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001852{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001853 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001854
1855 // typedef struct objc_object *id;
1856 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1857 assert(ptr && "'id' incorrectly typed");
1858 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1859 assert(rec && "'id' incorrectly typed");
1860 IdStructType = rec;
1861}
1862
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001863void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001864{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001865 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001866
1867 // typedef struct objc_selector *SEL;
1868 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1869 assert(ptr && "'SEL' incorrectly typed");
1870 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1871 assert(rec && "'SEL' incorrectly typed");
1872 SelStructType = rec;
1873}
1874
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001875void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001876{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001877 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001878}
1879
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001880void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001881{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001882 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001883
1884 // typedef struct objc_class *Class;
1885 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1886 assert(ptr && "'Class' incorrectly typed");
1887 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1888 assert(rec && "'Class' incorrectly typed");
1889 ClassStructType = rec;
1890}
1891
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001892void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1893 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001894 "'NSConstantString' type already set!");
1895
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001896 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001897}
1898
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001899/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00001900/// TargetInfo, produce the corresponding type. The unsigned @p Type
1901/// is actually a value of type @c TargetInfo::IntType.
1902QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001903 switch (Type) {
1904 case TargetInfo::NoInt: return QualType();
1905 case TargetInfo::SignedShort: return ShortTy;
1906 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1907 case TargetInfo::SignedInt: return IntTy;
1908 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1909 case TargetInfo::SignedLong: return LongTy;
1910 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1911 case TargetInfo::SignedLongLong: return LongLongTy;
1912 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1913 }
1914
1915 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00001916 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001917}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001918
1919//===----------------------------------------------------------------------===//
1920// Type Predicates.
1921//===----------------------------------------------------------------------===//
1922
1923/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1924/// to an object type. This includes "id" and "Class" (two 'special' pointers
1925/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1926/// ID type).
1927bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1928 if (Ty->isObjCQualifiedIdType())
1929 return true;
1930
Steve Naroff6ae98502008-10-21 18:24:04 +00001931 // Blocks are objects.
1932 if (Ty->isBlockPointerType())
1933 return true;
1934
1935 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001936 if (!Ty->isPointerType())
1937 return false;
1938
1939 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1940 // pointer types. This looks for the typedef specifically, not for the
1941 // underlying type.
1942 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1943 return true;
1944
1945 // If this a pointer to an interface (e.g. NSString*), it is ok.
1946 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1947}
1948
Chris Lattner6ac46a42008-04-07 06:51:04 +00001949//===----------------------------------------------------------------------===//
1950// Type Compatibility Testing
1951//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001952
Steve Naroff1c7d0672008-09-04 15:10:53 +00001953/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001954/// block types. Types must be strictly compatible here. For example,
1955/// C unfortunately doesn't produce an error for the following:
1956///
1957/// int (*emptyArgFunc)();
1958/// int (*intArgList)(int) = emptyArgFunc;
1959///
1960/// For blocks, we will produce an error for the following (similar to C++):
1961///
1962/// int (^emptyArgBlock)();
1963/// int (^intArgBlock)(int) = emptyArgBlock;
1964///
1965/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1966///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001967bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00001968 const FunctionType *lbase = lhs->getAsFunctionType();
1969 const FunctionType *rbase = rhs->getAsFunctionType();
1970 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1971 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1972 if (lproto && rproto)
1973 return !mergeTypes(lhs, rhs).isNull();
1974 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00001975}
1976
Chris Lattner6ac46a42008-04-07 06:51:04 +00001977/// areCompatVectorTypes - Return true if the two specified vector types are
1978/// compatible.
1979static bool areCompatVectorTypes(const VectorType *LHS,
1980 const VectorType *RHS) {
1981 assert(LHS->isCanonical() && RHS->isCanonical());
1982 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00001983 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00001984}
1985
Eli Friedman3d815e72008-08-22 00:56:42 +00001986/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001987/// compatible for assignment from RHS to LHS. This handles validation of any
1988/// protocol qualifiers on the LHS or RHS.
1989///
Eli Friedman3d815e72008-08-22 00:56:42 +00001990bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1991 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001992 // Verify that the base decls are compatible: the RHS must be a subclass of
1993 // the LHS.
1994 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1995 return false;
1996
1997 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1998 // protocol qualified at all, then we are good.
1999 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2000 return true;
2001
2002 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2003 // isn't a superset.
2004 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2005 return true; // FIXME: should return false!
2006
2007 // Finally, we must have two protocol-qualified interfaces.
2008 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2009 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2010 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2011 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2012 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2013 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2014
2015 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2016 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2017 // LHS in a single parallel scan until we run out of elements in LHS.
2018 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2019 ObjCProtocolDecl *LHSProto = *LHSPI;
2020
2021 while (RHSPI != RHSPE) {
2022 ObjCProtocolDecl *RHSProto = *RHSPI++;
2023 // If the RHS has a protocol that the LHS doesn't, ignore it.
2024 if (RHSProto != LHSProto)
2025 continue;
2026
2027 // Otherwise, the RHS does have this element.
2028 ++LHSPI;
2029 if (LHSPI == LHSPE)
2030 return true; // All protocols in LHS exist in RHS.
2031
2032 LHSProto = *LHSPI;
2033 }
2034
2035 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2036 return false;
2037}
2038
Steve Naroffec0550f2007-10-15 20:41:53 +00002039/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2040/// both shall have the identically qualified version of a compatible type.
2041/// C99 6.2.7p1: Two types have compatible types if their types are the
2042/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002043bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2044 return !mergeTypes(LHS, RHS).isNull();
2045}
2046
2047QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2048 const FunctionType *lbase = lhs->getAsFunctionType();
2049 const FunctionType *rbase = rhs->getAsFunctionType();
2050 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2051 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2052 bool allLTypes = true;
2053 bool allRTypes = true;
2054
2055 // Check return type
2056 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2057 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002058 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2059 allLTypes = false;
2060 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2061 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002062
2063 if (lproto && rproto) { // two C99 style function prototypes
2064 unsigned lproto_nargs = lproto->getNumArgs();
2065 unsigned rproto_nargs = rproto->getNumArgs();
2066
2067 // Compatible functions must have the same number of arguments
2068 if (lproto_nargs != rproto_nargs)
2069 return QualType();
2070
2071 // Variadic and non-variadic functions aren't compatible
2072 if (lproto->isVariadic() != rproto->isVariadic())
2073 return QualType();
2074
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002075 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2076 return QualType();
2077
Eli Friedman3d815e72008-08-22 00:56:42 +00002078 // Check argument compatibility
2079 llvm::SmallVector<QualType, 10> types;
2080 for (unsigned i = 0; i < lproto_nargs; i++) {
2081 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2082 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2083 QualType argtype = mergeTypes(largtype, rargtype);
2084 if (argtype.isNull()) return QualType();
2085 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002086 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2087 allLTypes = false;
2088 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2089 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002090 }
2091 if (allLTypes) return lhs;
2092 if (allRTypes) return rhs;
2093 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002094 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002095 }
2096
2097 if (lproto) allRTypes = false;
2098 if (rproto) allLTypes = false;
2099
2100 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2101 if (proto) {
2102 if (proto->isVariadic()) return QualType();
2103 // Check that the types are compatible with the types that
2104 // would result from default argument promotions (C99 6.7.5.3p15).
2105 // The only types actually affected are promotable integer
2106 // types and floats, which would be passed as a different
2107 // type depending on whether the prototype is visible.
2108 unsigned proto_nargs = proto->getNumArgs();
2109 for (unsigned i = 0; i < proto_nargs; ++i) {
2110 QualType argTy = proto->getArgType(i);
2111 if (argTy->isPromotableIntegerType() ||
2112 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2113 return QualType();
2114 }
2115
2116 if (allLTypes) return lhs;
2117 if (allRTypes) return rhs;
2118 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002119 proto->getNumArgs(), lproto->isVariadic(),
2120 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002121 }
2122
2123 if (allLTypes) return lhs;
2124 if (allRTypes) return rhs;
2125 return getFunctionTypeNoProto(retType);
2126}
2127
2128QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002129 // C++ [expr]: If an expression initially has the type "reference to T", the
2130 // type is adjusted to "T" prior to any further analysis, the expression
2131 // designates the object or function denoted by the reference, and the
2132 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002133 // FIXME: C++ shouldn't be going through here! The rules are different
2134 // enough that they should be handled separately.
2135 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002136 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002137 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002138 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002139
Eli Friedman3d815e72008-08-22 00:56:42 +00002140 QualType LHSCan = getCanonicalType(LHS),
2141 RHSCan = getCanonicalType(RHS);
2142
2143 // If two types are identical, they are compatible.
2144 if (LHSCan == RHSCan)
2145 return LHS;
2146
2147 // If the qualifiers are different, the types aren't compatible
2148 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2149 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2150 return QualType();
2151
2152 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2153 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2154
Chris Lattner1adb8832008-01-14 05:45:46 +00002155 // We want to consider the two function types to be the same for these
2156 // comparisons, just force one to the other.
2157 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2158 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002159
2160 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002161 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2162 LHSClass = Type::ConstantArray;
2163 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2164 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002165
Nate Begeman213541a2008-04-18 23:10:10 +00002166 // Canonicalize ExtVector -> Vector.
2167 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2168 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002169
Chris Lattnerb0489812008-04-07 06:38:24 +00002170 // Consider qualified interfaces and interfaces the same.
2171 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2172 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002173
Chris Lattnera36a61f2008-04-07 05:43:21 +00002174 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002175 if (LHSClass != RHSClass) {
Steve Naroffbc76dd02008-12-10 22:14:21 +00002176 // ID is compatible with all qualified id types.
2177 if (LHS->isObjCQualifiedIdType()) {
2178 if (const PointerType *PT = RHS->getAsPointerType()) {
2179 QualType pType = PT->getPointeeType();
2180 if (isObjCIdType(pType))
2181 return LHS;
2182 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2183 // Unfortunately, this API is part of Sema (which we don't have access
2184 // to. Need to refactor. The following check is insufficient, since we
2185 // need to make sure the class implements the protocol.
2186 if (pType->isObjCInterfaceType())
2187 return LHS;
2188 }
2189 }
2190 if (RHS->isObjCQualifiedIdType()) {
2191 if (const PointerType *PT = LHS->getAsPointerType()) {
2192 QualType pType = PT->getPointeeType();
2193 if (isObjCIdType(pType))
2194 return RHS;
2195 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2196 // Unfortunately, this API is part of Sema (which we don't have access
2197 // to. Need to refactor. The following check is insufficient, since we
2198 // need to make sure the class implements the protocol.
2199 if (pType->isObjCInterfaceType())
2200 return RHS;
2201 }
2202 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002203 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2204 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002205 if (const EnumType* ETy = LHS->getAsEnumType()) {
2206 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2207 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002208 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002209 if (const EnumType* ETy = RHS->getAsEnumType()) {
2210 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2211 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002212 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002213
Eli Friedman3d815e72008-08-22 00:56:42 +00002214 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002215 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002216
Steve Naroff4a746782008-01-09 22:43:08 +00002217 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002218 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002219 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002220 {
2221 // Merge two pointer types, while trying to preserve typedef info
2222 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2223 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2224 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2225 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002226 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2227 return LHS;
2228 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2229 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002230 return getPointerType(ResultType);
2231 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002232 case Type::BlockPointer:
2233 {
2234 // Merge two block pointer types, while trying to preserve typedef info
2235 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2236 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2237 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2238 if (ResultType.isNull()) return QualType();
2239 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2240 return LHS;
2241 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2242 return RHS;
2243 return getBlockPointerType(ResultType);
2244 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002245 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002246 {
2247 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2248 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2249 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2250 return QualType();
2251
2252 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2253 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2254 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2255 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002256 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2257 return LHS;
2258 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2259 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002260 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2261 ArrayType::ArraySizeModifier(), 0);
2262 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2263 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002264 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2265 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002266 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2267 return LHS;
2268 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2269 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002270 if (LVAT) {
2271 // FIXME: This isn't correct! But tricky to implement because
2272 // the array's size has to be the size of LHS, but the type
2273 // has to be different.
2274 return LHS;
2275 }
2276 if (RVAT) {
2277 // FIXME: This isn't correct! But tricky to implement because
2278 // the array's size has to be the size of RHS, but the type
2279 // has to be different.
2280 return RHS;
2281 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002282 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2283 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002284 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002285 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002286 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002287 return mergeFunctionTypes(LHS, RHS);
2288 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002289 // FIXME: Why are these compatible?
2290 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2291 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2292 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002293 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002294 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002295 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002296 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002297 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2298 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002299 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002300 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002301 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2302 // for checking assignment/comparison safety
2303 return QualType();
Steve Naroffbc76dd02008-12-10 22:14:21 +00002304 case Type::ObjCQualifiedId:
2305 // Distinct qualified id's are not compatible.
2306 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002307 default:
2308 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002309 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002310 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002311}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002312
Chris Lattner5426bf62008-04-07 07:01:58 +00002313//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002314// Integer Predicates
2315//===----------------------------------------------------------------------===//
2316unsigned ASTContext::getIntWidth(QualType T) {
2317 if (T == BoolTy)
2318 return 1;
2319 // At the moment, only bool has padding bits
2320 return (unsigned)getTypeSize(T);
2321}
2322
2323QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2324 assert(T->isSignedIntegerType() && "Unexpected type");
2325 if (const EnumType* ETy = T->getAsEnumType())
2326 T = ETy->getDecl()->getIntegerType();
2327 const BuiltinType* BTy = T->getAsBuiltinType();
2328 assert (BTy && "Unexpected signed integer type");
2329 switch (BTy->getKind()) {
2330 case BuiltinType::Char_S:
2331 case BuiltinType::SChar:
2332 return UnsignedCharTy;
2333 case BuiltinType::Short:
2334 return UnsignedShortTy;
2335 case BuiltinType::Int:
2336 return UnsignedIntTy;
2337 case BuiltinType::Long:
2338 return UnsignedLongTy;
2339 case BuiltinType::LongLong:
2340 return UnsignedLongLongTy;
2341 default:
2342 assert(0 && "Unexpected signed integer type");
2343 return QualType();
2344 }
2345}
2346
2347
2348//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002349// Serialization Support
2350//===----------------------------------------------------------------------===//
2351
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002352/// Emit - Serialize an ASTContext object to Bitcode.
2353void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002354 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002355 S.EmitRef(SourceMgr);
2356 S.EmitRef(Target);
2357 S.EmitRef(Idents);
2358 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002359
Ted Kremenekfee04522007-10-31 22:44:07 +00002360 // Emit the size of the type vector so that we can reserve that size
2361 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002362 S.EmitInt(Types.size());
2363
Ted Kremenek03ed4402007-11-13 22:02:55 +00002364 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2365 I!=E;++I)
2366 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002367
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002368 S.EmitOwnedPtr(TUDecl);
2369
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002370 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002371}
2372
Ted Kremenek0f84c002007-11-13 00:25:37 +00002373ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002374
2375 // Read the language options.
2376 LangOptions LOpts;
2377 LOpts.Read(D);
2378
Ted Kremenekfee04522007-10-31 22:44:07 +00002379 SourceManager &SM = D.ReadRef<SourceManager>();
2380 TargetInfo &t = D.ReadRef<TargetInfo>();
2381 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2382 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002383
Ted Kremenekfee04522007-10-31 22:44:07 +00002384 unsigned size_reserve = D.ReadInt();
2385
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002386 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2387 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002388
Ted Kremenek03ed4402007-11-13 22:02:55 +00002389 for (unsigned i = 0; i < size_reserve; ++i)
2390 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002391
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002392 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2393
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002394 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002395
2396 return A;
2397}