blob: 233a661a4c9a0e62953a4282948422a30f3d2940 [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 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1214 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1215 VAT->getSizeModifier(),
1216 VAT->getIndexTypeQualifier());
1217}
1218
1219
1220const ArrayType *ASTContext::getAsArrayType(QualType T) {
1221 // Handle the non-qualified case efficiently.
1222 if (T.getCVRQualifiers() == 0) {
1223 // Handle the common positive case fast.
1224 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1225 return AT;
1226 }
1227
1228 // Handle the common negative case fast, ignoring CVR qualifiers.
1229 QualType CType = T->getCanonicalTypeInternal();
1230
1231 // Make sure to look through type qualifiers (like ASQuals) for the negative
1232 // test.
1233 if (!isa<ArrayType>(CType) &&
1234 !isa<ArrayType>(CType.getUnqualifiedType()))
1235 return 0;
1236
1237 // Apply any CVR qualifiers from the array type to the element type. This
1238 // implements C99 6.7.3p8: "If the specification of an array type includes
1239 // any type qualifiers, the element type is so qualified, not the array type."
1240
1241 // If we get here, we either have type qualifiers on the type, or we have
1242 // sugar such as a typedef in the way. If we have type qualifiers on the type
1243 // we must propagate them down into the elemeng type.
1244 unsigned CVRQuals = T.getCVRQualifiers();
1245 unsigned AddrSpace = 0;
1246 Type *Ty = T.getTypePtr();
1247
1248 // Rip through ASQualType's and typedefs to get to a concrete type.
1249 while (1) {
1250 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1251 AddrSpace = ASQT->getAddressSpace();
1252 Ty = ASQT->getBaseType();
1253 } else {
1254 T = Ty->getDesugaredType();
1255 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1256 break;
1257 CVRQuals |= T.getCVRQualifiers();
1258 Ty = T.getTypePtr();
1259 }
1260 }
1261
1262 // If we have a simple case, just return now.
1263 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1264 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1265 return ATy;
1266
1267 // Otherwise, we have an array and we have qualifiers on it. Push the
1268 // qualifiers into the array element type and return a new array type.
1269 // Get the canonical version of the element with the extra qualifiers on it.
1270 // This can recursively sink qualifiers through multiple levels of arrays.
1271 QualType NewEltTy = ATy->getElementType();
1272 if (AddrSpace)
1273 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1274 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1275
1276 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1277 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1278 CAT->getSizeModifier(),
1279 CAT->getIndexTypeQualifier()));
1280 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1281 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1282 IAT->getSizeModifier(),
1283 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001284
Douglas Gregor898574e2008-12-05 23:32:09 +00001285 if (const DependentSizedArrayType *DSAT
1286 = dyn_cast<DependentSizedArrayType>(ATy))
1287 return cast<ArrayType>(
1288 getDependentSizedArrayType(NewEltTy,
1289 DSAT->getSizeExpr(),
1290 DSAT->getSizeModifier(),
1291 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001292
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001293 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1294 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1295 VAT->getSizeModifier(),
1296 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001297}
1298
1299
Chris Lattnere6327742008-04-02 05:18:44 +00001300/// getArrayDecayedType - Return the properly qualified result of decaying the
1301/// specified array type to a pointer. This operation is non-trivial when
1302/// handling typedefs etc. The canonical type of "T" must be an array type,
1303/// this returns a pointer to a properly qualified element of the array.
1304///
1305/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1306QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001307 // Get the element type with 'getAsArrayType' so that we don't lose any
1308 // typedefs in the element type of the array. This also handles propagation
1309 // of type qualifiers from the array type into the element type if present
1310 // (C99 6.7.3p8).
1311 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1312 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001313
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001314 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001315
1316 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001317 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001318}
1319
Reid Spencer5f016e22007-07-11 17:01:13 +00001320/// getFloatingRank - Return a relative rank for floating point types.
1321/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001322static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001323 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001325
Christopher Lambebb97e92008-02-04 02:31:56 +00001326 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001327 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 case BuiltinType::Float: return FloatRank;
1329 case BuiltinType::Double: return DoubleRank;
1330 case BuiltinType::LongDouble: return LongDoubleRank;
1331 }
1332}
1333
Steve Naroff716c7302007-08-27 01:41:48 +00001334/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1335/// point or a complex type (based on typeDomain/typeSize).
1336/// 'typeDomain' is a real floating point or complex type.
1337/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001338QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1339 QualType Domain) const {
1340 FloatingRank EltRank = getFloatingRank(Size);
1341 if (Domain->isComplexType()) {
1342 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001343 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001344 case FloatRank: return FloatComplexTy;
1345 case DoubleRank: return DoubleComplexTy;
1346 case LongDoubleRank: return LongDoubleComplexTy;
1347 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 }
Chris Lattner1361b112008-04-06 23:58:54 +00001349
1350 assert(Domain->isRealFloatingType() && "Unknown domain!");
1351 switch (EltRank) {
1352 default: assert(0 && "getFloatingRank(): illegal value for rank");
1353 case FloatRank: return FloatTy;
1354 case DoubleRank: return DoubleTy;
1355 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001356 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001357}
1358
Chris Lattner7cfeb082008-04-06 23:55:33 +00001359/// getFloatingTypeOrder - Compare the rank of the two specified floating
1360/// point types, ignoring the domain of the type (i.e. 'double' ==
1361/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1362/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001363int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1364 FloatingRank LHSR = getFloatingRank(LHS);
1365 FloatingRank RHSR = getFloatingRank(RHS);
1366
1367 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001368 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001369 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001370 return 1;
1371 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001372}
1373
Chris Lattnerf52ab252008-04-06 22:59:24 +00001374/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1375/// routine will assert if passed a built-in type that isn't an integer or enum,
1376/// or if it is not canonicalized.
1377static unsigned getIntegerRank(Type *T) {
1378 assert(T->isCanonical() && "T should be canonicalized");
1379 if (isa<EnumType>(T))
1380 return 4;
1381
1382 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001383 default: assert(0 && "getIntegerRank(): not a built-in integer");
1384 case BuiltinType::Bool:
1385 return 1;
1386 case BuiltinType::Char_S:
1387 case BuiltinType::Char_U:
1388 case BuiltinType::SChar:
1389 case BuiltinType::UChar:
1390 return 2;
1391 case BuiltinType::Short:
1392 case BuiltinType::UShort:
1393 return 3;
1394 case BuiltinType::Int:
1395 case BuiltinType::UInt:
1396 return 4;
1397 case BuiltinType::Long:
1398 case BuiltinType::ULong:
1399 return 5;
1400 case BuiltinType::LongLong:
1401 case BuiltinType::ULongLong:
1402 return 6;
Chris Lattnerf52ab252008-04-06 22:59:24 +00001403 }
1404}
1405
Chris Lattner7cfeb082008-04-06 23:55:33 +00001406/// getIntegerTypeOrder - Returns the highest ranked integer type:
1407/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1408/// LHS < RHS, return -1.
1409int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001410 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1411 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001412 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001413
Chris Lattnerf52ab252008-04-06 22:59:24 +00001414 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1415 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001416
Chris Lattner7cfeb082008-04-06 23:55:33 +00001417 unsigned LHSRank = getIntegerRank(LHSC);
1418 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419
Chris Lattner7cfeb082008-04-06 23:55:33 +00001420 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1421 if (LHSRank == RHSRank) return 0;
1422 return LHSRank > RHSRank ? 1 : -1;
1423 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001424
Chris Lattner7cfeb082008-04-06 23:55:33 +00001425 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1426 if (LHSUnsigned) {
1427 // If the unsigned [LHS] type is larger, return it.
1428 if (LHSRank >= RHSRank)
1429 return 1;
1430
1431 // If the signed type can represent all values of the unsigned type, it
1432 // wins. Because we are dealing with 2's complement and types that are
1433 // powers of two larger than each other, this is always safe.
1434 return -1;
1435 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001436
Chris Lattner7cfeb082008-04-06 23:55:33 +00001437 // If the unsigned [RHS] type is larger, return it.
1438 if (RHSRank >= LHSRank)
1439 return -1;
1440
1441 // If the signed type can represent all values of the unsigned type, it
1442 // wins. Because we are dealing with 2's complement and types that are
1443 // powers of two larger than each other, this is always safe.
1444 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001445}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001446
1447// getCFConstantStringType - Return the type used for constant CFStrings.
1448QualType ASTContext::getCFConstantStringType() {
1449 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001450 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001451 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001452 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001453 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001454
1455 // const int *isa;
1456 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001457 // int flags;
1458 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001459 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001460 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001461 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001462 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001463 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001464 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001465
Anders Carlssonf06273f2007-11-19 00:25:30 +00001466 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerb048c982008-04-06 04:47:34 +00001467 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner8e25d862008-03-16 00:16:02 +00001468 FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001469
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001470 CFConstantStringTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001471 }
1472
1473 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001474}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001475
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001476QualType ASTContext::getObjCFastEnumerationStateType()
1477{
1478 if (!ObjCFastEnumerationStateTypeDecl) {
1479 QualType FieldTypes[] = {
1480 UnsignedLongTy,
1481 getPointerType(ObjCIdType),
1482 getPointerType(UnsignedLongTy),
1483 getConstantArrayType(UnsignedLongTy,
1484 llvm::APInt(32, 5), ArrayType::Normal, 0)
1485 };
1486
1487 FieldDecl *FieldDecls[4];
1488 for (size_t i = 0; i < 4; ++i)
1489 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1490 FieldTypes[i]);
1491
1492 ObjCFastEnumerationStateTypeDecl =
1493 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001494 &Idents.get("__objcFastEnumerationState"));
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001495
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001496 ObjCFastEnumerationStateTypeDecl->defineBody(*this, FieldDecls, 4);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001497 }
1498
1499 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1500}
1501
Anders Carlssone8c49532007-10-29 06:33:42 +00001502// This returns true if a type has been typedefed to BOOL:
1503// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001504static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001505 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001506 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1507 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001508
1509 return false;
1510}
1511
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001513/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001515 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001516
1517 // Make all integer and enum types at least as large as an int
1518 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001519 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001520 // Treat arrays as pointers, since that's how they're passed in.
1521 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001522 sz = getTypeSize(VoidPtrTy);
1523 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001524}
1525
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001526/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001527/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001528void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001529 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001530 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001531 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001532 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001533 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001534 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001535 // Compute size of all parameters.
1536 // Start with computing size of a pointer in number of bytes.
1537 // FIXME: There might(should) be a better way of doing this computation!
1538 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001539 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001540 // The first two arguments (self and _cmd) are pointers; account for
1541 // their size.
1542 int ParmOffset = 2 * PtrSize;
1543 int NumOfParams = Decl->getNumParams();
1544 for (int i = 0; i < NumOfParams; i++) {
1545 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 int sz = getObjCEncodingTypeSize (PType);
1547 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001548 ParmOffset += sz;
1549 }
1550 S += llvm::utostr(ParmOffset);
1551 S += "@0:";
1552 S += llvm::utostr(PtrSize);
1553
1554 // Argument types.
1555 ParmOffset = 2 * PtrSize;
1556 for (int i = 0; i < NumOfParams; i++) {
1557 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001558 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001559 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001560 getObjCEncodingForTypeQualifier(
1561 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001562 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001563 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001564 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001565 }
1566}
1567
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001568/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1569/// method declaration. If non-NULL, Container must be either an
1570/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1571/// NULL when getting encodings for protocol properties.
1572void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1573 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001574 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001575 // Collect information from the property implementation decl(s).
1576 bool Dynamic = false;
1577 ObjCPropertyImplDecl *SynthesizePID = 0;
1578
1579 // FIXME: Duplicated code due to poor abstraction.
1580 if (Container) {
1581 if (const ObjCCategoryImplDecl *CID =
1582 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1583 for (ObjCCategoryImplDecl::propimpl_iterator
1584 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1585 ObjCPropertyImplDecl *PID = *i;
1586 if (PID->getPropertyDecl() == PD) {
1587 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1588 Dynamic = true;
1589 } else {
1590 SynthesizePID = PID;
1591 }
1592 }
1593 }
1594 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001595 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001596 for (ObjCCategoryImplDecl::propimpl_iterator
1597 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1598 ObjCPropertyImplDecl *PID = *i;
1599 if (PID->getPropertyDecl() == PD) {
1600 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1601 Dynamic = true;
1602 } else {
1603 SynthesizePID = PID;
1604 }
1605 }
1606 }
1607 }
1608 }
1609
1610 // FIXME: This is not very efficient.
1611 S = "T";
1612
1613 // Encode result type.
1614 // FIXME: GCC uses a generating_property_type_encoding mode during
1615 // this part. Investigate.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001616 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001617
1618 if (PD->isReadOnly()) {
1619 S += ",R";
1620 } else {
1621 switch (PD->getSetterKind()) {
1622 case ObjCPropertyDecl::Assign: break;
1623 case ObjCPropertyDecl::Copy: S += ",C"; break;
1624 case ObjCPropertyDecl::Retain: S += ",&"; break;
1625 }
1626 }
1627
1628 // It really isn't clear at all what this means, since properties
1629 // are "dynamic by default".
1630 if (Dynamic)
1631 S += ",D";
1632
1633 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1634 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001635 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001636 }
1637
1638 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1639 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001640 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001641 }
1642
1643 if (SynthesizePID) {
1644 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1645 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001646 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001647 }
1648
1649 // FIXME: OBJCGC: weak & strong
1650}
1651
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001652void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001653 bool NameFields) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001654 // We follow the behavior of gcc, expanding structures which are
1655 // directly pointed to, and expanding embedded structures. Note that
1656 // these rules are sufficient to prevent recursive encoding of the
1657 // same type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001658 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001659}
1660
1661void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1662 bool ExpandPointedToStructures,
1663 bool ExpandStructures,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001664 bool NameFields) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00001665 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001666 char encoding;
1667 switch (BT->getKind()) {
Chris Lattner71763312008-04-06 22:05:18 +00001668 default: assert(0 && "Unhandled builtin type kind");
1669 case BuiltinType::Void: encoding = 'v'; break;
1670 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001671 case BuiltinType::Char_U:
Chris Lattner71763312008-04-06 22:05:18 +00001672 case BuiltinType::UChar: encoding = 'C'; break;
1673 case BuiltinType::UShort: encoding = 'S'; break;
1674 case BuiltinType::UInt: encoding = 'I'; break;
1675 case BuiltinType::ULong: encoding = 'L'; break;
1676 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001677 case BuiltinType::Char_S:
Chris Lattner71763312008-04-06 22:05:18 +00001678 case BuiltinType::SChar: encoding = 'c'; break;
1679 case BuiltinType::Short: encoding = 's'; break;
1680 case BuiltinType::Int: encoding = 'i'; break;
1681 case BuiltinType::Long: encoding = 'l'; break;
1682 case BuiltinType::LongLong: encoding = 'q'; break;
1683 case BuiltinType::Float: encoding = 'f'; break;
1684 case BuiltinType::Double: encoding = 'd'; break;
1685 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001686 }
1687
1688 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001689 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001690 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001691 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001692 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1693 ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001694 ExpandStructures, NameFields);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001695 }
1696 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001697 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001698 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001699 S += '@';
1700 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001701 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001702 S += '#';
1703 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001704 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001705 S += ':';
1706 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001707 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001708
1709 if (PointeeTy->isCharType()) {
1710 // char pointer types should be encoded as '*' unless it is a
1711 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001712 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001713 S += '*';
1714 return;
1715 }
1716 }
1717
1718 S += '^';
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001719 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001720 false, ExpandPointedToStructures,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001721 NameFields);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001722 } else if (const ArrayType *AT =
1723 // Ignore type qualifiers etc.
1724 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001725 S += '[';
1726
1727 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1728 S += llvm::utostr(CAT->getSize().getZExtValue());
1729 else
1730 assert(0 && "Unhandled array type!");
1731
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001732 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001733 false, ExpandStructures, NameFields);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001734 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001735 } else if (T->getAsFunctionType()) {
1736 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001737 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00001738 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001739 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00001740 // Anonymous structures print as '?'
1741 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1742 S += II->getName();
1743 } else {
1744 S += '?';
1745 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001746 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001747 S += '=';
1748 for (int i = 0; i < RDecl->getNumMembers(); i++) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001749 FieldDecl *FD = RDecl->getMember(i);
1750 if (NameFields) {
1751 S += '"';
Chris Lattner39f34e92008-11-24 04:00:27 +00001752 S += FD->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001753 S += '"';
1754 }
1755
1756 // Special case bit-fields.
1757 if (const Expr *E = FD->getBitWidth()) {
1758 // FIXME: Fix constness.
1759 ASTContext *Ctx = const_cast<ASTContext*>(this);
1760 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1761 // FIXME: Obj-C is losing information about the type size
1762 // here. Investigate if this is a problem.
1763 S += 'b';
1764 S += llvm::utostr(N);
1765 } else {
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001766 getObjCEncodingForTypeImpl(FD->getType(), S, false, true, NameFields);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001767 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001768 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001769 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00001770 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001771 } else if (T->isEnumeralType()) {
1772 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00001773 } else if (T->isBlockPointerType()) {
1774 S += '^'; // This type string is the same as general pointers.
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001775 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001776 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001777}
1778
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001779void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001780 std::string& S) const {
1781 if (QT & Decl::OBJC_TQ_In)
1782 S += 'n';
1783 if (QT & Decl::OBJC_TQ_Inout)
1784 S += 'N';
1785 if (QT & Decl::OBJC_TQ_Out)
1786 S += 'o';
1787 if (QT & Decl::OBJC_TQ_Bycopy)
1788 S += 'O';
1789 if (QT & Decl::OBJC_TQ_Byref)
1790 S += 'R';
1791 if (QT & Decl::OBJC_TQ_Oneway)
1792 S += 'V';
1793}
1794
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001795void ASTContext::setBuiltinVaListType(QualType T)
1796{
1797 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1798
1799 BuiltinVaListType = T;
1800}
1801
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001802void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001803{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001804 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001805
1806 // typedef struct objc_object *id;
1807 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1808 assert(ptr && "'id' incorrectly typed");
1809 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1810 assert(rec && "'id' incorrectly typed");
1811 IdStructType = rec;
1812}
1813
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001814void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001815{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001816 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001817
1818 // typedef struct objc_selector *SEL;
1819 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1820 assert(ptr && "'SEL' incorrectly typed");
1821 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1822 assert(rec && "'SEL' incorrectly typed");
1823 SelStructType = rec;
1824}
1825
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001826void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001827{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001828 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001829}
1830
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001831void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001832{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001833 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001834
1835 // typedef struct objc_class *Class;
1836 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1837 assert(ptr && "'Class' incorrectly typed");
1838 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1839 assert(rec && "'Class' incorrectly typed");
1840 ClassStructType = rec;
1841}
1842
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001843void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1844 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001845 "'NSConstantString' type already set!");
1846
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001847 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001848}
1849
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001850/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00001851/// TargetInfo, produce the corresponding type. The unsigned @p Type
1852/// is actually a value of type @c TargetInfo::IntType.
1853QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001854 switch (Type) {
1855 case TargetInfo::NoInt: return QualType();
1856 case TargetInfo::SignedShort: return ShortTy;
1857 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1858 case TargetInfo::SignedInt: return IntTy;
1859 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1860 case TargetInfo::SignedLong: return LongTy;
1861 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1862 case TargetInfo::SignedLongLong: return LongLongTy;
1863 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1864 }
1865
1866 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00001867 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001868}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001869
1870//===----------------------------------------------------------------------===//
1871// Type Predicates.
1872//===----------------------------------------------------------------------===//
1873
1874/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1875/// to an object type. This includes "id" and "Class" (two 'special' pointers
1876/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1877/// ID type).
1878bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1879 if (Ty->isObjCQualifiedIdType())
1880 return true;
1881
Steve Naroff6ae98502008-10-21 18:24:04 +00001882 // Blocks are objects.
1883 if (Ty->isBlockPointerType())
1884 return true;
1885
1886 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00001887 if (!Ty->isPointerType())
1888 return false;
1889
1890 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1891 // pointer types. This looks for the typedef specifically, not for the
1892 // underlying type.
1893 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1894 return true;
1895
1896 // If this a pointer to an interface (e.g. NSString*), it is ok.
1897 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1898}
1899
Chris Lattner6ac46a42008-04-07 06:51:04 +00001900//===----------------------------------------------------------------------===//
1901// Type Compatibility Testing
1902//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00001903
Steve Naroff1c7d0672008-09-04 15:10:53 +00001904/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00001905/// block types. Types must be strictly compatible here. For example,
1906/// C unfortunately doesn't produce an error for the following:
1907///
1908/// int (*emptyArgFunc)();
1909/// int (*intArgList)(int) = emptyArgFunc;
1910///
1911/// For blocks, we will produce an error for the following (similar to C++):
1912///
1913/// int (^emptyArgBlock)();
1914/// int (^intArgBlock)(int) = emptyArgBlock;
1915///
1916/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1917///
Steve Naroff1c7d0672008-09-04 15:10:53 +00001918bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00001919 const FunctionType *lbase = lhs->getAsFunctionType();
1920 const FunctionType *rbase = rhs->getAsFunctionType();
1921 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1922 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1923 if (lproto && rproto)
1924 return !mergeTypes(lhs, rhs).isNull();
1925 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00001926}
1927
Chris Lattner6ac46a42008-04-07 06:51:04 +00001928/// areCompatVectorTypes - Return true if the two specified vector types are
1929/// compatible.
1930static bool areCompatVectorTypes(const VectorType *LHS,
1931 const VectorType *RHS) {
1932 assert(LHS->isCanonical() && RHS->isCanonical());
1933 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00001934 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00001935}
1936
Eli Friedman3d815e72008-08-22 00:56:42 +00001937/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00001938/// compatible for assignment from RHS to LHS. This handles validation of any
1939/// protocol qualifiers on the LHS or RHS.
1940///
Eli Friedman3d815e72008-08-22 00:56:42 +00001941bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1942 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00001943 // Verify that the base decls are compatible: the RHS must be a subclass of
1944 // the LHS.
1945 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1946 return false;
1947
1948 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1949 // protocol qualified at all, then we are good.
1950 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1951 return true;
1952
1953 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1954 // isn't a superset.
1955 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1956 return true; // FIXME: should return false!
1957
1958 // Finally, we must have two protocol-qualified interfaces.
1959 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1960 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1961 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1962 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1963 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1964 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1965
1966 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1967 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1968 // LHS in a single parallel scan until we run out of elements in LHS.
1969 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1970 ObjCProtocolDecl *LHSProto = *LHSPI;
1971
1972 while (RHSPI != RHSPE) {
1973 ObjCProtocolDecl *RHSProto = *RHSPI++;
1974 // If the RHS has a protocol that the LHS doesn't, ignore it.
1975 if (RHSProto != LHSProto)
1976 continue;
1977
1978 // Otherwise, the RHS does have this element.
1979 ++LHSPI;
1980 if (LHSPI == LHSPE)
1981 return true; // All protocols in LHS exist in RHS.
1982
1983 LHSProto = *LHSPI;
1984 }
1985
1986 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1987 return false;
1988}
1989
Steve Naroffec0550f2007-10-15 20:41:53 +00001990/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1991/// both shall have the identically qualified version of a compatible type.
1992/// C99 6.2.7p1: Two types have compatible types if their types are the
1993/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00001994bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
1995 return !mergeTypes(LHS, RHS).isNull();
1996}
1997
1998QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
1999 const FunctionType *lbase = lhs->getAsFunctionType();
2000 const FunctionType *rbase = rhs->getAsFunctionType();
2001 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2002 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2003 bool allLTypes = true;
2004 bool allRTypes = true;
2005
2006 // Check return type
2007 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2008 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002009 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2010 allLTypes = false;
2011 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2012 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002013
2014 if (lproto && rproto) { // two C99 style function prototypes
2015 unsigned lproto_nargs = lproto->getNumArgs();
2016 unsigned rproto_nargs = rproto->getNumArgs();
2017
2018 // Compatible functions must have the same number of arguments
2019 if (lproto_nargs != rproto_nargs)
2020 return QualType();
2021
2022 // Variadic and non-variadic functions aren't compatible
2023 if (lproto->isVariadic() != rproto->isVariadic())
2024 return QualType();
2025
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002026 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2027 return QualType();
2028
Eli Friedman3d815e72008-08-22 00:56:42 +00002029 // Check argument compatibility
2030 llvm::SmallVector<QualType, 10> types;
2031 for (unsigned i = 0; i < lproto_nargs; i++) {
2032 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2033 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2034 QualType argtype = mergeTypes(largtype, rargtype);
2035 if (argtype.isNull()) return QualType();
2036 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002037 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2038 allLTypes = false;
2039 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2040 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002041 }
2042 if (allLTypes) return lhs;
2043 if (allRTypes) return rhs;
2044 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002045 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002046 }
2047
2048 if (lproto) allRTypes = false;
2049 if (rproto) allLTypes = false;
2050
2051 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2052 if (proto) {
2053 if (proto->isVariadic()) return QualType();
2054 // Check that the types are compatible with the types that
2055 // would result from default argument promotions (C99 6.7.5.3p15).
2056 // The only types actually affected are promotable integer
2057 // types and floats, which would be passed as a different
2058 // type depending on whether the prototype is visible.
2059 unsigned proto_nargs = proto->getNumArgs();
2060 for (unsigned i = 0; i < proto_nargs; ++i) {
2061 QualType argTy = proto->getArgType(i);
2062 if (argTy->isPromotableIntegerType() ||
2063 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2064 return QualType();
2065 }
2066
2067 if (allLTypes) return lhs;
2068 if (allRTypes) return rhs;
2069 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002070 proto->getNumArgs(), lproto->isVariadic(),
2071 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002072 }
2073
2074 if (allLTypes) return lhs;
2075 if (allRTypes) return rhs;
2076 return getFunctionTypeNoProto(retType);
2077}
2078
2079QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002080 // C++ [expr]: If an expression initially has the type "reference to T", the
2081 // type is adjusted to "T" prior to any further analysis, the expression
2082 // designates the object or function denoted by the reference, and the
2083 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002084 // FIXME: C++ shouldn't be going through here! The rules are different
2085 // enough that they should be handled separately.
2086 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002087 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002088 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002089 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002090
Eli Friedman3d815e72008-08-22 00:56:42 +00002091 QualType LHSCan = getCanonicalType(LHS),
2092 RHSCan = getCanonicalType(RHS);
2093
2094 // If two types are identical, they are compatible.
2095 if (LHSCan == RHSCan)
2096 return LHS;
2097
2098 // If the qualifiers are different, the types aren't compatible
2099 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2100 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2101 return QualType();
2102
2103 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2104 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2105
Chris Lattner1adb8832008-01-14 05:45:46 +00002106 // We want to consider the two function types to be the same for these
2107 // comparisons, just force one to the other.
2108 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2109 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002110
2111 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002112 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2113 LHSClass = Type::ConstantArray;
2114 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2115 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002116
Nate Begeman213541a2008-04-18 23:10:10 +00002117 // Canonicalize ExtVector -> Vector.
2118 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2119 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002120
Chris Lattnerb0489812008-04-07 06:38:24 +00002121 // Consider qualified interfaces and interfaces the same.
2122 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2123 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002124
Steve Naroff4e78fd02008-12-10 20:07:25 +00002125 // ID is compatible with all qualified id types.
2126 if (LHS->isObjCQualifiedIdType()) {
2127 if (const PointerType *PT = RHS->getAsPointerType()) {
2128 QualType pType = PT->getPointeeType();
2129 if (isObjCIdType(pType))
2130 return LHS;
2131 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2132 // Unfortunately, this API is part of Sema (which we don't have access
2133 // to. Need to refactor. The following check is insufficient, since we
2134 // need to make sure the class implements the protocol.
2135 if (pType->isObjCInterfaceType())
2136 return LHS;
2137 }
2138 }
2139 if (RHS->isObjCQualifiedIdType()) {
2140 if (const PointerType *PT = LHS->getAsPointerType()) {
2141 QualType pType = PT->getPointeeType();
2142 if (isObjCIdType(pType))
2143 return RHS;
2144 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2145 // Unfortunately, this API is part of Sema (which we don't have access
2146 // to. Need to refactor. The following check is insufficient, since we
2147 // need to make sure the class implements the protocol.
2148 if (pType->isObjCInterfaceType())
2149 return RHS;
2150 }
2151 }
Chris Lattnera36a61f2008-04-07 05:43:21 +00002152 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002153 if (LHSClass != RHSClass) {
Eli Friedman3d815e72008-08-22 00:56:42 +00002154
Chris Lattner1adb8832008-01-14 05:45:46 +00002155 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2156 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002157 if (const EnumType* ETy = LHS->getAsEnumType()) {
2158 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2159 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002160 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002161 if (const EnumType* ETy = RHS->getAsEnumType()) {
2162 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2163 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002164 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002165
Eli Friedman3d815e72008-08-22 00:56:42 +00002166 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002167 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002168
Steve Naroff4a746782008-01-09 22:43:08 +00002169 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002170 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002171 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002172 {
2173 // Merge two pointer types, while trying to preserve typedef info
2174 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2175 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2176 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2177 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002178 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2179 return LHS;
2180 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2181 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002182 return getPointerType(ResultType);
2183 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002184 case Type::BlockPointer:
2185 {
2186 // Merge two block pointer types, while trying to preserve typedef info
2187 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2188 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2189 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2190 if (ResultType.isNull()) return QualType();
2191 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2192 return LHS;
2193 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2194 return RHS;
2195 return getBlockPointerType(ResultType);
2196 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002197 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002198 {
2199 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2200 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2201 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2202 return QualType();
2203
2204 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2205 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2206 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2207 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002208 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2209 return LHS;
2210 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2211 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002212 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2213 ArrayType::ArraySizeModifier(), 0);
2214 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2215 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002216 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2217 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002218 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2219 return LHS;
2220 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2221 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002222 if (LVAT) {
2223 // FIXME: This isn't correct! But tricky to implement because
2224 // the array's size has to be the size of LHS, but the type
2225 // has to be different.
2226 return LHS;
2227 }
2228 if (RVAT) {
2229 // FIXME: This isn't correct! But tricky to implement because
2230 // the array's size has to be the size of RHS, but the type
2231 // has to be different.
2232 return RHS;
2233 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002234 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2235 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002236 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002237 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002238 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002239 return mergeFunctionTypes(LHS, RHS);
2240 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002241 // FIXME: Why are these compatible?
2242 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2243 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2244 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002245 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002246 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002247 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002248 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002249 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2250 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002251 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002252 case Type::ObjCInterface:
Eli Friedman3d815e72008-08-22 00:56:42 +00002253 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2254 // for checking assignment/comparison safety
2255 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002256 default:
2257 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002258 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002259 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002260}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002261
Chris Lattner5426bf62008-04-07 07:01:58 +00002262//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002263// Integer Predicates
2264//===----------------------------------------------------------------------===//
2265unsigned ASTContext::getIntWidth(QualType T) {
2266 if (T == BoolTy)
2267 return 1;
2268 // At the moment, only bool has padding bits
2269 return (unsigned)getTypeSize(T);
2270}
2271
2272QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2273 assert(T->isSignedIntegerType() && "Unexpected type");
2274 if (const EnumType* ETy = T->getAsEnumType())
2275 T = ETy->getDecl()->getIntegerType();
2276 const BuiltinType* BTy = T->getAsBuiltinType();
2277 assert (BTy && "Unexpected signed integer type");
2278 switch (BTy->getKind()) {
2279 case BuiltinType::Char_S:
2280 case BuiltinType::SChar:
2281 return UnsignedCharTy;
2282 case BuiltinType::Short:
2283 return UnsignedShortTy;
2284 case BuiltinType::Int:
2285 return UnsignedIntTy;
2286 case BuiltinType::Long:
2287 return UnsignedLongTy;
2288 case BuiltinType::LongLong:
2289 return UnsignedLongLongTy;
2290 default:
2291 assert(0 && "Unexpected signed integer type");
2292 return QualType();
2293 }
2294}
2295
2296
2297//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002298// Serialization Support
2299//===----------------------------------------------------------------------===//
2300
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002301/// Emit - Serialize an ASTContext object to Bitcode.
2302void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002303 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002304 S.EmitRef(SourceMgr);
2305 S.EmitRef(Target);
2306 S.EmitRef(Idents);
2307 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002308
Ted Kremenekfee04522007-10-31 22:44:07 +00002309 // Emit the size of the type vector so that we can reserve that size
2310 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002311 S.EmitInt(Types.size());
2312
Ted Kremenek03ed4402007-11-13 22:02:55 +00002313 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2314 I!=E;++I)
2315 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002316
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002317 S.EmitOwnedPtr(TUDecl);
2318
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002319 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002320}
2321
Ted Kremenek0f84c002007-11-13 00:25:37 +00002322ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002323
2324 // Read the language options.
2325 LangOptions LOpts;
2326 LOpts.Read(D);
2327
Ted Kremenekfee04522007-10-31 22:44:07 +00002328 SourceManager &SM = D.ReadRef<SourceManager>();
2329 TargetInfo &t = D.ReadRef<TargetInfo>();
2330 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2331 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002332
Ted Kremenekfee04522007-10-31 22:44:07 +00002333 unsigned size_reserve = D.ReadInt();
2334
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002335 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2336 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002337
Ted Kremenek03ed4402007-11-13 22:02:55 +00002338 for (unsigned i = 0; i < size_reserve; ++i)
2339 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002340
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002341 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2342
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002343 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002344
2345 return A;
2346}