blob: 06bcd8d00ded7ddcbed4a0797df459e4b40094ff [file] [log] [blame]
John McCall526ab472011-10-25 17:37:35 +00001//===--- SemaPseudoObject.cpp - Semantic Analysis for Pseudo-Objects ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
John McCall526ab472011-10-25 17:37:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis for expressions involving
10// pseudo-object references. Pseudo-objects are conceptual objects
11// whose storage is entirely abstract and all accesses to which are
12// translated through some sort of abstraction barrier.
13//
14// For example, Objective-C objects can have "properties", either
15// declared or undeclared. A property may be accessed by writing
16// expr.prop
17// where 'expr' is an r-value of Objective-C pointer type and 'prop'
18// is the name of the property. If this expression is used in a context
19// needing an r-value, it is treated as if it were a message-send
20// of the associated 'getter' selector, typically:
21// [expr prop]
22// If it is used as the LHS of a simple assignment, it is treated
23// as a message-send of the associated 'setter' selector, typically:
24// [expr setProp: RHS]
25// If it is used as the LHS of a compound assignment, or the operand
26// of a unary increment or decrement, both are required; for example,
27// 'expr.prop *= 100' would be translated to:
28// [expr setProp: [expr prop] * 100]
29//
30//===----------------------------------------------------------------------===//
31
32#include "clang/Sema/SemaInternal.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000033#include "clang/AST/ExprCXX.h"
John McCall526ab472011-10-25 17:37:35 +000034#include "clang/AST/ExprObjC.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000035#include "clang/Basic/CharInfo.h"
John McCall526ab472011-10-25 17:37:35 +000036#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000037#include "clang/Sema/Initialization.h"
38#include "clang/Sema/ScopeInfo.h"
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +000039#include "llvm/ADT/SmallString.h"
John McCall526ab472011-10-25 17:37:35 +000040
41using namespace clang;
42using namespace sema;
43
John McCallfe96e0b2011-11-06 09:01:30 +000044namespace {
45 // Basically just a very focused copy of TreeTransform.
Alexey Bataevf7630272015-11-25 12:01:00 +000046 struct Rebuilder {
John McCallfe96e0b2011-11-06 09:01:30 +000047 Sema &S;
Alexey Bataevf7630272015-11-25 12:01:00 +000048 unsigned MSPropertySubscriptCount;
49 typedef llvm::function_ref<Expr *(Expr *, unsigned)> SpecificRebuilderRefTy;
50 const SpecificRebuilderRefTy &SpecificCallback;
51 Rebuilder(Sema &S, const SpecificRebuilderRefTy &SpecificCallback)
52 : S(S), MSPropertySubscriptCount(0),
53 SpecificCallback(SpecificCallback) {}
John McCallfe96e0b2011-11-06 09:01:30 +000054
Alexey Bataevf7630272015-11-25 12:01:00 +000055 Expr *rebuildObjCPropertyRefExpr(ObjCPropertyRefExpr *refExpr) {
56 // Fortunately, the constraint that we're rebuilding something
57 // with a base limits the number of cases here.
58 if (refExpr->isClassReceiver() || refExpr->isSuperReceiver())
59 return refExpr;
60
61 if (refExpr->isExplicitProperty()) {
62 return new (S.Context) ObjCPropertyRefExpr(
63 refExpr->getExplicitProperty(), refExpr->getType(),
64 refExpr->getValueKind(), refExpr->getObjectKind(),
65 refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
66 }
67 return new (S.Context) ObjCPropertyRefExpr(
68 refExpr->getImplicitPropertyGetter(),
69 refExpr->getImplicitPropertySetter(), refExpr->getType(),
70 refExpr->getValueKind(), refExpr->getObjectKind(),
71 refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
72 }
73 Expr *rebuildObjCSubscriptRefExpr(ObjCSubscriptRefExpr *refExpr) {
74 assert(refExpr->getBaseExpr());
75 assert(refExpr->getKeyExpr());
76
77 return new (S.Context) ObjCSubscriptRefExpr(
78 SpecificCallback(refExpr->getBaseExpr(), 0),
79 SpecificCallback(refExpr->getKeyExpr(), 1), refExpr->getType(),
80 refExpr->getValueKind(), refExpr->getObjectKind(),
81 refExpr->getAtIndexMethodDecl(), refExpr->setAtIndexMethodDecl(),
82 refExpr->getRBracket());
83 }
84 Expr *rebuildMSPropertyRefExpr(MSPropertyRefExpr *refExpr) {
85 assert(refExpr->getBaseExpr());
86
87 return new (S.Context) MSPropertyRefExpr(
88 SpecificCallback(refExpr->getBaseExpr(), 0),
89 refExpr->getPropertyDecl(), refExpr->isArrow(), refExpr->getType(),
90 refExpr->getValueKind(), refExpr->getQualifierLoc(),
91 refExpr->getMemberLoc());
92 }
93 Expr *rebuildMSPropertySubscriptExpr(MSPropertySubscriptExpr *refExpr) {
94 assert(refExpr->getBase());
95 assert(refExpr->getIdx());
96
97 auto *NewBase = rebuild(refExpr->getBase());
98 ++MSPropertySubscriptCount;
99 return new (S.Context) MSPropertySubscriptExpr(
100 NewBase,
101 SpecificCallback(refExpr->getIdx(), MSPropertySubscriptCount),
102 refExpr->getType(), refExpr->getValueKind(), refExpr->getObjectKind(),
103 refExpr->getRBracketLoc());
104 }
John McCallfe96e0b2011-11-06 09:01:30 +0000105
106 Expr *rebuild(Expr *e) {
107 // Fast path: nothing to look through.
Alexey Bataevf7630272015-11-25 12:01:00 +0000108 if (auto *PRE = dyn_cast<ObjCPropertyRefExpr>(e))
109 return rebuildObjCPropertyRefExpr(PRE);
110 if (auto *SRE = dyn_cast<ObjCSubscriptRefExpr>(e))
111 return rebuildObjCSubscriptRefExpr(SRE);
112 if (auto *MSPRE = dyn_cast<MSPropertyRefExpr>(e))
113 return rebuildMSPropertyRefExpr(MSPRE);
114 if (auto *MSPSE = dyn_cast<MSPropertySubscriptExpr>(e))
115 return rebuildMSPropertySubscriptExpr(MSPSE);
John McCallfe96e0b2011-11-06 09:01:30 +0000116
117 // Otherwise, we should look through and rebuild anything that
118 // IgnoreParens would.
119
120 if (ParenExpr *parens = dyn_cast<ParenExpr>(e)) {
121 e = rebuild(parens->getSubExpr());
122 return new (S.Context) ParenExpr(parens->getLParen(),
123 parens->getRParen(),
124 e);
125 }
126
127 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(e)) {
128 assert(uop->getOpcode() == UO_Extension);
129 e = rebuild(uop->getSubExpr());
130 return new (S.Context) UnaryOperator(e, uop->getOpcode(),
131 uop->getType(),
132 uop->getValueKind(),
133 uop->getObjectKind(),
Aaron Ballmana5038552018-01-09 13:07:03 +0000134 uop->getOperatorLoc(),
135 uop->canOverflow());
John McCallfe96e0b2011-11-06 09:01:30 +0000136 }
137
138 if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
139 assert(!gse->isResultDependent());
140 unsigned resultIndex = gse->getResultIndex();
141 unsigned numAssocs = gse->getNumAssocs();
142
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000143 SmallVector<Expr *, 8> assocExprs;
144 SmallVector<TypeSourceInfo *, 8> assocTypes;
145 assocExprs.reserve(numAssocs);
146 assocTypes.reserve(numAssocs);
John McCallfe96e0b2011-11-06 09:01:30 +0000147
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000148 for (const GenericSelectionExpr::Association &assoc :
149 gse->associations()) {
150 Expr *assocExpr = assoc.getAssociationExpr();
151 if (assoc.isSelected())
152 assocExpr = rebuild(assocExpr);
153 assocExprs.push_back(assocExpr);
154 assocTypes.push_back(assoc.getTypeSourceInfo());
John McCallfe96e0b2011-11-06 09:01:30 +0000155 }
156
Bruno Riccidb076832019-01-26 14:15:10 +0000157 return GenericSelectionExpr::Create(
158 S.Context, gse->getGenericLoc(), gse->getControllingExpr(),
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000159 assocTypes, assocExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
Bruno Riccidb076832019-01-26 14:15:10 +0000160 gse->containsUnexpandedParameterPack(), resultIndex);
John McCallfe96e0b2011-11-06 09:01:30 +0000161 }
162
Eli Friedman75807f22013-07-20 00:40:58 +0000163 if (ChooseExpr *ce = dyn_cast<ChooseExpr>(e)) {
164 assert(!ce->isConditionDependent());
165
166 Expr *LHS = ce->getLHS(), *RHS = ce->getRHS();
167 Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS;
168 rebuiltExpr = rebuild(rebuiltExpr);
169
170 return new (S.Context) ChooseExpr(ce->getBuiltinLoc(),
171 ce->getCond(),
172 LHS, RHS,
173 rebuiltExpr->getType(),
174 rebuiltExpr->getValueKind(),
175 rebuiltExpr->getObjectKind(),
176 ce->getRParenLoc(),
177 ce->isConditionTrue(),
178 rebuiltExpr->isTypeDependent(),
179 rebuiltExpr->isValueDependent());
180 }
181
John McCallfe96e0b2011-11-06 09:01:30 +0000182 llvm_unreachable("bad expression to rebuild!");
183 }
184 };
185
John McCallfe96e0b2011-11-06 09:01:30 +0000186 class PseudoOpBuilder {
187 public:
188 Sema &S;
189 unsigned ResultIndex;
190 SourceLocation GenericLoc;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000191 bool IsUnique;
John McCallfe96e0b2011-11-06 09:01:30 +0000192 SmallVector<Expr *, 4> Semantics;
193
Akira Hatanaka797afe32018-03-20 01:47:58 +0000194 PseudoOpBuilder(Sema &S, SourceLocation genericLoc, bool IsUnique)
John McCallfe96e0b2011-11-06 09:01:30 +0000195 : S(S), ResultIndex(PseudoObjectExpr::NoResult),
Akira Hatanaka797afe32018-03-20 01:47:58 +0000196 GenericLoc(genericLoc), IsUnique(IsUnique) {}
John McCallfe96e0b2011-11-06 09:01:30 +0000197
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000198 virtual ~PseudoOpBuilder() {}
Matt Beaumont-Gayfb3cb9a2011-11-08 01:53:17 +0000199
John McCallfe96e0b2011-11-06 09:01:30 +0000200 /// Add a normal semantic expression.
201 void addSemanticExpr(Expr *semantic) {
202 Semantics.push_back(semantic);
203 }
204
205 /// Add the 'result' semantic expression.
206 void addResultSemanticExpr(Expr *resultExpr) {
207 assert(ResultIndex == PseudoObjectExpr::NoResult);
208 ResultIndex = Semantics.size();
209 Semantics.push_back(resultExpr);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000210 // An OVE is not unique if it is used as the result expression.
211 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
212 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000213 }
214
215 ExprResult buildRValueOperation(Expr *op);
216 ExprResult buildAssignmentOperation(Scope *Sc,
217 SourceLocation opLoc,
218 BinaryOperatorKind opcode,
219 Expr *LHS, Expr *RHS);
220 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
221 UnaryOperatorKind opcode,
222 Expr *op);
223
Jordan Rosed3934582012-09-28 22:21:30 +0000224 virtual ExprResult complete(Expr *syntacticForm);
John McCallfe96e0b2011-11-06 09:01:30 +0000225
226 OpaqueValueExpr *capture(Expr *op);
227 OpaqueValueExpr *captureValueAsResult(Expr *op);
228
229 void setResultToLastSemantic() {
230 assert(ResultIndex == PseudoObjectExpr::NoResult);
231 ResultIndex = Semantics.size() - 1;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000232 // An OVE is not unique if it is used as the result expression.
233 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
234 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000235 }
236
237 /// Return true if assignments have a non-void result.
Alexey Bataev60520e22015-12-10 04:38:18 +0000238 static bool CanCaptureValue(Expr *exp) {
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000239 if (exp->isGLValue())
240 return true;
241 QualType ty = exp->getType();
Eli Friedman00fa4292012-11-13 23:16:33 +0000242 assert(!ty->isIncompleteType());
243 assert(!ty->isDependentType());
244
245 if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl())
246 return ClassDecl->isTriviallyCopyable();
247 return true;
248 }
John McCallfe96e0b2011-11-06 09:01:30 +0000249
250 virtual Expr *rebuildAndCaptureObject(Expr *) = 0;
251 virtual ExprResult buildGet() = 0;
252 virtual ExprResult buildSet(Expr *, SourceLocation,
253 bool captureSetValueAsResult) = 0;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000254 /// Should the result of an assignment be the formal result of the
Alexey Bataev60520e22015-12-10 04:38:18 +0000255 /// setter call or the value that was passed to the setter?
256 ///
257 /// Different pseudo-object language features use different language rules
258 /// for this.
259 /// The default is to use the set value. Currently, this affects the
260 /// behavior of simple assignments, compound assignments, and prefix
261 /// increment and decrement.
262 /// Postfix increment and decrement always use the getter result as the
263 /// expression result.
264 ///
265 /// If this method returns true, and the set value isn't capturable for
266 /// some reason, the result of the expression will be void.
267 virtual bool captureSetValueAsResult() const { return true; }
John McCallfe96e0b2011-11-06 09:01:30 +0000268 };
269
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +0000270 /// A PseudoOpBuilder for Objective-C \@properties.
John McCallfe96e0b2011-11-06 09:01:30 +0000271 class ObjCPropertyOpBuilder : public PseudoOpBuilder {
272 ObjCPropertyRefExpr *RefExpr;
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000273 ObjCPropertyRefExpr *SyntacticRefExpr;
John McCallfe96e0b2011-11-06 09:01:30 +0000274 OpaqueValueExpr *InstanceReceiver;
275 ObjCMethodDecl *Getter;
276
277 ObjCMethodDecl *Setter;
278 Selector SetterSelector;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000279 Selector GetterSelector;
John McCallfe96e0b2011-11-06 09:01:30 +0000280
281 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000282 ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr, bool IsUnique)
283 : PseudoOpBuilder(S, refExpr->getLocation(), IsUnique),
284 RefExpr(refExpr), SyntacticRefExpr(nullptr),
285 InstanceReceiver(nullptr), Getter(nullptr), Setter(nullptr) {
John McCallfe96e0b2011-11-06 09:01:30 +0000286 }
287
288 ExprResult buildRValueOperation(Expr *op);
289 ExprResult buildAssignmentOperation(Scope *Sc,
290 SourceLocation opLoc,
291 BinaryOperatorKind opcode,
292 Expr *LHS, Expr *RHS);
293 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
294 UnaryOperatorKind opcode,
295 Expr *op);
296
297 bool tryBuildGetOfReference(Expr *op, ExprResult &result);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000298 bool findSetter(bool warn=true);
John McCallfe96e0b2011-11-06 09:01:30 +0000299 bool findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000300 void DiagnoseUnsupportedPropertyUse();
John McCallfe96e0b2011-11-06 09:01:30 +0000301
Craig Toppere14c0f82014-03-12 04:55:44 +0000302 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
303 ExprResult buildGet() override;
304 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
305 ExprResult complete(Expr *SyntacticForm) override;
Jordan Rosed3934582012-09-28 22:21:30 +0000306
307 bool isWeakProperty() const;
John McCallfe96e0b2011-11-06 09:01:30 +0000308 };
Ted Kremeneke65b0862012-03-06 20:05:56 +0000309
310 /// A PseudoOpBuilder for Objective-C array/dictionary indexing.
311 class ObjCSubscriptOpBuilder : public PseudoOpBuilder {
312 ObjCSubscriptRefExpr *RefExpr;
313 OpaqueValueExpr *InstanceBase;
314 OpaqueValueExpr *InstanceKey;
315 ObjCMethodDecl *AtIndexGetter;
316 Selector AtIndexGetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000317
Ted Kremeneke65b0862012-03-06 20:05:56 +0000318 ObjCMethodDecl *AtIndexSetter;
319 Selector AtIndexSetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000320
Ted Kremeneke65b0862012-03-06 20:05:56 +0000321 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000322 ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr, bool IsUnique)
323 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
324 RefExpr(refExpr), InstanceBase(nullptr), InstanceKey(nullptr),
325 AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
Craig Topperc3ec1492014-05-26 06:22:03 +0000326
Ted Kremeneke65b0862012-03-06 20:05:56 +0000327 ExprResult buildRValueOperation(Expr *op);
328 ExprResult buildAssignmentOperation(Scope *Sc,
329 SourceLocation opLoc,
330 BinaryOperatorKind opcode,
331 Expr *LHS, Expr *RHS);
Craig Toppere14c0f82014-03-12 04:55:44 +0000332 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
333
Ted Kremeneke65b0862012-03-06 20:05:56 +0000334 bool findAtIndexGetter();
335 bool findAtIndexSetter();
Craig Toppere14c0f82014-03-12 04:55:44 +0000336
337 ExprResult buildGet() override;
338 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Ted Kremeneke65b0862012-03-06 20:05:56 +0000339 };
340
John McCall5e77d762013-04-16 07:28:30 +0000341 class MSPropertyOpBuilder : public PseudoOpBuilder {
342 MSPropertyRefExpr *RefExpr;
Alexey Bataev69103472015-10-14 04:05:42 +0000343 OpaqueValueExpr *InstanceBase;
Alexey Bataevf7630272015-11-25 12:01:00 +0000344 SmallVector<Expr *, 4> CallArgs;
345
346 MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
John McCall5e77d762013-04-16 07:28:30 +0000347
348 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000349 MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr, bool IsUnique)
350 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
351 RefExpr(refExpr), InstanceBase(nullptr) {}
352 MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr, bool IsUnique)
353 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
Alexey Bataevf7630272015-11-25 12:01:00 +0000354 InstanceBase(nullptr) {
355 RefExpr = getBaseMSProperty(refExpr);
356 }
John McCall5e77d762013-04-16 07:28:30 +0000357
Craig Toppere14c0f82014-03-12 04:55:44 +0000358 Expr *rebuildAndCaptureObject(Expr *) override;
359 ExprResult buildGet() override;
360 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Alexey Bataev60520e22015-12-10 04:38:18 +0000361 bool captureSetValueAsResult() const override { return false; }
John McCall5e77d762013-04-16 07:28:30 +0000362 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000363}
John McCallfe96e0b2011-11-06 09:01:30 +0000364
365/// Capture the given expression in an OpaqueValueExpr.
366OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
367 // Make a new OVE whose source is the given expression.
Fangrui Song6907ce22018-07-30 19:24:48 +0000368 OpaqueValueExpr *captured =
John McCallfe96e0b2011-11-06 09:01:30 +0000369 new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
Douglas Gregor2d5aea02012-02-23 22:17:26 +0000370 e->getValueKind(), e->getObjectKind(),
371 e);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000372 if (IsUnique)
373 captured->setIsUnique(true);
374
John McCallfe96e0b2011-11-06 09:01:30 +0000375 // Make sure we bind that in the semantics.
376 addSemanticExpr(captured);
377 return captured;
378}
379
380/// Capture the given expression as the result of this pseudo-object
381/// operation. This routine is safe against expressions which may
382/// already be captured.
383///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000384/// \returns the captured expression, which will be the
John McCallfe96e0b2011-11-06 09:01:30 +0000385/// same as the input if the input was already captured
386OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
387 assert(ResultIndex == PseudoObjectExpr::NoResult);
388
389 // If the expression hasn't already been captured, just capture it
Fangrui Song6907ce22018-07-30 19:24:48 +0000390 // and set the new semantic
John McCallfe96e0b2011-11-06 09:01:30 +0000391 if (!isa<OpaqueValueExpr>(e)) {
392 OpaqueValueExpr *cap = capture(e);
393 setResultToLastSemantic();
394 return cap;
395 }
396
397 // Otherwise, it must already be one of our semantic expressions;
398 // set ResultIndex to its index.
399 unsigned index = 0;
400 for (;; ++index) {
401 assert(index < Semantics.size() &&
402 "captured expression not found in semantics!");
403 if (e == Semantics[index]) break;
404 }
405 ResultIndex = index;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000406 // An OVE is not unique if it is used as the result expression.
407 cast<OpaqueValueExpr>(e)->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000408 return cast<OpaqueValueExpr>(e);
409}
410
411/// The routine which creates the final PseudoObjectExpr.
412ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
413 return PseudoObjectExpr::Create(S.Context, syntactic,
414 Semantics, ResultIndex);
415}
416
417/// The main skeleton for building an r-value operation.
418ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
419 Expr *syntacticBase = rebuildAndCaptureObject(op);
420
421 ExprResult getExpr = buildGet();
422 if (getExpr.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000423 addResultSemanticExpr(getExpr.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000424
425 return complete(syntacticBase);
426}
427
428/// The basic skeleton for building a simple or compound
429/// assignment operation.
430ExprResult
431PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
432 BinaryOperatorKind opcode,
433 Expr *LHS, Expr *RHS) {
434 assert(BinaryOperator::isAssignmentOp(opcode));
435
436 Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
437 OpaqueValueExpr *capturedRHS = capture(RHS);
438
John McCallee04aeb2015-08-22 00:35:27 +0000439 // In some very specific cases, semantic analysis of the RHS as an
440 // expression may require it to be rewritten. In these cases, we
441 // cannot safely keep the OVE around. Fortunately, we don't really
442 // need to: we don't use this particular OVE in multiple places, and
443 // no clients rely that closely on matching up expressions in the
444 // semantic expression with expressions from the syntactic form.
445 Expr *semanticRHS = capturedRHS;
446 if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
447 semanticRHS = RHS;
448 Semantics.pop_back();
449 }
450
John McCallfe96e0b2011-11-06 09:01:30 +0000451 Expr *syntactic;
452
453 ExprResult result;
454 if (opcode == BO_Assign) {
John McCallee04aeb2015-08-22 00:35:27 +0000455 result = semanticRHS;
John McCallfe96e0b2011-11-06 09:01:30 +0000456 syntactic = new (S.Context) BinaryOperator(syntacticLHS, capturedRHS,
457 opcode, capturedRHS->getType(),
458 capturedRHS->getValueKind(),
Adam Nemet484aa452017-03-27 19:17:25 +0000459 OK_Ordinary, opcLoc,
460 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000461 } else {
462 ExprResult opLHS = buildGet();
463 if (opLHS.isInvalid()) return ExprError();
464
465 // Build an ordinary, non-compound operation.
466 BinaryOperatorKind nonCompound =
467 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000468 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000469 if (result.isInvalid()) return ExprError();
470
471 syntactic =
472 new (S.Context) CompoundAssignOperator(syntacticLHS, capturedRHS, opcode,
473 result.get()->getType(),
474 result.get()->getValueKind(),
475 OK_Ordinary,
476 opLHS.get()->getType(),
477 result.get()->getType(),
Adam Nemet484aa452017-03-27 19:17:25 +0000478 opcLoc, FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000479 }
480
481 // The result of the assignment, if not void, is the value set into
482 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000483 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000484 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000485 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000486 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
487 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
488 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000489
490 return complete(syntactic);
491}
492
493/// The basic skeleton for building an increment or decrement
494/// operation.
495ExprResult
496PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
497 UnaryOperatorKind opcode,
498 Expr *op) {
499 assert(UnaryOperator::isIncrementDecrementOp(opcode));
500
501 Expr *syntacticOp = rebuildAndCaptureObject(op);
502
503 // Load the value.
504 ExprResult result = buildGet();
505 if (result.isInvalid()) return ExprError();
506
507 QualType resultType = result.get()->getType();
508
509 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000510 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000511 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000512 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000513 setResultToLastSemantic();
514 }
515
516 // Add or subtract a literal 1.
517 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
518 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
519 GenericLoc);
520
521 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000522 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000523 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000524 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000525 }
526 if (result.isInvalid()) return ExprError();
527
528 // Store that back into the result. The value stored is the result
529 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000530 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
531 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000532 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000533 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000534 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
535 !result.get()->getType()->isVoidType() &&
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000536 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
537 setResultToLastSemantic();
538
539 UnaryOperator *syntactic = new (S.Context) UnaryOperator(
540 syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
541 !resultType->isDependentType()
542 ? S.Context.getTypeSize(resultType) >=
543 S.Context.getTypeSize(S.Context.IntTy)
544 : false);
545 return complete(syntactic);
546}
547
John McCallfe96e0b2011-11-06 09:01:30 +0000548
549//===----------------------------------------------------------------------===//
550// Objective-C @property and implicit property references
551//===----------------------------------------------------------------------===//
552
553/// Look up a method in the receiver type of an Objective-C property
554/// reference.
John McCall526ab472011-10-25 17:37:35 +0000555static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
556 const ObjCPropertyRefExpr *PRE) {
John McCall526ab472011-10-25 17:37:35 +0000557 if (PRE->isObjectReceiver()) {
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000558 const ObjCObjectPointerType *PT =
559 PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
John McCallfe96e0b2011-11-06 09:01:30 +0000560
561 // Special case for 'self' in class method implementations.
562 if (PT->isObjCClassType() &&
563 S.isSelfExpr(const_cast<Expr*>(PRE->getBase()))) {
564 // This cast is safe because isSelfExpr is only true within
565 // methods.
566 ObjCMethodDecl *method =
567 cast<ObjCMethodDecl>(S.CurContext->getNonClosureAncestor());
568 return S.LookupMethodInObjectType(sel,
569 S.Context.getObjCInterfaceType(method->getClassInterface()),
570 /*instance*/ false);
571 }
572
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000573 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
John McCall526ab472011-10-25 17:37:35 +0000574 }
575
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000576 if (PRE->isSuperReceiver()) {
577 if (const ObjCObjectPointerType *PT =
578 PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
579 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
580
581 return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
582 }
583
584 assert(PRE->isClassReceiver() && "Invalid expression");
585 QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
586 return S.LookupMethodInObjectType(sel, IT, false);
John McCall526ab472011-10-25 17:37:35 +0000587}
588
Jordan Rosed3934582012-09-28 22:21:30 +0000589bool ObjCPropertyOpBuilder::isWeakProperty() const {
590 QualType T;
591 if (RefExpr->isExplicitProperty()) {
592 const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty();
593 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Bob Wilsonf4f54e32016-05-25 05:41:57 +0000594 return true;
Jordan Rosed3934582012-09-28 22:21:30 +0000595
596 T = Prop->getType();
597 } else if (Getter) {
Alp Toker314cc812014-01-25 16:55:45 +0000598 T = Getter->getReturnType();
Jordan Rosed3934582012-09-28 22:21:30 +0000599 } else {
600 return false;
601 }
602
603 return T.getObjCLifetime() == Qualifiers::OCL_Weak;
604}
605
John McCallfe96e0b2011-11-06 09:01:30 +0000606bool ObjCPropertyOpBuilder::findGetter() {
607 if (Getter) return true;
John McCall526ab472011-10-25 17:37:35 +0000608
John McCallcfef5462011-11-07 22:49:50 +0000609 // For implicit properties, just trust the lookup we already did.
610 if (RefExpr->isImplicitProperty()) {
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000611 if ((Getter = RefExpr->getImplicitPropertyGetter())) {
612 GetterSelector = Getter->getSelector();
613 return true;
614 }
615 else {
616 // Must build the getter selector the hard way.
617 ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter();
618 assert(setter && "both setter and getter are null - cannot happen");
Fangrui Song6907ce22018-07-30 19:24:48 +0000619 IdentifierInfo *setterName =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000620 setter->getSelector().getIdentifierInfoForSlot(0);
Alp Toker541d5072014-06-07 23:30:53 +0000621 IdentifierInfo *getterName =
622 &S.Context.Idents.get(setterName->getName().substr(3));
Fangrui Song6907ce22018-07-30 19:24:48 +0000623 GetterSelector =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000624 S.PP.getSelectorTable().getNullarySelector(getterName);
625 return false;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000626 }
John McCallcfef5462011-11-07 22:49:50 +0000627 }
628
629 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
630 Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
Craig Topperc3ec1492014-05-26 06:22:03 +0000631 return (Getter != nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000632}
633
634/// Try to find the most accurate setter declaration for the property
635/// reference.
636///
Fangrui Song6907ce22018-07-30 19:24:48 +0000637/// \return true if a setter was found, in which case Setter
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000638bool ObjCPropertyOpBuilder::findSetter(bool warn) {
John McCallfe96e0b2011-11-06 09:01:30 +0000639 // For implicit properties, just trust the lookup we already did.
640 if (RefExpr->isImplicitProperty()) {
641 if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
642 Setter = setter;
643 SetterSelector = setter->getSelector();
644 return true;
John McCall526ab472011-10-25 17:37:35 +0000645 } else {
John McCallfe96e0b2011-11-06 09:01:30 +0000646 IdentifierInfo *getterName =
647 RefExpr->getImplicitPropertyGetter()->getSelector()
648 .getIdentifierInfoForSlot(0);
649 SetterSelector =
Adrian Prantla4ce9062013-06-07 22:29:12 +0000650 SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
651 S.PP.getSelectorTable(),
652 getterName);
John McCallfe96e0b2011-11-06 09:01:30 +0000653 return false;
John McCall526ab472011-10-25 17:37:35 +0000654 }
John McCallfe96e0b2011-11-06 09:01:30 +0000655 }
656
657 // For explicit properties, this is more involved.
658 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
659 SetterSelector = prop->getSetterName();
660
661 // Do a normal method lookup first.
662 if (ObjCMethodDecl *setter =
663 LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
Jordan Rosed01e83a2012-10-10 16:42:25 +0000664 if (setter->isPropertyAccessor() && warn)
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000665 if (const ObjCInterfaceDecl *IFace =
666 dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) {
Craig Topperbf3e3272014-08-30 16:55:52 +0000667 StringRef thisPropertyName = prop->getName();
Jordan Rosea7d03842013-02-08 22:30:41 +0000668 // Try flipping the case of the first character.
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000669 char front = thisPropertyName.front();
Jordan Rosea7d03842013-02-08 22:30:41 +0000670 front = isLowercase(front) ? toUppercase(front) : toLowercase(front);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000671 SmallString<100> PropertyName = thisPropertyName;
672 PropertyName[0] = front;
673 IdentifierInfo *AltMember = &S.PP.getIdentifierTable().get(PropertyName);
Manman Ren5b786402016-01-28 18:49:28 +0000674 if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration(
675 AltMember, prop->getQueryKind()))
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000676 if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) {
Richard Smithf8812672016-12-02 22:38:31 +0000677 S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000678 << prop << prop1 << setter->getSelector();
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000679 S.Diag(prop->getLocation(), diag::note_property_declare);
680 S.Diag(prop1->getLocation(), diag::note_property_declare);
681 }
682 }
John McCallfe96e0b2011-11-06 09:01:30 +0000683 Setter = setter;
684 return true;
685 }
686
687 // That can fail in the somewhat crazy situation that we're
688 // type-checking a message send within the @interface declaration
689 // that declared the @property. But it's not clear that that's
690 // valuable to support.
691
692 return false;
693}
694
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000695void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() {
Fariborz Jahanian55513282014-05-28 18:12:10 +0000696 if (S.getCurLexicalContext()->isObjCContainer() &&
697 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl &&
698 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) {
699 if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) {
700 S.Diag(RefExpr->getLocation(),
701 diag::err_property_function_in_objc_container);
702 S.Diag(prop->getLocation(), diag::note_property_declare);
Fariborz Jahanian55513282014-05-28 18:12:10 +0000703 }
704 }
Fariborz Jahanian55513282014-05-28 18:12:10 +0000705}
706
John McCallfe96e0b2011-11-06 09:01:30 +0000707/// Capture the base object of an Objective-C property expression.
708Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000709 assert(InstanceReceiver == nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000710
711 // If we have a base, capture it in an OVE and rebuild the syntactic
712 // form to use the OVE as its base.
713 if (RefExpr->isObjectReceiver()) {
714 InstanceReceiver = capture(RefExpr->getBase());
Alexey Bataevf7630272015-11-25 12:01:00 +0000715 syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * {
716 return InstanceReceiver;
717 }).rebuild(syntacticBase);
John McCallfe96e0b2011-11-06 09:01:30 +0000718 }
719
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000720 if (ObjCPropertyRefExpr *
721 refE = dyn_cast<ObjCPropertyRefExpr>(syntacticBase->IgnoreParens()))
722 SyntacticRefExpr = refE;
723
John McCallfe96e0b2011-11-06 09:01:30 +0000724 return syntacticBase;
725}
726
727/// Load from an Objective-C property reference.
728ExprResult ObjCPropertyOpBuilder::buildGet() {
729 findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000730 if (!Getter) {
731 DiagnoseUnsupportedPropertyUse();
732 return ExprError();
733 }
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000734
735 if (SyntacticRefExpr)
736 SyntacticRefExpr->setIsMessagingGetter();
737
Douglas Gregore83b9562015-07-07 03:57:53 +0000738 QualType receiverType = RefExpr->getReceiverType(S.Context);
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000739 if (!Getter->isImplicit())
740 S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true);
John McCallfe96e0b2011-11-06 09:01:30 +0000741 // Build a message-send.
742 ExprResult msg;
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000743 if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
744 RefExpr->isObjectReceiver()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000745 assert(InstanceReceiver || RefExpr->isSuperReceiver());
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000746 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
747 GenericLoc, Getter->getSelector(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000748 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000749 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000750 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000751 GenericLoc, Getter->getSelector(),
752 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000753 }
754 return msg;
755}
John McCall526ab472011-10-25 17:37:35 +0000756
John McCallfe96e0b2011-11-06 09:01:30 +0000757/// Store to an Objective-C property reference.
758///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000759/// \param captureSetValueAsResult If true, capture the actual
John McCallfe96e0b2011-11-06 09:01:30 +0000760/// value being set as the value of the property operation.
761ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
762 bool captureSetValueAsResult) {
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000763 if (!findSetter(false)) {
764 DiagnoseUnsupportedPropertyUse();
765 return ExprError();
766 }
John McCallfe96e0b2011-11-06 09:01:30 +0000767
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000768 if (SyntacticRefExpr)
769 SyntacticRefExpr->setIsMessagingSetter();
770
Douglas Gregore83b9562015-07-07 03:57:53 +0000771 QualType receiverType = RefExpr->getReceiverType(S.Context);
John McCallfe96e0b2011-11-06 09:01:30 +0000772
773 // Use assignment constraints when possible; they give us better
774 // diagnostics. "When possible" basically means anything except a
775 // C++ class type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000776 if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000777 QualType paramType = (*Setter->param_begin())->getType()
778 .substObjCMemberType(
779 receiverType,
780 Setter->getDeclContext(),
781 ObjCSubstitutionContext::Parameter);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000782 if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000783 ExprResult opResult = op;
784 Sema::AssignConvertType assignResult
785 = S.CheckSingleAssignmentConstraints(paramType, opResult);
Richard Smithe15a3702016-10-06 23:12:58 +0000786 if (opResult.isInvalid() ||
787 S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType,
John McCallfe96e0b2011-11-06 09:01:30 +0000788 op->getType(), opResult.get(),
789 Sema::AA_Assigning))
790 return ExprError();
791
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000792 op = opResult.get();
John McCallfe96e0b2011-11-06 09:01:30 +0000793 assert(op && "successful assignment left argument invalid?");
John McCall526ab472011-10-25 17:37:35 +0000794 }
795 }
796
John McCallfe96e0b2011-11-06 09:01:30 +0000797 // Arguments.
798 Expr *args[] = { op };
John McCall526ab472011-10-25 17:37:35 +0000799
John McCallfe96e0b2011-11-06 09:01:30 +0000800 // Build a message-send.
801 ExprResult msg;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000802 if (!Setter->isImplicit())
803 S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true);
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000804 if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
805 RefExpr->isObjectReceiver()) {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000806 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
807 GenericLoc, SetterSelector, Setter,
808 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000809 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000810 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
811 GenericLoc,
812 SetterSelector, Setter,
813 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000814 }
815
816 if (!msg.isInvalid() && captureSetValueAsResult) {
817 ObjCMessageExpr *msgExpr =
818 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
819 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000820 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +0000821 msgExpr->setArg(0, captureValueAsResult(arg));
John McCallfe96e0b2011-11-06 09:01:30 +0000822 }
823
824 return msg;
John McCall526ab472011-10-25 17:37:35 +0000825}
826
John McCallfe96e0b2011-11-06 09:01:30 +0000827/// @property-specific behavior for doing lvalue-to-rvalue conversion.
828ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) {
829 // Explicit properties always have getters, but implicit ones don't.
830 // Check that before proceeding.
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000831 if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000832 S.Diag(RefExpr->getLocation(), diag::err_getter_not_found)
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000833 << RefExpr->getSourceRange();
John McCall526ab472011-10-25 17:37:35 +0000834 return ExprError();
835 }
836
John McCallfe96e0b2011-11-06 09:01:30 +0000837 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
John McCall526ab472011-10-25 17:37:35 +0000838 if (result.isInvalid()) return ExprError();
839
John McCallfe96e0b2011-11-06 09:01:30 +0000840 if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType())
841 S.DiagnosePropertyAccessorMismatch(RefExpr->getExplicitProperty(),
842 Getter, RefExpr->getLocation());
843
844 // As a special case, if the method returns 'id', try to get
845 // a better type from the property.
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000846 if (RefExpr->isExplicitProperty() && result.get()->isRValue()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000847 QualType receiverType = RefExpr->getReceiverType(S.Context);
848 QualType propType = RefExpr->getExplicitProperty()
849 ->getUsageType(receiverType);
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000850 if (result.get()->getType()->isObjCIdType()) {
851 if (const ObjCObjectPointerType *ptr
852 = propType->getAs<ObjCObjectPointerType>()) {
853 if (!ptr->isObjCIdType())
854 result = S.ImpCastExprToType(result.get(), propType, CK_BitCast);
855 }
856 }
Brian Kelleycafd9122017-03-29 17:55:11 +0000857 if (propType.getObjCLifetime() == Qualifiers::OCL_Weak &&
858 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
859 RefExpr->getLocation()))
860 S.getCurFunction()->markSafeWeakUse(RefExpr);
John McCallfe96e0b2011-11-06 09:01:30 +0000861 }
862
John McCall526ab472011-10-25 17:37:35 +0000863 return result;
864}
865
John McCallfe96e0b2011-11-06 09:01:30 +0000866/// Try to build this as a call to a getter that returns a reference.
867///
868/// \return true if it was possible, whether or not it actually
869/// succeeded
870bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op,
871 ExprResult &result) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000872 if (!S.getLangOpts().CPlusPlus) return false;
John McCallfe96e0b2011-11-06 09:01:30 +0000873
874 findGetter();
Olivier Goffart4c182c82014-08-04 17:28:05 +0000875 if (!Getter) {
876 // The property has no setter and no getter! This can happen if the type is
877 // invalid. Error have already been reported.
878 result = ExprError();
879 return true;
880 }
John McCallfe96e0b2011-11-06 09:01:30 +0000881
882 // Only do this if the getter returns an l-value reference type.
Alp Toker314cc812014-01-25 16:55:45 +0000883 QualType resultType = Getter->getReturnType();
John McCallfe96e0b2011-11-06 09:01:30 +0000884 if (!resultType->isLValueReferenceType()) return false;
885
886 result = buildRValueOperation(op);
887 return true;
888}
889
890/// @property-specific behavior for doing assignments.
891ExprResult
892ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc,
893 SourceLocation opcLoc,
894 BinaryOperatorKind opcode,
895 Expr *LHS, Expr *RHS) {
John McCall526ab472011-10-25 17:37:35 +0000896 assert(BinaryOperator::isAssignmentOp(opcode));
John McCall526ab472011-10-25 17:37:35 +0000897
898 // If there's no setter, we have no choice but to try to assign to
899 // the result of the getter.
John McCallfe96e0b2011-11-06 09:01:30 +0000900 if (!findSetter()) {
901 ExprResult result;
902 if (tryBuildGetOfReference(LHS, result)) {
903 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000904 return S.BuildBinOp(Sc, opcLoc, opcode, result.get(), RHS);
John McCall526ab472011-10-25 17:37:35 +0000905 }
906
907 // Otherwise, it's an error.
John McCallfe96e0b2011-11-06 09:01:30 +0000908 S.Diag(opcLoc, diag::err_nosetter_property_assignment)
909 << unsigned(RefExpr->isImplicitProperty())
910 << SetterSelector
John McCall526ab472011-10-25 17:37:35 +0000911 << LHS->getSourceRange() << RHS->getSourceRange();
912 return ExprError();
913 }
914
915 // If there is a setter, we definitely want to use it.
916
John McCallfe96e0b2011-11-06 09:01:30 +0000917 // Verify that we can do a compound assignment.
918 if (opcode != BO_Assign && !findGetter()) {
919 S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment)
John McCall526ab472011-10-25 17:37:35 +0000920 << LHS->getSourceRange() << RHS->getSourceRange();
921 return ExprError();
922 }
923
John McCallfe96e0b2011-11-06 09:01:30 +0000924 ExprResult result =
925 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
John McCall526ab472011-10-25 17:37:35 +0000926 if (result.isInvalid()) return ExprError();
927
John McCallfe96e0b2011-11-06 09:01:30 +0000928 // Various warnings about property assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000929 if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) {
John McCallfe96e0b2011-11-06 09:01:30 +0000930 S.checkRetainCycles(InstanceReceiver->getSourceExpr(), RHS);
931 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
932 }
933
John McCall526ab472011-10-25 17:37:35 +0000934 return result;
935}
John McCallfe96e0b2011-11-06 09:01:30 +0000936
937/// @property-specific behavior for doing increments and decrements.
938ExprResult
939ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
940 UnaryOperatorKind opcode,
941 Expr *op) {
942 // If there's no setter, we have no choice but to try to assign to
943 // the result of the getter.
944 if (!findSetter()) {
945 ExprResult result;
946 if (tryBuildGetOfReference(op, result)) {
947 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000948 return S.BuildUnaryOp(Sc, opcLoc, opcode, result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000949 }
950
951 // Otherwise, it's an error.
952 S.Diag(opcLoc, diag::err_nosetter_property_incdec)
953 << unsigned(RefExpr->isImplicitProperty())
954 << unsigned(UnaryOperator::isDecrementOp(opcode))
955 << SetterSelector
956 << op->getSourceRange();
957 return ExprError();
958 }
959
960 // If there is a setter, we definitely want to use it.
961
962 // We also need a getter.
963 if (!findGetter()) {
964 assert(RefExpr->isImplicitProperty());
965 S.Diag(opcLoc, diag::err_nogetter_property_incdec)
966 << unsigned(UnaryOperator::isDecrementOp(opcode))
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000967 << GetterSelector
John McCallfe96e0b2011-11-06 09:01:30 +0000968 << op->getSourceRange();
969 return ExprError();
970 }
971
972 return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op);
973}
974
Jordan Rosed3934582012-09-28 22:21:30 +0000975ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) {
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000976 if (isWeakProperty() && !S.isUnevaluatedContext() &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000977 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000978 SyntacticForm->getBeginLoc()))
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000979 S.getCurFunction()->recordUseOfWeak(SyntacticRefExpr,
980 SyntacticRefExpr->isMessagingGetter());
Jordan Rosed3934582012-09-28 22:21:30 +0000981
982 return PseudoOpBuilder::complete(SyntacticForm);
983}
984
Ted Kremeneke65b0862012-03-06 20:05:56 +0000985// ObjCSubscript build stuff.
986//
987
Fangrui Song6907ce22018-07-30 19:24:48 +0000988/// objective-c subscripting-specific behavior for doing lvalue-to-rvalue
Ted Kremeneke65b0862012-03-06 20:05:56 +0000989/// conversion.
Fangrui Song6907ce22018-07-30 19:24:48 +0000990/// FIXME. Remove this routine if it is proven that no additional
Ted Kremeneke65b0862012-03-06 20:05:56 +0000991/// specifity is needed.
992ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) {
993 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
994 if (result.isInvalid()) return ExprError();
995 return result;
996}
997
998/// objective-c subscripting-specific behavior for doing assignments.
999ExprResult
1000ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc,
1001 SourceLocation opcLoc,
1002 BinaryOperatorKind opcode,
1003 Expr *LHS, Expr *RHS) {
1004 assert(BinaryOperator::isAssignmentOp(opcode));
1005 // There must be a method to do the Index'ed assignment.
1006 if (!findAtIndexSetter())
1007 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001008
Ted Kremeneke65b0862012-03-06 20:05:56 +00001009 // Verify that we can do a compound assignment.
1010 if (opcode != BO_Assign && !findAtIndexGetter())
1011 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Ted Kremeneke65b0862012-03-06 20:05:56 +00001013 ExprResult result =
1014 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
1015 if (result.isInvalid()) return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001016
Ted Kremeneke65b0862012-03-06 20:05:56 +00001017 // Various warnings about objc Index'ed assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001018 if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001019 S.checkRetainCycles(InstanceBase->getSourceExpr(), RHS);
1020 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
1021 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001022
Ted Kremeneke65b0862012-03-06 20:05:56 +00001023 return result;
1024}
1025
1026/// Capture the base object of an Objective-C Index'ed expression.
1027Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001028 assert(InstanceBase == nullptr);
1029
Ted Kremeneke65b0862012-03-06 20:05:56 +00001030 // Capture base expression in an OVE and rebuild the syntactic
1031 // form to use the OVE as its base expression.
1032 InstanceBase = capture(RefExpr->getBaseExpr());
1033 InstanceKey = capture(RefExpr->getKeyExpr());
Alexey Bataevf7630272015-11-25 12:01:00 +00001034
Ted Kremeneke65b0862012-03-06 20:05:56 +00001035 syntacticBase =
Alexey Bataevf7630272015-11-25 12:01:00 +00001036 Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1037 switch (Idx) {
1038 case 0:
1039 return InstanceBase;
1040 case 1:
1041 return InstanceKey;
1042 default:
1043 llvm_unreachable("Unexpected index for ObjCSubscriptExpr");
1044 }
1045 }).rebuild(syntacticBase);
1046
Ted Kremeneke65b0862012-03-06 20:05:56 +00001047 return syntacticBase;
1048}
1049
Fangrui Song6907ce22018-07-30 19:24:48 +00001050/// CheckSubscriptingKind - This routine decide what type
Ted Kremeneke65b0862012-03-06 20:05:56 +00001051/// of indexing represented by "FromE" is being done.
Fangrui Song6907ce22018-07-30 19:24:48 +00001052Sema::ObjCSubscriptKind
Ted Kremeneke65b0862012-03-06 20:05:56 +00001053 Sema::CheckSubscriptingKind(Expr *FromE) {
1054 // If the expression already has integral or enumeration type, we're golden.
1055 QualType T = FromE->getType();
1056 if (T->isIntegralOrEnumerationType())
1057 return OS_Array;
Fangrui Song6907ce22018-07-30 19:24:48 +00001058
Ted Kremeneke65b0862012-03-06 20:05:56 +00001059 // If we don't have a class type in C++, there's no way we can get an
1060 // expression of integral or enumeration type.
1061 const RecordType *RecordTy = T->getAs<RecordType>();
Fariborz Jahaniand13951f2014-09-10 20:55:31 +00001062 if (!RecordTy &&
1063 (T->isObjCObjectPointerType() || T->isVoidPointerType()))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001064 // All other scalar cases are assumed to be dictionary indexing which
1065 // caller handles, with diagnostics if needed.
1066 return OS_Dictionary;
Fangrui Song6907ce22018-07-30 19:24:48 +00001067 if (!getLangOpts().CPlusPlus ||
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001068 !RecordTy || RecordTy->isIncompleteType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001069 // No indexing can be done. Issue diagnostics and quit.
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001070 const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
1071 if (isa<StringLiteral>(IndexExpr))
1072 Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
1073 << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
1074 else
1075 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1076 << T;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001077 return OS_Error;
1078 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001079
Ted Kremeneke65b0862012-03-06 20:05:56 +00001080 // We must have a complete class type.
Fangrui Song6907ce22018-07-30 19:24:48 +00001081 if (RequireCompleteType(FromE->getExprLoc(), T,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001082 diag::err_objc_index_incomplete_class_type, FromE))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001083 return OS_Error;
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Ted Kremeneke65b0862012-03-06 20:05:56 +00001085 // Look for a conversion to an integral, enumeration type, or
1086 // objective-C pointer type.
Ted Kremeneke65b0862012-03-06 20:05:56 +00001087 int NoIntegrals=0, NoObjCIdPointers=0;
1088 SmallVector<CXXConversionDecl *, 4> ConversionDecls;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001089
1090 for (NamedDecl *D : cast<CXXRecordDecl>(RecordTy->getDecl())
1091 ->getVisibleConversionFunctions()) {
1092 if (CXXConversionDecl *Conversion =
1093 dyn_cast<CXXConversionDecl>(D->getUnderlyingDecl())) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001094 QualType CT = Conversion->getConversionType().getNonReferenceType();
1095 if (CT->isIntegralOrEnumerationType()) {
1096 ++NoIntegrals;
1097 ConversionDecls.push_back(Conversion);
1098 }
1099 else if (CT->isObjCIdType() ||CT->isBlockPointerType()) {
1100 ++NoObjCIdPointers;
1101 ConversionDecls.push_back(Conversion);
1102 }
1103 }
1104 }
1105 if (NoIntegrals ==1 && NoObjCIdPointers == 0)
1106 return OS_Array;
1107 if (NoIntegrals == 0 && NoObjCIdPointers == 1)
1108 return OS_Dictionary;
1109 if (NoIntegrals == 0 && NoObjCIdPointers == 0) {
1110 // No conversion function was found. Issue diagnostic and return.
1111 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1112 << FromE->getType();
1113 return OS_Error;
1114 }
1115 Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion)
1116 << FromE->getType();
1117 for (unsigned int i = 0; i < ConversionDecls.size(); i++)
Richard Smith01d96982016-12-02 23:00:28 +00001118 Diag(ConversionDecls[i]->getLocation(),
1119 diag::note_conv_function_declared_at);
1120
Ted Kremeneke65b0862012-03-06 20:05:56 +00001121 return OS_Error;
1122}
1123
Fariborz Jahanian90804912012-08-02 18:03:58 +00001124/// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF
1125/// objects used as dictionary subscript key objects.
Fangrui Song6907ce22018-07-30 19:24:48 +00001126static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001127 Expr *Key) {
1128 if (ContainerT.isNull())
1129 return;
1130 // dictionary subscripting.
1131 // - (id)objectForKeyedSubscript:(id)key;
1132 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001133 &S.Context.Idents.get("objectForKeyedSubscript")
Fariborz Jahanian90804912012-08-02 18:03:58 +00001134 };
1135 Selector GetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
Fangrui Song6907ce22018-07-30 19:24:48 +00001136 ObjCMethodDecl *Getter = S.LookupMethodInObjectType(GetterSelector, ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001137 true /*instance*/);
1138 if (!Getter)
1139 return;
Alp Toker03376dc2014-07-07 09:02:20 +00001140 QualType T = Getter->parameters()[0]->getType();
Brian Kelley11352a82017-03-29 18:09:02 +00001141 S.CheckObjCConversion(Key->getSourceRange(), T, Key,
1142 Sema::CCK_ImplicitConversion);
Fariborz Jahanian90804912012-08-02 18:03:58 +00001143}
1144
Ted Kremeneke65b0862012-03-06 20:05:56 +00001145bool ObjCSubscriptOpBuilder::findAtIndexGetter() {
1146 if (AtIndexGetter)
1147 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001148
Ted Kremeneke65b0862012-03-06 20:05:56 +00001149 Expr *BaseExpr = RefExpr->getBaseExpr();
1150 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001151
Ted Kremeneke65b0862012-03-06 20:05:56 +00001152 QualType ResultType;
1153 if (const ObjCObjectPointerType *PTy =
1154 BaseT->getAs<ObjCObjectPointerType>()) {
1155 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001156 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001157 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001158 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001159 if (Res == Sema::OS_Error) {
1160 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001161 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001162 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001163 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001164 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001165 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001166
Ted Kremeneke65b0862012-03-06 20:05:56 +00001167 if (ResultType.isNull()) {
1168 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1169 << BaseExpr->getType() << arrayRef;
1170 return false;
1171 }
1172 if (!arrayRef) {
1173 // dictionary subscripting.
1174 // - (id)objectForKeyedSubscript:(id)key;
1175 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001176 &S.Context.Idents.get("objectForKeyedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001177 };
1178 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1179 }
1180 else {
1181 // - (id)objectAtIndexedSubscript:(size_t)index;
1182 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001183 &S.Context.Idents.get("objectAtIndexedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001184 };
Fangrui Song6907ce22018-07-30 19:24:48 +00001185
Ted Kremeneke65b0862012-03-06 20:05:56 +00001186 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1187 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001188
1189 AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001190 true /*instance*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00001191
David Blaikiebbafb8a2012-03-11 07:00:24 +00001192 if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001193 AtIndexGetter = ObjCMethodDecl::Create(S.Context, SourceLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001194 SourceLocation(), AtIndexGetterSelector,
1195 S.Context.getObjCIdType() /*ReturnType*/,
Craig Topperc3ec1492014-05-26 06:22:03 +00001196 nullptr /*TypeSourceInfo */,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001197 S.Context.getTranslationUnitDecl(),
1198 true /*Instance*/, false/*isVariadic*/,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001199 /*isPropertyAccessor=*/false,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001200 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1201 ObjCMethodDecl::Required,
1202 false);
1203 ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1204 SourceLocation(), SourceLocation(),
1205 arrayRef ? &S.Context.Idents.get("index")
1206 : &S.Context.Idents.get("key"),
1207 arrayRef ? S.Context.UnsignedLongTy
1208 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001209 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001210 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001211 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001212 AtIndexGetter->setMethodParams(S.Context, Argument, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001213 }
1214
1215 if (!AtIndexGetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001216 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001217 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1218 << BaseExpr->getType() << 0 << arrayRef;
1219 return false;
1220 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001221 AtIndexGetter =
1222 S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1223 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001224 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001225 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001226
Ted Kremeneke65b0862012-03-06 20:05:56 +00001227 if (AtIndexGetter) {
Alp Toker03376dc2014-07-07 09:02:20 +00001228 QualType T = AtIndexGetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001229 if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1230 (!arrayRef && !T->isObjCObjectPointerType())) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001231 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001232 arrayRef ? diag::err_objc_subscript_index_type
1233 : diag::err_objc_subscript_key_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001234 S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001235 diag::note_parameter_type) << T;
1236 return false;
1237 }
Alp Toker314cc812014-01-25 16:55:45 +00001238 QualType R = AtIndexGetter->getReturnType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001239 if (!R->isObjCObjectPointerType()) {
1240 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1241 diag::err_objc_indexing_method_result_type) << R << arrayRef;
1242 S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1243 AtIndexGetter->getDeclName();
1244 }
1245 }
1246 return true;
1247}
1248
1249bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1250 if (AtIndexSetter)
1251 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001252
Ted Kremeneke65b0862012-03-06 20:05:56 +00001253 Expr *BaseExpr = RefExpr->getBaseExpr();
1254 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001255
Ted Kremeneke65b0862012-03-06 20:05:56 +00001256 QualType ResultType;
1257 if (const ObjCObjectPointerType *PTy =
1258 BaseT->getAs<ObjCObjectPointerType>()) {
1259 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001260 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
1262 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001263 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001264 if (Res == Sema::OS_Error) {
1265 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001266 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001267 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001268 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001269 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001270 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001271
Ted Kremeneke65b0862012-03-06 20:05:56 +00001272 if (ResultType.isNull()) {
1273 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1274 << BaseExpr->getType() << arrayRef;
1275 return false;
1276 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001277
Ted Kremeneke65b0862012-03-06 20:05:56 +00001278 if (!arrayRef) {
1279 // dictionary subscripting.
1280 // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1281 IdentifierInfo *KeyIdents[] = {
1282 &S.Context.Idents.get("setObject"),
1283 &S.Context.Idents.get("forKeyedSubscript")
1284 };
1285 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1286 }
1287 else {
1288 // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1289 IdentifierInfo *KeyIdents[] = {
1290 &S.Context.Idents.get("setObject"),
1291 &S.Context.Idents.get("atIndexedSubscript")
1292 };
1293 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1294 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001295 AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001296 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001297
David Blaikiebbafb8a2012-03-11 07:00:24 +00001298 if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001299 TypeSourceInfo *ReturnTInfo = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001300 QualType ReturnType = S.Context.VoidTy;
Alp Toker314cc812014-01-25 16:55:45 +00001301 AtIndexSetter = ObjCMethodDecl::Create(
1302 S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector,
1303 ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(),
1304 true /*Instance*/, false /*isVariadic*/,
1305 /*isPropertyAccessor=*/false,
1306 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1307 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001308 SmallVector<ParmVarDecl *, 2> Params;
1309 ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter,
1310 SourceLocation(), SourceLocation(),
1311 &S.Context.Idents.get("object"),
1312 S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001313 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001314 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001315 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001316 Params.push_back(object);
1317 ParmVarDecl *key = ParmVarDecl::Create(S.Context, AtIndexSetter,
1318 SourceLocation(), SourceLocation(),
1319 arrayRef ? &S.Context.Idents.get("index")
1320 : &S.Context.Idents.get("key"),
1321 arrayRef ? S.Context.UnsignedLongTy
1322 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001323 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001324 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001325 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001326 Params.push_back(key);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001327 AtIndexSetter->setMethodParams(S.Context, Params, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001328 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001329
Ted Kremeneke65b0862012-03-06 20:05:56 +00001330 if (!AtIndexSetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001331 if (!BaseT->isObjCIdType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001332 S.Diag(BaseExpr->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001333 diag::err_objc_subscript_method_not_found)
1334 << BaseExpr->getType() << 1 << arrayRef;
1335 return false;
1336 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001337 AtIndexSetter =
1338 S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector,
1339 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001340 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001341 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001342
Ted Kremeneke65b0862012-03-06 20:05:56 +00001343 bool err = false;
1344 if (AtIndexSetter && arrayRef) {
Alp Toker03376dc2014-07-07 09:02:20 +00001345 QualType T = AtIndexSetter->parameters()[1]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001346 if (!T->isIntegralOrEnumerationType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001347 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001348 diag::err_objc_subscript_index_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001349 S.Diag(AtIndexSetter->parameters()[1]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001350 diag::note_parameter_type) << T;
1351 err = true;
1352 }
Alp Toker03376dc2014-07-07 09:02:20 +00001353 T = AtIndexSetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001354 if (!T->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001355 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001356 diag::err_objc_subscript_object_type) << T << arrayRef;
Fangrui Song6907ce22018-07-30 19:24:48 +00001357 S.Diag(AtIndexSetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001358 diag::note_parameter_type) << T;
1359 err = true;
1360 }
1361 }
1362 else if (AtIndexSetter && !arrayRef)
1363 for (unsigned i=0; i <2; i++) {
Alp Toker03376dc2014-07-07 09:02:20 +00001364 QualType T = AtIndexSetter->parameters()[i]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001365 if (!T->isObjCObjectPointerType()) {
1366 if (i == 1)
1367 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1368 diag::err_objc_subscript_key_type) << T;
1369 else
1370 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1371 diag::err_objc_subscript_dic_object_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001372 S.Diag(AtIndexSetter->parameters()[i]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001373 diag::note_parameter_type) << T;
1374 err = true;
1375 }
1376 }
1377
1378 return !err;
1379}
1380
1381// Get the object at "Index" position in the container.
1382// [BaseExpr objectAtIndexedSubscript : IndexExpr];
1383ExprResult ObjCSubscriptOpBuilder::buildGet() {
1384 if (!findAtIndexGetter())
1385 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001386
Ted Kremeneke65b0862012-03-06 20:05:56 +00001387 QualType receiverType = InstanceBase->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001388
Ted Kremeneke65b0862012-03-06 20:05:56 +00001389 // Build a message-send.
1390 ExprResult msg;
1391 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001392
Ted Kremeneke65b0862012-03-06 20:05:56 +00001393 // Arguments.
1394 Expr *args[] = { Index };
1395 assert(InstanceBase);
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001396 if (AtIndexGetter)
1397 S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001398 msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1399 GenericLoc,
1400 AtIndexGetterSelector, AtIndexGetter,
1401 MultiExprArg(args, 1));
1402 return msg;
1403}
1404
1405/// Store into the container the "op" object at "Index"'ed location
1406/// by building this messaging expression:
1407/// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00001408/// \param captureSetValueAsResult If true, capture the actual
Ted Kremeneke65b0862012-03-06 20:05:56 +00001409/// value being set as the value of the property operation.
1410ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
1411 bool captureSetValueAsResult) {
1412 if (!findAtIndexSetter())
1413 return ExprError();
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001414 if (AtIndexSetter)
1415 S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001416 QualType receiverType = InstanceBase->getType();
1417 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001418
Ted Kremeneke65b0862012-03-06 20:05:56 +00001419 // Arguments.
1420 Expr *args[] = { op, Index };
Fangrui Song6907ce22018-07-30 19:24:48 +00001421
Ted Kremeneke65b0862012-03-06 20:05:56 +00001422 // Build a message-send.
1423 ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1424 GenericLoc,
1425 AtIndexSetterSelector,
1426 AtIndexSetter,
1427 MultiExprArg(args, 2));
Fangrui Song6907ce22018-07-30 19:24:48 +00001428
Ted Kremeneke65b0862012-03-06 20:05:56 +00001429 if (!msg.isInvalid() && captureSetValueAsResult) {
1430 ObjCMessageExpr *msgExpr =
1431 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
1432 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +00001433 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +00001434 msgExpr->setArg(0, captureValueAsResult(arg));
Ted Kremeneke65b0862012-03-06 20:05:56 +00001435 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001436
Ted Kremeneke65b0862012-03-06 20:05:56 +00001437 return msg;
1438}
1439
John McCallfe96e0b2011-11-06 09:01:30 +00001440//===----------------------------------------------------------------------===//
John McCall5e77d762013-04-16 07:28:30 +00001441// MSVC __declspec(property) references
1442//===----------------------------------------------------------------------===//
1443
Alexey Bataevf7630272015-11-25 12:01:00 +00001444MSPropertyRefExpr *
1445MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) {
1446 CallArgs.insert(CallArgs.begin(), E->getIdx());
1447 Expr *Base = E->getBase()->IgnoreParens();
1448 while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Base)) {
1449 CallArgs.insert(CallArgs.begin(), MSPropSubscript->getIdx());
1450 Base = MSPropSubscript->getBase()->IgnoreParens();
1451 }
1452 return cast<MSPropertyRefExpr>(Base);
1453}
1454
John McCall5e77d762013-04-16 07:28:30 +00001455Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Alexey Bataev69103472015-10-14 04:05:42 +00001456 InstanceBase = capture(RefExpr->getBaseExpr());
Aaron Ballman72f65632017-11-03 20:09:17 +00001457 llvm::for_each(CallArgs, [this](Expr *&Arg) { Arg = capture(Arg); });
Alexey Bataevf7630272015-11-25 12:01:00 +00001458 syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1459 switch (Idx) {
1460 case 0:
1461 return InstanceBase;
1462 default:
1463 assert(Idx <= CallArgs.size());
1464 return CallArgs[Idx - 1];
1465 }
1466 }).rebuild(syntacticBase);
John McCall5e77d762013-04-16 07:28:30 +00001467
1468 return syntacticBase;
1469}
1470
1471ExprResult MSPropertyOpBuilder::buildGet() {
1472 if (!RefExpr->getPropertyDecl()->hasGetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001473 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001474 << 0 /* getter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001475 return ExprError();
1476 }
1477
1478 UnqualifiedId GetterName;
1479 IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId();
1480 GetterName.setIdentifier(II, RefExpr->getMemberLoc());
1481 CXXScopeSpec SS;
1482 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001483 ExprResult GetterExpr =
1484 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1485 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1486 SourceLocation(), GetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001487 if (GetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001488 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001489 diag::err_cannot_find_suitable_accessor) << 0 /* getter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001490 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001491 return ExprError();
1492 }
1493
Richard Smith255b85f2019-05-08 01:36:36 +00001494 return S.BuildCallExpr(S.getCurScope(), GetterExpr.get(),
Alexey Bataevf7630272015-11-25 12:01:00 +00001495 RefExpr->getSourceRange().getBegin(), CallArgs,
John McCall5e77d762013-04-16 07:28:30 +00001496 RefExpr->getSourceRange().getEnd());
1497}
1498
1499ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl,
1500 bool captureSetValueAsResult) {
1501 if (!RefExpr->getPropertyDecl()->hasSetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001502 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001503 << 1 /* setter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001504 return ExprError();
1505 }
1506
1507 UnqualifiedId SetterName;
1508 IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId();
1509 SetterName.setIdentifier(II, RefExpr->getMemberLoc());
1510 CXXScopeSpec SS;
1511 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001512 ExprResult SetterExpr =
1513 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1514 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1515 SourceLocation(), SetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001516 if (SetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001517 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001518 diag::err_cannot_find_suitable_accessor) << 1 /* setter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001519 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001520 return ExprError();
1521 }
1522
Alexey Bataevf7630272015-11-25 12:01:00 +00001523 SmallVector<Expr*, 4> ArgExprs;
1524 ArgExprs.append(CallArgs.begin(), CallArgs.end());
John McCall5e77d762013-04-16 07:28:30 +00001525 ArgExprs.push_back(op);
Richard Smith255b85f2019-05-08 01:36:36 +00001526 return S.BuildCallExpr(S.getCurScope(), SetterExpr.get(),
John McCall5e77d762013-04-16 07:28:30 +00001527 RefExpr->getSourceRange().getBegin(), ArgExprs,
1528 op->getSourceRange().getEnd());
1529}
1530
1531//===----------------------------------------------------------------------===//
John McCallfe96e0b2011-11-06 09:01:30 +00001532// General Sema routines.
1533//===----------------------------------------------------------------------===//
1534
1535ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
1536 Expr *opaqueRef = E->IgnoreParens();
1537 if (ObjCPropertyRefExpr *refExpr
1538 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001539 ObjCPropertyOpBuilder builder(*this, refExpr, true);
John McCallfe96e0b2011-11-06 09:01:30 +00001540 return builder.buildRValueOperation(E);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001541 }
1542 else if (ObjCSubscriptRefExpr *refExpr
1543 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001544 ObjCSubscriptOpBuilder builder(*this, refExpr, true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001545 return builder.buildRValueOperation(E);
John McCall5e77d762013-04-16 07:28:30 +00001546 } else if (MSPropertyRefExpr *refExpr
1547 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001548 MSPropertyOpBuilder builder(*this, refExpr, true);
John McCall5e77d762013-04-16 07:28:30 +00001549 return builder.buildRValueOperation(E);
Alexey Bataevf7630272015-11-25 12:01:00 +00001550 } else if (MSPropertySubscriptExpr *RefExpr =
1551 dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001552 MSPropertyOpBuilder Builder(*this, RefExpr, true);
Alexey Bataevf7630272015-11-25 12:01:00 +00001553 return Builder.buildRValueOperation(E);
John McCallfe96e0b2011-11-06 09:01:30 +00001554 } else {
1555 llvm_unreachable("unknown pseudo-object kind!");
1556 }
1557}
1558
1559/// Check an increment or decrement of a pseudo-object expression.
1560ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc,
1561 UnaryOperatorKind opcode, Expr *op) {
1562 // Do nothing if the operand is dependent.
1563 if (op->isTypeDependent())
1564 return new (Context) UnaryOperator(op, opcode, Context.DependentTy,
Aaron Ballmana5038552018-01-09 13:07:03 +00001565 VK_RValue, OK_Ordinary, opcLoc, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001566
1567 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1568 Expr *opaqueRef = op->IgnoreParens();
1569 if (ObjCPropertyRefExpr *refExpr
1570 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001571 ObjCPropertyOpBuilder builder(*this, refExpr, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001572 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001573 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1574 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1575 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001576 } else if (MSPropertyRefExpr *refExpr
1577 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001578 MSPropertyOpBuilder builder(*this, refExpr, false);
John McCall5e77d762013-04-16 07:28:30 +00001579 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001580 } else if (MSPropertySubscriptExpr *RefExpr
1581 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001582 MSPropertyOpBuilder Builder(*this, RefExpr, false);
Alexey Bataevf7630272015-11-25 12:01:00 +00001583 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001584 } else {
1585 llvm_unreachable("unknown pseudo-object kind!");
1586 }
1587}
1588
1589ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1590 BinaryOperatorKind opcode,
1591 Expr *LHS, Expr *RHS) {
1592 // Do nothing if either argument is dependent.
1593 if (LHS->isTypeDependent() || RHS->isTypeDependent())
1594 return new (Context) BinaryOperator(LHS, RHS, opcode, Context.DependentTy,
Adam Nemet484aa452017-03-27 19:17:25 +00001595 VK_RValue, OK_Ordinary, opcLoc,
1596 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +00001597
1598 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001599 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1600 ExprResult result = CheckPlaceholderExpr(RHS);
1601 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001602 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001603 }
1604
Akira Hatanaka797afe32018-03-20 01:47:58 +00001605 bool IsSimpleAssign = opcode == BO_Assign;
John McCallfe96e0b2011-11-06 09:01:30 +00001606 Expr *opaqueRef = LHS->IgnoreParens();
1607 if (ObjCPropertyRefExpr *refExpr
1608 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001609 ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
John McCallfe96e0b2011-11-06 09:01:30 +00001610 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001611 } else if (ObjCSubscriptRefExpr *refExpr
1612 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001613 ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001614 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001615 } else if (MSPropertyRefExpr *refExpr
1616 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001617 MSPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001618 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1619 } else if (MSPropertySubscriptExpr *RefExpr
1620 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001621 MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001622 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001623 } else {
1624 llvm_unreachable("unknown pseudo-object kind!");
1625 }
1626}
John McCalle9290822011-11-30 04:42:31 +00001627
1628/// Given a pseudo-object reference, rebuild it without the opaque
1629/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1630/// This should never operate in-place.
1631static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001632 return Rebuilder(S,
1633 [=](Expr *E, unsigned) -> Expr * {
1634 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1635 })
1636 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001637}
1638
1639/// Given a pseudo-object expression, recreate what it looks like
1640/// syntactically without the attendant OpaqueValueExprs.
1641///
1642/// This is a hack which should be removed when TreeTransform is
1643/// capable of rebuilding a tree without stripping implicit
1644/// operations.
1645Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001646 Expr *syntax = E->getSyntacticForm();
1647 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1648 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1649 return new (Context) UnaryOperator(
1650 op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1651 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1652 } else if (CompoundAssignOperator *cop
1653 = dyn_cast<CompoundAssignOperator>(syntax)) {
1654 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001655 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
1656 return new (Context) CompoundAssignOperator(lhs, rhs, cop->getOpcode(),
1657 cop->getType(),
1658 cop->getValueKind(),
1659 cop->getObjectKind(),
1660 cop->getComputationLHSType(),
1661 cop->getComputationResultType(),
Adam Nemet484aa452017-03-27 19:17:25 +00001662 cop->getOperatorLoc(),
1663 FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001664 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1665 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1666 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
1667 return new (Context) BinaryOperator(lhs, rhs, bop->getOpcode(),
1668 bop->getType(), bop->getValueKind(),
1669 bop->getObjectKind(),
Adam Nemet484aa452017-03-27 19:17:25 +00001670 bop->getOperatorLoc(), FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001671 } else {
1672 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1673 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1674 }
1675}