blob: da777201af882be3767ce2112a515a88ef484e5c [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());
Melanie Blowerf5360d42020-05-01 10:32:06 -0700130 return UnaryOperator::Create(
131 S.Context, e, uop->getOpcode(), uop->getType(), uop->getValueKind(),
132 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow(),
133 S.CurFPFeatures);
John McCallfe96e0b2011-11-06 09:01:30 +0000134 }
135
136 if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
137 assert(!gse->isResultDependent());
138 unsigned resultIndex = gse->getResultIndex();
139 unsigned numAssocs = gse->getNumAssocs();
140
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000141 SmallVector<Expr *, 8> assocExprs;
142 SmallVector<TypeSourceInfo *, 8> assocTypes;
143 assocExprs.reserve(numAssocs);
144 assocTypes.reserve(numAssocs);
John McCallfe96e0b2011-11-06 09:01:30 +0000145
Mark de Wever21490282019-11-12 20:46:19 +0100146 for (const GenericSelectionExpr::Association assoc :
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000147 gse->associations()) {
148 Expr *assocExpr = assoc.getAssociationExpr();
149 if (assoc.isSelected())
150 assocExpr = rebuild(assocExpr);
151 assocExprs.push_back(assocExpr);
152 assocTypes.push_back(assoc.getTypeSourceInfo());
John McCallfe96e0b2011-11-06 09:01:30 +0000153 }
154
Bruno Riccidb076832019-01-26 14:15:10 +0000155 return GenericSelectionExpr::Create(
156 S.Context, gse->getGenericLoc(), gse->getControllingExpr(),
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000157 assocTypes, assocExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
Bruno Riccidb076832019-01-26 14:15:10 +0000158 gse->containsUnexpandedParameterPack(), resultIndex);
John McCallfe96e0b2011-11-06 09:01:30 +0000159 }
160
Eli Friedman75807f22013-07-20 00:40:58 +0000161 if (ChooseExpr *ce = dyn_cast<ChooseExpr>(e)) {
162 assert(!ce->isConditionDependent());
163
164 Expr *LHS = ce->getLHS(), *RHS = ce->getRHS();
165 Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS;
166 rebuiltExpr = rebuild(rebuiltExpr);
167
Haojian Wu876bb862020-03-17 08:33:37 +0100168 return new (S.Context)
169 ChooseExpr(ce->getBuiltinLoc(), ce->getCond(), LHS, RHS,
170 rebuiltExpr->getType(), rebuiltExpr->getValueKind(),
171 rebuiltExpr->getObjectKind(), ce->getRParenLoc(),
172 ce->isConditionTrue());
Eli Friedman75807f22013-07-20 00:40:58 +0000173 }
174
John McCallfe96e0b2011-11-06 09:01:30 +0000175 llvm_unreachable("bad expression to rebuild!");
176 }
177 };
178
John McCallfe96e0b2011-11-06 09:01:30 +0000179 class PseudoOpBuilder {
180 public:
181 Sema &S;
182 unsigned ResultIndex;
183 SourceLocation GenericLoc;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000184 bool IsUnique;
John McCallfe96e0b2011-11-06 09:01:30 +0000185 SmallVector<Expr *, 4> Semantics;
186
Akira Hatanaka797afe32018-03-20 01:47:58 +0000187 PseudoOpBuilder(Sema &S, SourceLocation genericLoc, bool IsUnique)
John McCallfe96e0b2011-11-06 09:01:30 +0000188 : S(S), ResultIndex(PseudoObjectExpr::NoResult),
Akira Hatanaka797afe32018-03-20 01:47:58 +0000189 GenericLoc(genericLoc), IsUnique(IsUnique) {}
John McCallfe96e0b2011-11-06 09:01:30 +0000190
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000191 virtual ~PseudoOpBuilder() {}
Matt Beaumont-Gayfb3cb9a2011-11-08 01:53:17 +0000192
John McCallfe96e0b2011-11-06 09:01:30 +0000193 /// Add a normal semantic expression.
194 void addSemanticExpr(Expr *semantic) {
195 Semantics.push_back(semantic);
196 }
197
198 /// Add the 'result' semantic expression.
199 void addResultSemanticExpr(Expr *resultExpr) {
200 assert(ResultIndex == PseudoObjectExpr::NoResult);
201 ResultIndex = Semantics.size();
202 Semantics.push_back(resultExpr);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000203 // An OVE is not unique if it is used as the result expression.
204 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
205 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000206 }
207
208 ExprResult buildRValueOperation(Expr *op);
209 ExprResult buildAssignmentOperation(Scope *Sc,
210 SourceLocation opLoc,
211 BinaryOperatorKind opcode,
212 Expr *LHS, Expr *RHS);
213 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
214 UnaryOperatorKind opcode,
215 Expr *op);
216
Jordan Rosed3934582012-09-28 22:21:30 +0000217 virtual ExprResult complete(Expr *syntacticForm);
John McCallfe96e0b2011-11-06 09:01:30 +0000218
219 OpaqueValueExpr *capture(Expr *op);
220 OpaqueValueExpr *captureValueAsResult(Expr *op);
221
222 void setResultToLastSemantic() {
223 assert(ResultIndex == PseudoObjectExpr::NoResult);
224 ResultIndex = Semantics.size() - 1;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000225 // An OVE is not unique if it is used as the result expression.
226 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
227 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000228 }
229
230 /// Return true if assignments have a non-void result.
Alexey Bataev60520e22015-12-10 04:38:18 +0000231 static bool CanCaptureValue(Expr *exp) {
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000232 if (exp->isGLValue())
233 return true;
234 QualType ty = exp->getType();
Eli Friedman00fa4292012-11-13 23:16:33 +0000235 assert(!ty->isIncompleteType());
236 assert(!ty->isDependentType());
237
238 if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl())
239 return ClassDecl->isTriviallyCopyable();
240 return true;
241 }
John McCallfe96e0b2011-11-06 09:01:30 +0000242
243 virtual Expr *rebuildAndCaptureObject(Expr *) = 0;
244 virtual ExprResult buildGet() = 0;
245 virtual ExprResult buildSet(Expr *, SourceLocation,
246 bool captureSetValueAsResult) = 0;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000247 /// Should the result of an assignment be the formal result of the
Alexey Bataev60520e22015-12-10 04:38:18 +0000248 /// setter call or the value that was passed to the setter?
249 ///
250 /// Different pseudo-object language features use different language rules
251 /// for this.
252 /// The default is to use the set value. Currently, this affects the
253 /// behavior of simple assignments, compound assignments, and prefix
254 /// increment and decrement.
255 /// Postfix increment and decrement always use the getter result as the
256 /// expression result.
257 ///
258 /// If this method returns true, and the set value isn't capturable for
259 /// some reason, the result of the expression will be void.
260 virtual bool captureSetValueAsResult() const { return true; }
John McCallfe96e0b2011-11-06 09:01:30 +0000261 };
262
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +0000263 /// A PseudoOpBuilder for Objective-C \@properties.
John McCallfe96e0b2011-11-06 09:01:30 +0000264 class ObjCPropertyOpBuilder : public PseudoOpBuilder {
265 ObjCPropertyRefExpr *RefExpr;
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000266 ObjCPropertyRefExpr *SyntacticRefExpr;
John McCallfe96e0b2011-11-06 09:01:30 +0000267 OpaqueValueExpr *InstanceReceiver;
268 ObjCMethodDecl *Getter;
269
270 ObjCMethodDecl *Setter;
271 Selector SetterSelector;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000272 Selector GetterSelector;
John McCallfe96e0b2011-11-06 09:01:30 +0000273
274 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000275 ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr, bool IsUnique)
276 : PseudoOpBuilder(S, refExpr->getLocation(), IsUnique),
277 RefExpr(refExpr), SyntacticRefExpr(nullptr),
278 InstanceReceiver(nullptr), Getter(nullptr), Setter(nullptr) {
John McCallfe96e0b2011-11-06 09:01:30 +0000279 }
280
281 ExprResult buildRValueOperation(Expr *op);
282 ExprResult buildAssignmentOperation(Scope *Sc,
283 SourceLocation opLoc,
284 BinaryOperatorKind opcode,
285 Expr *LHS, Expr *RHS);
286 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
287 UnaryOperatorKind opcode,
288 Expr *op);
289
290 bool tryBuildGetOfReference(Expr *op, ExprResult &result);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000291 bool findSetter(bool warn=true);
John McCallfe96e0b2011-11-06 09:01:30 +0000292 bool findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000293 void DiagnoseUnsupportedPropertyUse();
John McCallfe96e0b2011-11-06 09:01:30 +0000294
Craig Toppere14c0f82014-03-12 04:55:44 +0000295 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
296 ExprResult buildGet() override;
297 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
298 ExprResult complete(Expr *SyntacticForm) override;
Jordan Rosed3934582012-09-28 22:21:30 +0000299
300 bool isWeakProperty() const;
John McCallfe96e0b2011-11-06 09:01:30 +0000301 };
Ted Kremeneke65b0862012-03-06 20:05:56 +0000302
303 /// A PseudoOpBuilder for Objective-C array/dictionary indexing.
304 class ObjCSubscriptOpBuilder : public PseudoOpBuilder {
305 ObjCSubscriptRefExpr *RefExpr;
306 OpaqueValueExpr *InstanceBase;
307 OpaqueValueExpr *InstanceKey;
308 ObjCMethodDecl *AtIndexGetter;
309 Selector AtIndexGetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000310
Ted Kremeneke65b0862012-03-06 20:05:56 +0000311 ObjCMethodDecl *AtIndexSetter;
312 Selector AtIndexSetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000313
Ted Kremeneke65b0862012-03-06 20:05:56 +0000314 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000315 ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr, bool IsUnique)
316 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
317 RefExpr(refExpr), InstanceBase(nullptr), InstanceKey(nullptr),
318 AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
Craig Topperc3ec1492014-05-26 06:22:03 +0000319
Ted Kremeneke65b0862012-03-06 20:05:56 +0000320 ExprResult buildRValueOperation(Expr *op);
321 ExprResult buildAssignmentOperation(Scope *Sc,
322 SourceLocation opLoc,
323 BinaryOperatorKind opcode,
324 Expr *LHS, Expr *RHS);
Craig Toppere14c0f82014-03-12 04:55:44 +0000325 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
326
Ted Kremeneke65b0862012-03-06 20:05:56 +0000327 bool findAtIndexGetter();
328 bool findAtIndexSetter();
Craig Toppere14c0f82014-03-12 04:55:44 +0000329
330 ExprResult buildGet() override;
331 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Ted Kremeneke65b0862012-03-06 20:05:56 +0000332 };
333
John McCall5e77d762013-04-16 07:28:30 +0000334 class MSPropertyOpBuilder : public PseudoOpBuilder {
335 MSPropertyRefExpr *RefExpr;
Alexey Bataev69103472015-10-14 04:05:42 +0000336 OpaqueValueExpr *InstanceBase;
Alexey Bataevf7630272015-11-25 12:01:00 +0000337 SmallVector<Expr *, 4> CallArgs;
338
339 MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
John McCall5e77d762013-04-16 07:28:30 +0000340
341 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000342 MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr, bool IsUnique)
343 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
344 RefExpr(refExpr), InstanceBase(nullptr) {}
345 MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr, bool IsUnique)
346 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
Alexey Bataevf7630272015-11-25 12:01:00 +0000347 InstanceBase(nullptr) {
348 RefExpr = getBaseMSProperty(refExpr);
349 }
John McCall5e77d762013-04-16 07:28:30 +0000350
Craig Toppere14c0f82014-03-12 04:55:44 +0000351 Expr *rebuildAndCaptureObject(Expr *) override;
352 ExprResult buildGet() override;
353 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Alexey Bataev60520e22015-12-10 04:38:18 +0000354 bool captureSetValueAsResult() const override { return false; }
John McCall5e77d762013-04-16 07:28:30 +0000355 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000356}
John McCallfe96e0b2011-11-06 09:01:30 +0000357
358/// Capture the given expression in an OpaqueValueExpr.
359OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
360 // Make a new OVE whose source is the given expression.
Fangrui Song6907ce22018-07-30 19:24:48 +0000361 OpaqueValueExpr *captured =
John McCallfe96e0b2011-11-06 09:01:30 +0000362 new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
Douglas Gregor2d5aea02012-02-23 22:17:26 +0000363 e->getValueKind(), e->getObjectKind(),
364 e);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000365 if (IsUnique)
366 captured->setIsUnique(true);
367
John McCallfe96e0b2011-11-06 09:01:30 +0000368 // Make sure we bind that in the semantics.
369 addSemanticExpr(captured);
370 return captured;
371}
372
373/// Capture the given expression as the result of this pseudo-object
374/// operation. This routine is safe against expressions which may
375/// already be captured.
376///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000377/// \returns the captured expression, which will be the
John McCallfe96e0b2011-11-06 09:01:30 +0000378/// same as the input if the input was already captured
379OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
380 assert(ResultIndex == PseudoObjectExpr::NoResult);
381
382 // If the expression hasn't already been captured, just capture it
Fangrui Song6907ce22018-07-30 19:24:48 +0000383 // and set the new semantic
John McCallfe96e0b2011-11-06 09:01:30 +0000384 if (!isa<OpaqueValueExpr>(e)) {
385 OpaqueValueExpr *cap = capture(e);
386 setResultToLastSemantic();
387 return cap;
388 }
389
390 // Otherwise, it must already be one of our semantic expressions;
391 // set ResultIndex to its index.
392 unsigned index = 0;
393 for (;; ++index) {
394 assert(index < Semantics.size() &&
395 "captured expression not found in semantics!");
396 if (e == Semantics[index]) break;
397 }
398 ResultIndex = index;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000399 // An OVE is not unique if it is used as the result expression.
400 cast<OpaqueValueExpr>(e)->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000401 return cast<OpaqueValueExpr>(e);
402}
403
404/// The routine which creates the final PseudoObjectExpr.
405ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
406 return PseudoObjectExpr::Create(S.Context, syntactic,
407 Semantics, ResultIndex);
408}
409
410/// The main skeleton for building an r-value operation.
411ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
412 Expr *syntacticBase = rebuildAndCaptureObject(op);
413
414 ExprResult getExpr = buildGet();
415 if (getExpr.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000416 addResultSemanticExpr(getExpr.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000417
418 return complete(syntacticBase);
419}
420
421/// The basic skeleton for building a simple or compound
422/// assignment operation.
423ExprResult
424PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
425 BinaryOperatorKind opcode,
426 Expr *LHS, Expr *RHS) {
427 assert(BinaryOperator::isAssignmentOp(opcode));
428
429 Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
430 OpaqueValueExpr *capturedRHS = capture(RHS);
431
John McCallee04aeb2015-08-22 00:35:27 +0000432 // In some very specific cases, semantic analysis of the RHS as an
433 // expression may require it to be rewritten. In these cases, we
434 // cannot safely keep the OVE around. Fortunately, we don't really
435 // need to: we don't use this particular OVE in multiple places, and
436 // no clients rely that closely on matching up expressions in the
437 // semantic expression with expressions from the syntactic form.
438 Expr *semanticRHS = capturedRHS;
439 if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
440 semanticRHS = RHS;
441 Semantics.pop_back();
442 }
443
John McCallfe96e0b2011-11-06 09:01:30 +0000444 Expr *syntactic;
445
446 ExprResult result;
447 if (opcode == BO_Assign) {
John McCallee04aeb2015-08-22 00:35:27 +0000448 result = semanticRHS;
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700449 syntactic = BinaryOperator::Create(
450 S.Context, syntacticLHS, capturedRHS, opcode, capturedRHS->getType(),
Melanie Blower8812b0c2020-04-16 08:45:26 -0700451 capturedRHS->getValueKind(), OK_Ordinary, opcLoc, S.CurFPFeatures);
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700452
John McCallfe96e0b2011-11-06 09:01:30 +0000453 } else {
454 ExprResult opLHS = buildGet();
455 if (opLHS.isInvalid()) return ExprError();
456
457 // Build an ordinary, non-compound operation.
458 BinaryOperatorKind nonCompound =
459 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000460 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000461 if (result.isInvalid()) return ExprError();
462
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700463 syntactic = CompoundAssignOperator::Create(
464 S.Context, syntacticLHS, capturedRHS, opcode, result.get()->getType(),
Melanie Blower8812b0c2020-04-16 08:45:26 -0700465 result.get()->getValueKind(), OK_Ordinary, opcLoc, S.CurFPFeatures,
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700466 opLHS.get()->getType(), result.get()->getType());
John McCallfe96e0b2011-11-06 09:01:30 +0000467 }
468
469 // The result of the assignment, if not void, is the value set into
470 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000471 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000472 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000473 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000474 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
475 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
476 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000477
478 return complete(syntactic);
479}
480
481/// The basic skeleton for building an increment or decrement
482/// operation.
483ExprResult
484PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
485 UnaryOperatorKind opcode,
486 Expr *op) {
487 assert(UnaryOperator::isIncrementDecrementOp(opcode));
488
489 Expr *syntacticOp = rebuildAndCaptureObject(op);
490
491 // Load the value.
492 ExprResult result = buildGet();
493 if (result.isInvalid()) return ExprError();
494
495 QualType resultType = result.get()->getType();
496
497 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000498 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000499 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000500 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000501 setResultToLastSemantic();
502 }
503
504 // Add or subtract a literal 1.
505 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
506 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
507 GenericLoc);
508
509 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000510 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000511 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000512 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000513 }
514 if (result.isInvalid()) return ExprError();
515
516 // Store that back into the result. The value stored is the result
517 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000518 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
519 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000520 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000521 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000522 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
523 !result.get()->getType()->isVoidType() &&
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000524 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
525 setResultToLastSemantic();
526
Melanie Blowerf5360d42020-05-01 10:32:06 -0700527 UnaryOperator *syntactic =
528 UnaryOperator::Create(S.Context, syntacticOp, opcode, resultType,
529 VK_LValue, OK_Ordinary, opcLoc,
530 !resultType->isDependentType()
531 ? S.Context.getTypeSize(resultType) >=
532 S.Context.getTypeSize(S.Context.IntTy)
533 : false,
534 S.CurFPFeatures);
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000535 return complete(syntactic);
536}
537
John McCallfe96e0b2011-11-06 09:01:30 +0000538
539//===----------------------------------------------------------------------===//
540// Objective-C @property and implicit property references
541//===----------------------------------------------------------------------===//
542
543/// Look up a method in the receiver type of an Objective-C property
544/// reference.
John McCall526ab472011-10-25 17:37:35 +0000545static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
546 const ObjCPropertyRefExpr *PRE) {
John McCall526ab472011-10-25 17:37:35 +0000547 if (PRE->isObjectReceiver()) {
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000548 const ObjCObjectPointerType *PT =
549 PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
John McCallfe96e0b2011-11-06 09:01:30 +0000550
551 // Special case for 'self' in class method implementations.
552 if (PT->isObjCClassType() &&
553 S.isSelfExpr(const_cast<Expr*>(PRE->getBase()))) {
554 // This cast is safe because isSelfExpr is only true within
555 // methods.
556 ObjCMethodDecl *method =
557 cast<ObjCMethodDecl>(S.CurContext->getNonClosureAncestor());
558 return S.LookupMethodInObjectType(sel,
559 S.Context.getObjCInterfaceType(method->getClassInterface()),
560 /*instance*/ false);
561 }
562
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000563 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
John McCall526ab472011-10-25 17:37:35 +0000564 }
565
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000566 if (PRE->isSuperReceiver()) {
567 if (const ObjCObjectPointerType *PT =
568 PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
569 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
570
571 return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
572 }
573
574 assert(PRE->isClassReceiver() && "Invalid expression");
575 QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
576 return S.LookupMethodInObjectType(sel, IT, false);
John McCall526ab472011-10-25 17:37:35 +0000577}
578
Jordan Rosed3934582012-09-28 22:21:30 +0000579bool ObjCPropertyOpBuilder::isWeakProperty() const {
580 QualType T;
581 if (RefExpr->isExplicitProperty()) {
582 const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty();
Puyan Lotfi9721fbf2020-04-23 02:20:56 -0400583 if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak)
Bob Wilsonf4f54e32016-05-25 05:41:57 +0000584 return true;
Jordan Rosed3934582012-09-28 22:21:30 +0000585
586 T = Prop->getType();
587 } else if (Getter) {
Alp Toker314cc812014-01-25 16:55:45 +0000588 T = Getter->getReturnType();
Jordan Rosed3934582012-09-28 22:21:30 +0000589 } else {
590 return false;
591 }
592
593 return T.getObjCLifetime() == Qualifiers::OCL_Weak;
594}
595
John McCallfe96e0b2011-11-06 09:01:30 +0000596bool ObjCPropertyOpBuilder::findGetter() {
597 if (Getter) return true;
John McCall526ab472011-10-25 17:37:35 +0000598
John McCallcfef5462011-11-07 22:49:50 +0000599 // For implicit properties, just trust the lookup we already did.
600 if (RefExpr->isImplicitProperty()) {
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000601 if ((Getter = RefExpr->getImplicitPropertyGetter())) {
602 GetterSelector = Getter->getSelector();
603 return true;
604 }
605 else {
606 // Must build the getter selector the hard way.
607 ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter();
608 assert(setter && "both setter and getter are null - cannot happen");
Fangrui Song6907ce22018-07-30 19:24:48 +0000609 IdentifierInfo *setterName =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000610 setter->getSelector().getIdentifierInfoForSlot(0);
Alp Toker541d5072014-06-07 23:30:53 +0000611 IdentifierInfo *getterName =
612 &S.Context.Idents.get(setterName->getName().substr(3));
Fangrui Song6907ce22018-07-30 19:24:48 +0000613 GetterSelector =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000614 S.PP.getSelectorTable().getNullarySelector(getterName);
615 return false;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000616 }
John McCallcfef5462011-11-07 22:49:50 +0000617 }
618
619 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
620 Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
Craig Topperc3ec1492014-05-26 06:22:03 +0000621 return (Getter != nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000622}
623
624/// Try to find the most accurate setter declaration for the property
625/// reference.
626///
Fangrui Song6907ce22018-07-30 19:24:48 +0000627/// \return true if a setter was found, in which case Setter
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000628bool ObjCPropertyOpBuilder::findSetter(bool warn) {
John McCallfe96e0b2011-11-06 09:01:30 +0000629 // For implicit properties, just trust the lookup we already did.
630 if (RefExpr->isImplicitProperty()) {
631 if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
632 Setter = setter;
633 SetterSelector = setter->getSelector();
634 return true;
John McCall526ab472011-10-25 17:37:35 +0000635 } else {
John McCallfe96e0b2011-11-06 09:01:30 +0000636 IdentifierInfo *getterName =
637 RefExpr->getImplicitPropertyGetter()->getSelector()
638 .getIdentifierInfoForSlot(0);
639 SetterSelector =
Adrian Prantla4ce9062013-06-07 22:29:12 +0000640 SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
641 S.PP.getSelectorTable(),
642 getterName);
John McCallfe96e0b2011-11-06 09:01:30 +0000643 return false;
John McCall526ab472011-10-25 17:37:35 +0000644 }
John McCallfe96e0b2011-11-06 09:01:30 +0000645 }
646
647 // For explicit properties, this is more involved.
648 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
649 SetterSelector = prop->getSetterName();
650
651 // Do a normal method lookup first.
652 if (ObjCMethodDecl *setter =
653 LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
Jordan Rosed01e83a2012-10-10 16:42:25 +0000654 if (setter->isPropertyAccessor() && warn)
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000655 if (const ObjCInterfaceDecl *IFace =
656 dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) {
Craig Topperbf3e3272014-08-30 16:55:52 +0000657 StringRef thisPropertyName = prop->getName();
Jordan Rosea7d03842013-02-08 22:30:41 +0000658 // Try flipping the case of the first character.
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000659 char front = thisPropertyName.front();
Jordan Rosea7d03842013-02-08 22:30:41 +0000660 front = isLowercase(front) ? toUppercase(front) : toLowercase(front);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000661 SmallString<100> PropertyName = thisPropertyName;
662 PropertyName[0] = front;
663 IdentifierInfo *AltMember = &S.PP.getIdentifierTable().get(PropertyName);
Manman Ren5b786402016-01-28 18:49:28 +0000664 if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration(
665 AltMember, prop->getQueryKind()))
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000666 if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) {
Richard Smithf8812672016-12-02 22:38:31 +0000667 S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000668 << prop << prop1 << setter->getSelector();
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000669 S.Diag(prop->getLocation(), diag::note_property_declare);
670 S.Diag(prop1->getLocation(), diag::note_property_declare);
671 }
672 }
John McCallfe96e0b2011-11-06 09:01:30 +0000673 Setter = setter;
674 return true;
675 }
676
677 // That can fail in the somewhat crazy situation that we're
678 // type-checking a message send within the @interface declaration
679 // that declared the @property. But it's not clear that that's
680 // valuable to support.
681
682 return false;
683}
684
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000685void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() {
Fariborz Jahanian55513282014-05-28 18:12:10 +0000686 if (S.getCurLexicalContext()->isObjCContainer() &&
687 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl &&
688 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) {
689 if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) {
690 S.Diag(RefExpr->getLocation(),
691 diag::err_property_function_in_objc_container);
692 S.Diag(prop->getLocation(), diag::note_property_declare);
Fariborz Jahanian55513282014-05-28 18:12:10 +0000693 }
694 }
Fariborz Jahanian55513282014-05-28 18:12:10 +0000695}
696
John McCallfe96e0b2011-11-06 09:01:30 +0000697/// Capture the base object of an Objective-C property expression.
698Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000699 assert(InstanceReceiver == nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000700
701 // If we have a base, capture it in an OVE and rebuild the syntactic
702 // form to use the OVE as its base.
703 if (RefExpr->isObjectReceiver()) {
704 InstanceReceiver = capture(RefExpr->getBase());
Alexey Bataevf7630272015-11-25 12:01:00 +0000705 syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * {
706 return InstanceReceiver;
707 }).rebuild(syntacticBase);
John McCallfe96e0b2011-11-06 09:01:30 +0000708 }
709
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000710 if (ObjCPropertyRefExpr *
711 refE = dyn_cast<ObjCPropertyRefExpr>(syntacticBase->IgnoreParens()))
712 SyntacticRefExpr = refE;
713
John McCallfe96e0b2011-11-06 09:01:30 +0000714 return syntacticBase;
715}
716
717/// Load from an Objective-C property reference.
718ExprResult ObjCPropertyOpBuilder::buildGet() {
719 findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000720 if (!Getter) {
721 DiagnoseUnsupportedPropertyUse();
722 return ExprError();
723 }
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000724
725 if (SyntacticRefExpr)
726 SyntacticRefExpr->setIsMessagingGetter();
727
Douglas Gregore83b9562015-07-07 03:57:53 +0000728 QualType receiverType = RefExpr->getReceiverType(S.Context);
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000729 if (!Getter->isImplicit())
730 S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true);
John McCallfe96e0b2011-11-06 09:01:30 +0000731 // Build a message-send.
732 ExprResult msg;
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000733 if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
734 RefExpr->isObjectReceiver()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000735 assert(InstanceReceiver || RefExpr->isSuperReceiver());
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000736 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
737 GenericLoc, Getter->getSelector(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000738 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000739 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000740 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000741 GenericLoc, Getter->getSelector(),
742 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000743 }
744 return msg;
745}
John McCall526ab472011-10-25 17:37:35 +0000746
John McCallfe96e0b2011-11-06 09:01:30 +0000747/// Store to an Objective-C property reference.
748///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000749/// \param captureSetValueAsResult If true, capture the actual
John McCallfe96e0b2011-11-06 09:01:30 +0000750/// value being set as the value of the property operation.
751ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
752 bool captureSetValueAsResult) {
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000753 if (!findSetter(false)) {
754 DiagnoseUnsupportedPropertyUse();
755 return ExprError();
756 }
John McCallfe96e0b2011-11-06 09:01:30 +0000757
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000758 if (SyntacticRefExpr)
759 SyntacticRefExpr->setIsMessagingSetter();
760
Douglas Gregore83b9562015-07-07 03:57:53 +0000761 QualType receiverType = RefExpr->getReceiverType(S.Context);
John McCallfe96e0b2011-11-06 09:01:30 +0000762
763 // Use assignment constraints when possible; they give us better
764 // diagnostics. "When possible" basically means anything except a
765 // C++ class type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000766 if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000767 QualType paramType = (*Setter->param_begin())->getType()
768 .substObjCMemberType(
769 receiverType,
770 Setter->getDeclContext(),
771 ObjCSubstitutionContext::Parameter);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000772 if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000773 ExprResult opResult = op;
774 Sema::AssignConvertType assignResult
775 = S.CheckSingleAssignmentConstraints(paramType, opResult);
Richard Smithe15a3702016-10-06 23:12:58 +0000776 if (opResult.isInvalid() ||
777 S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType,
John McCallfe96e0b2011-11-06 09:01:30 +0000778 op->getType(), opResult.get(),
779 Sema::AA_Assigning))
780 return ExprError();
781
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000782 op = opResult.get();
John McCallfe96e0b2011-11-06 09:01:30 +0000783 assert(op && "successful assignment left argument invalid?");
John McCall526ab472011-10-25 17:37:35 +0000784 }
785 }
786
John McCallfe96e0b2011-11-06 09:01:30 +0000787 // Arguments.
788 Expr *args[] = { op };
John McCall526ab472011-10-25 17:37:35 +0000789
John McCallfe96e0b2011-11-06 09:01:30 +0000790 // Build a message-send.
791 ExprResult msg;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000792 if (!Setter->isImplicit())
793 S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true);
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000794 if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
795 RefExpr->isObjectReceiver()) {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000796 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
797 GenericLoc, SetterSelector, Setter,
798 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000799 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000800 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
801 GenericLoc,
802 SetterSelector, Setter,
803 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000804 }
805
806 if (!msg.isInvalid() && captureSetValueAsResult) {
807 ObjCMessageExpr *msgExpr =
808 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
809 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000810 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +0000811 msgExpr->setArg(0, captureValueAsResult(arg));
John McCallfe96e0b2011-11-06 09:01:30 +0000812 }
813
814 return msg;
John McCall526ab472011-10-25 17:37:35 +0000815}
816
John McCallfe96e0b2011-11-06 09:01:30 +0000817/// @property-specific behavior for doing lvalue-to-rvalue conversion.
818ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) {
819 // Explicit properties always have getters, but implicit ones don't.
820 // Check that before proceeding.
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000821 if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000822 S.Diag(RefExpr->getLocation(), diag::err_getter_not_found)
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000823 << RefExpr->getSourceRange();
John McCall526ab472011-10-25 17:37:35 +0000824 return ExprError();
825 }
826
John McCallfe96e0b2011-11-06 09:01:30 +0000827 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
John McCall526ab472011-10-25 17:37:35 +0000828 if (result.isInvalid()) return ExprError();
829
John McCallfe96e0b2011-11-06 09:01:30 +0000830 if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType())
831 S.DiagnosePropertyAccessorMismatch(RefExpr->getExplicitProperty(),
832 Getter, RefExpr->getLocation());
833
834 // As a special case, if the method returns 'id', try to get
835 // a better type from the property.
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000836 if (RefExpr->isExplicitProperty() && result.get()->isRValue()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000837 QualType receiverType = RefExpr->getReceiverType(S.Context);
838 QualType propType = RefExpr->getExplicitProperty()
839 ->getUsageType(receiverType);
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000840 if (result.get()->getType()->isObjCIdType()) {
841 if (const ObjCObjectPointerType *ptr
842 = propType->getAs<ObjCObjectPointerType>()) {
843 if (!ptr->isObjCIdType())
844 result = S.ImpCastExprToType(result.get(), propType, CK_BitCast);
845 }
846 }
Brian Kelleycafd9122017-03-29 17:55:11 +0000847 if (propType.getObjCLifetime() == Qualifiers::OCL_Weak &&
848 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
849 RefExpr->getLocation()))
850 S.getCurFunction()->markSafeWeakUse(RefExpr);
John McCallfe96e0b2011-11-06 09:01:30 +0000851 }
852
John McCall526ab472011-10-25 17:37:35 +0000853 return result;
854}
855
John McCallfe96e0b2011-11-06 09:01:30 +0000856/// Try to build this as a call to a getter that returns a reference.
857///
858/// \return true if it was possible, whether or not it actually
859/// succeeded
860bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op,
861 ExprResult &result) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000862 if (!S.getLangOpts().CPlusPlus) return false;
John McCallfe96e0b2011-11-06 09:01:30 +0000863
864 findGetter();
Olivier Goffart4c182c82014-08-04 17:28:05 +0000865 if (!Getter) {
866 // The property has no setter and no getter! This can happen if the type is
867 // invalid. Error have already been reported.
868 result = ExprError();
869 return true;
870 }
John McCallfe96e0b2011-11-06 09:01:30 +0000871
872 // Only do this if the getter returns an l-value reference type.
Alp Toker314cc812014-01-25 16:55:45 +0000873 QualType resultType = Getter->getReturnType();
John McCallfe96e0b2011-11-06 09:01:30 +0000874 if (!resultType->isLValueReferenceType()) return false;
875
876 result = buildRValueOperation(op);
877 return true;
878}
879
880/// @property-specific behavior for doing assignments.
881ExprResult
882ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc,
883 SourceLocation opcLoc,
884 BinaryOperatorKind opcode,
885 Expr *LHS, Expr *RHS) {
John McCall526ab472011-10-25 17:37:35 +0000886 assert(BinaryOperator::isAssignmentOp(opcode));
John McCall526ab472011-10-25 17:37:35 +0000887
888 // If there's no setter, we have no choice but to try to assign to
889 // the result of the getter.
John McCallfe96e0b2011-11-06 09:01:30 +0000890 if (!findSetter()) {
891 ExprResult result;
892 if (tryBuildGetOfReference(LHS, result)) {
893 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000894 return S.BuildBinOp(Sc, opcLoc, opcode, result.get(), RHS);
John McCall526ab472011-10-25 17:37:35 +0000895 }
896
897 // Otherwise, it's an error.
John McCallfe96e0b2011-11-06 09:01:30 +0000898 S.Diag(opcLoc, diag::err_nosetter_property_assignment)
899 << unsigned(RefExpr->isImplicitProperty())
900 << SetterSelector
John McCall526ab472011-10-25 17:37:35 +0000901 << LHS->getSourceRange() << RHS->getSourceRange();
902 return ExprError();
903 }
904
905 // If there is a setter, we definitely want to use it.
906
John McCallfe96e0b2011-11-06 09:01:30 +0000907 // Verify that we can do a compound assignment.
908 if (opcode != BO_Assign && !findGetter()) {
909 S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment)
John McCall526ab472011-10-25 17:37:35 +0000910 << LHS->getSourceRange() << RHS->getSourceRange();
911 return ExprError();
912 }
913
John McCallfe96e0b2011-11-06 09:01:30 +0000914 ExprResult result =
915 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
John McCall526ab472011-10-25 17:37:35 +0000916 if (result.isInvalid()) return ExprError();
917
John McCallfe96e0b2011-11-06 09:01:30 +0000918 // Various warnings about property assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000919 if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) {
John McCallfe96e0b2011-11-06 09:01:30 +0000920 S.checkRetainCycles(InstanceReceiver->getSourceExpr(), RHS);
921 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
922 }
923
John McCall526ab472011-10-25 17:37:35 +0000924 return result;
925}
John McCallfe96e0b2011-11-06 09:01:30 +0000926
927/// @property-specific behavior for doing increments and decrements.
928ExprResult
929ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
930 UnaryOperatorKind opcode,
931 Expr *op) {
932 // If there's no setter, we have no choice but to try to assign to
933 // the result of the getter.
934 if (!findSetter()) {
935 ExprResult result;
936 if (tryBuildGetOfReference(op, result)) {
937 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000938 return S.BuildUnaryOp(Sc, opcLoc, opcode, result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000939 }
940
941 // Otherwise, it's an error.
942 S.Diag(opcLoc, diag::err_nosetter_property_incdec)
943 << unsigned(RefExpr->isImplicitProperty())
944 << unsigned(UnaryOperator::isDecrementOp(opcode))
945 << SetterSelector
946 << op->getSourceRange();
947 return ExprError();
948 }
949
950 // If there is a setter, we definitely want to use it.
951
952 // We also need a getter.
953 if (!findGetter()) {
954 assert(RefExpr->isImplicitProperty());
955 S.Diag(opcLoc, diag::err_nogetter_property_incdec)
956 << unsigned(UnaryOperator::isDecrementOp(opcode))
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000957 << GetterSelector
John McCallfe96e0b2011-11-06 09:01:30 +0000958 << op->getSourceRange();
959 return ExprError();
960 }
961
962 return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op);
963}
964
Jordan Rosed3934582012-09-28 22:21:30 +0000965ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) {
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000966 if (isWeakProperty() && !S.isUnevaluatedContext() &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000967 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000968 SyntacticForm->getBeginLoc()))
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000969 S.getCurFunction()->recordUseOfWeak(SyntacticRefExpr,
970 SyntacticRefExpr->isMessagingGetter());
Jordan Rosed3934582012-09-28 22:21:30 +0000971
972 return PseudoOpBuilder::complete(SyntacticForm);
973}
974
Ted Kremeneke65b0862012-03-06 20:05:56 +0000975// ObjCSubscript build stuff.
976//
977
Fangrui Song6907ce22018-07-30 19:24:48 +0000978/// objective-c subscripting-specific behavior for doing lvalue-to-rvalue
Ted Kremeneke65b0862012-03-06 20:05:56 +0000979/// conversion.
Fangrui Song6907ce22018-07-30 19:24:48 +0000980/// FIXME. Remove this routine if it is proven that no additional
Ted Kremeneke65b0862012-03-06 20:05:56 +0000981/// specifity is needed.
982ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) {
983 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
984 if (result.isInvalid()) return ExprError();
985 return result;
986}
987
988/// objective-c subscripting-specific behavior for doing assignments.
989ExprResult
990ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc,
991 SourceLocation opcLoc,
992 BinaryOperatorKind opcode,
993 Expr *LHS, Expr *RHS) {
994 assert(BinaryOperator::isAssignmentOp(opcode));
995 // There must be a method to do the Index'ed assignment.
996 if (!findAtIndexSetter())
997 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +0000998
Ted Kremeneke65b0862012-03-06 20:05:56 +0000999 // Verify that we can do a compound assignment.
1000 if (opcode != BO_Assign && !findAtIndexGetter())
1001 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001002
Ted Kremeneke65b0862012-03-06 20:05:56 +00001003 ExprResult result =
1004 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
1005 if (result.isInvalid()) return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001006
Ted Kremeneke65b0862012-03-06 20:05:56 +00001007 // Various warnings about objc Index'ed assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001008 if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001009 S.checkRetainCycles(InstanceBase->getSourceExpr(), RHS);
1010 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
1011 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Ted Kremeneke65b0862012-03-06 20:05:56 +00001013 return result;
1014}
1015
1016/// Capture the base object of an Objective-C Index'ed expression.
1017Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001018 assert(InstanceBase == nullptr);
1019
Ted Kremeneke65b0862012-03-06 20:05:56 +00001020 // Capture base expression in an OVE and rebuild the syntactic
1021 // form to use the OVE as its base expression.
1022 InstanceBase = capture(RefExpr->getBaseExpr());
1023 InstanceKey = capture(RefExpr->getKeyExpr());
Alexey Bataevf7630272015-11-25 12:01:00 +00001024
Ted Kremeneke65b0862012-03-06 20:05:56 +00001025 syntacticBase =
Alexey Bataevf7630272015-11-25 12:01:00 +00001026 Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1027 switch (Idx) {
1028 case 0:
1029 return InstanceBase;
1030 case 1:
1031 return InstanceKey;
1032 default:
1033 llvm_unreachable("Unexpected index for ObjCSubscriptExpr");
1034 }
1035 }).rebuild(syntacticBase);
1036
Ted Kremeneke65b0862012-03-06 20:05:56 +00001037 return syntacticBase;
1038}
1039
Fangrui Song6907ce22018-07-30 19:24:48 +00001040/// CheckSubscriptingKind - This routine decide what type
Ted Kremeneke65b0862012-03-06 20:05:56 +00001041/// of indexing represented by "FromE" is being done.
Fangrui Song6907ce22018-07-30 19:24:48 +00001042Sema::ObjCSubscriptKind
Ted Kremeneke65b0862012-03-06 20:05:56 +00001043 Sema::CheckSubscriptingKind(Expr *FromE) {
1044 // If the expression already has integral or enumeration type, we're golden.
1045 QualType T = FromE->getType();
1046 if (T->isIntegralOrEnumerationType())
1047 return OS_Array;
Fangrui Song6907ce22018-07-30 19:24:48 +00001048
Ted Kremeneke65b0862012-03-06 20:05:56 +00001049 // If we don't have a class type in C++, there's no way we can get an
1050 // expression of integral or enumeration type.
1051 const RecordType *RecordTy = T->getAs<RecordType>();
Fariborz Jahaniand13951f2014-09-10 20:55:31 +00001052 if (!RecordTy &&
1053 (T->isObjCObjectPointerType() || T->isVoidPointerType()))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001054 // All other scalar cases are assumed to be dictionary indexing which
1055 // caller handles, with diagnostics if needed.
1056 return OS_Dictionary;
Fangrui Song6907ce22018-07-30 19:24:48 +00001057 if (!getLangOpts().CPlusPlus ||
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001058 !RecordTy || RecordTy->isIncompleteType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001059 // No indexing can be done. Issue diagnostics and quit.
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001060 const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
1061 if (isa<StringLiteral>(IndexExpr))
1062 Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
1063 << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
1064 else
1065 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1066 << T;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001067 return OS_Error;
1068 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001069
Ted Kremeneke65b0862012-03-06 20:05:56 +00001070 // We must have a complete class type.
Fangrui Song6907ce22018-07-30 19:24:48 +00001071 if (RequireCompleteType(FromE->getExprLoc(), T,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001072 diag::err_objc_index_incomplete_class_type, FromE))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001073 return OS_Error;
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Ted Kremeneke65b0862012-03-06 20:05:56 +00001075 // Look for a conversion to an integral, enumeration type, or
1076 // objective-C pointer type.
Ted Kremeneke65b0862012-03-06 20:05:56 +00001077 int NoIntegrals=0, NoObjCIdPointers=0;
1078 SmallVector<CXXConversionDecl *, 4> ConversionDecls;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001079
1080 for (NamedDecl *D : cast<CXXRecordDecl>(RecordTy->getDecl())
1081 ->getVisibleConversionFunctions()) {
1082 if (CXXConversionDecl *Conversion =
1083 dyn_cast<CXXConversionDecl>(D->getUnderlyingDecl())) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001084 QualType CT = Conversion->getConversionType().getNonReferenceType();
1085 if (CT->isIntegralOrEnumerationType()) {
1086 ++NoIntegrals;
1087 ConversionDecls.push_back(Conversion);
1088 }
1089 else if (CT->isObjCIdType() ||CT->isBlockPointerType()) {
1090 ++NoObjCIdPointers;
1091 ConversionDecls.push_back(Conversion);
1092 }
1093 }
1094 }
1095 if (NoIntegrals ==1 && NoObjCIdPointers == 0)
1096 return OS_Array;
1097 if (NoIntegrals == 0 && NoObjCIdPointers == 1)
1098 return OS_Dictionary;
1099 if (NoIntegrals == 0 && NoObjCIdPointers == 0) {
1100 // No conversion function was found. Issue diagnostic and return.
1101 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1102 << FromE->getType();
1103 return OS_Error;
1104 }
1105 Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion)
1106 << FromE->getType();
1107 for (unsigned int i = 0; i < ConversionDecls.size(); i++)
Richard Smith01d96982016-12-02 23:00:28 +00001108 Diag(ConversionDecls[i]->getLocation(),
1109 diag::note_conv_function_declared_at);
1110
Ted Kremeneke65b0862012-03-06 20:05:56 +00001111 return OS_Error;
1112}
1113
Fariborz Jahanian90804912012-08-02 18:03:58 +00001114/// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF
1115/// objects used as dictionary subscript key objects.
Fangrui Song6907ce22018-07-30 19:24:48 +00001116static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001117 Expr *Key) {
1118 if (ContainerT.isNull())
1119 return;
1120 // dictionary subscripting.
1121 // - (id)objectForKeyedSubscript:(id)key;
1122 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001123 &S.Context.Idents.get("objectForKeyedSubscript")
Fariborz Jahanian90804912012-08-02 18:03:58 +00001124 };
1125 Selector GetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
Fangrui Song6907ce22018-07-30 19:24:48 +00001126 ObjCMethodDecl *Getter = S.LookupMethodInObjectType(GetterSelector, ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001127 true /*instance*/);
1128 if (!Getter)
1129 return;
Alp Toker03376dc2014-07-07 09:02:20 +00001130 QualType T = Getter->parameters()[0]->getType();
Brian Kelley11352a82017-03-29 18:09:02 +00001131 S.CheckObjCConversion(Key->getSourceRange(), T, Key,
1132 Sema::CCK_ImplicitConversion);
Fariborz Jahanian90804912012-08-02 18:03:58 +00001133}
1134
Ted Kremeneke65b0862012-03-06 20:05:56 +00001135bool ObjCSubscriptOpBuilder::findAtIndexGetter() {
1136 if (AtIndexGetter)
1137 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001138
Ted Kremeneke65b0862012-03-06 20:05:56 +00001139 Expr *BaseExpr = RefExpr->getBaseExpr();
1140 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001141
Ted Kremeneke65b0862012-03-06 20:05:56 +00001142 QualType ResultType;
1143 if (const ObjCObjectPointerType *PTy =
1144 BaseT->getAs<ObjCObjectPointerType>()) {
1145 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001146 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001147 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001148 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001149 if (Res == Sema::OS_Error) {
1150 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001151 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001152 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001153 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001154 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001155 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001156
Ted Kremeneke65b0862012-03-06 20:05:56 +00001157 if (ResultType.isNull()) {
1158 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1159 << BaseExpr->getType() << arrayRef;
1160 return false;
1161 }
1162 if (!arrayRef) {
1163 // dictionary subscripting.
1164 // - (id)objectForKeyedSubscript:(id)key;
1165 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001166 &S.Context.Idents.get("objectForKeyedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001167 };
1168 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1169 }
1170 else {
1171 // - (id)objectAtIndexedSubscript:(size_t)index;
1172 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001173 &S.Context.Idents.get("objectAtIndexedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001174 };
Fangrui Song6907ce22018-07-30 19:24:48 +00001175
Ted Kremeneke65b0862012-03-06 20:05:56 +00001176 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1177 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001178
1179 AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001180 true /*instance*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00001181
David Blaikiebbafb8a2012-03-11 07:00:24 +00001182 if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
Adrian Prantl2073dd22019-11-04 14:28:14 -08001183 AtIndexGetter = ObjCMethodDecl::Create(
1184 S.Context, SourceLocation(), SourceLocation(), AtIndexGetterSelector,
1185 S.Context.getObjCIdType() /*ReturnType*/, nullptr /*TypeSourceInfo */,
1186 S.Context.getTranslationUnitDecl(), true /*Instance*/,
1187 false /*isVariadic*/,
1188 /*isPropertyAccessor=*/false,
1189 /*isSynthesizedAccessorStub=*/false,
1190 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1191 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001192 ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1193 SourceLocation(), SourceLocation(),
1194 arrayRef ? &S.Context.Idents.get("index")
1195 : &S.Context.Idents.get("key"),
1196 arrayRef ? S.Context.UnsignedLongTy
1197 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001198 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001199 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001200 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001201 AtIndexGetter->setMethodParams(S.Context, Argument, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001202 }
1203
1204 if (!AtIndexGetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001205 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001206 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1207 << BaseExpr->getType() << 0 << arrayRef;
1208 return false;
1209 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001210 AtIndexGetter =
1211 S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1212 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001213 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001214 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
Ted Kremeneke65b0862012-03-06 20:05:56 +00001216 if (AtIndexGetter) {
Alp Toker03376dc2014-07-07 09:02:20 +00001217 QualType T = AtIndexGetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001218 if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1219 (!arrayRef && !T->isObjCObjectPointerType())) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001220 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001221 arrayRef ? diag::err_objc_subscript_index_type
1222 : diag::err_objc_subscript_key_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001223 S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001224 diag::note_parameter_type) << T;
1225 return false;
1226 }
Alp Toker314cc812014-01-25 16:55:45 +00001227 QualType R = AtIndexGetter->getReturnType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001228 if (!R->isObjCObjectPointerType()) {
1229 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1230 diag::err_objc_indexing_method_result_type) << R << arrayRef;
1231 S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1232 AtIndexGetter->getDeclName();
1233 }
1234 }
1235 return true;
1236}
1237
1238bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1239 if (AtIndexSetter)
1240 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001241
Ted Kremeneke65b0862012-03-06 20:05:56 +00001242 Expr *BaseExpr = RefExpr->getBaseExpr();
1243 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001244
Ted Kremeneke65b0862012-03-06 20:05:56 +00001245 QualType ResultType;
1246 if (const ObjCObjectPointerType *PTy =
1247 BaseT->getAs<ObjCObjectPointerType>()) {
1248 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001249 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001250
1251 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001252 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001253 if (Res == Sema::OS_Error) {
1254 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001255 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001256 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001257 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001258 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001259 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001260
Ted Kremeneke65b0862012-03-06 20:05:56 +00001261 if (ResultType.isNull()) {
1262 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1263 << BaseExpr->getType() << arrayRef;
1264 return false;
1265 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001266
Ted Kremeneke65b0862012-03-06 20:05:56 +00001267 if (!arrayRef) {
1268 // dictionary subscripting.
1269 // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1270 IdentifierInfo *KeyIdents[] = {
1271 &S.Context.Idents.get("setObject"),
1272 &S.Context.Idents.get("forKeyedSubscript")
1273 };
1274 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1275 }
1276 else {
1277 // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1278 IdentifierInfo *KeyIdents[] = {
1279 &S.Context.Idents.get("setObject"),
1280 &S.Context.Idents.get("atIndexedSubscript")
1281 };
1282 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1283 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001284 AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001285 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001286
David Blaikiebbafb8a2012-03-11 07:00:24 +00001287 if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001288 TypeSourceInfo *ReturnTInfo = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001289 QualType ReturnType = S.Context.VoidTy;
Alp Toker314cc812014-01-25 16:55:45 +00001290 AtIndexSetter = ObjCMethodDecl::Create(
1291 S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector,
1292 ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(),
1293 true /*Instance*/, false /*isVariadic*/,
1294 /*isPropertyAccessor=*/false,
Adrian Prantl2073dd22019-11-04 14:28:14 -08001295 /*isSynthesizedAccessorStub=*/false,
Alp Toker314cc812014-01-25 16:55:45 +00001296 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1297 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001298 SmallVector<ParmVarDecl *, 2> Params;
1299 ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter,
1300 SourceLocation(), SourceLocation(),
1301 &S.Context.Idents.get("object"),
1302 S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001303 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001304 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001305 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001306 Params.push_back(object);
1307 ParmVarDecl *key = ParmVarDecl::Create(S.Context, AtIndexSetter,
1308 SourceLocation(), SourceLocation(),
1309 arrayRef ? &S.Context.Idents.get("index")
1310 : &S.Context.Idents.get("key"),
1311 arrayRef ? S.Context.UnsignedLongTy
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(key);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001317 AtIndexSetter->setMethodParams(S.Context, Params, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001318 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001319
Ted Kremeneke65b0862012-03-06 20:05:56 +00001320 if (!AtIndexSetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001321 if (!BaseT->isObjCIdType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001322 S.Diag(BaseExpr->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001323 diag::err_objc_subscript_method_not_found)
1324 << BaseExpr->getType() << 1 << arrayRef;
1325 return false;
1326 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001327 AtIndexSetter =
1328 S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector,
1329 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001330 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001331 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001332
Ted Kremeneke65b0862012-03-06 20:05:56 +00001333 bool err = false;
1334 if (AtIndexSetter && arrayRef) {
Alp Toker03376dc2014-07-07 09:02:20 +00001335 QualType T = AtIndexSetter->parameters()[1]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001336 if (!T->isIntegralOrEnumerationType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001337 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001338 diag::err_objc_subscript_index_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001339 S.Diag(AtIndexSetter->parameters()[1]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001340 diag::note_parameter_type) << T;
1341 err = true;
1342 }
Alp Toker03376dc2014-07-07 09:02:20 +00001343 T = AtIndexSetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001344 if (!T->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001345 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001346 diag::err_objc_subscript_object_type) << T << arrayRef;
Fangrui Song6907ce22018-07-30 19:24:48 +00001347 S.Diag(AtIndexSetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001348 diag::note_parameter_type) << T;
1349 err = true;
1350 }
1351 }
1352 else if (AtIndexSetter && !arrayRef)
1353 for (unsigned i=0; i <2; i++) {
Alp Toker03376dc2014-07-07 09:02:20 +00001354 QualType T = AtIndexSetter->parameters()[i]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001355 if (!T->isObjCObjectPointerType()) {
1356 if (i == 1)
1357 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1358 diag::err_objc_subscript_key_type) << T;
1359 else
1360 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1361 diag::err_objc_subscript_dic_object_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001362 S.Diag(AtIndexSetter->parameters()[i]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001363 diag::note_parameter_type) << T;
1364 err = true;
1365 }
1366 }
1367
1368 return !err;
1369}
1370
1371// Get the object at "Index" position in the container.
1372// [BaseExpr objectAtIndexedSubscript : IndexExpr];
1373ExprResult ObjCSubscriptOpBuilder::buildGet() {
1374 if (!findAtIndexGetter())
1375 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001376
Ted Kremeneke65b0862012-03-06 20:05:56 +00001377 QualType receiverType = InstanceBase->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001378
Ted Kremeneke65b0862012-03-06 20:05:56 +00001379 // Build a message-send.
1380 ExprResult msg;
1381 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001382
Ted Kremeneke65b0862012-03-06 20:05:56 +00001383 // Arguments.
1384 Expr *args[] = { Index };
1385 assert(InstanceBase);
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001386 if (AtIndexGetter)
1387 S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001388 msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1389 GenericLoc,
1390 AtIndexGetterSelector, AtIndexGetter,
1391 MultiExprArg(args, 1));
1392 return msg;
1393}
1394
1395/// Store into the container the "op" object at "Index"'ed location
1396/// by building this messaging expression:
1397/// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00001398/// \param captureSetValueAsResult If true, capture the actual
Ted Kremeneke65b0862012-03-06 20:05:56 +00001399/// value being set as the value of the property operation.
1400ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
1401 bool captureSetValueAsResult) {
1402 if (!findAtIndexSetter())
1403 return ExprError();
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001404 if (AtIndexSetter)
1405 S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001406 QualType receiverType = InstanceBase->getType();
1407 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001408
Ted Kremeneke65b0862012-03-06 20:05:56 +00001409 // Arguments.
1410 Expr *args[] = { op, Index };
Fangrui Song6907ce22018-07-30 19:24:48 +00001411
Ted Kremeneke65b0862012-03-06 20:05:56 +00001412 // Build a message-send.
1413 ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1414 GenericLoc,
1415 AtIndexSetterSelector,
1416 AtIndexSetter,
1417 MultiExprArg(args, 2));
Fangrui Song6907ce22018-07-30 19:24:48 +00001418
Ted Kremeneke65b0862012-03-06 20:05:56 +00001419 if (!msg.isInvalid() && captureSetValueAsResult) {
1420 ObjCMessageExpr *msgExpr =
1421 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
1422 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +00001423 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +00001424 msgExpr->setArg(0, captureValueAsResult(arg));
Ted Kremeneke65b0862012-03-06 20:05:56 +00001425 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001426
Ted Kremeneke65b0862012-03-06 20:05:56 +00001427 return msg;
1428}
1429
John McCallfe96e0b2011-11-06 09:01:30 +00001430//===----------------------------------------------------------------------===//
John McCall5e77d762013-04-16 07:28:30 +00001431// MSVC __declspec(property) references
1432//===----------------------------------------------------------------------===//
1433
Alexey Bataevf7630272015-11-25 12:01:00 +00001434MSPropertyRefExpr *
1435MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) {
1436 CallArgs.insert(CallArgs.begin(), E->getIdx());
1437 Expr *Base = E->getBase()->IgnoreParens();
1438 while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Base)) {
1439 CallArgs.insert(CallArgs.begin(), MSPropSubscript->getIdx());
1440 Base = MSPropSubscript->getBase()->IgnoreParens();
1441 }
1442 return cast<MSPropertyRefExpr>(Base);
1443}
1444
John McCall5e77d762013-04-16 07:28:30 +00001445Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Alexey Bataev69103472015-10-14 04:05:42 +00001446 InstanceBase = capture(RefExpr->getBaseExpr());
Aaron Ballman72f65632017-11-03 20:09:17 +00001447 llvm::for_each(CallArgs, [this](Expr *&Arg) { Arg = capture(Arg); });
Alexey Bataevf7630272015-11-25 12:01:00 +00001448 syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1449 switch (Idx) {
1450 case 0:
1451 return InstanceBase;
1452 default:
1453 assert(Idx <= CallArgs.size());
1454 return CallArgs[Idx - 1];
1455 }
1456 }).rebuild(syntacticBase);
John McCall5e77d762013-04-16 07:28:30 +00001457
1458 return syntacticBase;
1459}
1460
1461ExprResult MSPropertyOpBuilder::buildGet() {
1462 if (!RefExpr->getPropertyDecl()->hasGetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001463 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001464 << 0 /* getter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001465 return ExprError();
1466 }
1467
1468 UnqualifiedId GetterName;
1469 IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId();
1470 GetterName.setIdentifier(II, RefExpr->getMemberLoc());
1471 CXXScopeSpec SS;
1472 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001473 ExprResult GetterExpr =
1474 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1475 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1476 SourceLocation(), GetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001477 if (GetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001478 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001479 diag::err_cannot_find_suitable_accessor) << 0 /* getter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001480 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001481 return ExprError();
1482 }
1483
Richard Smith255b85f2019-05-08 01:36:36 +00001484 return S.BuildCallExpr(S.getCurScope(), GetterExpr.get(),
Alexey Bataevf7630272015-11-25 12:01:00 +00001485 RefExpr->getSourceRange().getBegin(), CallArgs,
John McCall5e77d762013-04-16 07:28:30 +00001486 RefExpr->getSourceRange().getEnd());
1487}
1488
1489ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl,
1490 bool captureSetValueAsResult) {
1491 if (!RefExpr->getPropertyDecl()->hasSetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001492 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001493 << 1 /* setter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001494 return ExprError();
1495 }
1496
1497 UnqualifiedId SetterName;
1498 IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId();
1499 SetterName.setIdentifier(II, RefExpr->getMemberLoc());
1500 CXXScopeSpec SS;
1501 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001502 ExprResult SetterExpr =
1503 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1504 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1505 SourceLocation(), SetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001506 if (SetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001507 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001508 diag::err_cannot_find_suitable_accessor) << 1 /* setter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001509 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001510 return ExprError();
1511 }
1512
Alexey Bataevf7630272015-11-25 12:01:00 +00001513 SmallVector<Expr*, 4> ArgExprs;
1514 ArgExprs.append(CallArgs.begin(), CallArgs.end());
John McCall5e77d762013-04-16 07:28:30 +00001515 ArgExprs.push_back(op);
Richard Smith255b85f2019-05-08 01:36:36 +00001516 return S.BuildCallExpr(S.getCurScope(), SetterExpr.get(),
John McCall5e77d762013-04-16 07:28:30 +00001517 RefExpr->getSourceRange().getBegin(), ArgExprs,
1518 op->getSourceRange().getEnd());
1519}
1520
1521//===----------------------------------------------------------------------===//
John McCallfe96e0b2011-11-06 09:01:30 +00001522// General Sema routines.
1523//===----------------------------------------------------------------------===//
1524
1525ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
1526 Expr *opaqueRef = E->IgnoreParens();
1527 if (ObjCPropertyRefExpr *refExpr
1528 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001529 ObjCPropertyOpBuilder builder(*this, refExpr, true);
John McCallfe96e0b2011-11-06 09:01:30 +00001530 return builder.buildRValueOperation(E);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001531 }
1532 else if (ObjCSubscriptRefExpr *refExpr
1533 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001534 ObjCSubscriptOpBuilder builder(*this, refExpr, true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001535 return builder.buildRValueOperation(E);
John McCall5e77d762013-04-16 07:28:30 +00001536 } else if (MSPropertyRefExpr *refExpr
1537 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001538 MSPropertyOpBuilder builder(*this, refExpr, true);
John McCall5e77d762013-04-16 07:28:30 +00001539 return builder.buildRValueOperation(E);
Alexey Bataevf7630272015-11-25 12:01:00 +00001540 } else if (MSPropertySubscriptExpr *RefExpr =
1541 dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001542 MSPropertyOpBuilder Builder(*this, RefExpr, true);
Alexey Bataevf7630272015-11-25 12:01:00 +00001543 return Builder.buildRValueOperation(E);
John McCallfe96e0b2011-11-06 09:01:30 +00001544 } else {
1545 llvm_unreachable("unknown pseudo-object kind!");
1546 }
1547}
1548
1549/// Check an increment or decrement of a pseudo-object expression.
1550ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc,
1551 UnaryOperatorKind opcode, Expr *op) {
1552 // Do nothing if the operand is dependent.
1553 if (op->isTypeDependent())
Melanie Blowerf5360d42020-05-01 10:32:06 -07001554 return UnaryOperator::Create(Context, op, opcode, Context.DependentTy,
1555 VK_RValue, OK_Ordinary, opcLoc, false,
1556 CurFPFeatures);
John McCallfe96e0b2011-11-06 09:01:30 +00001557
1558 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1559 Expr *opaqueRef = op->IgnoreParens();
1560 if (ObjCPropertyRefExpr *refExpr
1561 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001562 ObjCPropertyOpBuilder builder(*this, refExpr, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001563 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001564 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1565 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1566 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001567 } else if (MSPropertyRefExpr *refExpr
1568 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001569 MSPropertyOpBuilder builder(*this, refExpr, false);
John McCall5e77d762013-04-16 07:28:30 +00001570 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001571 } else if (MSPropertySubscriptExpr *RefExpr
1572 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001573 MSPropertyOpBuilder Builder(*this, RefExpr, false);
Alexey Bataevf7630272015-11-25 12:01:00 +00001574 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001575 } else {
1576 llvm_unreachable("unknown pseudo-object kind!");
1577 }
1578}
1579
1580ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1581 BinaryOperatorKind opcode,
1582 Expr *LHS, Expr *RHS) {
1583 // Do nothing if either argument is dependent.
1584 if (LHS->isTypeDependent() || RHS->isTypeDependent())
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001585 return BinaryOperator::Create(Context, LHS, RHS, opcode,
1586 Context.DependentTy, VK_RValue, OK_Ordinary,
Melanie Blower8812b0c2020-04-16 08:45:26 -07001587 opcLoc, CurFPFeatures);
John McCallfe96e0b2011-11-06 09:01:30 +00001588
1589 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001590 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1591 ExprResult result = CheckPlaceholderExpr(RHS);
1592 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001593 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001594 }
1595
Akira Hatanaka797afe32018-03-20 01:47:58 +00001596 bool IsSimpleAssign = opcode == BO_Assign;
John McCallfe96e0b2011-11-06 09:01:30 +00001597 Expr *opaqueRef = LHS->IgnoreParens();
1598 if (ObjCPropertyRefExpr *refExpr
1599 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001600 ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
John McCallfe96e0b2011-11-06 09:01:30 +00001601 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001602 } else if (ObjCSubscriptRefExpr *refExpr
1603 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001604 ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001605 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001606 } else if (MSPropertyRefExpr *refExpr
1607 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001608 MSPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001609 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1610 } else if (MSPropertySubscriptExpr *RefExpr
1611 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001612 MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001613 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001614 } else {
1615 llvm_unreachable("unknown pseudo-object kind!");
1616 }
1617}
John McCalle9290822011-11-30 04:42:31 +00001618
1619/// Given a pseudo-object reference, rebuild it without the opaque
1620/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1621/// This should never operate in-place.
1622static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001623 return Rebuilder(S,
1624 [=](Expr *E, unsigned) -> Expr * {
1625 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1626 })
1627 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001628}
1629
1630/// Given a pseudo-object expression, recreate what it looks like
1631/// syntactically without the attendant OpaqueValueExprs.
1632///
1633/// This is a hack which should be removed when TreeTransform is
1634/// capable of rebuilding a tree without stripping implicit
1635/// operations.
1636Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001637 Expr *syntax = E->getSyntacticForm();
1638 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1639 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
Melanie Blowerf5360d42020-05-01 10:32:06 -07001640 return UnaryOperator::Create(Context, op, uop->getOpcode(), uop->getType(),
1641 uop->getValueKind(), uop->getObjectKind(),
1642 uop->getOperatorLoc(), uop->canOverflow(),
1643 CurFPFeatures);
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001644 } else if (CompoundAssignOperator *cop
1645 = dyn_cast<CompoundAssignOperator>(syntax)) {
1646 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001647 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001648 return CompoundAssignOperator::Create(
1649 Context, lhs, rhs, cop->getOpcode(), cop->getType(),
1650 cop->getValueKind(), cop->getObjectKind(), cop->getOperatorLoc(),
Melanie Blower8812b0c2020-04-16 08:45:26 -07001651 CurFPFeatures, cop->getComputationLHSType(),
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001652 cop->getComputationResultType());
1653
John McCalle9290822011-11-30 04:42:31 +00001654 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1655 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1656 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001657 return BinaryOperator::Create(Context, lhs, rhs, bop->getOpcode(),
1658 bop->getType(), bop->getValueKind(),
1659 bop->getObjectKind(), bop->getOperatorLoc(),
Melanie Blower8812b0c2020-04-16 08:45:26 -07001660 CurFPFeatures);
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001661
Johannes Doerfertbefb4be2020-02-25 14:04:06 -08001662 } else if (isa<CallExpr>(syntax)) {
1663 return syntax;
John McCalle9290822011-11-30 04:42:31 +00001664 } else {
1665 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1666 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1667 }
1668}