blob: ba61332a44f164444b25883ee51e752192b547e1 [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
Devang Patel44a3dde2008-06-04 21:54:36 +0000475
Chris Lattner61710852008-10-05 17:34:18 +0000476/// getASTObjcInterfaceLayout - Get or compute information about the layout of
477/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000478/// position information.
479const ASTRecordLayout &
480ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
481 // Look up this layout, if already laid out, return what we have.
482 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
483 if (Entry) return *Entry;
484
485 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
486 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000487 ASTRecordLayout *NewEntry = NULL;
488 unsigned FieldCount = D->ivar_size();
489 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
490 FieldCount++;
491 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
492 unsigned Alignment = SL.getAlignment();
493 uint64_t Size = SL.getSize();
494 NewEntry = new ASTRecordLayout(Size, Alignment);
495 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000496 // Super class is at the beginning of the layout.
497 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000498 } else {
499 NewEntry = new ASTRecordLayout();
500 NewEntry->InitializeLayout(FieldCount);
501 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000502 Entry = NewEntry;
503
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000504 unsigned StructPacking = 0;
505 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
506 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000507
508 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
509 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
510 AA->getAlignment()));
511
512 // Layout each ivar sequentially.
513 unsigned i = 0;
514 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
515 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
516 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000517 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000518 }
519
520 // Finally, round the size of the total struct up to the alignment of the
521 // struct itself.
522 NewEntry->FinalizeLayout();
523 return *NewEntry;
524}
525
Devang Patel88a981b2007-11-01 19:11:01 +0000526/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000527/// specified record (struct/union/class), which indicates its size and field
528/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000529const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000530 D = D->getDefinition(*this);
531 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000532
Chris Lattner464175b2007-07-18 17:52:12 +0000533 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000534 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000535 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000536
Devang Patel88a981b2007-11-01 19:11:01 +0000537 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
538 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
539 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000540 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000541
Devang Patel8b277042008-06-04 21:22:16 +0000542 NewEntry->InitializeLayout(D->getNumMembers());
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000543 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000544
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000545 unsigned StructPacking = 0;
546 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
547 StructPacking = PA->getAlignment();
548
Eli Friedman4bd998b2008-05-30 09:31:38 +0000549 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000550 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
551 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000552
Eli Friedman4bd998b2008-05-30 09:31:38 +0000553 // Layout each field, for now, just sequentially, respecting alignment. In
554 // the future, this will need to be tweakable by targets.
555 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
556 const FieldDecl *FD = D->getMember(i);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000557 NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this);
Chris Lattner464175b2007-07-18 17:52:12 +0000558 }
Eli Friedman4bd998b2008-05-30 09:31:38 +0000559
560 // Finally, round the size of the total struct up to the alignment of the
561 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000562 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000563 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000564}
565
Chris Lattnera7674d82007-07-13 22:13:22 +0000566//===----------------------------------------------------------------------===//
567// Type creation/memoization methods
568//===----------------------------------------------------------------------===//
569
Christopher Lambebb97e92008-02-04 02:31:56 +0000570QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000571 QualType CanT = getCanonicalType(T);
572 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000573 return T;
574
575 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
576 // with CVR qualifiers from here on out.
Chris Lattnerf52ab252008-04-06 22:59:24 +0000577 assert(CanT.getAddressSpace() == 0 &&
Chris Lattnerf46699c2008-02-20 20:55:12 +0000578 "Type is already address space qualified");
579
580 // Check if we've already instantiated an address space qual'd type of this
581 // type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000582 llvm::FoldingSetNodeID ID;
Chris Lattnerf46699c2008-02-20 20:55:12 +0000583 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000584 void *InsertPos = 0;
585 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
586 return QualType(ASQy, 0);
587
588 // If the base type isn't canonical, this won't be a canonical type either,
589 // so fill in the canonical type field.
590 QualType Canonical;
591 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000592 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000593
594 // Get the new insert position for the node we care about.
595 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000596 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000597 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000598 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000599 ASQualTypes.InsertNode(New, InsertPos);
600 Types.push_back(New);
Chris Lattnerf46699c2008-02-20 20:55:12 +0000601 return QualType(New, T.getCVRQualifiers());
Christopher Lambebb97e92008-02-04 02:31:56 +0000602}
603
Chris Lattnera7674d82007-07-13 22:13:22 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605/// getComplexType - Return the uniqued reference to the type for a complex
606/// number with the specified element type.
607QualType ASTContext::getComplexType(QualType T) {
608 // Unique pointers, to guarantee there is only one pointer of a particular
609 // structure.
610 llvm::FoldingSetNodeID ID;
611 ComplexType::Profile(ID, T);
612
613 void *InsertPos = 0;
614 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
615 return QualType(CT, 0);
616
617 // If the pointee type isn't canonical, this won't be a canonical type either,
618 // so fill in the canonical type field.
619 QualType Canonical;
620 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000621 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000622
623 // Get the new insert position for the node we care about.
624 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000625 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 }
627 ComplexType *New = new ComplexType(T, Canonical);
628 Types.push_back(New);
629 ComplexTypes.InsertNode(New, InsertPos);
630 return QualType(New, 0);
631}
632
633
634/// getPointerType - Return the uniqued reference to the type for a pointer to
635/// the specified type.
636QualType ASTContext::getPointerType(QualType T) {
637 // Unique pointers, to guarantee there is only one pointer of a particular
638 // structure.
639 llvm::FoldingSetNodeID ID;
640 PointerType::Profile(ID, T);
641
642 void *InsertPos = 0;
643 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
644 return QualType(PT, 0);
645
646 // If the pointee type isn't canonical, this won't be a canonical type either,
647 // so fill in the canonical type field.
648 QualType Canonical;
649 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000650 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000651
652 // Get the new insert position for the node we care about.
653 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000654 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 }
656 PointerType *New = new PointerType(T, Canonical);
657 Types.push_back(New);
658 PointerTypes.InsertNode(New, InsertPos);
659 return QualType(New, 0);
660}
661
Steve Naroff5618bd42008-08-27 16:04:49 +0000662/// getBlockPointerType - Return the uniqued reference to the type for
663/// a pointer to the specified block.
664QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000665 assert(T->isFunctionType() && "block of function types only");
666 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000667 // structure.
668 llvm::FoldingSetNodeID ID;
669 BlockPointerType::Profile(ID, T);
670
671 void *InsertPos = 0;
672 if (BlockPointerType *PT =
673 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
674 return QualType(PT, 0);
675
Steve Naroff296e8d52008-08-28 19:20:44 +0000676 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000677 // type either so fill in the canonical type field.
678 QualType Canonical;
679 if (!T->isCanonical()) {
680 Canonical = getBlockPointerType(getCanonicalType(T));
681
682 // Get the new insert position for the node we care about.
683 BlockPointerType *NewIP =
684 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000685 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000686 }
687 BlockPointerType *New = new BlockPointerType(T, Canonical);
688 Types.push_back(New);
689 BlockPointerTypes.InsertNode(New, InsertPos);
690 return QualType(New, 0);
691}
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693/// getReferenceType - Return the uniqued reference to the type for a reference
694/// to the specified type.
695QualType ASTContext::getReferenceType(QualType T) {
696 // Unique pointers, to guarantee there is only one pointer of a particular
697 // structure.
698 llvm::FoldingSetNodeID ID;
699 ReferenceType::Profile(ID, T);
700
701 void *InsertPos = 0;
702 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
703 return QualType(RT, 0);
704
705 // If the referencee type isn't canonical, this won't be a canonical type
706 // either, so fill in the canonical type field.
707 QualType Canonical;
708 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000709 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000710
711 // Get the new insert position for the node we care about.
712 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000713 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 }
715
716 ReferenceType *New = new ReferenceType(T, Canonical);
717 Types.push_back(New);
718 ReferenceTypes.InsertNode(New, InsertPos);
719 return QualType(New, 0);
720}
721
Steve Narofffb22d962007-08-30 01:06:46 +0000722/// getConstantArrayType - Return the unique reference to the type for an
723/// array of the specified element type.
724QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000725 const llvm::APInt &ArySize,
726 ArrayType::ArraySizeModifier ASM,
727 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000729 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000730
731 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000732 if (ConstantArrayType *ATP =
733 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 return QualType(ATP, 0);
735
736 // If the element type isn't canonical, this won't be a canonical type either,
737 // so fill in the canonical type field.
738 QualType Canonical;
739 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000740 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000741 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000743 ConstantArrayType *NewIP =
744 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000745 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 }
747
Steve Naroffc9406122007-08-30 18:10:14 +0000748 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
749 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000750 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 Types.push_back(New);
752 return QualType(New, 0);
753}
754
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000755/// getVariableArrayType - Returns a non-unique reference to the type for a
756/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000757QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
758 ArrayType::ArraySizeModifier ASM,
759 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000760 // Since we don't unique expressions, it isn't possible to unique VLA's
761 // that have an expression provided for their size.
762
763 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
764 ASM, EltTypeQuals);
765
766 VariableArrayTypes.push_back(New);
767 Types.push_back(New);
768 return QualType(New, 0);
769}
770
Douglas Gregor898574e2008-12-05 23:32:09 +0000771/// getDependentSizedArrayType - Returns a non-unique reference to
772/// the type for a dependently-sized array of the specified element
773/// type. FIXME: We will need these to be uniqued, or at least
774/// comparable, at some point.
775QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
776 ArrayType::ArraySizeModifier ASM,
777 unsigned EltTypeQuals) {
778 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
779 "Size must be type- or value-dependent!");
780
781 // Since we don't unique expressions, it isn't possible to unique
782 // dependently-sized array types.
783
784 DependentSizedArrayType *New
785 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
786 ASM, EltTypeQuals);
787
788 DependentSizedArrayTypes.push_back(New);
789 Types.push_back(New);
790 return QualType(New, 0);
791}
792
Eli Friedmanc5773c42008-02-15 18:16:39 +0000793QualType ASTContext::getIncompleteArrayType(QualType EltTy,
794 ArrayType::ArraySizeModifier ASM,
795 unsigned EltTypeQuals) {
796 llvm::FoldingSetNodeID ID;
797 IncompleteArrayType::Profile(ID, EltTy);
798
799 void *InsertPos = 0;
800 if (IncompleteArrayType *ATP =
801 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
802 return QualType(ATP, 0);
803
804 // If the element type isn't canonical, this won't be a canonical type
805 // either, so fill in the canonical type field.
806 QualType Canonical;
807
808 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000809 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000810 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000811
812 // Get the new insert position for the node we care about.
813 IncompleteArrayType *NewIP =
814 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000815 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000816 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000817
818 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
819 ASM, EltTypeQuals);
820
821 IncompleteArrayTypes.InsertNode(New, InsertPos);
822 Types.push_back(New);
823 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000824}
825
Steve Naroff73322922007-07-18 18:00:27 +0000826/// getVectorType - Return the unique reference to a vector type of
827/// the specified element type and size. VectorType must be a built-in type.
828QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 BuiltinType *baseType;
830
Chris Lattnerf52ab252008-04-06 22:59:24 +0000831 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000832 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000833
834 // Check if we've already instantiated a vector of this type.
835 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000836 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 void *InsertPos = 0;
838 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
839 return QualType(VTP, 0);
840
841 // If the element type isn't canonical, this won't be a canonical type either,
842 // so fill in the canonical type field.
843 QualType Canonical;
844 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000845 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846
847 // Get the new insert position for the node we care about.
848 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000849 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 }
851 VectorType *New = new VectorType(vecType, NumElts, Canonical);
852 VectorTypes.InsertNode(New, InsertPos);
853 Types.push_back(New);
854 return QualType(New, 0);
855}
856
Nate Begeman213541a2008-04-18 23:10:10 +0000857/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +0000858/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +0000859QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +0000860 BuiltinType *baseType;
861
Chris Lattnerf52ab252008-04-06 22:59:24 +0000862 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +0000863 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +0000864
865 // Check if we've already instantiated a vector of this type.
866 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +0000867 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +0000868 void *InsertPos = 0;
869 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
870 return QualType(VTP, 0);
871
872 // If the element type isn't canonical, this won't be a canonical type either,
873 // so fill in the canonical type field.
874 QualType Canonical;
875 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000876 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +0000877
878 // Get the new insert position for the node we care about.
879 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000880 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +0000881 }
Nate Begeman213541a2008-04-18 23:10:10 +0000882 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +0000883 VectorTypes.InsertNode(New, InsertPos);
884 Types.push_back(New);
885 return QualType(New, 0);
886}
887
Reid Spencer5f016e22007-07-11 17:01:13 +0000888/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
889///
890QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
891 // Unique functions, to guarantee there is only one function of a particular
892 // structure.
893 llvm::FoldingSetNodeID ID;
894 FunctionTypeNoProto::Profile(ID, ResultTy);
895
896 void *InsertPos = 0;
897 if (FunctionTypeNoProto *FT =
898 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
899 return QualType(FT, 0);
900
901 QualType Canonical;
902 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000903 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
905 // Get the new insert position for the node we care about.
906 FunctionTypeNoProto *NewIP =
907 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000908 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 }
910
911 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
912 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +0000913 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 return QualType(New, 0);
915}
916
917/// getFunctionType - Return a normal function type with a typed argument
918/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +0000919QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000920 unsigned NumArgs, bool isVariadic,
921 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 // Unique functions, to guarantee there is only one function of a particular
923 // structure.
924 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000925 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
926 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927
928 void *InsertPos = 0;
929 if (FunctionTypeProto *FTP =
930 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
931 return QualType(FTP, 0);
932
933 // Determine whether the type being created is already canonical or not.
934 bool isCanonical = ResultTy->isCanonical();
935 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
936 if (!ArgArray[i]->isCanonical())
937 isCanonical = false;
938
939 // If this type isn't canonical, get the canonical version of it.
940 QualType Canonical;
941 if (!isCanonical) {
942 llvm::SmallVector<QualType, 16> CanonicalArgs;
943 CanonicalArgs.reserve(NumArgs);
944 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +0000945 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +0000946
Chris Lattnerf52ab252008-04-06 22:59:24 +0000947 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000949 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000950
951 // Get the new insert position for the node we care about.
952 FunctionTypeProto *NewIP =
953 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000954 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
956
957 // FunctionTypeProto objects are not allocated with new because they have a
958 // variable size array (for parameter types) at the end of them.
959 FunctionTypeProto *FTP =
960 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000961 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000963 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 Types.push_back(FTP);
965 FunctionTypeProtos.InsertNode(FTP, InsertPos);
966 return QualType(FTP, 0);
967}
968
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000969/// getTypeDeclType - Return the unique reference to the type for the
970/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000971QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000972 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000973 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
974
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000975 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000976 return getTypedefType(Typedef);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000977 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
978 return getTemplateTypeParmType(TP);
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000979 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000980 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000981
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000982 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000983 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
984 : new CXXRecordType(CXXRecord);
985 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000986 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000987 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
988 : new RecordType(Record);
989 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +0000990 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000991 Decl->TypeForDecl = new EnumType(Enum);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000992 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000993 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000994
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000995 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000996 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000997}
998
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000999/// setTagDefinition - Used by RecordDecl::defineBody to inform ASTContext
1000/// about which RecordDecl serves as the definition of a particular
1001/// struct/union/class. This will eventually be used by enums as well.
1002void ASTContext::setTagDefinition(TagDecl* D) {
1003 assert (D->isDefinition());
1004 cast<TagType>(D->TypeForDecl)->decl = D;
1005}
1006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007/// getTypedefType - Return the unique reference to the type for the
1008/// specified typename decl.
1009QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1010 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1011
Chris Lattnerf52ab252008-04-06 22:59:24 +00001012 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001013 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 Types.push_back(Decl->TypeForDecl);
1015 return QualType(Decl->TypeForDecl, 0);
1016}
1017
Douglas Gregor72c3f312008-12-05 18:15:24 +00001018/// getTemplateTypeParmType - Return the unique reference to the type
1019/// for the specified template type parameter declaration.
1020QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1021 if (!Decl->TypeForDecl) {
1022 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1023 Types.push_back(Decl->TypeForDecl);
1024 }
1025 return QualType(Decl->TypeForDecl, 0);
1026}
1027
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001029/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001030QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001031 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1032
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001033 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001034 Types.push_back(Decl->TypeForDecl);
1035 return QualType(Decl->TypeForDecl, 0);
1036}
1037
Chris Lattner88cb27a2008-04-07 04:56:42 +00001038/// CmpProtocolNames - Comparison predicate for sorting protocols
1039/// alphabetically.
1040static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1041 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001042 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001043}
1044
1045static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1046 unsigned &NumProtocols) {
1047 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1048
1049 // Sort protocols, keyed by name.
1050 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1051
1052 // Remove duplicates.
1053 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1054 NumProtocols = ProtocolsEnd-Protocols;
1055}
1056
1057
Chris Lattner065f0d72008-04-07 04:44:08 +00001058/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1059/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001060QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1061 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001062 // Sort the protocol list alphabetically to canonicalize it.
1063 SortAndUniqueProtocols(Protocols, NumProtocols);
1064
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001065 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001066 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001067
1068 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001069 if (ObjCQualifiedInterfaceType *QT =
1070 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001071 return QualType(QT, 0);
1072
1073 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001074 ObjCQualifiedInterfaceType *QType =
1075 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001076 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001077 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001078 return QualType(QType, 0);
1079}
1080
Chris Lattner88cb27a2008-04-07 04:56:42 +00001081/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1082/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001083QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001084 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001085 // Sort the protocol list alphabetically to canonicalize it.
1086 SortAndUniqueProtocols(Protocols, NumProtocols);
1087
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001088 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001089 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001090
1091 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001092 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001093 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001094 return QualType(QT, 0);
1095
1096 // No Match;
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001097 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001098 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001099 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001100 return QualType(QType, 0);
1101}
1102
Steve Naroff9752f252007-08-01 18:02:17 +00001103/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1104/// TypeOfExpr AST's (since expression's are never shared). For example,
1105/// multiple declarations that refer to "typeof(x)" all contain different
1106/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1107/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001108QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001109 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff9752f252007-08-01 18:02:17 +00001110 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1111 Types.push_back(toe);
1112 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001113}
1114
Steve Naroff9752f252007-08-01 18:02:17 +00001115/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1116/// TypeOfType AST's. The only motivation to unique these nodes would be
1117/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1118/// an issue. This doesn't effect the type checker, since it operates
1119/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001120QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001121 QualType Canonical = getCanonicalType(tofType);
Steve Naroff9752f252007-08-01 18:02:17 +00001122 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1123 Types.push_back(tot);
1124 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001125}
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127/// getTagDeclType - Return the unique reference to the type for the
1128/// specified TagDecl (struct/union/class/enum) decl.
1129QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001130 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001131 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132}
1133
1134/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1135/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1136/// needs to agree with the definition in <stddef.h>.
1137QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001138 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001139}
1140
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001141/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001142/// width of characters in wide strings, The value is target dependent and
1143/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001144QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001145 if (LangOpts.CPlusPlus)
1146 return WCharTy;
1147
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001148 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1149 // wide-character type?
1150 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001151}
1152
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001153/// getSignedWCharType - Return the type of "signed wchar_t".
1154/// Used when in C++, as a GCC extension.
1155QualType ASTContext::getSignedWCharType() const {
1156 // FIXME: derive from "Target" ?
1157 return WCharTy;
1158}
1159
1160/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1161/// Used when in C++, as a GCC extension.
1162QualType ASTContext::getUnsignedWCharType() const {
1163 // FIXME: derive from "Target" ?
1164 return UnsignedIntTy;
1165}
1166
Chris Lattner8b9023b2007-07-13 03:05:23 +00001167/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1168/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1169QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001170 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001171}
1172
Chris Lattnere6327742008-04-02 05:18:44 +00001173//===----------------------------------------------------------------------===//
1174// Type Operators
1175//===----------------------------------------------------------------------===//
1176
Chris Lattner77c96472008-04-06 22:41:35 +00001177/// getCanonicalType - Return the canonical (structural) type corresponding to
1178/// the specified potentially non-canonical type. The non-canonical version
1179/// of a type may have many "decorated" versions of types. Decorators can
1180/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1181/// to be free of any of these, allowing two canonical types to be compared
1182/// for exact equality with a simple pointer comparison.
1183QualType ASTContext::getCanonicalType(QualType T) {
1184 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001185
1186 // If the result has type qualifiers, make sure to canonicalize them as well.
1187 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1188 if (TypeQuals == 0) return CanType;
1189
1190 // If the type qualifiers are on an array type, get the canonical type of the
1191 // array with the qualifiers applied to the element type.
1192 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1193 if (!AT)
1194 return CanType.getQualifiedType(TypeQuals);
1195
1196 // Get the canonical version of the element with the extra qualifiers on it.
1197 // This can recursively sink qualifiers through multiple levels of arrays.
1198 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1199 NewEltTy = getCanonicalType(NewEltTy);
1200
1201 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1202 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1203 CAT->getIndexTypeQualifier());
1204 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1205 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1206 IAT->getIndexTypeQualifier());
1207
Douglas Gregor898574e2008-12-05 23:32:09 +00001208 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1209 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1210 DSAT->getSizeModifier(),
1211 DSAT->getIndexTypeQualifier());
1212
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001213 // FIXME: What is the ownership of size expressions in VLAs?
1214 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1215 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1216 VAT->getSizeModifier(),
1217 VAT->getIndexTypeQualifier());
1218}
1219
1220
1221const ArrayType *ASTContext::getAsArrayType(QualType T) {
1222 // Handle the non-qualified case efficiently.
1223 if (T.getCVRQualifiers() == 0) {
1224 // Handle the common positive case fast.
1225 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1226 return AT;
1227 }
1228
1229 // Handle the common negative case fast, ignoring CVR qualifiers.
1230 QualType CType = T->getCanonicalTypeInternal();
1231
1232 // Make sure to look through type qualifiers (like ASQuals) for the negative
1233 // test.
1234 if (!isa<ArrayType>(CType) &&
1235 !isa<ArrayType>(CType.getUnqualifiedType()))
1236 return 0;
1237
1238 // Apply any CVR qualifiers from the array type to the element type. This
1239 // implements C99 6.7.3p8: "If the specification of an array type includes
1240 // any type qualifiers, the element type is so qualified, not the array type."
1241
1242 // If we get here, we either have type qualifiers on the type, or we have
1243 // sugar such as a typedef in the way. If we have type qualifiers on the type
1244 // we must propagate them down into the elemeng type.
1245 unsigned CVRQuals = T.getCVRQualifiers();
1246 unsigned AddrSpace = 0;
1247 Type *Ty = T.getTypePtr();
1248
1249 // Rip through ASQualType's and typedefs to get to a concrete type.
1250 while (1) {
1251 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1252 AddrSpace = ASQT->getAddressSpace();
1253 Ty = ASQT->getBaseType();
1254 } else {
1255 T = Ty->getDesugaredType();
1256 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1257 break;
1258 CVRQuals |= T.getCVRQualifiers();
1259 Ty = T.getTypePtr();
1260 }
1261 }
1262
1263 // If we have a simple case, just return now.
1264 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1265 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1266 return ATy;
1267
1268 // Otherwise, we have an array and we have qualifiers on it. Push the
1269 // qualifiers into the array element type and return a new array type.
1270 // Get the canonical version of the element with the extra qualifiers on it.
1271 // This can recursively sink qualifiers through multiple levels of arrays.
1272 QualType NewEltTy = ATy->getElementType();
1273 if (AddrSpace)
1274 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1275 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1276
1277 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1278 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1279 CAT->getSizeModifier(),
1280 CAT->getIndexTypeQualifier()));
1281 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1282 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1283 IAT->getSizeModifier(),
1284 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001285
1286 // FIXME: What is the ownership of size expressions in
1287 // dependent-sized array types?
1288 if (const DependentSizedArrayType *DSAT
1289 = dyn_cast<DependentSizedArrayType>(ATy))
1290 return cast<ArrayType>(
1291 getDependentSizedArrayType(NewEltTy,
1292 DSAT->getSizeExpr(),
1293 DSAT->getSizeModifier(),
1294 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001295
1296 // FIXME: What is the ownership of size expressions in VLAs?
1297 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1298 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1299 VAT->getSizeModifier(),
1300 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001301}
1302
1303
Chris Lattnere6327742008-04-02 05:18:44 +00001304/// getArrayDecayedType - Return the properly qualified result of decaying the
1305/// specified array type to a pointer. This operation is non-trivial when
1306/// handling typedefs etc. The canonical type of "T" must be an array type,
1307/// this returns a pointer to a properly qualified element of the array.
1308///
1309/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1310QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001311 // Get the element type with 'getAsArrayType' so that we don't lose any
1312 // typedefs in the element type of the array. This also handles propagation
1313 // of type qualifiers from the array type into the element type if present
1314 // (C99 6.7.3p8).
1315 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1316 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001317
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001318 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001319
1320 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001321 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001322}
1323
Reid Spencer5f016e22007-07-11 17:01:13 +00001324/// getFloatingRank - Return a relative rank for floating point types.
1325/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001326static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001327 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001329
Christopher Lambebb97e92008-02-04 02:31:56 +00001330 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001331 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 case BuiltinType::Float: return FloatRank;
1333 case BuiltinType::Double: return DoubleRank;
1334 case BuiltinType::LongDouble: return LongDoubleRank;
1335 }
1336}
1337
Steve Naroff716c7302007-08-27 01:41:48 +00001338/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1339/// point or a complex type (based on typeDomain/typeSize).
1340/// 'typeDomain' is a real floating point or complex type.
1341/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001342QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1343 QualType Domain) const {
1344 FloatingRank EltRank = getFloatingRank(Size);
1345 if (Domain->isComplexType()) {
1346 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001347 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001348 case FloatRank: return FloatComplexTy;
1349 case DoubleRank: return DoubleComplexTy;
1350 case LongDoubleRank: return LongDoubleComplexTy;
1351 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 }
Chris Lattner1361b112008-04-06 23:58:54 +00001353
1354 assert(Domain->isRealFloatingType() && "Unknown domain!");
1355 switch (EltRank) {
1356 default: assert(0 && "getFloatingRank(): illegal value for rank");
1357 case FloatRank: return FloatTy;
1358 case DoubleRank: return DoubleTy;
1359 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001360 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001361}
1362
Chris Lattner7cfeb082008-04-06 23:55:33 +00001363/// getFloatingTypeOrder - Compare the rank of the two specified floating
1364/// point types, ignoring the domain of the type (i.e. 'double' ==
1365/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1366/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001367int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1368 FloatingRank LHSR = getFloatingRank(LHS);
1369 FloatingRank RHSR = getFloatingRank(RHS);
1370
1371 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001372 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001373 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001374 return 1;
1375 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001376}
1377
Chris Lattnerf52ab252008-04-06 22:59:24 +00001378/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1379/// routine will assert if passed a built-in type that isn't an integer or enum,
1380/// or if it is not canonicalized.
1381static unsigned getIntegerRank(Type *T) {
1382 assert(T->isCanonical() && "T should be canonicalized");
1383 if (isa<EnumType>(T))
1384 return 4;
1385
1386 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001387 default: assert(0 && "getIntegerRank(): not a built-in integer");
1388 case BuiltinType::Bool:
1389 return 1;
1390 case BuiltinType::Char_S:
1391 case BuiltinType::Char_U:
1392 case BuiltinType::SChar:
1393 case BuiltinType::UChar:
1394 return 2;
1395 case BuiltinType::Short:
1396 case BuiltinType::UShort:
1397 return 3;
1398 case BuiltinType::Int:
1399 case BuiltinType::UInt:
1400 return 4;
1401 case BuiltinType::Long:
1402 case BuiltinType::ULong:
1403 return 5;
1404 case BuiltinType::LongLong:
1405 case BuiltinType::ULongLong:
1406 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001407 }
1408}
1409
Chris Lattner7cfeb082008-04-06 23:55:33 +00001410/// getIntegerTypeOrder - Returns the highest ranked integer type:
1411/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1412/// LHS < RHS, return -1.
1413int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001414 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1415 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001416 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001417
Chris Lattnerf52ab252008-04-06 22:59:24 +00001418 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1419 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001420
Chris Lattner7cfeb082008-04-06 23:55:33 +00001421 unsigned LHSRank = getIntegerRank(LHSC);
1422 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001423
Chris Lattner7cfeb082008-04-06 23:55:33 +00001424 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1425 if (LHSRank == RHSRank) return 0;
1426 return LHSRank > RHSRank ? 1 : -1;
1427 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001428
Chris Lattner7cfeb082008-04-06 23:55:33 +00001429 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1430 if (LHSUnsigned) {
1431 // If the unsigned [LHS] type is larger, return it.
1432 if (LHSRank >= RHSRank)
1433 return 1;
1434
1435 // If the signed type can represent all values of the unsigned type, it
1436 // wins. Because we are dealing with 2's complement and types that are
1437 // powers of two larger than each other, this is always safe.
1438 return -1;
1439 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001440
Chris Lattner7cfeb082008-04-06 23:55:33 +00001441 // If the unsigned [RHS] type is larger, return it.
1442 if (RHSRank >= LHSRank)
1443 return -1;
1444
1445 // If the signed type can represent all values of the unsigned type, it
1446 // wins. Because we are dealing with 2's complement and types that are
1447 // powers of two larger than each other, this is always safe.
1448 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001449}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001450
1451// getCFConstantStringType - Return the type used for constant CFStrings.
1452QualType ASTContext::getCFConstantStringType() {
1453 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001454 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001455 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001456 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001457 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001458
1459 // const int *isa;
1460 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001461 // int flags;
1462 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001463 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001464 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001465 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001466 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001467 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001468 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001469
Anders Carlssonf06273f2007-11-19 00:25:30 +00001470 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001471 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001472 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001473
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001474 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001475 }
1476
1477 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001478}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001479
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001480QualType ASTContext::getObjCFastEnumerationStateType()
1481{
1482 if (!ObjCFastEnumerationStateTypeDecl) {
1483 QualType FieldTypes[] = {
1484 UnsignedLongTy,
1485 getPointerType(ObjCIdType),
1486 getPointerType(UnsignedLongTy),
1487 getConstantArrayType(UnsignedLongTy,
1488 llvm::APInt(32, 5), ArrayType::Normal, 0)
1489 };
1490
1491 FieldDecl *FieldDecls[4];
1492 for (size_t i = 0; i < 4; ++i)
1493 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1494 FieldTypes[i]);
1495
1496 ObjCFastEnumerationStateTypeDecl =
1497 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001498 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001499
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001500 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001501 }
1502
1503 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1504}
1505
Anders Carlssone8c49532007-10-29 06:33:42 +00001506// This returns true if a type has been typedefed to BOOL:
1507// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001508static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001509 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001510 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1511 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001512
1513 return false;
1514}
1515
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001517/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001519 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001520
1521 // Make all integer and enum types at least as large as an int
1522 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001523 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001524 // Treat arrays as pointers, since that's how they're passed in.
1525 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001526 sz = getTypeSize(VoidPtrTy);
1527 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001528}
1529
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001531/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001532void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001533 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001534 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001535 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001537 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001538 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001539 // Compute size of all parameters.
1540 // Start with computing size of a pointer in number of bytes.
1541 // FIXME: There might(should) be a better way of doing this computation!
1542 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001543 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001544 // The first two arguments (self and _cmd) are pointers; account for
1545 // their size.
1546 int ParmOffset = 2 * PtrSize;
1547 int NumOfParams = Decl->getNumParams();
1548 for (int i = 0; i < NumOfParams; i++) {
1549 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001550 int sz = getObjCEncodingTypeSize (PType);
1551 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001552 ParmOffset += sz;
1553 }
1554 S += llvm::utostr(ParmOffset);
1555 S += "@0:";
1556 S += llvm::utostr(PtrSize);
1557
1558 // Argument types.
1559 ParmOffset = 2 * PtrSize;
1560 for (int i = 0; i < NumOfParams; i++) {
1561 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001562 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001563 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001564 getObjCEncodingForTypeQualifier(
1565 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001566 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001567 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001568 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001569 }
1570}
1571
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001572/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1573/// method declaration. If non-NULL, Container must be either an
1574/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1575/// NULL when getting encodings for protocol properties.
1576void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1577 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001578 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001579 // Collect information from the property implementation decl(s).
1580 bool Dynamic = false;
1581 ObjCPropertyImplDecl *SynthesizePID = 0;
1582
1583 // FIXME: Duplicated code due to poor abstraction.
1584 if (Container) {
1585 if (const ObjCCategoryImplDecl *CID =
1586 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1587 for (ObjCCategoryImplDecl::propimpl_iterator
1588 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1589 ObjCPropertyImplDecl *PID = *i;
1590 if (PID->getPropertyDecl() == PD) {
1591 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1592 Dynamic = true;
1593 } else {
1594 SynthesizePID = PID;
1595 }
1596 }
1597 }
1598 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001599 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001600 for (ObjCCategoryImplDecl::propimpl_iterator
1601 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1602 ObjCPropertyImplDecl *PID = *i;
1603 if (PID->getPropertyDecl() == PD) {
1604 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1605 Dynamic = true;
1606 } else {
1607 SynthesizePID = PID;
1608 }
1609 }
1610 }
1611 }
1612 }
1613
1614 // FIXME: This is not very efficient.
1615 S = "T";
1616
1617 // Encode result type.
1618 // FIXME: GCC uses a generating_property_type_encoding mode during
1619 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001620 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001621
1622 if (PD->isReadOnly()) {
1623 S += ",R";
1624 } else {
1625 switch (PD->getSetterKind()) {
1626 case ObjCPropertyDecl::Assign: break;
1627 case ObjCPropertyDecl::Copy: S += ",C"; break;
1628 case ObjCPropertyDecl::Retain: S += ",&"; break;
1629 }
1630 }
1631
1632 // It really isn't clear at all what this means, since properties
1633 // are "dynamic by default".
1634 if (Dynamic)
1635 S += ",D";
1636
1637 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1638 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001639 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001640 }
1641
1642 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1643 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001644 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001645 }
1646
1647 if (SynthesizePID) {
1648 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1649 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001650 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001651 }
1652
1653 // FIXME: OBJCGC: weak & strong
1654}
1655
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001656void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001657 bool NameFields) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001658 // We follow the behavior of gcc, expanding structures which are
1659 // directly pointed to, and expanding embedded structures. Note that
1660 // these rules are sufficient to prevent recursive encoding of the
1661 // same type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001662 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001663}
1664
1665void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1666 bool ExpandPointedToStructures,
1667 bool ExpandStructures,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001668 bool NameFields) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001669 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001670 char encoding;
1671 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001672 default: assert(0 && "Unhandled builtin type kind");
1673 case BuiltinType::Void: encoding = 'v'; break;
1674 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001675 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001676 case BuiltinType::UChar: encoding = 'C'; break;
1677 case BuiltinType::UShort: encoding = 'S'; break;
1678 case BuiltinType::UInt: encoding = 'I'; break;
1679 case BuiltinType::ULong: encoding = 'L'; break;
1680 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001681 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001682 case BuiltinType::SChar: encoding = 'c'; break;
1683 case BuiltinType::Short: encoding = 's'; break;
1684 case BuiltinType::Int: encoding = 'i'; break;
1685 case BuiltinType::Long: encoding = 'l'; break;
1686 case BuiltinType::LongLong: encoding = 'q'; break;
1687 case BuiltinType::Float: encoding = 'f'; break;
1688 case BuiltinType::Double: encoding = 'd'; break;
1689 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001690 }
1691
1692 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001693 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001694 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001695 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001696 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1697 ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001698 ExpandStructures, NameFields);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001699 }
1700 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001701 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001702 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001703 S += '@';
1704 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001705 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001706 S += '#';
1707 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001708 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001709 S += ':';
1710 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001711 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001712
1713 if (PointeeTy->isCharType()) {
1714 // char pointer types should be encoded as '*' unless it is a
1715 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001716 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001717 S += '*';
1718 return;
1719 }
1720 }
1721
1722 S += '^';
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001723 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001724 false, ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001725 NameFields);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001726 } else if (const ArrayType *AT =
1727 // Ignore type qualifiers etc.
1728 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001729 S += '[';
1730
1731 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1732 S += llvm::utostr(CAT->getSize().getZExtValue());
1733 else
1734 assert(0 && "Unhandled array type!");
1735
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001736 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001737 false, ExpandStructures, NameFields);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001738 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001739 } else if (T->getAsFunctionType()) {
1740 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001741 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001742 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001743 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001744 // Anonymous structures print as '?'
1745 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1746 S += II->getName();
1747 } else {
1748 S += '?';
1749 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001750 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001751 S += '=';
1752 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001753 FieldDecl *FD = RDecl->getMember(i);
1754 if (NameFields) {
1755 S += '"';
Chris Lattner39f34e92008-11-24 04:00:27 +00001756 S += FD->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001757 S += '"';
1758 }
1759
1760 // Special case bit-fields.
1761 if (const Expr *E = FD->getBitWidth()) {
1762 // FIXME: Fix constness.
1763 ASTContext *Ctx = const_cast<ASTContext*>(this);
1764 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1765 // FIXME: Obj-C is losing information about the type size
1766 // here. Investigate if this is a problem.
1767 S += 'b';
1768 S += llvm::utostr(N);
1769 } else {
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001770 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001771 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001772 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001773 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001774 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001775 } else if (T->isEnumeralType()) {
1776 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001777 } else if (T->isBlockPointerType()) {
1778 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001779 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001780 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001781}
1782
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001783void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001784 std::string& S) const {
1785 if (QT & Decl::OBJC_TQ_In)
1786 S += 'n';
1787 if (QT & Decl::OBJC_TQ_Inout)
1788 S += 'N';
1789 if (QT & Decl::OBJC_TQ_Out)
1790 S += 'o';
1791 if (QT & Decl::OBJC_TQ_Bycopy)
1792 S += 'O';
1793 if (QT & Decl::OBJC_TQ_Byref)
1794 S += 'R';
1795 if (QT & Decl::OBJC_TQ_Oneway)
1796 S += 'V';
1797}
1798
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001799void ASTContext::setBuiltinVaListType(QualType T)
1800{
1801 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1802
1803 BuiltinVaListType = T;
1804}
1805
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001806void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001807{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001808 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001809
1810 // typedef struct objc_object *id;
1811 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1812 assert(ptr && "'id' incorrectly typed");
1813 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1814 assert(rec && "'id' incorrectly typed");
1815 IdStructType = rec;
1816}
1817
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001818void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001819{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001820 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001821
1822 // typedef struct objc_selector *SEL;
1823 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1824 assert(ptr && "'SEL' incorrectly typed");
1825 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1826 assert(rec && "'SEL' incorrectly typed");
1827 SelStructType = rec;
1828}
1829
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001830void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001831{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001832 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001833}
1834
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001835void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001836{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001837 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001838
1839 // typedef struct objc_class *Class;
1840 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1841 assert(ptr && "'Class' incorrectly typed");
1842 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1843 assert(rec && "'Class' incorrectly typed");
1844 ClassStructType = rec;
1845}
1846
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001847void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1848 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001849 "'NSConstantString' type already set!");
1850
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001851 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001852}
1853
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001854/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00001855/// TargetInfo, produce the corresponding type. The unsigned @p Type
1856/// is actually a value of type @c TargetInfo::IntType.
1857QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001858 switch (Type) {
1859 case TargetInfo::NoInt: return QualType();
1860 case TargetInfo::SignedShort: return ShortTy;
1861 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1862 case TargetInfo::SignedInt: return IntTy;
1863 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1864 case TargetInfo::SignedLong: return LongTy;
1865 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1866 case TargetInfo::SignedLongLong: return LongLongTy;
1867 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1868 }
1869
1870 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00001871 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001872}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001873
1874//===----------------------------------------------------------------------===//
1875// Type Predicates.
1876//===----------------------------------------------------------------------===//
1877
1878/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1879/// to an object type. This includes "id" and "Class" (two 'special' pointers
1880/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1881/// ID type).
1882bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1883 if (Ty->isObjCQualifiedIdType())
1884 return true;
1885
Steve Naroff6ae98502008-10-21 18:24:04 +00001886 // Blocks are objects.
1887 if (Ty->isBlockPointerType())
1888 return true;
1889
1890 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001891 if (!Ty->isPointerType())
1892 return false;
1893
1894 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1895 // pointer types. This looks for the typedef specifically, not for the
1896 // underlying type.
1897 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1898 return true;
1899
1900 // If this a pointer to an interface (e.g. NSString*), it is ok.
1901 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1902}
1903
Chris Lattner6ac46a42008-04-07 06:51:04 +00001904//===----------------------------------------------------------------------===//
1905// Type Compatibility Testing
1906//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001907
Steve Naroff1c7d0672008-09-04 15:10:53 +00001908/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001909/// block types. Types must be strictly compatible here. For example,
1910/// C unfortunately doesn't produce an error for the following:
1911///
1912/// int (*emptyArgFunc)();
1913/// int (*intArgList)(int) = emptyArgFunc;
1914///
1915/// For blocks, we will produce an error for the following (similar to C++):
1916///
1917/// int (^emptyArgBlock)();
1918/// int (^intArgBlock)(int) = emptyArgBlock;
1919///
1920/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1921///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001922bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00001923 const FunctionType *lbase = lhs->getAsFunctionType();
1924 const FunctionType *rbase = rhs->getAsFunctionType();
1925 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1926 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1927 if (lproto && rproto)
1928 return !mergeTypes(lhs, rhs).isNull();
1929 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00001930}
1931
Chris Lattner6ac46a42008-04-07 06:51:04 +00001932/// areCompatVectorTypes - Return true if the two specified vector types are
1933/// compatible.
1934static bool areCompatVectorTypes(const VectorType *LHS,
1935 const VectorType *RHS) {
1936 assert(LHS->isCanonical() && RHS->isCanonical());
1937 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00001938 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00001939}
1940
Eli Friedman3d815e72008-08-22 00:56:42 +00001941/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001942/// compatible for assignment from RHS to LHS. This handles validation of any
1943/// protocol qualifiers on the LHS or RHS.
1944///
Eli Friedman3d815e72008-08-22 00:56:42 +00001945bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1946 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001947 // Verify that the base decls are compatible: the RHS must be a subclass of
1948 // the LHS.
1949 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1950 return false;
1951
1952 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1953 // protocol qualified at all, then we are good.
1954 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1955 return true;
1956
1957 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1958 // isn't a superset.
1959 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1960 return true; // FIXME: should return false!
1961
1962 // Finally, we must have two protocol-qualified interfaces.
1963 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1964 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1965 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1966 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1967 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1968 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1969
1970 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1971 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1972 // LHS in a single parallel scan until we run out of elements in LHS.
1973 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1974 ObjCProtocolDecl *LHSProto = *LHSPI;
1975
1976 while (RHSPI != RHSPE) {
1977 ObjCProtocolDecl *RHSProto = *RHSPI++;
1978 // If the RHS has a protocol that the LHS doesn't, ignore it.
1979 if (RHSProto != LHSProto)
1980 continue;
1981
1982 // Otherwise, the RHS does have this element.
1983 ++LHSPI;
1984 if (LHSPI == LHSPE)
1985 return true; // All protocols in LHS exist in RHS.
1986
1987 LHSProto = *LHSPI;
1988 }
1989
1990 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1991 return false;
1992}
1993
Steve Naroffec0550f2007-10-15 20:41:53 +00001994/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1995/// both shall have the identically qualified version of a compatible type.
1996/// C99 6.2.7p1: Two types have compatible types if their types are the
1997/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001998bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1999 return !mergeTypes(LHS, RHS).isNull();
2000}
2001
2002QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2003 const FunctionType *lbase = lhs->getAsFunctionType();
2004 const FunctionType *rbase = rhs->getAsFunctionType();
2005 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2006 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2007 bool allLTypes = true;
2008 bool allRTypes = true;
2009
2010 // Check return type
2011 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2012 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002013 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2014 allLTypes = false;
2015 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2016 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002017
2018 if (lproto && rproto) { // two C99 style function prototypes
2019 unsigned lproto_nargs = lproto->getNumArgs();
2020 unsigned rproto_nargs = rproto->getNumArgs();
2021
2022 // Compatible functions must have the same number of arguments
2023 if (lproto_nargs != rproto_nargs)
2024 return QualType();
2025
2026 // Variadic and non-variadic functions aren't compatible
2027 if (lproto->isVariadic() != rproto->isVariadic())
2028 return QualType();
2029
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002030 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2031 return QualType();
2032
Eli Friedman3d815e72008-08-22 00:56:42 +00002033 // Check argument compatibility
2034 llvm::SmallVector<QualType, 10> types;
2035 for (unsigned i = 0; i < lproto_nargs; i++) {
2036 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2037 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2038 QualType argtype = mergeTypes(largtype, rargtype);
2039 if (argtype.isNull()) return QualType();
2040 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002041 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2042 allLTypes = false;
2043 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2044 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002045 }
2046 if (allLTypes) return lhs;
2047 if (allRTypes) return rhs;
2048 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002049 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002050 }
2051
2052 if (lproto) allRTypes = false;
2053 if (rproto) allLTypes = false;
2054
2055 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2056 if (proto) {
2057 if (proto->isVariadic()) return QualType();
2058 // Check that the types are compatible with the types that
2059 // would result from default argument promotions (C99 6.7.5.3p15).
2060 // The only types actually affected are promotable integer
2061 // types and floats, which would be passed as a different
2062 // type depending on whether the prototype is visible.
2063 unsigned proto_nargs = proto->getNumArgs();
2064 for (unsigned i = 0; i < proto_nargs; ++i) {
2065 QualType argTy = proto->getArgType(i);
2066 if (argTy->isPromotableIntegerType() ||
2067 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2068 return QualType();
2069 }
2070
2071 if (allLTypes) return lhs;
2072 if (allRTypes) return rhs;
2073 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002074 proto->getNumArgs(), lproto->isVariadic(),
2075 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002076 }
2077
2078 if (allLTypes) return lhs;
2079 if (allRTypes) return rhs;
2080 return getFunctionTypeNoProto(retType);
2081}
2082
2083QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002084 // C++ [expr]: If an expression initially has the type "reference to T", the
2085 // type is adjusted to "T" prior to any further analysis, the expression
2086 // designates the object or function denoted by the reference, and the
2087 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002088 // FIXME: C++ shouldn't be going through here! The rules are different
2089 // enough that they should be handled separately.
2090 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002091 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002092 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002093 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002094
Eli Friedman3d815e72008-08-22 00:56:42 +00002095 QualType LHSCan = getCanonicalType(LHS),
2096 RHSCan = getCanonicalType(RHS);
2097
2098 // If two types are identical, they are compatible.
2099 if (LHSCan == RHSCan)
2100 return LHS;
2101
2102 // If the qualifiers are different, the types aren't compatible
2103 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2104 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2105 return QualType();
2106
2107 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2108 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2109
Chris Lattner1adb8832008-01-14 05:45:46 +00002110 // We want to consider the two function types to be the same for these
2111 // comparisons, just force one to the other.
2112 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2113 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002114
2115 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002116 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2117 LHSClass = Type::ConstantArray;
2118 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2119 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002120
Nate Begeman213541a2008-04-18 23:10:10 +00002121 // Canonicalize ExtVector -> Vector.
2122 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2123 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002124
Chris Lattnerb0489812008-04-07 06:38:24 +00002125 // Consider qualified interfaces and interfaces the same.
2126 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2127 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002128
Chris Lattnera36a61f2008-04-07 05:43:21 +00002129 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002130 if (LHSClass != RHSClass) {
Steve Naroff97341622008-06-04 15:07:33 +00002131 // ID is compatible with all qualified id types.
Eli Friedman3d815e72008-08-22 00:56:42 +00002132 if (LHS->isObjCQualifiedIdType()) {
Steve Naroff87f3b932008-10-20 18:19:10 +00002133 if (const PointerType *PT = RHS->getAsPointerType()) {
2134 QualType pType = PT->getPointeeType();
2135 if (isObjCIdType(pType))
Eli Friedman3d815e72008-08-22 00:56:42 +00002136 return LHS;
Steve Naroff87f3b932008-10-20 18:19:10 +00002137 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2138 // Unfortunately, this API is part of Sema (which we don't have access
2139 // to. Need to refactor. The following check is insufficient, since we
2140 // need to make sure the class implements the protocol.
2141 if (pType->isObjCInterfaceType())
2142 return LHS;
2143 }
Steve Naroff97341622008-06-04 15:07:33 +00002144 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002145 if (RHS->isObjCQualifiedIdType()) {
Steve Naroff87f3b932008-10-20 18:19:10 +00002146 if (const PointerType *PT = LHS->getAsPointerType()) {
2147 QualType pType = PT->getPointeeType();
2148 if (isObjCIdType(pType))
Eli Friedman3d815e72008-08-22 00:56:42 +00002149 return RHS;
Steve Naroff87f3b932008-10-20 18:19:10 +00002150 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2151 // Unfortunately, this API is part of Sema (which we don't have access
2152 // to. Need to refactor. The following check is insufficient, since we
2153 // need to make sure the class implements the protocol.
2154 if (pType->isObjCInterfaceType())
2155 return RHS;
2156 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002157 }
2158
Chris Lattner1adb8832008-01-14 05:45:46 +00002159 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2160 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002161 if (const EnumType* ETy = LHS->getAsEnumType()) {
2162 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2163 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002164 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002165 if (const EnumType* ETy = RHS->getAsEnumType()) {
2166 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2167 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002168 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002169
Eli Friedman3d815e72008-08-22 00:56:42 +00002170 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002171 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002172
Steve Naroff4a746782008-01-09 22:43:08 +00002173 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002174 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002175 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002176 {
2177 // Merge two pointer types, while trying to preserve typedef info
2178 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2179 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2180 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2181 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002182 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2183 return LHS;
2184 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2185 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002186 return getPointerType(ResultType);
2187 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002188 case Type::BlockPointer:
2189 {
2190 // Merge two block pointer types, while trying to preserve typedef info
2191 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2192 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2193 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2194 if (ResultType.isNull()) return QualType();
2195 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2196 return LHS;
2197 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2198 return RHS;
2199 return getBlockPointerType(ResultType);
2200 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002201 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002202 {
2203 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2204 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2205 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2206 return QualType();
2207
2208 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2209 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2210 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2211 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002212 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2213 return LHS;
2214 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2215 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002216 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2217 ArrayType::ArraySizeModifier(), 0);
2218 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2219 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002220 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2221 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002222 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2223 return LHS;
2224 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2225 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002226 if (LVAT) {
2227 // FIXME: This isn't correct! But tricky to implement because
2228 // the array's size has to be the size of LHS, but the type
2229 // has to be different.
2230 return LHS;
2231 }
2232 if (RVAT) {
2233 // FIXME: This isn't correct! But tricky to implement because
2234 // the array's size has to be the size of RHS, but the type
2235 // has to be different.
2236 return RHS;
2237 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002238 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2239 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002240 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002241 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002242 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002243 return mergeFunctionTypes(LHS, RHS);
2244 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002245 // FIXME: Why are these compatible?
2246 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2247 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2248 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002249 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002250 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002251 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002252 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002253 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2254 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002255 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002256 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002257 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2258 // for checking assignment/comparison safety
2259 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002260 default:
2261 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002262 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002263 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002264}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002265
Chris Lattner5426bf62008-04-07 07:01:58 +00002266//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002267// Integer Predicates
2268//===----------------------------------------------------------------------===//
2269unsigned ASTContext::getIntWidth(QualType T) {
2270 if (T == BoolTy)
2271 return 1;
2272 // At the moment, only bool has padding bits
2273 return (unsigned)getTypeSize(T);
2274}
2275
2276QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2277 assert(T->isSignedIntegerType() && "Unexpected type");
2278 if (const EnumType* ETy = T->getAsEnumType())
2279 T = ETy->getDecl()->getIntegerType();
2280 const BuiltinType* BTy = T->getAsBuiltinType();
2281 assert (BTy && "Unexpected signed integer type");
2282 switch (BTy->getKind()) {
2283 case BuiltinType::Char_S:
2284 case BuiltinType::SChar:
2285 return UnsignedCharTy;
2286 case BuiltinType::Short:
2287 return UnsignedShortTy;
2288 case BuiltinType::Int:
2289 return UnsignedIntTy;
2290 case BuiltinType::Long:
2291 return UnsignedLongTy;
2292 case BuiltinType::LongLong:
2293 return UnsignedLongLongTy;
2294 default:
2295 assert(0 && "Unexpected signed integer type");
2296 return QualType();
2297 }
2298}
2299
2300
2301//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002302// Serialization Support
2303//===----------------------------------------------------------------------===//
2304
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002305/// Emit - Serialize an ASTContext object to Bitcode.
2306void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002307 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002308 S.EmitRef(SourceMgr);
2309 S.EmitRef(Target);
2310 S.EmitRef(Idents);
2311 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002312
Ted Kremenekfee04522007-10-31 22:44:07 +00002313 // Emit the size of the type vector so that we can reserve that size
2314 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002315 S.EmitInt(Types.size());
2316
Ted Kremenek03ed4402007-11-13 22:02:55 +00002317 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2318 I!=E;++I)
2319 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002320
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002321 S.EmitOwnedPtr(TUDecl);
2322
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002323 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002324}
2325
Ted Kremenek0f84c002007-11-13 00:25:37 +00002326ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002327
2328 // Read the language options.
2329 LangOptions LOpts;
2330 LOpts.Read(D);
2331
Ted Kremenekfee04522007-10-31 22:44:07 +00002332 SourceManager &SM = D.ReadRef<SourceManager>();
2333 TargetInfo &t = D.ReadRef<TargetInfo>();
2334 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2335 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002336
Ted Kremenekfee04522007-10-31 22:44:07 +00002337 unsigned size_reserve = D.ReadInt();
2338
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002339 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2340 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002341
Ted Kremenek03ed4402007-11-13 22:02:55 +00002342 for (unsigned i = 0; i < size_reserve; ++i)
2343 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002344
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002345 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2346
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002347 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002348
2349 return A;
2350}