blob: 6a541f9693ec2f58381bf3e37a9c0910abc445ca [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;
John McCallfe96e0b2011-11-06 09:01:30 +0000451 syntactic = new (S.Context) BinaryOperator(syntacticLHS, capturedRHS,
452 opcode, capturedRHS->getType(),
453 capturedRHS->getValueKind(),
Adam Nemet484aa452017-03-27 19:17:25 +0000454 OK_Ordinary, opcLoc,
455 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000456 } else {
457 ExprResult opLHS = buildGet();
458 if (opLHS.isInvalid()) return ExprError();
459
460 // Build an ordinary, non-compound operation.
461 BinaryOperatorKind nonCompound =
462 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000463 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000464 if (result.isInvalid()) return ExprError();
465
466 syntactic =
467 new (S.Context) CompoundAssignOperator(syntacticLHS, capturedRHS, opcode,
468 result.get()->getType(),
469 result.get()->getValueKind(),
470 OK_Ordinary,
471 opLHS.get()->getType(),
472 result.get()->getType(),
Adam Nemet484aa452017-03-27 19:17:25 +0000473 opcLoc, FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000474 }
475
476 // The result of the assignment, if not void, is the value set into
477 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000478 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000479 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000480 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000481 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
482 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
483 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000484
485 return complete(syntactic);
486}
487
488/// The basic skeleton for building an increment or decrement
489/// operation.
490ExprResult
491PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
492 UnaryOperatorKind opcode,
493 Expr *op) {
494 assert(UnaryOperator::isIncrementDecrementOp(opcode));
495
496 Expr *syntacticOp = rebuildAndCaptureObject(op);
497
498 // Load the value.
499 ExprResult result = buildGet();
500 if (result.isInvalid()) return ExprError();
501
502 QualType resultType = result.get()->getType();
503
504 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000505 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000506 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000507 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000508 setResultToLastSemantic();
509 }
510
511 // Add or subtract a literal 1.
512 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
513 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
514 GenericLoc);
515
516 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000517 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000518 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000519 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000520 }
521 if (result.isInvalid()) return ExprError();
522
523 // Store that back into the result. The value stored is the result
524 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000525 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
526 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000527 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000528 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000529 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
530 !result.get()->getType()->isVoidType() &&
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000531 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
532 setResultToLastSemantic();
533
534 UnaryOperator *syntactic = new (S.Context) UnaryOperator(
535 syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
536 !resultType->isDependentType()
537 ? S.Context.getTypeSize(resultType) >=
538 S.Context.getTypeSize(S.Context.IntTy)
539 : false);
540 return complete(syntactic);
541}
542
John McCallfe96e0b2011-11-06 09:01:30 +0000543
544//===----------------------------------------------------------------------===//
545// Objective-C @property and implicit property references
546//===----------------------------------------------------------------------===//
547
548/// Look up a method in the receiver type of an Objective-C property
549/// reference.
John McCall526ab472011-10-25 17:37:35 +0000550static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
551 const ObjCPropertyRefExpr *PRE) {
John McCall526ab472011-10-25 17:37:35 +0000552 if (PRE->isObjectReceiver()) {
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000553 const ObjCObjectPointerType *PT =
554 PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
John McCallfe96e0b2011-11-06 09:01:30 +0000555
556 // Special case for 'self' in class method implementations.
557 if (PT->isObjCClassType() &&
558 S.isSelfExpr(const_cast<Expr*>(PRE->getBase()))) {
559 // This cast is safe because isSelfExpr is only true within
560 // methods.
561 ObjCMethodDecl *method =
562 cast<ObjCMethodDecl>(S.CurContext->getNonClosureAncestor());
563 return S.LookupMethodInObjectType(sel,
564 S.Context.getObjCInterfaceType(method->getClassInterface()),
565 /*instance*/ false);
566 }
567
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000568 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
John McCall526ab472011-10-25 17:37:35 +0000569 }
570
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000571 if (PRE->isSuperReceiver()) {
572 if (const ObjCObjectPointerType *PT =
573 PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
574 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
575
576 return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
577 }
578
579 assert(PRE->isClassReceiver() && "Invalid expression");
580 QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
581 return S.LookupMethodInObjectType(sel, IT, false);
John McCall526ab472011-10-25 17:37:35 +0000582}
583
Jordan Rosed3934582012-09-28 22:21:30 +0000584bool ObjCPropertyOpBuilder::isWeakProperty() const {
585 QualType T;
586 if (RefExpr->isExplicitProperty()) {
587 const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty();
588 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Bob Wilsonf4f54e32016-05-25 05:41:57 +0000589 return true;
Jordan Rosed3934582012-09-28 22:21:30 +0000590
591 T = Prop->getType();
592 } else if (Getter) {
Alp Toker314cc812014-01-25 16:55:45 +0000593 T = Getter->getReturnType();
Jordan Rosed3934582012-09-28 22:21:30 +0000594 } else {
595 return false;
596 }
597
598 return T.getObjCLifetime() == Qualifiers::OCL_Weak;
599}
600
John McCallfe96e0b2011-11-06 09:01:30 +0000601bool ObjCPropertyOpBuilder::findGetter() {
602 if (Getter) return true;
John McCall526ab472011-10-25 17:37:35 +0000603
John McCallcfef5462011-11-07 22:49:50 +0000604 // For implicit properties, just trust the lookup we already did.
605 if (RefExpr->isImplicitProperty()) {
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000606 if ((Getter = RefExpr->getImplicitPropertyGetter())) {
607 GetterSelector = Getter->getSelector();
608 return true;
609 }
610 else {
611 // Must build the getter selector the hard way.
612 ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter();
613 assert(setter && "both setter and getter are null - cannot happen");
Fangrui Song6907ce22018-07-30 19:24:48 +0000614 IdentifierInfo *setterName =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000615 setter->getSelector().getIdentifierInfoForSlot(0);
Alp Toker541d5072014-06-07 23:30:53 +0000616 IdentifierInfo *getterName =
617 &S.Context.Idents.get(setterName->getName().substr(3));
Fangrui Song6907ce22018-07-30 19:24:48 +0000618 GetterSelector =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000619 S.PP.getSelectorTable().getNullarySelector(getterName);
620 return false;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000621 }
John McCallcfef5462011-11-07 22:49:50 +0000622 }
623
624 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
625 Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
Craig Topperc3ec1492014-05-26 06:22:03 +0000626 return (Getter != nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000627}
628
629/// Try to find the most accurate setter declaration for the property
630/// reference.
631///
Fangrui Song6907ce22018-07-30 19:24:48 +0000632/// \return true if a setter was found, in which case Setter
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000633bool ObjCPropertyOpBuilder::findSetter(bool warn) {
John McCallfe96e0b2011-11-06 09:01:30 +0000634 // For implicit properties, just trust the lookup we already did.
635 if (RefExpr->isImplicitProperty()) {
636 if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
637 Setter = setter;
638 SetterSelector = setter->getSelector();
639 return true;
John McCall526ab472011-10-25 17:37:35 +0000640 } else {
John McCallfe96e0b2011-11-06 09:01:30 +0000641 IdentifierInfo *getterName =
642 RefExpr->getImplicitPropertyGetter()->getSelector()
643 .getIdentifierInfoForSlot(0);
644 SetterSelector =
Adrian Prantla4ce9062013-06-07 22:29:12 +0000645 SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
646 S.PP.getSelectorTable(),
647 getterName);
John McCallfe96e0b2011-11-06 09:01:30 +0000648 return false;
John McCall526ab472011-10-25 17:37:35 +0000649 }
John McCallfe96e0b2011-11-06 09:01:30 +0000650 }
651
652 // For explicit properties, this is more involved.
653 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
654 SetterSelector = prop->getSetterName();
655
656 // Do a normal method lookup first.
657 if (ObjCMethodDecl *setter =
658 LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
Jordan Rosed01e83a2012-10-10 16:42:25 +0000659 if (setter->isPropertyAccessor() && warn)
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000660 if (const ObjCInterfaceDecl *IFace =
661 dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) {
Craig Topperbf3e3272014-08-30 16:55:52 +0000662 StringRef thisPropertyName = prop->getName();
Jordan Rosea7d03842013-02-08 22:30:41 +0000663 // Try flipping the case of the first character.
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000664 char front = thisPropertyName.front();
Jordan Rosea7d03842013-02-08 22:30:41 +0000665 front = isLowercase(front) ? toUppercase(front) : toLowercase(front);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000666 SmallString<100> PropertyName = thisPropertyName;
667 PropertyName[0] = front;
668 IdentifierInfo *AltMember = &S.PP.getIdentifierTable().get(PropertyName);
Manman Ren5b786402016-01-28 18:49:28 +0000669 if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration(
670 AltMember, prop->getQueryKind()))
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000671 if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) {
Richard Smithf8812672016-12-02 22:38:31 +0000672 S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000673 << prop << prop1 << setter->getSelector();
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000674 S.Diag(prop->getLocation(), diag::note_property_declare);
675 S.Diag(prop1->getLocation(), diag::note_property_declare);
676 }
677 }
John McCallfe96e0b2011-11-06 09:01:30 +0000678 Setter = setter;
679 return true;
680 }
681
682 // That can fail in the somewhat crazy situation that we're
683 // type-checking a message send within the @interface declaration
684 // that declared the @property. But it's not clear that that's
685 // valuable to support.
686
687 return false;
688}
689
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000690void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() {
Fariborz Jahanian55513282014-05-28 18:12:10 +0000691 if (S.getCurLexicalContext()->isObjCContainer() &&
692 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl &&
693 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) {
694 if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) {
695 S.Diag(RefExpr->getLocation(),
696 diag::err_property_function_in_objc_container);
697 S.Diag(prop->getLocation(), diag::note_property_declare);
Fariborz Jahanian55513282014-05-28 18:12:10 +0000698 }
699 }
Fariborz Jahanian55513282014-05-28 18:12:10 +0000700}
701
John McCallfe96e0b2011-11-06 09:01:30 +0000702/// Capture the base object of an Objective-C property expression.
703Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000704 assert(InstanceReceiver == nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000705
706 // If we have a base, capture it in an OVE and rebuild the syntactic
707 // form to use the OVE as its base.
708 if (RefExpr->isObjectReceiver()) {
709 InstanceReceiver = capture(RefExpr->getBase());
Alexey Bataevf7630272015-11-25 12:01:00 +0000710 syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * {
711 return InstanceReceiver;
712 }).rebuild(syntacticBase);
John McCallfe96e0b2011-11-06 09:01:30 +0000713 }
714
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000715 if (ObjCPropertyRefExpr *
716 refE = dyn_cast<ObjCPropertyRefExpr>(syntacticBase->IgnoreParens()))
717 SyntacticRefExpr = refE;
718
John McCallfe96e0b2011-11-06 09:01:30 +0000719 return syntacticBase;
720}
721
722/// Load from an Objective-C property reference.
723ExprResult ObjCPropertyOpBuilder::buildGet() {
724 findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000725 if (!Getter) {
726 DiagnoseUnsupportedPropertyUse();
727 return ExprError();
728 }
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000729
730 if (SyntacticRefExpr)
731 SyntacticRefExpr->setIsMessagingGetter();
732
Douglas Gregore83b9562015-07-07 03:57:53 +0000733 QualType receiverType = RefExpr->getReceiverType(S.Context);
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000734 if (!Getter->isImplicit())
735 S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true);
John McCallfe96e0b2011-11-06 09:01:30 +0000736 // Build a message-send.
737 ExprResult msg;
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000738 if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
739 RefExpr->isObjectReceiver()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000740 assert(InstanceReceiver || RefExpr->isSuperReceiver());
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000741 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
742 GenericLoc, Getter->getSelector(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000743 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000744 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000745 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000746 GenericLoc, Getter->getSelector(),
747 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000748 }
749 return msg;
750}
John McCall526ab472011-10-25 17:37:35 +0000751
John McCallfe96e0b2011-11-06 09:01:30 +0000752/// Store to an Objective-C property reference.
753///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000754/// \param captureSetValueAsResult If true, capture the actual
John McCallfe96e0b2011-11-06 09:01:30 +0000755/// value being set as the value of the property operation.
756ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
757 bool captureSetValueAsResult) {
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000758 if (!findSetter(false)) {
759 DiagnoseUnsupportedPropertyUse();
760 return ExprError();
761 }
John McCallfe96e0b2011-11-06 09:01:30 +0000762
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000763 if (SyntacticRefExpr)
764 SyntacticRefExpr->setIsMessagingSetter();
765
Douglas Gregore83b9562015-07-07 03:57:53 +0000766 QualType receiverType = RefExpr->getReceiverType(S.Context);
John McCallfe96e0b2011-11-06 09:01:30 +0000767
768 // Use assignment constraints when possible; they give us better
769 // diagnostics. "When possible" basically means anything except a
770 // C++ class type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000771 if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000772 QualType paramType = (*Setter->param_begin())->getType()
773 .substObjCMemberType(
774 receiverType,
775 Setter->getDeclContext(),
776 ObjCSubstitutionContext::Parameter);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000777 if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000778 ExprResult opResult = op;
779 Sema::AssignConvertType assignResult
780 = S.CheckSingleAssignmentConstraints(paramType, opResult);
Richard Smithe15a3702016-10-06 23:12:58 +0000781 if (opResult.isInvalid() ||
782 S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType,
John McCallfe96e0b2011-11-06 09:01:30 +0000783 op->getType(), opResult.get(),
784 Sema::AA_Assigning))
785 return ExprError();
786
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000787 op = opResult.get();
John McCallfe96e0b2011-11-06 09:01:30 +0000788 assert(op && "successful assignment left argument invalid?");
John McCall526ab472011-10-25 17:37:35 +0000789 }
790 }
791
John McCallfe96e0b2011-11-06 09:01:30 +0000792 // Arguments.
793 Expr *args[] = { op };
John McCall526ab472011-10-25 17:37:35 +0000794
John McCallfe96e0b2011-11-06 09:01:30 +0000795 // Build a message-send.
796 ExprResult msg;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000797 if (!Setter->isImplicit())
798 S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true);
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000799 if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
800 RefExpr->isObjectReceiver()) {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000801 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
802 GenericLoc, SetterSelector, Setter,
803 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000804 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000805 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
806 GenericLoc,
807 SetterSelector, Setter,
808 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000809 }
810
811 if (!msg.isInvalid() && captureSetValueAsResult) {
812 ObjCMessageExpr *msgExpr =
813 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
814 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000815 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +0000816 msgExpr->setArg(0, captureValueAsResult(arg));
John McCallfe96e0b2011-11-06 09:01:30 +0000817 }
818
819 return msg;
John McCall526ab472011-10-25 17:37:35 +0000820}
821
John McCallfe96e0b2011-11-06 09:01:30 +0000822/// @property-specific behavior for doing lvalue-to-rvalue conversion.
823ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) {
824 // Explicit properties always have getters, but implicit ones don't.
825 // Check that before proceeding.
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000826 if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000827 S.Diag(RefExpr->getLocation(), diag::err_getter_not_found)
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000828 << RefExpr->getSourceRange();
John McCall526ab472011-10-25 17:37:35 +0000829 return ExprError();
830 }
831
John McCallfe96e0b2011-11-06 09:01:30 +0000832 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
John McCall526ab472011-10-25 17:37:35 +0000833 if (result.isInvalid()) return ExprError();
834
John McCallfe96e0b2011-11-06 09:01:30 +0000835 if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType())
836 S.DiagnosePropertyAccessorMismatch(RefExpr->getExplicitProperty(),
837 Getter, RefExpr->getLocation());
838
839 // As a special case, if the method returns 'id', try to get
840 // a better type from the property.
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000841 if (RefExpr->isExplicitProperty() && result.get()->isRValue()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000842 QualType receiverType = RefExpr->getReceiverType(S.Context);
843 QualType propType = RefExpr->getExplicitProperty()
844 ->getUsageType(receiverType);
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000845 if (result.get()->getType()->isObjCIdType()) {
846 if (const ObjCObjectPointerType *ptr
847 = propType->getAs<ObjCObjectPointerType>()) {
848 if (!ptr->isObjCIdType())
849 result = S.ImpCastExprToType(result.get(), propType, CK_BitCast);
850 }
851 }
Brian Kelleycafd9122017-03-29 17:55:11 +0000852 if (propType.getObjCLifetime() == Qualifiers::OCL_Weak &&
853 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
854 RefExpr->getLocation()))
855 S.getCurFunction()->markSafeWeakUse(RefExpr);
John McCallfe96e0b2011-11-06 09:01:30 +0000856 }
857
John McCall526ab472011-10-25 17:37:35 +0000858 return result;
859}
860
John McCallfe96e0b2011-11-06 09:01:30 +0000861/// Try to build this as a call to a getter that returns a reference.
862///
863/// \return true if it was possible, whether or not it actually
864/// succeeded
865bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op,
866 ExprResult &result) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000867 if (!S.getLangOpts().CPlusPlus) return false;
John McCallfe96e0b2011-11-06 09:01:30 +0000868
869 findGetter();
Olivier Goffart4c182c82014-08-04 17:28:05 +0000870 if (!Getter) {
871 // The property has no setter and no getter! This can happen if the type is
872 // invalid. Error have already been reported.
873 result = ExprError();
874 return true;
875 }
John McCallfe96e0b2011-11-06 09:01:30 +0000876
877 // Only do this if the getter returns an l-value reference type.
Alp Toker314cc812014-01-25 16:55:45 +0000878 QualType resultType = Getter->getReturnType();
John McCallfe96e0b2011-11-06 09:01:30 +0000879 if (!resultType->isLValueReferenceType()) return false;
880
881 result = buildRValueOperation(op);
882 return true;
883}
884
885/// @property-specific behavior for doing assignments.
886ExprResult
887ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc,
888 SourceLocation opcLoc,
889 BinaryOperatorKind opcode,
890 Expr *LHS, Expr *RHS) {
John McCall526ab472011-10-25 17:37:35 +0000891 assert(BinaryOperator::isAssignmentOp(opcode));
John McCall526ab472011-10-25 17:37:35 +0000892
893 // If there's no setter, we have no choice but to try to assign to
894 // the result of the getter.
John McCallfe96e0b2011-11-06 09:01:30 +0000895 if (!findSetter()) {
896 ExprResult result;
897 if (tryBuildGetOfReference(LHS, result)) {
898 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000899 return S.BuildBinOp(Sc, opcLoc, opcode, result.get(), RHS);
John McCall526ab472011-10-25 17:37:35 +0000900 }
901
902 // Otherwise, it's an error.
John McCallfe96e0b2011-11-06 09:01:30 +0000903 S.Diag(opcLoc, diag::err_nosetter_property_assignment)
904 << unsigned(RefExpr->isImplicitProperty())
905 << SetterSelector
John McCall526ab472011-10-25 17:37:35 +0000906 << LHS->getSourceRange() << RHS->getSourceRange();
907 return ExprError();
908 }
909
910 // If there is a setter, we definitely want to use it.
911
John McCallfe96e0b2011-11-06 09:01:30 +0000912 // Verify that we can do a compound assignment.
913 if (opcode != BO_Assign && !findGetter()) {
914 S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment)
John McCall526ab472011-10-25 17:37:35 +0000915 << LHS->getSourceRange() << RHS->getSourceRange();
916 return ExprError();
917 }
918
John McCallfe96e0b2011-11-06 09:01:30 +0000919 ExprResult result =
920 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
John McCall526ab472011-10-25 17:37:35 +0000921 if (result.isInvalid()) return ExprError();
922
John McCallfe96e0b2011-11-06 09:01:30 +0000923 // Various warnings about property assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000924 if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) {
John McCallfe96e0b2011-11-06 09:01:30 +0000925 S.checkRetainCycles(InstanceReceiver->getSourceExpr(), RHS);
926 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
927 }
928
John McCall526ab472011-10-25 17:37:35 +0000929 return result;
930}
John McCallfe96e0b2011-11-06 09:01:30 +0000931
932/// @property-specific behavior for doing increments and decrements.
933ExprResult
934ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
935 UnaryOperatorKind opcode,
936 Expr *op) {
937 // If there's no setter, we have no choice but to try to assign to
938 // the result of the getter.
939 if (!findSetter()) {
940 ExprResult result;
941 if (tryBuildGetOfReference(op, result)) {
942 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000943 return S.BuildUnaryOp(Sc, opcLoc, opcode, result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000944 }
945
946 // Otherwise, it's an error.
947 S.Diag(opcLoc, diag::err_nosetter_property_incdec)
948 << unsigned(RefExpr->isImplicitProperty())
949 << unsigned(UnaryOperator::isDecrementOp(opcode))
950 << SetterSelector
951 << op->getSourceRange();
952 return ExprError();
953 }
954
955 // If there is a setter, we definitely want to use it.
956
957 // We also need a getter.
958 if (!findGetter()) {
959 assert(RefExpr->isImplicitProperty());
960 S.Diag(opcLoc, diag::err_nogetter_property_incdec)
961 << unsigned(UnaryOperator::isDecrementOp(opcode))
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000962 << GetterSelector
John McCallfe96e0b2011-11-06 09:01:30 +0000963 << op->getSourceRange();
964 return ExprError();
965 }
966
967 return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op);
968}
969
Jordan Rosed3934582012-09-28 22:21:30 +0000970ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) {
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000971 if (isWeakProperty() && !S.isUnevaluatedContext() &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000972 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000973 SyntacticForm->getBeginLoc()))
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000974 S.getCurFunction()->recordUseOfWeak(SyntacticRefExpr,
975 SyntacticRefExpr->isMessagingGetter());
Jordan Rosed3934582012-09-28 22:21:30 +0000976
977 return PseudoOpBuilder::complete(SyntacticForm);
978}
979
Ted Kremeneke65b0862012-03-06 20:05:56 +0000980// ObjCSubscript build stuff.
981//
982
Fangrui Song6907ce22018-07-30 19:24:48 +0000983/// objective-c subscripting-specific behavior for doing lvalue-to-rvalue
Ted Kremeneke65b0862012-03-06 20:05:56 +0000984/// conversion.
Fangrui Song6907ce22018-07-30 19:24:48 +0000985/// FIXME. Remove this routine if it is proven that no additional
Ted Kremeneke65b0862012-03-06 20:05:56 +0000986/// specifity is needed.
987ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) {
988 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
989 if (result.isInvalid()) return ExprError();
990 return result;
991}
992
993/// objective-c subscripting-specific behavior for doing assignments.
994ExprResult
995ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc,
996 SourceLocation opcLoc,
997 BinaryOperatorKind opcode,
998 Expr *LHS, Expr *RHS) {
999 assert(BinaryOperator::isAssignmentOp(opcode));
1000 // There must be a method to do the Index'ed assignment.
1001 if (!findAtIndexSetter())
1002 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001003
Ted Kremeneke65b0862012-03-06 20:05:56 +00001004 // Verify that we can do a compound assignment.
1005 if (opcode != BO_Assign && !findAtIndexGetter())
1006 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001007
Ted Kremeneke65b0862012-03-06 20:05:56 +00001008 ExprResult result =
1009 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
1010 if (result.isInvalid()) return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001011
Ted Kremeneke65b0862012-03-06 20:05:56 +00001012 // Various warnings about objc Index'ed assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001013 if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001014 S.checkRetainCycles(InstanceBase->getSourceExpr(), RHS);
1015 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
1016 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001017
Ted Kremeneke65b0862012-03-06 20:05:56 +00001018 return result;
1019}
1020
1021/// Capture the base object of an Objective-C Index'ed expression.
1022Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001023 assert(InstanceBase == nullptr);
1024
Ted Kremeneke65b0862012-03-06 20:05:56 +00001025 // Capture base expression in an OVE and rebuild the syntactic
1026 // form to use the OVE as its base expression.
1027 InstanceBase = capture(RefExpr->getBaseExpr());
1028 InstanceKey = capture(RefExpr->getKeyExpr());
Alexey Bataevf7630272015-11-25 12:01:00 +00001029
Ted Kremeneke65b0862012-03-06 20:05:56 +00001030 syntacticBase =
Alexey Bataevf7630272015-11-25 12:01:00 +00001031 Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1032 switch (Idx) {
1033 case 0:
1034 return InstanceBase;
1035 case 1:
1036 return InstanceKey;
1037 default:
1038 llvm_unreachable("Unexpected index for ObjCSubscriptExpr");
1039 }
1040 }).rebuild(syntacticBase);
1041
Ted Kremeneke65b0862012-03-06 20:05:56 +00001042 return syntacticBase;
1043}
1044
Fangrui Song6907ce22018-07-30 19:24:48 +00001045/// CheckSubscriptingKind - This routine decide what type
Ted Kremeneke65b0862012-03-06 20:05:56 +00001046/// of indexing represented by "FromE" is being done.
Fangrui Song6907ce22018-07-30 19:24:48 +00001047Sema::ObjCSubscriptKind
Ted Kremeneke65b0862012-03-06 20:05:56 +00001048 Sema::CheckSubscriptingKind(Expr *FromE) {
1049 // If the expression already has integral or enumeration type, we're golden.
1050 QualType T = FromE->getType();
1051 if (T->isIntegralOrEnumerationType())
1052 return OS_Array;
Fangrui Song6907ce22018-07-30 19:24:48 +00001053
Ted Kremeneke65b0862012-03-06 20:05:56 +00001054 // If we don't have a class type in C++, there's no way we can get an
1055 // expression of integral or enumeration type.
1056 const RecordType *RecordTy = T->getAs<RecordType>();
Fariborz Jahaniand13951f2014-09-10 20:55:31 +00001057 if (!RecordTy &&
1058 (T->isObjCObjectPointerType() || T->isVoidPointerType()))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001059 // All other scalar cases are assumed to be dictionary indexing which
1060 // caller handles, with diagnostics if needed.
1061 return OS_Dictionary;
Fangrui Song6907ce22018-07-30 19:24:48 +00001062 if (!getLangOpts().CPlusPlus ||
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001063 !RecordTy || RecordTy->isIncompleteType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001064 // No indexing can be done. Issue diagnostics and quit.
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001065 const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
1066 if (isa<StringLiteral>(IndexExpr))
1067 Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
1068 << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
1069 else
1070 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1071 << T;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001072 return OS_Error;
1073 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Ted Kremeneke65b0862012-03-06 20:05:56 +00001075 // We must have a complete class type.
Fangrui Song6907ce22018-07-30 19:24:48 +00001076 if (RequireCompleteType(FromE->getExprLoc(), T,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001077 diag::err_objc_index_incomplete_class_type, FromE))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001078 return OS_Error;
Fangrui Song6907ce22018-07-30 19:24:48 +00001079
Ted Kremeneke65b0862012-03-06 20:05:56 +00001080 // Look for a conversion to an integral, enumeration type, or
1081 // objective-C pointer type.
Ted Kremeneke65b0862012-03-06 20:05:56 +00001082 int NoIntegrals=0, NoObjCIdPointers=0;
1083 SmallVector<CXXConversionDecl *, 4> ConversionDecls;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001084
1085 for (NamedDecl *D : cast<CXXRecordDecl>(RecordTy->getDecl())
1086 ->getVisibleConversionFunctions()) {
1087 if (CXXConversionDecl *Conversion =
1088 dyn_cast<CXXConversionDecl>(D->getUnderlyingDecl())) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001089 QualType CT = Conversion->getConversionType().getNonReferenceType();
1090 if (CT->isIntegralOrEnumerationType()) {
1091 ++NoIntegrals;
1092 ConversionDecls.push_back(Conversion);
1093 }
1094 else if (CT->isObjCIdType() ||CT->isBlockPointerType()) {
1095 ++NoObjCIdPointers;
1096 ConversionDecls.push_back(Conversion);
1097 }
1098 }
1099 }
1100 if (NoIntegrals ==1 && NoObjCIdPointers == 0)
1101 return OS_Array;
1102 if (NoIntegrals == 0 && NoObjCIdPointers == 1)
1103 return OS_Dictionary;
1104 if (NoIntegrals == 0 && NoObjCIdPointers == 0) {
1105 // No conversion function was found. Issue diagnostic and return.
1106 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1107 << FromE->getType();
1108 return OS_Error;
1109 }
1110 Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion)
1111 << FromE->getType();
1112 for (unsigned int i = 0; i < ConversionDecls.size(); i++)
Richard Smith01d96982016-12-02 23:00:28 +00001113 Diag(ConversionDecls[i]->getLocation(),
1114 diag::note_conv_function_declared_at);
1115
Ted Kremeneke65b0862012-03-06 20:05:56 +00001116 return OS_Error;
1117}
1118
Fariborz Jahanian90804912012-08-02 18:03:58 +00001119/// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF
1120/// objects used as dictionary subscript key objects.
Fangrui Song6907ce22018-07-30 19:24:48 +00001121static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001122 Expr *Key) {
1123 if (ContainerT.isNull())
1124 return;
1125 // dictionary subscripting.
1126 // - (id)objectForKeyedSubscript:(id)key;
1127 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001128 &S.Context.Idents.get("objectForKeyedSubscript")
Fariborz Jahanian90804912012-08-02 18:03:58 +00001129 };
1130 Selector GetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
Fangrui Song6907ce22018-07-30 19:24:48 +00001131 ObjCMethodDecl *Getter = S.LookupMethodInObjectType(GetterSelector, ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001132 true /*instance*/);
1133 if (!Getter)
1134 return;
Alp Toker03376dc2014-07-07 09:02:20 +00001135 QualType T = Getter->parameters()[0]->getType();
Brian Kelley11352a82017-03-29 18:09:02 +00001136 S.CheckObjCConversion(Key->getSourceRange(), T, Key,
1137 Sema::CCK_ImplicitConversion);
Fariborz Jahanian90804912012-08-02 18:03:58 +00001138}
1139
Ted Kremeneke65b0862012-03-06 20:05:56 +00001140bool ObjCSubscriptOpBuilder::findAtIndexGetter() {
1141 if (AtIndexGetter)
1142 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001143
Ted Kremeneke65b0862012-03-06 20:05:56 +00001144 Expr *BaseExpr = RefExpr->getBaseExpr();
1145 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001146
Ted Kremeneke65b0862012-03-06 20:05:56 +00001147 QualType ResultType;
1148 if (const ObjCObjectPointerType *PTy =
1149 BaseT->getAs<ObjCObjectPointerType>()) {
1150 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001151 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001152 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001153 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001154 if (Res == Sema::OS_Error) {
1155 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001156 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001157 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001158 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001159 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001160 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001161
Ted Kremeneke65b0862012-03-06 20:05:56 +00001162 if (ResultType.isNull()) {
1163 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1164 << BaseExpr->getType() << arrayRef;
1165 return false;
1166 }
1167 if (!arrayRef) {
1168 // dictionary subscripting.
1169 // - (id)objectForKeyedSubscript:(id)key;
1170 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001171 &S.Context.Idents.get("objectForKeyedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001172 };
1173 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1174 }
1175 else {
1176 // - (id)objectAtIndexedSubscript:(size_t)index;
1177 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001178 &S.Context.Idents.get("objectAtIndexedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001179 };
Fangrui Song6907ce22018-07-30 19:24:48 +00001180
Ted Kremeneke65b0862012-03-06 20:05:56 +00001181 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1182 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001183
1184 AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001185 true /*instance*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00001186
David Blaikiebbafb8a2012-03-11 07:00:24 +00001187 if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
Adrian Prantl2073dd22019-11-04 14:28:14 -08001188 AtIndexGetter = ObjCMethodDecl::Create(
1189 S.Context, SourceLocation(), SourceLocation(), AtIndexGetterSelector,
1190 S.Context.getObjCIdType() /*ReturnType*/, nullptr /*TypeSourceInfo */,
1191 S.Context.getTranslationUnitDecl(), true /*Instance*/,
1192 false /*isVariadic*/,
1193 /*isPropertyAccessor=*/false,
1194 /*isSynthesizedAccessorStub=*/false,
1195 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1196 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001197 ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1198 SourceLocation(), SourceLocation(),
1199 arrayRef ? &S.Context.Idents.get("index")
1200 : &S.Context.Idents.get("key"),
1201 arrayRef ? S.Context.UnsignedLongTy
1202 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001203 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001204 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001205 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001206 AtIndexGetter->setMethodParams(S.Context, Argument, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001207 }
1208
1209 if (!AtIndexGetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001210 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001211 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1212 << BaseExpr->getType() << 0 << arrayRef;
1213 return false;
1214 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001215 AtIndexGetter =
1216 S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1217 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001218 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001219 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001220
Ted Kremeneke65b0862012-03-06 20:05:56 +00001221 if (AtIndexGetter) {
Alp Toker03376dc2014-07-07 09:02:20 +00001222 QualType T = AtIndexGetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001223 if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1224 (!arrayRef && !T->isObjCObjectPointerType())) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001225 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001226 arrayRef ? diag::err_objc_subscript_index_type
1227 : diag::err_objc_subscript_key_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001228 S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001229 diag::note_parameter_type) << T;
1230 return false;
1231 }
Alp Toker314cc812014-01-25 16:55:45 +00001232 QualType R = AtIndexGetter->getReturnType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001233 if (!R->isObjCObjectPointerType()) {
1234 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1235 diag::err_objc_indexing_method_result_type) << R << arrayRef;
1236 S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1237 AtIndexGetter->getDeclName();
1238 }
1239 }
1240 return true;
1241}
1242
1243bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1244 if (AtIndexSetter)
1245 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001246
Ted Kremeneke65b0862012-03-06 20:05:56 +00001247 Expr *BaseExpr = RefExpr->getBaseExpr();
1248 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001249
Ted Kremeneke65b0862012-03-06 20:05:56 +00001250 QualType ResultType;
1251 if (const ObjCObjectPointerType *PTy =
1252 BaseT->getAs<ObjCObjectPointerType>()) {
1253 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001254 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001255
1256 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001257 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001258 if (Res == Sema::OS_Error) {
1259 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001260 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001261 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001262 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001263 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001264 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001265
Ted Kremeneke65b0862012-03-06 20:05:56 +00001266 if (ResultType.isNull()) {
1267 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1268 << BaseExpr->getType() << arrayRef;
1269 return false;
1270 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001271
Ted Kremeneke65b0862012-03-06 20:05:56 +00001272 if (!arrayRef) {
1273 // dictionary subscripting.
1274 // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1275 IdentifierInfo *KeyIdents[] = {
1276 &S.Context.Idents.get("setObject"),
1277 &S.Context.Idents.get("forKeyedSubscript")
1278 };
1279 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1280 }
1281 else {
1282 // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1283 IdentifierInfo *KeyIdents[] = {
1284 &S.Context.Idents.get("setObject"),
1285 &S.Context.Idents.get("atIndexedSubscript")
1286 };
1287 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1288 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001289 AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001290 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001291
David Blaikiebbafb8a2012-03-11 07:00:24 +00001292 if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001293 TypeSourceInfo *ReturnTInfo = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001294 QualType ReturnType = S.Context.VoidTy;
Alp Toker314cc812014-01-25 16:55:45 +00001295 AtIndexSetter = ObjCMethodDecl::Create(
1296 S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector,
1297 ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(),
1298 true /*Instance*/, false /*isVariadic*/,
1299 /*isPropertyAccessor=*/false,
Adrian Prantl2073dd22019-11-04 14:28:14 -08001300 /*isSynthesizedAccessorStub=*/false,
Alp Toker314cc812014-01-25 16:55:45 +00001301 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1302 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001303 SmallVector<ParmVarDecl *, 2> Params;
1304 ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter,
1305 SourceLocation(), SourceLocation(),
1306 &S.Context.Idents.get("object"),
1307 S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001308 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001309 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001310 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001311 Params.push_back(object);
1312 ParmVarDecl *key = ParmVarDecl::Create(S.Context, AtIndexSetter,
1313 SourceLocation(), SourceLocation(),
1314 arrayRef ? &S.Context.Idents.get("index")
1315 : &S.Context.Idents.get("key"),
1316 arrayRef ? S.Context.UnsignedLongTy
1317 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001318 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001319 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001320 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001321 Params.push_back(key);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001322 AtIndexSetter->setMethodParams(S.Context, Params, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001323 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001324
Ted Kremeneke65b0862012-03-06 20:05:56 +00001325 if (!AtIndexSetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001326 if (!BaseT->isObjCIdType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001327 S.Diag(BaseExpr->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001328 diag::err_objc_subscript_method_not_found)
1329 << BaseExpr->getType() << 1 << arrayRef;
1330 return false;
1331 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001332 AtIndexSetter =
1333 S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector,
1334 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001335 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001336 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001337
Ted Kremeneke65b0862012-03-06 20:05:56 +00001338 bool err = false;
1339 if (AtIndexSetter && arrayRef) {
Alp Toker03376dc2014-07-07 09:02:20 +00001340 QualType T = AtIndexSetter->parameters()[1]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001341 if (!T->isIntegralOrEnumerationType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001342 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001343 diag::err_objc_subscript_index_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001344 S.Diag(AtIndexSetter->parameters()[1]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001345 diag::note_parameter_type) << T;
1346 err = true;
1347 }
Alp Toker03376dc2014-07-07 09:02:20 +00001348 T = AtIndexSetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001349 if (!T->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001350 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001351 diag::err_objc_subscript_object_type) << T << arrayRef;
Fangrui Song6907ce22018-07-30 19:24:48 +00001352 S.Diag(AtIndexSetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001353 diag::note_parameter_type) << T;
1354 err = true;
1355 }
1356 }
1357 else if (AtIndexSetter && !arrayRef)
1358 for (unsigned i=0; i <2; i++) {
Alp Toker03376dc2014-07-07 09:02:20 +00001359 QualType T = AtIndexSetter->parameters()[i]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001360 if (!T->isObjCObjectPointerType()) {
1361 if (i == 1)
1362 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1363 diag::err_objc_subscript_key_type) << T;
1364 else
1365 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1366 diag::err_objc_subscript_dic_object_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001367 S.Diag(AtIndexSetter->parameters()[i]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001368 diag::note_parameter_type) << T;
1369 err = true;
1370 }
1371 }
1372
1373 return !err;
1374}
1375
1376// Get the object at "Index" position in the container.
1377// [BaseExpr objectAtIndexedSubscript : IndexExpr];
1378ExprResult ObjCSubscriptOpBuilder::buildGet() {
1379 if (!findAtIndexGetter())
1380 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001381
Ted Kremeneke65b0862012-03-06 20:05:56 +00001382 QualType receiverType = InstanceBase->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001383
Ted Kremeneke65b0862012-03-06 20:05:56 +00001384 // Build a message-send.
1385 ExprResult msg;
1386 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001387
Ted Kremeneke65b0862012-03-06 20:05:56 +00001388 // Arguments.
1389 Expr *args[] = { Index };
1390 assert(InstanceBase);
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001391 if (AtIndexGetter)
1392 S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001393 msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1394 GenericLoc,
1395 AtIndexGetterSelector, AtIndexGetter,
1396 MultiExprArg(args, 1));
1397 return msg;
1398}
1399
1400/// Store into the container the "op" object at "Index"'ed location
1401/// by building this messaging expression:
1402/// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00001403/// \param captureSetValueAsResult If true, capture the actual
Ted Kremeneke65b0862012-03-06 20:05:56 +00001404/// value being set as the value of the property operation.
1405ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
1406 bool captureSetValueAsResult) {
1407 if (!findAtIndexSetter())
1408 return ExprError();
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001409 if (AtIndexSetter)
1410 S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001411 QualType receiverType = InstanceBase->getType();
1412 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001413
Ted Kremeneke65b0862012-03-06 20:05:56 +00001414 // Arguments.
1415 Expr *args[] = { op, Index };
Fangrui Song6907ce22018-07-30 19:24:48 +00001416
Ted Kremeneke65b0862012-03-06 20:05:56 +00001417 // Build a message-send.
1418 ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1419 GenericLoc,
1420 AtIndexSetterSelector,
1421 AtIndexSetter,
1422 MultiExprArg(args, 2));
Fangrui Song6907ce22018-07-30 19:24:48 +00001423
Ted Kremeneke65b0862012-03-06 20:05:56 +00001424 if (!msg.isInvalid() && captureSetValueAsResult) {
1425 ObjCMessageExpr *msgExpr =
1426 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
1427 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +00001428 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +00001429 msgExpr->setArg(0, captureValueAsResult(arg));
Ted Kremeneke65b0862012-03-06 20:05:56 +00001430 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001431
Ted Kremeneke65b0862012-03-06 20:05:56 +00001432 return msg;
1433}
1434
John McCallfe96e0b2011-11-06 09:01:30 +00001435//===----------------------------------------------------------------------===//
John McCall5e77d762013-04-16 07:28:30 +00001436// MSVC __declspec(property) references
1437//===----------------------------------------------------------------------===//
1438
Alexey Bataevf7630272015-11-25 12:01:00 +00001439MSPropertyRefExpr *
1440MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) {
1441 CallArgs.insert(CallArgs.begin(), E->getIdx());
1442 Expr *Base = E->getBase()->IgnoreParens();
1443 while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Base)) {
1444 CallArgs.insert(CallArgs.begin(), MSPropSubscript->getIdx());
1445 Base = MSPropSubscript->getBase()->IgnoreParens();
1446 }
1447 return cast<MSPropertyRefExpr>(Base);
1448}
1449
John McCall5e77d762013-04-16 07:28:30 +00001450Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Alexey Bataev69103472015-10-14 04:05:42 +00001451 InstanceBase = capture(RefExpr->getBaseExpr());
Aaron Ballman72f65632017-11-03 20:09:17 +00001452 llvm::for_each(CallArgs, [this](Expr *&Arg) { Arg = capture(Arg); });
Alexey Bataevf7630272015-11-25 12:01:00 +00001453 syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1454 switch (Idx) {
1455 case 0:
1456 return InstanceBase;
1457 default:
1458 assert(Idx <= CallArgs.size());
1459 return CallArgs[Idx - 1];
1460 }
1461 }).rebuild(syntacticBase);
John McCall5e77d762013-04-16 07:28:30 +00001462
1463 return syntacticBase;
1464}
1465
1466ExprResult MSPropertyOpBuilder::buildGet() {
1467 if (!RefExpr->getPropertyDecl()->hasGetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001468 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001469 << 0 /* getter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001470 return ExprError();
1471 }
1472
1473 UnqualifiedId GetterName;
1474 IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId();
1475 GetterName.setIdentifier(II, RefExpr->getMemberLoc());
1476 CXXScopeSpec SS;
1477 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001478 ExprResult GetterExpr =
1479 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1480 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1481 SourceLocation(), GetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001482 if (GetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001483 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001484 diag::err_cannot_find_suitable_accessor) << 0 /* getter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001485 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001486 return ExprError();
1487 }
1488
Richard Smith255b85f2019-05-08 01:36:36 +00001489 return S.BuildCallExpr(S.getCurScope(), GetterExpr.get(),
Alexey Bataevf7630272015-11-25 12:01:00 +00001490 RefExpr->getSourceRange().getBegin(), CallArgs,
John McCall5e77d762013-04-16 07:28:30 +00001491 RefExpr->getSourceRange().getEnd());
1492}
1493
1494ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl,
1495 bool captureSetValueAsResult) {
1496 if (!RefExpr->getPropertyDecl()->hasSetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001497 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001498 << 1 /* setter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001499 return ExprError();
1500 }
1501
1502 UnqualifiedId SetterName;
1503 IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId();
1504 SetterName.setIdentifier(II, RefExpr->getMemberLoc());
1505 CXXScopeSpec SS;
1506 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001507 ExprResult SetterExpr =
1508 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1509 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1510 SourceLocation(), SetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001511 if (SetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001512 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001513 diag::err_cannot_find_suitable_accessor) << 1 /* setter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001514 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001515 return ExprError();
1516 }
1517
Alexey Bataevf7630272015-11-25 12:01:00 +00001518 SmallVector<Expr*, 4> ArgExprs;
1519 ArgExprs.append(CallArgs.begin(), CallArgs.end());
John McCall5e77d762013-04-16 07:28:30 +00001520 ArgExprs.push_back(op);
Richard Smith255b85f2019-05-08 01:36:36 +00001521 return S.BuildCallExpr(S.getCurScope(), SetterExpr.get(),
John McCall5e77d762013-04-16 07:28:30 +00001522 RefExpr->getSourceRange().getBegin(), ArgExprs,
1523 op->getSourceRange().getEnd());
1524}
1525
1526//===----------------------------------------------------------------------===//
John McCallfe96e0b2011-11-06 09:01:30 +00001527// General Sema routines.
1528//===----------------------------------------------------------------------===//
1529
1530ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
1531 Expr *opaqueRef = E->IgnoreParens();
1532 if (ObjCPropertyRefExpr *refExpr
1533 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001534 ObjCPropertyOpBuilder builder(*this, refExpr, true);
John McCallfe96e0b2011-11-06 09:01:30 +00001535 return builder.buildRValueOperation(E);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001536 }
1537 else if (ObjCSubscriptRefExpr *refExpr
1538 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001539 ObjCSubscriptOpBuilder builder(*this, refExpr, true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001540 return builder.buildRValueOperation(E);
John McCall5e77d762013-04-16 07:28:30 +00001541 } else if (MSPropertyRefExpr *refExpr
1542 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001543 MSPropertyOpBuilder builder(*this, refExpr, true);
John McCall5e77d762013-04-16 07:28:30 +00001544 return builder.buildRValueOperation(E);
Alexey Bataevf7630272015-11-25 12:01:00 +00001545 } else if (MSPropertySubscriptExpr *RefExpr =
1546 dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001547 MSPropertyOpBuilder Builder(*this, RefExpr, true);
Alexey Bataevf7630272015-11-25 12:01:00 +00001548 return Builder.buildRValueOperation(E);
John McCallfe96e0b2011-11-06 09:01:30 +00001549 } else {
1550 llvm_unreachable("unknown pseudo-object kind!");
1551 }
1552}
1553
1554/// Check an increment or decrement of a pseudo-object expression.
1555ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc,
1556 UnaryOperatorKind opcode, Expr *op) {
1557 // Do nothing if the operand is dependent.
1558 if (op->isTypeDependent())
1559 return new (Context) UnaryOperator(op, opcode, Context.DependentTy,
Aaron Ballmana5038552018-01-09 13:07:03 +00001560 VK_RValue, OK_Ordinary, opcLoc, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001561
1562 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1563 Expr *opaqueRef = op->IgnoreParens();
1564 if (ObjCPropertyRefExpr *refExpr
1565 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001566 ObjCPropertyOpBuilder builder(*this, refExpr, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001567 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001568 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1569 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1570 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001571 } else if (MSPropertyRefExpr *refExpr
1572 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001573 MSPropertyOpBuilder builder(*this, refExpr, false);
John McCall5e77d762013-04-16 07:28:30 +00001574 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001575 } else if (MSPropertySubscriptExpr *RefExpr
1576 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001577 MSPropertyOpBuilder Builder(*this, RefExpr, false);
Alexey Bataevf7630272015-11-25 12:01:00 +00001578 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001579 } else {
1580 llvm_unreachable("unknown pseudo-object kind!");
1581 }
1582}
1583
1584ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1585 BinaryOperatorKind opcode,
1586 Expr *LHS, Expr *RHS) {
1587 // Do nothing if either argument is dependent.
1588 if (LHS->isTypeDependent() || RHS->isTypeDependent())
1589 return new (Context) BinaryOperator(LHS, RHS, opcode, Context.DependentTy,
Adam Nemet484aa452017-03-27 19:17:25 +00001590 VK_RValue, OK_Ordinary, opcLoc,
1591 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +00001592
1593 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001594 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1595 ExprResult result = CheckPlaceholderExpr(RHS);
1596 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001597 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001598 }
1599
Akira Hatanaka797afe32018-03-20 01:47:58 +00001600 bool IsSimpleAssign = opcode == BO_Assign;
John McCallfe96e0b2011-11-06 09:01:30 +00001601 Expr *opaqueRef = LHS->IgnoreParens();
1602 if (ObjCPropertyRefExpr *refExpr
1603 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001604 ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
John McCallfe96e0b2011-11-06 09:01:30 +00001605 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001606 } else if (ObjCSubscriptRefExpr *refExpr
1607 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001608 ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001609 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001610 } else if (MSPropertyRefExpr *refExpr
1611 = dyn_cast<MSPropertyRefExpr>(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);
1614 } else if (MSPropertySubscriptExpr *RefExpr
1615 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001616 MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001617 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001618 } else {
1619 llvm_unreachable("unknown pseudo-object kind!");
1620 }
1621}
John McCalle9290822011-11-30 04:42:31 +00001622
1623/// Given a pseudo-object reference, rebuild it without the opaque
1624/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1625/// This should never operate in-place.
1626static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001627 return Rebuilder(S,
1628 [=](Expr *E, unsigned) -> Expr * {
1629 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1630 })
1631 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001632}
1633
1634/// Given a pseudo-object expression, recreate what it looks like
1635/// syntactically without the attendant OpaqueValueExprs.
1636///
1637/// This is a hack which should be removed when TreeTransform is
1638/// capable of rebuilding a tree without stripping implicit
1639/// operations.
1640Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001641 Expr *syntax = E->getSyntacticForm();
1642 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1643 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1644 return new (Context) UnaryOperator(
1645 op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1646 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1647 } else if (CompoundAssignOperator *cop
1648 = dyn_cast<CompoundAssignOperator>(syntax)) {
1649 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001650 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
1651 return new (Context) CompoundAssignOperator(lhs, rhs, cop->getOpcode(),
1652 cop->getType(),
1653 cop->getValueKind(),
1654 cop->getObjectKind(),
1655 cop->getComputationLHSType(),
1656 cop->getComputationResultType(),
Adam Nemet484aa452017-03-27 19:17:25 +00001657 cop->getOperatorLoc(),
1658 FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001659 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1660 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1661 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
1662 return new (Context) BinaryOperator(lhs, rhs, bop->getOpcode(),
1663 bop->getType(), bop->getValueKind(),
1664 bop->getObjectKind(),
Adam Nemet484aa452017-03-27 19:17:25 +00001665 bop->getOperatorLoc(), FPOptions());
Johannes Doerfertbefb4be2020-02-25 14:04:06 -08001666 } else if (isa<CallExpr>(syntax)) {
1667 return syntax;
John McCalle9290822011-11-30 04:42:31 +00001668 } else {
1669 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1670 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1671 }
1672}