blob: 4413b24fa9581105ab1419129f782926755b497d [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
Mark de Wever21490282019-11-12 20:46:19 +0100148 for (const GenericSelectionExpr::Association assoc :
Bruno Ricci1ec7fd32019-01-29 12:57:11 +0000149 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
Haojian Wu876bb862020-03-17 08:33:37 +0100170 return new (S.Context)
171 ChooseExpr(ce->getBuiltinLoc(), ce->getCond(), LHS, RHS,
172 rebuiltExpr->getType(), rebuiltExpr->getValueKind(),
173 rebuiltExpr->getObjectKind(), ce->getRParenLoc(),
174 ce->isConditionTrue());
Eli Friedman75807f22013-07-20 00:40:58 +0000175 }
176
John McCallfe96e0b2011-11-06 09:01:30 +0000177 llvm_unreachable("bad expression to rebuild!");
178 }
179 };
180
John McCallfe96e0b2011-11-06 09:01:30 +0000181 class PseudoOpBuilder {
182 public:
183 Sema &S;
184 unsigned ResultIndex;
185 SourceLocation GenericLoc;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000186 bool IsUnique;
John McCallfe96e0b2011-11-06 09:01:30 +0000187 SmallVector<Expr *, 4> Semantics;
188
Akira Hatanaka797afe32018-03-20 01:47:58 +0000189 PseudoOpBuilder(Sema &S, SourceLocation genericLoc, bool IsUnique)
John McCallfe96e0b2011-11-06 09:01:30 +0000190 : S(S), ResultIndex(PseudoObjectExpr::NoResult),
Akira Hatanaka797afe32018-03-20 01:47:58 +0000191 GenericLoc(genericLoc), IsUnique(IsUnique) {}
John McCallfe96e0b2011-11-06 09:01:30 +0000192
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000193 virtual ~PseudoOpBuilder() {}
Matt Beaumont-Gayfb3cb9a2011-11-08 01:53:17 +0000194
John McCallfe96e0b2011-11-06 09:01:30 +0000195 /// Add a normal semantic expression.
196 void addSemanticExpr(Expr *semantic) {
197 Semantics.push_back(semantic);
198 }
199
200 /// Add the 'result' semantic expression.
201 void addResultSemanticExpr(Expr *resultExpr) {
202 assert(ResultIndex == PseudoObjectExpr::NoResult);
203 ResultIndex = Semantics.size();
204 Semantics.push_back(resultExpr);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000205 // An OVE is not unique if it is used as the result expression.
206 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
207 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000208 }
209
210 ExprResult buildRValueOperation(Expr *op);
211 ExprResult buildAssignmentOperation(Scope *Sc,
212 SourceLocation opLoc,
213 BinaryOperatorKind opcode,
214 Expr *LHS, Expr *RHS);
215 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
216 UnaryOperatorKind opcode,
217 Expr *op);
218
Jordan Rosed3934582012-09-28 22:21:30 +0000219 virtual ExprResult complete(Expr *syntacticForm);
John McCallfe96e0b2011-11-06 09:01:30 +0000220
221 OpaqueValueExpr *capture(Expr *op);
222 OpaqueValueExpr *captureValueAsResult(Expr *op);
223
224 void setResultToLastSemantic() {
225 assert(ResultIndex == PseudoObjectExpr::NoResult);
226 ResultIndex = Semantics.size() - 1;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000227 // An OVE is not unique if it is used as the result expression.
228 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
229 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000230 }
231
232 /// Return true if assignments have a non-void result.
Alexey Bataev60520e22015-12-10 04:38:18 +0000233 static bool CanCaptureValue(Expr *exp) {
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000234 if (exp->isGLValue())
235 return true;
236 QualType ty = exp->getType();
Eli Friedman00fa4292012-11-13 23:16:33 +0000237 assert(!ty->isIncompleteType());
238 assert(!ty->isDependentType());
239
240 if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl())
241 return ClassDecl->isTriviallyCopyable();
242 return true;
243 }
John McCallfe96e0b2011-11-06 09:01:30 +0000244
245 virtual Expr *rebuildAndCaptureObject(Expr *) = 0;
246 virtual ExprResult buildGet() = 0;
247 virtual ExprResult buildSet(Expr *, SourceLocation,
248 bool captureSetValueAsResult) = 0;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000249 /// Should the result of an assignment be the formal result of the
Alexey Bataev60520e22015-12-10 04:38:18 +0000250 /// setter call or the value that was passed to the setter?
251 ///
252 /// Different pseudo-object language features use different language rules
253 /// for this.
254 /// The default is to use the set value. Currently, this affects the
255 /// behavior of simple assignments, compound assignments, and prefix
256 /// increment and decrement.
257 /// Postfix increment and decrement always use the getter result as the
258 /// expression result.
259 ///
260 /// If this method returns true, and the set value isn't capturable for
261 /// some reason, the result of the expression will be void.
262 virtual bool captureSetValueAsResult() const { return true; }
John McCallfe96e0b2011-11-06 09:01:30 +0000263 };
264
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +0000265 /// A PseudoOpBuilder for Objective-C \@properties.
John McCallfe96e0b2011-11-06 09:01:30 +0000266 class ObjCPropertyOpBuilder : public PseudoOpBuilder {
267 ObjCPropertyRefExpr *RefExpr;
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000268 ObjCPropertyRefExpr *SyntacticRefExpr;
John McCallfe96e0b2011-11-06 09:01:30 +0000269 OpaqueValueExpr *InstanceReceiver;
270 ObjCMethodDecl *Getter;
271
272 ObjCMethodDecl *Setter;
273 Selector SetterSelector;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000274 Selector GetterSelector;
John McCallfe96e0b2011-11-06 09:01:30 +0000275
276 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000277 ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr, bool IsUnique)
278 : PseudoOpBuilder(S, refExpr->getLocation(), IsUnique),
279 RefExpr(refExpr), SyntacticRefExpr(nullptr),
280 InstanceReceiver(nullptr), Getter(nullptr), Setter(nullptr) {
John McCallfe96e0b2011-11-06 09:01:30 +0000281 }
282
283 ExprResult buildRValueOperation(Expr *op);
284 ExprResult buildAssignmentOperation(Scope *Sc,
285 SourceLocation opLoc,
286 BinaryOperatorKind opcode,
287 Expr *LHS, Expr *RHS);
288 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
289 UnaryOperatorKind opcode,
290 Expr *op);
291
292 bool tryBuildGetOfReference(Expr *op, ExprResult &result);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000293 bool findSetter(bool warn=true);
John McCallfe96e0b2011-11-06 09:01:30 +0000294 bool findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000295 void DiagnoseUnsupportedPropertyUse();
John McCallfe96e0b2011-11-06 09:01:30 +0000296
Craig Toppere14c0f82014-03-12 04:55:44 +0000297 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
298 ExprResult buildGet() override;
299 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
300 ExprResult complete(Expr *SyntacticForm) override;
Jordan Rosed3934582012-09-28 22:21:30 +0000301
302 bool isWeakProperty() const;
John McCallfe96e0b2011-11-06 09:01:30 +0000303 };
Ted Kremeneke65b0862012-03-06 20:05:56 +0000304
305 /// A PseudoOpBuilder for Objective-C array/dictionary indexing.
306 class ObjCSubscriptOpBuilder : public PseudoOpBuilder {
307 ObjCSubscriptRefExpr *RefExpr;
308 OpaqueValueExpr *InstanceBase;
309 OpaqueValueExpr *InstanceKey;
310 ObjCMethodDecl *AtIndexGetter;
311 Selector AtIndexGetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000312
Ted Kremeneke65b0862012-03-06 20:05:56 +0000313 ObjCMethodDecl *AtIndexSetter;
314 Selector AtIndexSetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000315
Ted Kremeneke65b0862012-03-06 20:05:56 +0000316 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000317 ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr, bool IsUnique)
318 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
319 RefExpr(refExpr), InstanceBase(nullptr), InstanceKey(nullptr),
320 AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
Craig Topperc3ec1492014-05-26 06:22:03 +0000321
Ted Kremeneke65b0862012-03-06 20:05:56 +0000322 ExprResult buildRValueOperation(Expr *op);
323 ExprResult buildAssignmentOperation(Scope *Sc,
324 SourceLocation opLoc,
325 BinaryOperatorKind opcode,
326 Expr *LHS, Expr *RHS);
Craig Toppere14c0f82014-03-12 04:55:44 +0000327 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
328
Ted Kremeneke65b0862012-03-06 20:05:56 +0000329 bool findAtIndexGetter();
330 bool findAtIndexSetter();
Craig Toppere14c0f82014-03-12 04:55:44 +0000331
332 ExprResult buildGet() override;
333 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Ted Kremeneke65b0862012-03-06 20:05:56 +0000334 };
335
John McCall5e77d762013-04-16 07:28:30 +0000336 class MSPropertyOpBuilder : public PseudoOpBuilder {
337 MSPropertyRefExpr *RefExpr;
Alexey Bataev69103472015-10-14 04:05:42 +0000338 OpaqueValueExpr *InstanceBase;
Alexey Bataevf7630272015-11-25 12:01:00 +0000339 SmallVector<Expr *, 4> CallArgs;
340
341 MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
John McCall5e77d762013-04-16 07:28:30 +0000342
343 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000344 MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr, bool IsUnique)
345 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
346 RefExpr(refExpr), InstanceBase(nullptr) {}
347 MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr, bool IsUnique)
348 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
Alexey Bataevf7630272015-11-25 12:01:00 +0000349 InstanceBase(nullptr) {
350 RefExpr = getBaseMSProperty(refExpr);
351 }
John McCall5e77d762013-04-16 07:28:30 +0000352
Craig Toppere14c0f82014-03-12 04:55:44 +0000353 Expr *rebuildAndCaptureObject(Expr *) override;
354 ExprResult buildGet() override;
355 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Alexey Bataev60520e22015-12-10 04:38:18 +0000356 bool captureSetValueAsResult() const override { return false; }
John McCall5e77d762013-04-16 07:28:30 +0000357 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000358}
John McCallfe96e0b2011-11-06 09:01:30 +0000359
360/// Capture the given expression in an OpaqueValueExpr.
361OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
362 // Make a new OVE whose source is the given expression.
Fangrui Song6907ce22018-07-30 19:24:48 +0000363 OpaqueValueExpr *captured =
John McCallfe96e0b2011-11-06 09:01:30 +0000364 new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
Douglas Gregor2d5aea02012-02-23 22:17:26 +0000365 e->getValueKind(), e->getObjectKind(),
366 e);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000367 if (IsUnique)
368 captured->setIsUnique(true);
369
John McCallfe96e0b2011-11-06 09:01:30 +0000370 // Make sure we bind that in the semantics.
371 addSemanticExpr(captured);
372 return captured;
373}
374
375/// Capture the given expression as the result of this pseudo-object
376/// operation. This routine is safe against expressions which may
377/// already be captured.
378///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000379/// \returns the captured expression, which will be the
John McCallfe96e0b2011-11-06 09:01:30 +0000380/// same as the input if the input was already captured
381OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
382 assert(ResultIndex == PseudoObjectExpr::NoResult);
383
384 // If the expression hasn't already been captured, just capture it
Fangrui Song6907ce22018-07-30 19:24:48 +0000385 // and set the new semantic
John McCallfe96e0b2011-11-06 09:01:30 +0000386 if (!isa<OpaqueValueExpr>(e)) {
387 OpaqueValueExpr *cap = capture(e);
388 setResultToLastSemantic();
389 return cap;
390 }
391
392 // Otherwise, it must already be one of our semantic expressions;
393 // set ResultIndex to its index.
394 unsigned index = 0;
395 for (;; ++index) {
396 assert(index < Semantics.size() &&
397 "captured expression not found in semantics!");
398 if (e == Semantics[index]) break;
399 }
400 ResultIndex = index;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000401 // An OVE is not unique if it is used as the result expression.
402 cast<OpaqueValueExpr>(e)->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000403 return cast<OpaqueValueExpr>(e);
404}
405
406/// The routine which creates the final PseudoObjectExpr.
407ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
408 return PseudoObjectExpr::Create(S.Context, syntactic,
409 Semantics, ResultIndex);
410}
411
412/// The main skeleton for building an r-value operation.
413ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
414 Expr *syntacticBase = rebuildAndCaptureObject(op);
415
416 ExprResult getExpr = buildGet();
417 if (getExpr.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000418 addResultSemanticExpr(getExpr.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000419
420 return complete(syntacticBase);
421}
422
423/// The basic skeleton for building a simple or compound
424/// assignment operation.
425ExprResult
426PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
427 BinaryOperatorKind opcode,
428 Expr *LHS, Expr *RHS) {
429 assert(BinaryOperator::isAssignmentOp(opcode));
430
431 Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
432 OpaqueValueExpr *capturedRHS = capture(RHS);
433
John McCallee04aeb2015-08-22 00:35:27 +0000434 // In some very specific cases, semantic analysis of the RHS as an
435 // expression may require it to be rewritten. In these cases, we
436 // cannot safely keep the OVE around. Fortunately, we don't really
437 // need to: we don't use this particular OVE in multiple places, and
438 // no clients rely that closely on matching up expressions in the
439 // semantic expression with expressions from the syntactic form.
440 Expr *semanticRHS = capturedRHS;
441 if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
442 semanticRHS = RHS;
443 Semantics.pop_back();
444 }
445
John McCallfe96e0b2011-11-06 09:01:30 +0000446 Expr *syntactic;
447
448 ExprResult result;
449 if (opcode == BO_Assign) {
John McCallee04aeb2015-08-22 00:35:27 +0000450 result = semanticRHS;
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700451 syntactic = BinaryOperator::Create(
452 S.Context, syntacticLHS, capturedRHS, opcode, capturedRHS->getType(),
Melanie Blower8812b0c2020-04-16 08:45:26 -0700453 capturedRHS->getValueKind(), OK_Ordinary, opcLoc, S.CurFPFeatures);
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700454
John McCallfe96e0b2011-11-06 09:01:30 +0000455 } else {
456 ExprResult opLHS = buildGet();
457 if (opLHS.isInvalid()) return ExprError();
458
459 // Build an ordinary, non-compound operation.
460 BinaryOperatorKind nonCompound =
461 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000462 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000463 if (result.isInvalid()) return ExprError();
464
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700465 syntactic = CompoundAssignOperator::Create(
466 S.Context, syntacticLHS, capturedRHS, opcode, result.get()->getType(),
Melanie Blower8812b0c2020-04-16 08:45:26 -0700467 result.get()->getValueKind(), OK_Ordinary, opcLoc, S.CurFPFeatures,
Melanie Blower2ba4e3a2020-04-10 13:34:46 -0700468 opLHS.get()->getType(), result.get()->getType());
John McCallfe96e0b2011-11-06 09:01:30 +0000469 }
470
471 // The result of the assignment, if not void, is the value set into
472 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000473 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000474 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000475 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000476 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
477 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
478 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000479
480 return complete(syntactic);
481}
482
483/// The basic skeleton for building an increment or decrement
484/// operation.
485ExprResult
486PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
487 UnaryOperatorKind opcode,
488 Expr *op) {
489 assert(UnaryOperator::isIncrementDecrementOp(opcode));
490
491 Expr *syntacticOp = rebuildAndCaptureObject(op);
492
493 // Load the value.
494 ExprResult result = buildGet();
495 if (result.isInvalid()) return ExprError();
496
497 QualType resultType = result.get()->getType();
498
499 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000500 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000501 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000502 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000503 setResultToLastSemantic();
504 }
505
506 // Add or subtract a literal 1.
507 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
508 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
509 GenericLoc);
510
511 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000512 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000513 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000514 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000515 }
516 if (result.isInvalid()) return ExprError();
517
518 // Store that back into the result. The value stored is the result
519 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000520 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
521 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000522 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000523 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000524 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
525 !result.get()->getType()->isVoidType() &&
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000526 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
527 setResultToLastSemantic();
528
529 UnaryOperator *syntactic = new (S.Context) UnaryOperator(
530 syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
531 !resultType->isDependentType()
532 ? S.Context.getTypeSize(resultType) >=
533 S.Context.getTypeSize(S.Context.IntTy)
534 : false);
535 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();
583 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_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())
1554 return new (Context) UnaryOperator(op, opcode, Context.DependentTy,
Aaron Ballmana5038552018-01-09 13:07:03 +00001555 VK_RValue, OK_Ordinary, opcLoc, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001556
1557 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1558 Expr *opaqueRef = op->IgnoreParens();
1559 if (ObjCPropertyRefExpr *refExpr
1560 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001561 ObjCPropertyOpBuilder builder(*this, refExpr, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001562 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001563 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1564 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1565 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001566 } else if (MSPropertyRefExpr *refExpr
1567 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001568 MSPropertyOpBuilder builder(*this, refExpr, false);
John McCall5e77d762013-04-16 07:28:30 +00001569 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001570 } else if (MSPropertySubscriptExpr *RefExpr
1571 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001572 MSPropertyOpBuilder Builder(*this, RefExpr, false);
Alexey Bataevf7630272015-11-25 12:01:00 +00001573 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001574 } else {
1575 llvm_unreachable("unknown pseudo-object kind!");
1576 }
1577}
1578
1579ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1580 BinaryOperatorKind opcode,
1581 Expr *LHS, Expr *RHS) {
1582 // Do nothing if either argument is dependent.
1583 if (LHS->isTypeDependent() || RHS->isTypeDependent())
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001584 return BinaryOperator::Create(Context, LHS, RHS, opcode,
1585 Context.DependentTy, VK_RValue, OK_Ordinary,
Melanie Blower8812b0c2020-04-16 08:45:26 -07001586 opcLoc, CurFPFeatures);
John McCallfe96e0b2011-11-06 09:01:30 +00001587
1588 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001589 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1590 ExprResult result = CheckPlaceholderExpr(RHS);
1591 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001592 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001593 }
1594
Akira Hatanaka797afe32018-03-20 01:47:58 +00001595 bool IsSimpleAssign = opcode == BO_Assign;
John McCallfe96e0b2011-11-06 09:01:30 +00001596 Expr *opaqueRef = LHS->IgnoreParens();
1597 if (ObjCPropertyRefExpr *refExpr
1598 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001599 ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
John McCallfe96e0b2011-11-06 09:01:30 +00001600 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001601 } else if (ObjCSubscriptRefExpr *refExpr
1602 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001603 ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001604 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001605 } else if (MSPropertyRefExpr *refExpr
1606 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001607 MSPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001608 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1609 } else if (MSPropertySubscriptExpr *RefExpr
1610 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001611 MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001612 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001613 } else {
1614 llvm_unreachable("unknown pseudo-object kind!");
1615 }
1616}
John McCalle9290822011-11-30 04:42:31 +00001617
1618/// Given a pseudo-object reference, rebuild it without the opaque
1619/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1620/// This should never operate in-place.
1621static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001622 return Rebuilder(S,
1623 [=](Expr *E, unsigned) -> Expr * {
1624 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1625 })
1626 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001627}
1628
1629/// Given a pseudo-object expression, recreate what it looks like
1630/// syntactically without the attendant OpaqueValueExprs.
1631///
1632/// This is a hack which should be removed when TreeTransform is
1633/// capable of rebuilding a tree without stripping implicit
1634/// operations.
1635Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001636 Expr *syntax = E->getSyntacticForm();
1637 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1638 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1639 return new (Context) UnaryOperator(
1640 op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1641 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1642 } else if (CompoundAssignOperator *cop
1643 = dyn_cast<CompoundAssignOperator>(syntax)) {
1644 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001645 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001646 return CompoundAssignOperator::Create(
1647 Context, lhs, rhs, cop->getOpcode(), cop->getType(),
1648 cop->getValueKind(), cop->getObjectKind(), cop->getOperatorLoc(),
Melanie Blower8812b0c2020-04-16 08:45:26 -07001649 CurFPFeatures, cop->getComputationLHSType(),
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001650 cop->getComputationResultType());
1651
John McCalle9290822011-11-30 04:42:31 +00001652 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1653 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1654 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001655 return BinaryOperator::Create(Context, lhs, rhs, bop->getOpcode(),
1656 bop->getType(), bop->getValueKind(),
1657 bop->getObjectKind(), bop->getOperatorLoc(),
Melanie Blower8812b0c2020-04-16 08:45:26 -07001658 CurFPFeatures);
Melanie Blower2ba4e3a2020-04-10 13:34:46 -07001659
Johannes Doerfertbefb4be2020-02-25 14:04:06 -08001660 } else if (isa<CallExpr>(syntax)) {
1661 return syntax;
John McCalle9290822011-11-30 04:42:31 +00001662 } else {
1663 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1664 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1665 }
1666}