1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
//===--- SemaPseudoObject.cpp - Semantic Analysis for Pseudo-Objects ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for expressions involving
// pseudo-object references. Pseudo-objects are conceptual objects
// whose storage is entirely abstract and all accesses to which are
// translated through some sort of abstraction barrier.
//
// For example, Objective-C objects can have "properties", either
// declared or undeclared. A property may be accessed by writing
// expr.prop
// where 'expr' is an r-value of Objective-C pointer type and 'prop'
// is the name of the property. If this expression is used in a context
// needing an r-value, it is treated as if it were a message-send
// of the associated 'getter' selector, typically:
// [expr prop]
// If it is used as the LHS of a simple assignment, it is treated
// as a message-send of the associated 'setter' selector, typically:
// [expr setProp: RHS]
// If it is used as the LHS of a compound assignment, or the operand
// of a unary increment or decrement, both are required; for example,
// 'expr.prop *= 100' would be translated to:
// [expr setProp: [expr prop] * 100]
//
//===----------------------------------------------------------------------===//
#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/Initialization.h"
#include "clang/AST/ExprObjC.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang;
using namespace sema;
static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
const ObjCPropertyRefExpr *PRE) {
if (PRE->isObjectReceiver()) {
const ObjCObjectPointerType *PT =
PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
}
if (PRE->isSuperReceiver()) {
if (const ObjCObjectPointerType *PT =
PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
}
assert(PRE->isClassReceiver() && "Invalid expression");
QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
return S.LookupMethodInObjectType(sel, IT, false);
}
ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
assert(E->getValueKind() == VK_LValue &&
E->getObjectKind() == OK_ObjCProperty);
const ObjCPropertyRefExpr *PRE = E->getObjCProperty();
QualType ReceiverType;
if (PRE->isObjectReceiver())
ReceiverType = PRE->getBase()->getType();
else if (PRE->isSuperReceiver())
ReceiverType = PRE->getSuperReceiverType();
else
ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver());
ExprValueKind VK = VK_RValue;
QualType T;
if (PRE->isImplicitProperty()) {
if (ObjCMethodDecl *GetterMethod =
PRE->getImplicitPropertyGetter()) {
T = getMessageSendResultType(ReceiverType, GetterMethod,
PRE->isClassReceiver(),
PRE->isSuperReceiver());
VK = Expr::getValueKindForType(GetterMethod->getResultType());
} else {
Diag(PRE->getLocation(), diag::err_getter_not_found)
<< PRE->getBase()->getType();
return ExprError();
}
} else {
ObjCPropertyDecl *prop = PRE->getExplicitProperty();
ObjCMethodDecl *getter =
LookupMethodInReceiverType(*this, prop->getGetterName(), PRE);
if (getter && !getter->hasRelatedResultType())
DiagnosePropertyAccessorMismatch(prop, getter, PRE->getLocation());
if (!getter) getter = prop->getGetterMethodDecl();
// Figure out the type of the expression. Mostly this is the
// result type of the getter, if possible.
if (getter) {
T = getMessageSendResultType(ReceiverType, getter,
PRE->isClassReceiver(),
PRE->isSuperReceiver());
VK = Expr::getValueKindForType(getter->getResultType());
// As a special case, if the method returns 'id', try to get a
// better type from the property.
if (VK == VK_RValue && T->isObjCIdType() &&
prop->getType()->isObjCRetainableType())
T = prop->getType();
} else {
T = prop->getType();
VK = Expr::getValueKindForType(T);
T = T.getNonLValueExprType(Context);
}
}
E->setType(T);
E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, E, 0, VK);
ExprResult Result = MaybeBindToTemporary(E);
if (!Result.isInvalid())
E = Result.take();
return Owned(E);
}
namespace {
struct PseudoObjectInfo {
const ObjCPropertyRefExpr *RefExpr;
bool HasSetter;
Selector SetterSelector;
ParmVarDecl *SetterParam;
QualType SetterParamType;
void setSetter(ObjCMethodDecl *setter) {
HasSetter = true;
SetterParam = *setter->param_begin();
SetterParamType = SetterParam->getType().getUnqualifiedType();
}
PseudoObjectInfo(Sema &S, Expr *E)
: RefExpr(E->getObjCProperty()), HasSetter(false), SetterParam(0) {
assert(E->getValueKind() == VK_LValue &&
E->getObjectKind() == OK_ObjCProperty);
// Try to find a setter.
// For implicit properties, just trust the lookup we already did.
if (RefExpr->isImplicitProperty()) {
if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
setSetter(setter);
SetterSelector = setter->getSelector();
} else {
IdentifierInfo *getterName =
RefExpr->getImplicitPropertyGetter()->getSelector()
.getIdentifierInfoForSlot(0);
SetterSelector =
SelectorTable::constructSetterName(S.PP.getIdentifierTable(),
S.PP.getSelectorTable(),
getterName);
}
return;
}
// For explicit properties, this is more involved.
ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
SetterSelector = prop->getSetterName();
// Do a normal method lookup first.
if (ObjCMethodDecl *setter =
LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
setSetter(setter);
return;
}
// If that failed, trust the type on the @property declaration.
if (!prop->isReadOnly()) {
HasSetter = true;
SetterParamType = prop->getType().getUnqualifiedType();
}
}
};
}
/// Check an increment or decrement of a pseudo-object expression.
ExprResult Sema::checkPseudoObjectIncDec(Scope *S, SourceLocation opcLoc,
UnaryOperatorKind opcode, Expr *op) {
assert(UnaryOperator::isIncrementDecrementOp(opcode));
PseudoObjectInfo info(*this, op);
// If there's no setter, we have no choice but to try to assign to
// the result of the getter.
if (!info.HasSetter) {
QualType resultType = info.RefExpr->getGetterResultType();
assert(!resultType.isNull() && "property has no setter and no getter!");
// Only do this if the getter returns an l-value reference type.
if (const LValueReferenceType *refType
= resultType->getAs<LValueReferenceType>()) {
op = ImplicitCastExpr::Create(Context, refType->getPointeeType(),
CK_GetObjCProperty, op, 0, VK_LValue);
return BuildUnaryOp(S, opcLoc, opcode, op);
}
// Otherwise, it's an error.
Diag(opcLoc, diag::err_nosetter_property_incdec)
<< unsigned(info.RefExpr->isImplicitProperty())
<< unsigned(UnaryOperator::isDecrementOp(opcode))
<< info.SetterSelector
<< op->getSourceRange();
return ExprError();
}
// ++/-- behave like compound assignments, i.e. they need a getter.
QualType getterResultType = info.RefExpr->getGetterResultType();
if (getterResultType.isNull()) {
assert(info.RefExpr->isImplicitProperty());
Diag(opcLoc, diag::err_nogetter_property_incdec)
<< unsigned(UnaryOperator::isDecrementOp(opcode))
<< info.RefExpr->getImplicitPropertyGetter()->getSelector()
<< op->getSourceRange();
return ExprError();
}
// HACK: change the type of the operand to prevent further placeholder
// transformation.
op->setType(getterResultType.getNonLValueExprType(Context));
op->setObjectKind(OK_Ordinary);
ExprResult result = CreateBuiltinUnaryOp(opcLoc, opcode, op);
if (result.isInvalid()) return ExprError();
// Change the object kind back.
op->setObjectKind(OK_ObjCProperty);
return result;
}
ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
BinaryOperatorKind opcode,
Expr *LHS, Expr *RHS) {
assert(BinaryOperator::isAssignmentOp(opcode));
PseudoObjectInfo info(*this, LHS);
// If there's no setter, we have no choice but to try to assign to
// the result of the getter.
if (!info.HasSetter) {
QualType resultType = info.RefExpr->getGetterResultType();
assert(!resultType
|