blob: 8a671b1487247ae37cb1f0fa54fc1685a824ada7 [file] [log] [blame]
John McCall526ab472011-10-25 17:37:35 +00001//===--- SemaPseudoObject.cpp - Semantic Analysis for Pseudo-Objects ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions involving
11// pseudo-object references. Pseudo-objects are conceptual objects
12// whose storage is entirely abstract and all accesses to which are
13// translated through some sort of abstraction barrier.
14//
15// For example, Objective-C objects can have "properties", either
16// declared or undeclared. A property may be accessed by writing
17// expr.prop
18// where 'expr' is an r-value of Objective-C pointer type and 'prop'
19// is the name of the property. If this expression is used in a context
20// needing an r-value, it is treated as if it were a message-send
21// of the associated 'getter' selector, typically:
22// [expr prop]
23// If it is used as the LHS of a simple assignment, it is treated
24// as a message-send of the associated 'setter' selector, typically:
25// [expr setProp: RHS]
26// If it is used as the LHS of a compound assignment, or the operand
27// of a unary increment or decrement, both are required; for example,
28// 'expr.prop *= 100' would be translated to:
29// [expr setProp: [expr prop] * 100]
30//
31//===----------------------------------------------------------------------===//
32
33#include "clang/Sema/SemaInternal.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000034#include "clang/AST/ExprCXX.h"
John McCall526ab472011-10-25 17:37:35 +000035#include "clang/AST/ExprObjC.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000036#include "clang/Basic/CharInfo.h"
John McCall526ab472011-10-25 17:37:35 +000037#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000038#include "clang/Sema/Initialization.h"
39#include "clang/Sema/ScopeInfo.h"
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +000040#include "llvm/ADT/SmallString.h"
John McCall526ab472011-10-25 17:37:35 +000041
42using namespace clang;
43using namespace sema;
44
John McCallfe96e0b2011-11-06 09:01:30 +000045namespace {
46 // Basically just a very focused copy of TreeTransform.
Alexey Bataevf7630272015-11-25 12:01:00 +000047 struct Rebuilder {
John McCallfe96e0b2011-11-06 09:01:30 +000048 Sema &S;
Alexey Bataevf7630272015-11-25 12:01:00 +000049 unsigned MSPropertySubscriptCount;
50 typedef llvm::function_ref<Expr *(Expr *, unsigned)> SpecificRebuilderRefTy;
51 const SpecificRebuilderRefTy &SpecificCallback;
52 Rebuilder(Sema &S, const SpecificRebuilderRefTy &SpecificCallback)
53 : S(S), MSPropertySubscriptCount(0),
54 SpecificCallback(SpecificCallback) {}
John McCallfe96e0b2011-11-06 09:01:30 +000055
Alexey Bataevf7630272015-11-25 12:01:00 +000056 Expr *rebuildObjCPropertyRefExpr(ObjCPropertyRefExpr *refExpr) {
57 // Fortunately, the constraint that we're rebuilding something
58 // with a base limits the number of cases here.
59 if (refExpr->isClassReceiver() || refExpr->isSuperReceiver())
60 return refExpr;
61
62 if (refExpr->isExplicitProperty()) {
63 return new (S.Context) ObjCPropertyRefExpr(
64 refExpr->getExplicitProperty(), refExpr->getType(),
65 refExpr->getValueKind(), refExpr->getObjectKind(),
66 refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
67 }
68 return new (S.Context) ObjCPropertyRefExpr(
69 refExpr->getImplicitPropertyGetter(),
70 refExpr->getImplicitPropertySetter(), refExpr->getType(),
71 refExpr->getValueKind(), refExpr->getObjectKind(),
72 refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
73 }
74 Expr *rebuildObjCSubscriptRefExpr(ObjCSubscriptRefExpr *refExpr) {
75 assert(refExpr->getBaseExpr());
76 assert(refExpr->getKeyExpr());
77
78 return new (S.Context) ObjCSubscriptRefExpr(
79 SpecificCallback(refExpr->getBaseExpr(), 0),
80 SpecificCallback(refExpr->getKeyExpr(), 1), refExpr->getType(),
81 refExpr->getValueKind(), refExpr->getObjectKind(),
82 refExpr->getAtIndexMethodDecl(), refExpr->setAtIndexMethodDecl(),
83 refExpr->getRBracket());
84 }
85 Expr *rebuildMSPropertyRefExpr(MSPropertyRefExpr *refExpr) {
86 assert(refExpr->getBaseExpr());
87
88 return new (S.Context) MSPropertyRefExpr(
89 SpecificCallback(refExpr->getBaseExpr(), 0),
90 refExpr->getPropertyDecl(), refExpr->isArrow(), refExpr->getType(),
91 refExpr->getValueKind(), refExpr->getQualifierLoc(),
92 refExpr->getMemberLoc());
93 }
94 Expr *rebuildMSPropertySubscriptExpr(MSPropertySubscriptExpr *refExpr) {
95 assert(refExpr->getBase());
96 assert(refExpr->getIdx());
97
98 auto *NewBase = rebuild(refExpr->getBase());
99 ++MSPropertySubscriptCount;
100 return new (S.Context) MSPropertySubscriptExpr(
101 NewBase,
102 SpecificCallback(refExpr->getIdx(), MSPropertySubscriptCount),
103 refExpr->getType(), refExpr->getValueKind(), refExpr->getObjectKind(),
104 refExpr->getRBracketLoc());
105 }
John McCallfe96e0b2011-11-06 09:01:30 +0000106
107 Expr *rebuild(Expr *e) {
108 // Fast path: nothing to look through.
Alexey Bataevf7630272015-11-25 12:01:00 +0000109 if (auto *PRE = dyn_cast<ObjCPropertyRefExpr>(e))
110 return rebuildObjCPropertyRefExpr(PRE);
111 if (auto *SRE = dyn_cast<ObjCSubscriptRefExpr>(e))
112 return rebuildObjCSubscriptRefExpr(SRE);
113 if (auto *MSPRE = dyn_cast<MSPropertyRefExpr>(e))
114 return rebuildMSPropertyRefExpr(MSPRE);
115 if (auto *MSPSE = dyn_cast<MSPropertySubscriptExpr>(e))
116 return rebuildMSPropertySubscriptExpr(MSPSE);
John McCallfe96e0b2011-11-06 09:01:30 +0000117
118 // Otherwise, we should look through and rebuild anything that
119 // IgnoreParens would.
120
121 if (ParenExpr *parens = dyn_cast<ParenExpr>(e)) {
122 e = rebuild(parens->getSubExpr());
123 return new (S.Context) ParenExpr(parens->getLParen(),
124 parens->getRParen(),
125 e);
126 }
127
128 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(e)) {
129 assert(uop->getOpcode() == UO_Extension);
130 e = rebuild(uop->getSubExpr());
131 return new (S.Context) UnaryOperator(e, uop->getOpcode(),
132 uop->getType(),
133 uop->getValueKind(),
134 uop->getObjectKind(),
Aaron Ballmana5038552018-01-09 13:07:03 +0000135 uop->getOperatorLoc(),
136 uop->canOverflow());
John McCallfe96e0b2011-11-06 09:01:30 +0000137 }
138
139 if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
140 assert(!gse->isResultDependent());
141 unsigned resultIndex = gse->getResultIndex();
142 unsigned numAssocs = gse->getNumAssocs();
143
144 SmallVector<Expr*, 8> assocs(numAssocs);
145 SmallVector<TypeSourceInfo*, 8> assocTypes(numAssocs);
146
147 for (unsigned i = 0; i != numAssocs; ++i) {
148 Expr *assoc = gse->getAssocExpr(i);
149 if (i == resultIndex) assoc = rebuild(assoc);
150 assocs[i] = assoc;
151 assocTypes[i] = gse->getAssocTypeSourceInfo(i);
152 }
153
154 return new (S.Context) GenericSelectionExpr(S.Context,
155 gse->getGenericLoc(),
156 gse->getControllingExpr(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000157 assocTypes,
158 assocs,
John McCallfe96e0b2011-11-06 09:01:30 +0000159 gse->getDefaultLoc(),
160 gse->getRParenLoc(),
161 gse->containsUnexpandedParameterPack(),
162 resultIndex);
163 }
164
Eli Friedman75807f22013-07-20 00:40:58 +0000165 if (ChooseExpr *ce = dyn_cast<ChooseExpr>(e)) {
166 assert(!ce->isConditionDependent());
167
168 Expr *LHS = ce->getLHS(), *RHS = ce->getRHS();
169 Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS;
170 rebuiltExpr = rebuild(rebuiltExpr);
171
172 return new (S.Context) ChooseExpr(ce->getBuiltinLoc(),
173 ce->getCond(),
174 LHS, RHS,
175 rebuiltExpr->getType(),
176 rebuiltExpr->getValueKind(),
177 rebuiltExpr->getObjectKind(),
178 ce->getRParenLoc(),
179 ce->isConditionTrue(),
180 rebuiltExpr->isTypeDependent(),
181 rebuiltExpr->isValueDependent());
182 }
183
John McCallfe96e0b2011-11-06 09:01:30 +0000184 llvm_unreachable("bad expression to rebuild!");
185 }
186 };
187
John McCallfe96e0b2011-11-06 09:01:30 +0000188 class PseudoOpBuilder {
189 public:
190 Sema &S;
191 unsigned ResultIndex;
192 SourceLocation GenericLoc;
193 SmallVector<Expr *, 4> Semantics;
194
195 PseudoOpBuilder(Sema &S, SourceLocation genericLoc)
196 : S(S), ResultIndex(PseudoObjectExpr::NoResult),
197 GenericLoc(genericLoc) {}
198
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000199 virtual ~PseudoOpBuilder() {}
Matt Beaumont-Gayfb3cb9a2011-11-08 01:53:17 +0000200
John McCallfe96e0b2011-11-06 09:01:30 +0000201 /// Add a normal semantic expression.
202 void addSemanticExpr(Expr *semantic) {
203 Semantics.push_back(semantic);
204 }
205
206 /// Add the 'result' semantic expression.
207 void addResultSemanticExpr(Expr *resultExpr) {
208 assert(ResultIndex == PseudoObjectExpr::NoResult);
209 ResultIndex = Semantics.size();
210 Semantics.push_back(resultExpr);
211 }
212
213 ExprResult buildRValueOperation(Expr *op);
214 ExprResult buildAssignmentOperation(Scope *Sc,
215 SourceLocation opLoc,
216 BinaryOperatorKind opcode,
217 Expr *LHS, Expr *RHS);
218 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
219 UnaryOperatorKind opcode,
220 Expr *op);
221
Jordan Rosed3934582012-09-28 22:21:30 +0000222 virtual ExprResult complete(Expr *syntacticForm);
John McCallfe96e0b2011-11-06 09:01:30 +0000223
224 OpaqueValueExpr *capture(Expr *op);
225 OpaqueValueExpr *captureValueAsResult(Expr *op);
226
227 void setResultToLastSemantic() {
228 assert(ResultIndex == PseudoObjectExpr::NoResult);
229 ResultIndex = Semantics.size() - 1;
230 }
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;
Alexey Bataev60520e22015-12-10 04:38:18 +0000249 /// \brief Should the result of an assignment be the formal result of the
250 /// 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:
277 ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr) :
278 PseudoOpBuilder(S, refExpr->getLocation()), RefExpr(refExpr),
Craig Topperc3ec1492014-05-26 06:22:03 +0000279 SyntacticRefExpr(nullptr), InstanceReceiver(nullptr), Getter(nullptr),
280 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;
312
313 ObjCMethodDecl *AtIndexSetter;
314 Selector AtIndexSetterSelector;
315
316 public:
317 ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr) :
318 PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
319 RefExpr(refExpr),
Craig Topperc3ec1492014-05-26 06:22:03 +0000320 InstanceBase(nullptr), InstanceKey(nullptr),
321 AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
322
Ted Kremeneke65b0862012-03-06 20:05:56 +0000323 ExprResult buildRValueOperation(Expr *op);
324 ExprResult buildAssignmentOperation(Scope *Sc,
325 SourceLocation opLoc,
326 BinaryOperatorKind opcode,
327 Expr *LHS, Expr *RHS);
Craig Toppere14c0f82014-03-12 04:55:44 +0000328 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
329
Ted Kremeneke65b0862012-03-06 20:05:56 +0000330 bool findAtIndexGetter();
331 bool findAtIndexSetter();
Craig Toppere14c0f82014-03-12 04:55:44 +0000332
333 ExprResult buildGet() override;
334 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Ted Kremeneke65b0862012-03-06 20:05:56 +0000335 };
336
John McCall5e77d762013-04-16 07:28:30 +0000337 class MSPropertyOpBuilder : public PseudoOpBuilder {
338 MSPropertyRefExpr *RefExpr;
Alexey Bataev69103472015-10-14 04:05:42 +0000339 OpaqueValueExpr *InstanceBase;
Alexey Bataevf7630272015-11-25 12:01:00 +0000340 SmallVector<Expr *, 4> CallArgs;
341
342 MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
John McCall5e77d762013-04-16 07:28:30 +0000343
344 public:
345 MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr) :
346 PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
Alexey Bataev69103472015-10-14 04:05:42 +0000347 RefExpr(refExpr), InstanceBase(nullptr) {}
Alexey Bataevf7630272015-11-25 12:01:00 +0000348 MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr)
349 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
350 InstanceBase(nullptr) {
351 RefExpr = getBaseMSProperty(refExpr);
352 }
John McCall5e77d762013-04-16 07:28:30 +0000353
Craig Toppere14c0f82014-03-12 04:55:44 +0000354 Expr *rebuildAndCaptureObject(Expr *) override;
355 ExprResult buildGet() override;
356 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Alexey Bataev60520e22015-12-10 04:38:18 +0000357 bool captureSetValueAsResult() const override { return false; }
John McCall5e77d762013-04-16 07:28:30 +0000358 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000359}
John McCallfe96e0b2011-11-06 09:01:30 +0000360
361/// Capture the given expression in an OpaqueValueExpr.
362OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
363 // Make a new OVE whose source is the given expression.
364 OpaqueValueExpr *captured =
365 new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
Douglas Gregor2d5aea02012-02-23 22:17:26 +0000366 e->getValueKind(), e->getObjectKind(),
367 e);
John McCallfe96e0b2011-11-06 09:01:30 +0000368
369 // Make sure we bind that in the semantics.
370 addSemanticExpr(captured);
371 return captured;
372}
373
374/// Capture the given expression as the result of this pseudo-object
375/// operation. This routine is safe against expressions which may
376/// already be captured.
377///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000378/// \returns the captured expression, which will be the
John McCallfe96e0b2011-11-06 09:01:30 +0000379/// same as the input if the input was already captured
380OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
381 assert(ResultIndex == PseudoObjectExpr::NoResult);
382
383 // If the expression hasn't already been captured, just capture it
384 // and set the new semantic
385 if (!isa<OpaqueValueExpr>(e)) {
386 OpaqueValueExpr *cap = capture(e);
387 setResultToLastSemantic();
388 return cap;
389 }
390
391 // Otherwise, it must already be one of our semantic expressions;
392 // set ResultIndex to its index.
393 unsigned index = 0;
394 for (;; ++index) {
395 assert(index < Semantics.size() &&
396 "captured expression not found in semantics!");
397 if (e == Semantics[index]) break;
398 }
399 ResultIndex = index;
400 return cast<OpaqueValueExpr>(e);
401}
402
403/// The routine which creates the final PseudoObjectExpr.
404ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
405 return PseudoObjectExpr::Create(S.Context, syntactic,
406 Semantics, ResultIndex);
407}
408
409/// The main skeleton for building an r-value operation.
410ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
411 Expr *syntacticBase = rebuildAndCaptureObject(op);
412
413 ExprResult getExpr = buildGet();
414 if (getExpr.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000415 addResultSemanticExpr(getExpr.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000416
417 return complete(syntacticBase);
418}
419
420/// The basic skeleton for building a simple or compound
421/// assignment operation.
422ExprResult
423PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
424 BinaryOperatorKind opcode,
425 Expr *LHS, Expr *RHS) {
426 assert(BinaryOperator::isAssignmentOp(opcode));
427
428 Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
429 OpaqueValueExpr *capturedRHS = capture(RHS);
430
John McCallee04aeb2015-08-22 00:35:27 +0000431 // In some very specific cases, semantic analysis of the RHS as an
432 // expression may require it to be rewritten. In these cases, we
433 // cannot safely keep the OVE around. Fortunately, we don't really
434 // need to: we don't use this particular OVE in multiple places, and
435 // no clients rely that closely on matching up expressions in the
436 // semantic expression with expressions from the syntactic form.
437 Expr *semanticRHS = capturedRHS;
438 if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
439 semanticRHS = RHS;
440 Semantics.pop_back();
441 }
442
John McCallfe96e0b2011-11-06 09:01:30 +0000443 Expr *syntactic;
444
445 ExprResult result;
446 if (opcode == BO_Assign) {
John McCallee04aeb2015-08-22 00:35:27 +0000447 result = semanticRHS;
John McCallfe96e0b2011-11-06 09:01:30 +0000448 syntactic = new (S.Context) BinaryOperator(syntacticLHS, capturedRHS,
449 opcode, capturedRHS->getType(),
450 capturedRHS->getValueKind(),
Adam Nemet484aa452017-03-27 19:17:25 +0000451 OK_Ordinary, opcLoc,
452 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000453 } else {
454 ExprResult opLHS = buildGet();
455 if (opLHS.isInvalid()) return ExprError();
456
457 // Build an ordinary, non-compound operation.
458 BinaryOperatorKind nonCompound =
459 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000460 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000461 if (result.isInvalid()) return ExprError();
462
463 syntactic =
464 new (S.Context) CompoundAssignOperator(syntacticLHS, capturedRHS, opcode,
465 result.get()->getType(),
466 result.get()->getValueKind(),
467 OK_Ordinary,
468 opLHS.get()->getType(),
469 result.get()->getType(),
Adam Nemet484aa452017-03-27 19:17:25 +0000470 opcLoc, FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000471 }
472
473 // The result of the assignment, if not void, is the value set into
474 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000475 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000476 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000477 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000478 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
479 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
480 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000481
482 return complete(syntactic);
483}
484
485/// The basic skeleton for building an increment or decrement
486/// operation.
487ExprResult
488PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
489 UnaryOperatorKind opcode,
490 Expr *op) {
491 assert(UnaryOperator::isIncrementDecrementOp(opcode));
492
493 Expr *syntacticOp = rebuildAndCaptureObject(op);
494
495 // Load the value.
496 ExprResult result = buildGet();
497 if (result.isInvalid()) return ExprError();
498
499 QualType resultType = result.get()->getType();
500
501 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000502 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000503 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000504 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000505 setResultToLastSemantic();
506 }
507
508 // Add or subtract a literal 1.
509 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
510 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
511 GenericLoc);
512
513 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000514 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000515 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000516 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000517 }
518 if (result.isInvalid()) return ExprError();
519
520 // Store that back into the result. The value stored is the result
521 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000522 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
523 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000524 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000525 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000526 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
527 !result.get()->getType()->isVoidType() &&
Aaron Ballmana5038552018-01-09 13:07:03 +0000528 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
529 setResultToLastSemantic();
530
531 UnaryOperator *syntactic = new (S.Context) UnaryOperator(
532 syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
533 !resultType->isDependentType()
534 ? S.Context.getTypeSize(resultType) >=
535 S.Context.getTypeSize(S.Context.IntTy)
536 : false);
537 return complete(syntactic);
538}
539
John McCallfe96e0b2011-11-06 09:01:30 +0000540
541//===----------------------------------------------------------------------===//
542// Objective-C @property and implicit property references
543//===----------------------------------------------------------------------===//
544
545/// Look up a method in the receiver type of an Objective-C property
546/// reference.
John McCall526ab472011-10-25 17:37:35 +0000547static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
548 const ObjCPropertyRefExpr *PRE) {
John McCall526ab472011-10-25 17:37:35 +0000549 if (PRE->isObjectReceiver()) {
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000550 const ObjCObjectPointerType *PT =
551 PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
John McCallfe96e0b2011-11-06 09:01:30 +0000552
553 // Special case for 'self' in class method implementations.
554 if (PT->isObjCClassType() &&
555 S.isSelfExpr(const_cast<Expr*>(PRE->getBase()))) {
556 // This cast is safe because isSelfExpr is only true within
557 // methods.
558 ObjCMethodDecl *method =
559 cast<ObjCMethodDecl>(S.CurContext->getNonClosureAncestor());
560 return S.LookupMethodInObjectType(sel,
561 S.Context.getObjCInterfaceType(method->getClassInterface()),
562 /*instance*/ false);
563 }
564
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000565 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
John McCall526ab472011-10-25 17:37:35 +0000566 }
567
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000568 if (PRE->isSuperReceiver()) {
569 if (const ObjCObjectPointerType *PT =
570 PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
571 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
572
573 return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
574 }
575
576 assert(PRE->isClassReceiver() && "Invalid expression");
577 QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
578 return S.LookupMethodInObjectType(sel, IT, false);
John McCall526ab472011-10-25 17:37:35 +0000579}
580
Jordan Rosed3934582012-09-28 22:21:30 +0000581bool ObjCPropertyOpBuilder::isWeakProperty() const {
582 QualType T;
583 if (RefExpr->isExplicitProperty()) {
584 const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty();
585 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Bob Wilsonf4f54e32016-05-25 05:41:57 +0000586 return true;
Jordan Rosed3934582012-09-28 22:21:30 +0000587
588 T = Prop->getType();
589 } else if (Getter) {
Alp Toker314cc812014-01-25 16:55:45 +0000590 T = Getter->getReturnType();
Jordan Rosed3934582012-09-28 22:21:30 +0000591 } else {
592 return false;
593 }
594
595 return T.getObjCLifetime() == Qualifiers::OCL_Weak;
596}
597
John McCallfe96e0b2011-11-06 09:01:30 +0000598bool ObjCPropertyOpBuilder::findGetter() {
599 if (Getter) return true;
John McCall526ab472011-10-25 17:37:35 +0000600
John McCallcfef5462011-11-07 22:49:50 +0000601 // For implicit properties, just trust the lookup we already did.
602 if (RefExpr->isImplicitProperty()) {
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000603 if ((Getter = RefExpr->getImplicitPropertyGetter())) {
604 GetterSelector = Getter->getSelector();
605 return true;
606 }
607 else {
608 // Must build the getter selector the hard way.
609 ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter();
610 assert(setter && "both setter and getter are null - cannot happen");
611 IdentifierInfo *setterName =
612 setter->getSelector().getIdentifierInfoForSlot(0);
Alp Toker541d5072014-06-07 23:30:53 +0000613 IdentifierInfo *getterName =
614 &S.Context.Idents.get(setterName->getName().substr(3));
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000615 GetterSelector =
616 S.PP.getSelectorTable().getNullarySelector(getterName);
617 return false;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000618 }
John McCallcfef5462011-11-07 22:49:50 +0000619 }
620
621 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
622 Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
Craig Topperc3ec1492014-05-26 06:22:03 +0000623 return (Getter != nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000624}
625
626/// Try to find the most accurate setter declaration for the property
627/// reference.
628///
629/// \return true if a setter was found, in which case Setter
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000630bool ObjCPropertyOpBuilder::findSetter(bool warn) {
John McCallfe96e0b2011-11-06 09:01:30 +0000631 // For implicit properties, just trust the lookup we already did.
632 if (RefExpr->isImplicitProperty()) {
633 if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
634 Setter = setter;
635 SetterSelector = setter->getSelector();
636 return true;
John McCall526ab472011-10-25 17:37:35 +0000637 } else {
John McCallfe96e0b2011-11-06 09:01:30 +0000638 IdentifierInfo *getterName =
639 RefExpr->getImplicitPropertyGetter()->getSelector()
640 .getIdentifierInfoForSlot(0);
641 SetterSelector =
Adrian Prantla4ce9062013-06-07 22:29:12 +0000642 SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
643 S.PP.getSelectorTable(),
644 getterName);
John McCallfe96e0b2011-11-06 09:01:30 +0000645 return false;
John McCall526ab472011-10-25 17:37:35 +0000646 }
John McCallfe96e0b2011-11-06 09:01:30 +0000647 }
648
649 // For explicit properties, this is more involved.
650 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
651 SetterSelector = prop->getSetterName();
652
653 // Do a normal method lookup first.
654 if (ObjCMethodDecl *setter =
655 LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
Jordan Rosed01e83a2012-10-10 16:42:25 +0000656 if (setter->isPropertyAccessor() && warn)
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000657 if (const ObjCInterfaceDecl *IFace =
658 dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) {
Craig Topperbf3e3272014-08-30 16:55:52 +0000659 StringRef thisPropertyName = prop->getName();
Jordan Rosea7d03842013-02-08 22:30:41 +0000660 // Try flipping the case of the first character.
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000661 char front = thisPropertyName.front();
Jordan Rosea7d03842013-02-08 22:30:41 +0000662 front = isLowercase(front) ? toUppercase(front) : toLowercase(front);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000663 SmallString<100> PropertyName = thisPropertyName;
664 PropertyName[0] = front;
665 IdentifierInfo *AltMember = &S.PP.getIdentifierTable().get(PropertyName);
Manman Ren5b786402016-01-28 18:49:28 +0000666 if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration(
667 AltMember, prop->getQueryKind()))
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000668 if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) {
Richard Smithf8812672016-12-02 22:38:31 +0000669 S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000670 << prop << prop1 << setter->getSelector();
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000671 S.Diag(prop->getLocation(), diag::note_property_declare);
672 S.Diag(prop1->getLocation(), diag::note_property_declare);
673 }
674 }
John McCallfe96e0b2011-11-06 09:01:30 +0000675 Setter = setter;
676 return true;
677 }
678
679 // That can fail in the somewhat crazy situation that we're
680 // type-checking a message send within the @interface declaration
681 // that declared the @property. But it's not clear that that's
682 // valuable to support.
683
684 return false;
685}
686
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000687void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() {
Fariborz Jahanian55513282014-05-28 18:12:10 +0000688 if (S.getCurLexicalContext()->isObjCContainer() &&
689 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl &&
690 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) {
691 if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) {
692 S.Diag(RefExpr->getLocation(),
693 diag::err_property_function_in_objc_container);
694 S.Diag(prop->getLocation(), diag::note_property_declare);
Fariborz Jahanian55513282014-05-28 18:12:10 +0000695 }
696 }
Fariborz Jahanian55513282014-05-28 18:12:10 +0000697}
698
John McCallfe96e0b2011-11-06 09:01:30 +0000699/// Capture the base object of an Objective-C property expression.
700Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000701 assert(InstanceReceiver == nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000702
703 // If we have a base, capture it in an OVE and rebuild the syntactic
704 // form to use the OVE as its base.
705 if (RefExpr->isObjectReceiver()) {
706 InstanceReceiver = capture(RefExpr->getBase());
Alexey Bataevf7630272015-11-25 12:01:00 +0000707 syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * {
708 return InstanceReceiver;
709 }).rebuild(syntacticBase);
John McCallfe96e0b2011-11-06 09:01:30 +0000710 }
711
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000712 if (ObjCPropertyRefExpr *
713 refE = dyn_cast<ObjCPropertyRefExpr>(syntacticBase->IgnoreParens()))
714 SyntacticRefExpr = refE;
715
John McCallfe96e0b2011-11-06 09:01:30 +0000716 return syntacticBase;
717}
718
719/// Load from an Objective-C property reference.
720ExprResult ObjCPropertyOpBuilder::buildGet() {
721 findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000722 if (!Getter) {
723 DiagnoseUnsupportedPropertyUse();
724 return ExprError();
725 }
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000726
727 if (SyntacticRefExpr)
728 SyntacticRefExpr->setIsMessagingGetter();
729
Douglas Gregore83b9562015-07-07 03:57:53 +0000730 QualType receiverType = RefExpr->getReceiverType(S.Context);
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000731 if (!Getter->isImplicit())
732 S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true);
John McCallfe96e0b2011-11-06 09:01:30 +0000733 // Build a message-send.
734 ExprResult msg;
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000735 if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
736 RefExpr->isObjectReceiver()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000737 assert(InstanceReceiver || RefExpr->isSuperReceiver());
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000738 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
739 GenericLoc, Getter->getSelector(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000740 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000741 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000742 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000743 GenericLoc, Getter->getSelector(),
744 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000745 }
746 return msg;
747}
John McCall526ab472011-10-25 17:37:35 +0000748
John McCallfe96e0b2011-11-06 09:01:30 +0000749/// Store to an Objective-C property reference.
750///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000751/// \param captureSetValueAsResult If true, capture the actual
John McCallfe96e0b2011-11-06 09:01:30 +0000752/// value being set as the value of the property operation.
753ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
754 bool captureSetValueAsResult) {
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000755 if (!findSetter(false)) {
756 DiagnoseUnsupportedPropertyUse();
757 return ExprError();
758 }
John McCallfe96e0b2011-11-06 09:01:30 +0000759
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000760 if (SyntacticRefExpr)
761 SyntacticRefExpr->setIsMessagingSetter();
762
Douglas Gregore83b9562015-07-07 03:57:53 +0000763 QualType receiverType = RefExpr->getReceiverType(S.Context);
John McCallfe96e0b2011-11-06 09:01:30 +0000764
765 // Use assignment constraints when possible; they give us better
766 // diagnostics. "When possible" basically means anything except a
767 // C++ class type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000768 if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000769 QualType paramType = (*Setter->param_begin())->getType()
770 .substObjCMemberType(
771 receiverType,
772 Setter->getDeclContext(),
773 ObjCSubstitutionContext::Parameter);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000774 if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000775 ExprResult opResult = op;
776 Sema::AssignConvertType assignResult
777 = S.CheckSingleAssignmentConstraints(paramType, opResult);
Richard Smithe15a3702016-10-06 23:12:58 +0000778 if (opResult.isInvalid() ||
779 S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType,
John McCallfe96e0b2011-11-06 09:01:30 +0000780 op->getType(), opResult.get(),
781 Sema::AA_Assigning))
782 return ExprError();
783
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000784 op = opResult.get();
John McCallfe96e0b2011-11-06 09:01:30 +0000785 assert(op && "successful assignment left argument invalid?");
John McCall526ab472011-10-25 17:37:35 +0000786 }
787 }
788
John McCallfe96e0b2011-11-06 09:01:30 +0000789 // Arguments.
790 Expr *args[] = { op };
John McCall526ab472011-10-25 17:37:35 +0000791
John McCallfe96e0b2011-11-06 09:01:30 +0000792 // Build a message-send.
793 ExprResult msg;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000794 if (!Setter->isImplicit())
795 S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true);
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000796 if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
797 RefExpr->isObjectReceiver()) {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000798 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
799 GenericLoc, SetterSelector, Setter,
800 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000801 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000802 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
803 GenericLoc,
804 SetterSelector, Setter,
805 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000806 }
807
808 if (!msg.isInvalid() && captureSetValueAsResult) {
809 ObjCMessageExpr *msgExpr =
810 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
811 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000812 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +0000813 msgExpr->setArg(0, captureValueAsResult(arg));
John McCallfe96e0b2011-11-06 09:01:30 +0000814 }
815
816 return msg;
John McCall526ab472011-10-25 17:37:35 +0000817}
818
John McCallfe96e0b2011-11-06 09:01:30 +0000819/// @property-specific behavior for doing lvalue-to-rvalue conversion.
820ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) {
821 // Explicit properties always have getters, but implicit ones don't.
822 // Check that before proceeding.
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000823 if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000824 S.Diag(RefExpr->getLocation(), diag::err_getter_not_found)
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000825 << RefExpr->getSourceRange();
John McCall526ab472011-10-25 17:37:35 +0000826 return ExprError();
827 }
828
John McCallfe96e0b2011-11-06 09:01:30 +0000829 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
John McCall526ab472011-10-25 17:37:35 +0000830 if (result.isInvalid()) return ExprError();
831
John McCallfe96e0b2011-11-06 09:01:30 +0000832 if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType())
833 S.DiagnosePropertyAccessorMismatch(RefExpr->getExplicitProperty(),
834 Getter, RefExpr->getLocation());
835
836 // As a special case, if the method returns 'id', try to get
837 // a better type from the property.
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000838 if (RefExpr->isExplicitProperty() && result.get()->isRValue()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000839 QualType receiverType = RefExpr->getReceiverType(S.Context);
840 QualType propType = RefExpr->getExplicitProperty()
841 ->getUsageType(receiverType);
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000842 if (result.get()->getType()->isObjCIdType()) {
843 if (const ObjCObjectPointerType *ptr
844 = propType->getAs<ObjCObjectPointerType>()) {
845 if (!ptr->isObjCIdType())
846 result = S.ImpCastExprToType(result.get(), propType, CK_BitCast);
847 }
848 }
Brian Kelleycafd9122017-03-29 17:55:11 +0000849 if (propType.getObjCLifetime() == Qualifiers::OCL_Weak &&
850 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
851 RefExpr->getLocation()))
852 S.getCurFunction()->markSafeWeakUse(RefExpr);
John McCallfe96e0b2011-11-06 09:01:30 +0000853 }
854
John McCall526ab472011-10-25 17:37:35 +0000855 return result;
856}
857
John McCallfe96e0b2011-11-06 09:01:30 +0000858/// Try to build this as a call to a getter that returns a reference.
859///
860/// \return true if it was possible, whether or not it actually
861/// succeeded
862bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op,
863 ExprResult &result) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000864 if (!S.getLangOpts().CPlusPlus) return false;
John McCallfe96e0b2011-11-06 09:01:30 +0000865
866 findGetter();
Olivier Goffart4c182c82014-08-04 17:28:05 +0000867 if (!Getter) {
868 // The property has no setter and no getter! This can happen if the type is
869 // invalid. Error have already been reported.
870 result = ExprError();
871 return true;
872 }
John McCallfe96e0b2011-11-06 09:01:30 +0000873
874 // Only do this if the getter returns an l-value reference type.
Alp Toker314cc812014-01-25 16:55:45 +0000875 QualType resultType = Getter->getReturnType();
John McCallfe96e0b2011-11-06 09:01:30 +0000876 if (!resultType->isLValueReferenceType()) return false;
877
878 result = buildRValueOperation(op);
879 return true;
880}
881
882/// @property-specific behavior for doing assignments.
883ExprResult
884ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc,
885 SourceLocation opcLoc,
886 BinaryOperatorKind opcode,
887 Expr *LHS, Expr *RHS) {
John McCall526ab472011-10-25 17:37:35 +0000888 assert(BinaryOperator::isAssignmentOp(opcode));
John McCall526ab472011-10-25 17:37:35 +0000889
890 // If there's no setter, we have no choice but to try to assign to
891 // the result of the getter.
John McCallfe96e0b2011-11-06 09:01:30 +0000892 if (!findSetter()) {
893 ExprResult result;
894 if (tryBuildGetOfReference(LHS, result)) {
895 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000896 return S.BuildBinOp(Sc, opcLoc, opcode, result.get(), RHS);
John McCall526ab472011-10-25 17:37:35 +0000897 }
898
899 // Otherwise, it's an error.
John McCallfe96e0b2011-11-06 09:01:30 +0000900 S.Diag(opcLoc, diag::err_nosetter_property_assignment)
901 << unsigned(RefExpr->isImplicitProperty())
902 << SetterSelector
John McCall526ab472011-10-25 17:37:35 +0000903 << LHS->getSourceRange() << RHS->getSourceRange();
904 return ExprError();
905 }
906
907 // If there is a setter, we definitely want to use it.
908
John McCallfe96e0b2011-11-06 09:01:30 +0000909 // Verify that we can do a compound assignment.
910 if (opcode != BO_Assign && !findGetter()) {
911 S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment)
John McCall526ab472011-10-25 17:37:35 +0000912 << LHS->getSourceRange() << RHS->getSourceRange();
913 return ExprError();
914 }
915
John McCallfe96e0b2011-11-06 09:01:30 +0000916 ExprResult result =
917 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
John McCall526ab472011-10-25 17:37:35 +0000918 if (result.isInvalid()) return ExprError();
919
John McCallfe96e0b2011-11-06 09:01:30 +0000920 // Various warnings about property assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000921 if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) {
John McCallfe96e0b2011-11-06 09:01:30 +0000922 S.checkRetainCycles(InstanceReceiver->getSourceExpr(), RHS);
923 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
924 }
925
John McCall526ab472011-10-25 17:37:35 +0000926 return result;
927}
John McCallfe96e0b2011-11-06 09:01:30 +0000928
929/// @property-specific behavior for doing increments and decrements.
930ExprResult
931ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
932 UnaryOperatorKind opcode,
933 Expr *op) {
934 // If there's no setter, we have no choice but to try to assign to
935 // the result of the getter.
936 if (!findSetter()) {
937 ExprResult result;
938 if (tryBuildGetOfReference(op, result)) {
939 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000940 return S.BuildUnaryOp(Sc, opcLoc, opcode, result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000941 }
942
943 // Otherwise, it's an error.
944 S.Diag(opcLoc, diag::err_nosetter_property_incdec)
945 << unsigned(RefExpr->isImplicitProperty())
946 << unsigned(UnaryOperator::isDecrementOp(opcode))
947 << SetterSelector
948 << op->getSourceRange();
949 return ExprError();
950 }
951
952 // If there is a setter, we definitely want to use it.
953
954 // We also need a getter.
955 if (!findGetter()) {
956 assert(RefExpr->isImplicitProperty());
957 S.Diag(opcLoc, diag::err_nogetter_property_incdec)
958 << unsigned(UnaryOperator::isDecrementOp(opcode))
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000959 << GetterSelector
John McCallfe96e0b2011-11-06 09:01:30 +0000960 << op->getSourceRange();
961 return ExprError();
962 }
963
964 return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op);
965}
966
Jordan Rosed3934582012-09-28 22:21:30 +0000967ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) {
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000968 if (isWeakProperty() && !S.isUnevaluatedContext() &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000969 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
970 SyntacticForm->getLocStart()))
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000971 S.getCurFunction()->recordUseOfWeak(SyntacticRefExpr,
972 SyntacticRefExpr->isMessagingGetter());
Jordan Rosed3934582012-09-28 22:21:30 +0000973
974 return PseudoOpBuilder::complete(SyntacticForm);
975}
976
Ted Kremeneke65b0862012-03-06 20:05:56 +0000977// ObjCSubscript build stuff.
978//
979
980/// objective-c subscripting-specific behavior for doing lvalue-to-rvalue
981/// conversion.
982/// FIXME. Remove this routine if it is proven that no additional
983/// specifity is needed.
984ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) {
985 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
986 if (result.isInvalid()) return ExprError();
987 return result;
988}
989
990/// objective-c subscripting-specific behavior for doing assignments.
991ExprResult
992ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc,
993 SourceLocation opcLoc,
994 BinaryOperatorKind opcode,
995 Expr *LHS, Expr *RHS) {
996 assert(BinaryOperator::isAssignmentOp(opcode));
997 // There must be a method to do the Index'ed assignment.
998 if (!findAtIndexSetter())
999 return ExprError();
1000
1001 // Verify that we can do a compound assignment.
1002 if (opcode != BO_Assign && !findAtIndexGetter())
1003 return ExprError();
1004
1005 ExprResult result =
1006 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
1007 if (result.isInvalid()) return ExprError();
1008
1009 // Various warnings about objc Index'ed assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001010 if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001011 S.checkRetainCycles(InstanceBase->getSourceExpr(), RHS);
1012 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
1013 }
1014
1015 return result;
1016}
1017
1018/// Capture the base object of an Objective-C Index'ed expression.
1019Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001020 assert(InstanceBase == nullptr);
1021
Ted Kremeneke65b0862012-03-06 20:05:56 +00001022 // Capture base expression in an OVE and rebuild the syntactic
1023 // form to use the OVE as its base expression.
1024 InstanceBase = capture(RefExpr->getBaseExpr());
1025 InstanceKey = capture(RefExpr->getKeyExpr());
Alexey Bataevf7630272015-11-25 12:01:00 +00001026
Ted Kremeneke65b0862012-03-06 20:05:56 +00001027 syntacticBase =
Alexey Bataevf7630272015-11-25 12:01:00 +00001028 Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1029 switch (Idx) {
1030 case 0:
1031 return InstanceBase;
1032 case 1:
1033 return InstanceKey;
1034 default:
1035 llvm_unreachable("Unexpected index for ObjCSubscriptExpr");
1036 }
1037 }).rebuild(syntacticBase);
1038
Ted Kremeneke65b0862012-03-06 20:05:56 +00001039 return syntacticBase;
1040}
1041
1042/// CheckSubscriptingKind - This routine decide what type
1043/// of indexing represented by "FromE" is being done.
1044Sema::ObjCSubscriptKind
1045 Sema::CheckSubscriptingKind(Expr *FromE) {
1046 // If the expression already has integral or enumeration type, we're golden.
1047 QualType T = FromE->getType();
1048 if (T->isIntegralOrEnumerationType())
1049 return OS_Array;
1050
1051 // If we don't have a class type in C++, there's no way we can get an
1052 // expression of integral or enumeration type.
1053 const RecordType *RecordTy = T->getAs<RecordType>();
Fariborz Jahaniand13951f2014-09-10 20:55:31 +00001054 if (!RecordTy &&
1055 (T->isObjCObjectPointerType() || T->isVoidPointerType()))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001056 // All other scalar cases are assumed to be dictionary indexing which
1057 // caller handles, with diagnostics if needed.
1058 return OS_Dictionary;
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001059 if (!getLangOpts().CPlusPlus ||
1060 !RecordTy || RecordTy->isIncompleteType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001061 // No indexing can be done. Issue diagnostics and quit.
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001062 const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
1063 if (isa<StringLiteral>(IndexExpr))
1064 Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
1065 << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
1066 else
1067 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1068 << T;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001069 return OS_Error;
1070 }
1071
1072 // We must have a complete class type.
1073 if (RequireCompleteType(FromE->getExprLoc(), T,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001074 diag::err_objc_index_incomplete_class_type, FromE))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001075 return OS_Error;
1076
1077 // Look for a conversion to an integral, enumeration type, or
1078 // objective-C pointer type.
Ted Kremeneke65b0862012-03-06 20:05:56 +00001079 int NoIntegrals=0, NoObjCIdPointers=0;
1080 SmallVector<CXXConversionDecl *, 4> ConversionDecls;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001081
1082 for (NamedDecl *D : cast<CXXRecordDecl>(RecordTy->getDecl())
1083 ->getVisibleConversionFunctions()) {
1084 if (CXXConversionDecl *Conversion =
1085 dyn_cast<CXXConversionDecl>(D->getUnderlyingDecl())) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001086 QualType CT = Conversion->getConversionType().getNonReferenceType();
1087 if (CT->isIntegralOrEnumerationType()) {
1088 ++NoIntegrals;
1089 ConversionDecls.push_back(Conversion);
1090 }
1091 else if (CT->isObjCIdType() ||CT->isBlockPointerType()) {
1092 ++NoObjCIdPointers;
1093 ConversionDecls.push_back(Conversion);
1094 }
1095 }
1096 }
1097 if (NoIntegrals ==1 && NoObjCIdPointers == 0)
1098 return OS_Array;
1099 if (NoIntegrals == 0 && NoObjCIdPointers == 1)
1100 return OS_Dictionary;
1101 if (NoIntegrals == 0 && NoObjCIdPointers == 0) {
1102 // No conversion function was found. Issue diagnostic and return.
1103 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1104 << FromE->getType();
1105 return OS_Error;
1106 }
1107 Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion)
1108 << FromE->getType();
1109 for (unsigned int i = 0; i < ConversionDecls.size(); i++)
Richard Smith01d96982016-12-02 23:00:28 +00001110 Diag(ConversionDecls[i]->getLocation(),
1111 diag::note_conv_function_declared_at);
1112
Ted Kremeneke65b0862012-03-06 20:05:56 +00001113 return OS_Error;
1114}
1115
Fariborz Jahanian90804912012-08-02 18:03:58 +00001116/// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF
1117/// objects used as dictionary subscript key objects.
1118static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT,
1119 Expr *Key) {
1120 if (ContainerT.isNull())
1121 return;
1122 // dictionary subscripting.
1123 // - (id)objectForKeyedSubscript:(id)key;
1124 IdentifierInfo *KeyIdents[] = {
1125 &S.Context.Idents.get("objectForKeyedSubscript")
1126 };
1127 Selector GetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1128 ObjCMethodDecl *Getter = S.LookupMethodInObjectType(GetterSelector, ContainerT,
1129 true /*instance*/);
1130 if (!Getter)
1131 return;
Alp Toker03376dc2014-07-07 09:02:20 +00001132 QualType T = Getter->parameters()[0]->getType();
Brian Kelley11352a82017-03-29 18:09:02 +00001133 S.CheckObjCConversion(Key->getSourceRange(), T, Key,
1134 Sema::CCK_ImplicitConversion);
Fariborz Jahanian90804912012-08-02 18:03:58 +00001135}
1136
Ted Kremeneke65b0862012-03-06 20:05:56 +00001137bool ObjCSubscriptOpBuilder::findAtIndexGetter() {
1138 if (AtIndexGetter)
1139 return true;
1140
1141 Expr *BaseExpr = RefExpr->getBaseExpr();
1142 QualType BaseT = BaseExpr->getType();
1143
1144 QualType ResultType;
1145 if (const ObjCObjectPointerType *PTy =
1146 BaseT->getAs<ObjCObjectPointerType>()) {
1147 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001148 }
1149 Sema::ObjCSubscriptKind Res =
1150 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001151 if (Res == Sema::OS_Error) {
1152 if (S.getLangOpts().ObjCAutoRefCount)
1153 CheckKeyForObjCARCConversion(S, ResultType,
1154 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001155 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001156 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001157 bool arrayRef = (Res == Sema::OS_Array);
1158
1159 if (ResultType.isNull()) {
1160 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1161 << BaseExpr->getType() << arrayRef;
1162 return false;
1163 }
1164 if (!arrayRef) {
1165 // dictionary subscripting.
1166 // - (id)objectForKeyedSubscript:(id)key;
1167 IdentifierInfo *KeyIdents[] = {
1168 &S.Context.Idents.get("objectForKeyedSubscript")
1169 };
1170 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1171 }
1172 else {
1173 // - (id)objectAtIndexedSubscript:(size_t)index;
1174 IdentifierInfo *KeyIdents[] = {
1175 &S.Context.Idents.get("objectAtIndexedSubscript")
1176 };
1177
1178 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1179 }
1180
1181 AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType,
1182 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001183
David Blaikiebbafb8a2012-03-11 07:00:24 +00001184 if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001185 AtIndexGetter = ObjCMethodDecl::Create(S.Context, SourceLocation(),
1186 SourceLocation(), AtIndexGetterSelector,
1187 S.Context.getObjCIdType() /*ReturnType*/,
Craig Topperc3ec1492014-05-26 06:22:03 +00001188 nullptr /*TypeSourceInfo */,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001189 S.Context.getTranslationUnitDecl(),
1190 true /*Instance*/, false/*isVariadic*/,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001191 /*isPropertyAccessor=*/false,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001192 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1193 ObjCMethodDecl::Required,
1194 false);
1195 ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1196 SourceLocation(), SourceLocation(),
1197 arrayRef ? &S.Context.Idents.get("index")
1198 : &S.Context.Idents.get("key"),
1199 arrayRef ? S.Context.UnsignedLongTy
1200 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001201 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001202 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001203 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001204 AtIndexGetter->setMethodParams(S.Context, Argument, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001205 }
1206
1207 if (!AtIndexGetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001208 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001209 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1210 << BaseExpr->getType() << 0 << arrayRef;
1211 return false;
1212 }
1213 AtIndexGetter =
1214 S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1215 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001216 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001217 }
1218
1219 if (AtIndexGetter) {
Alp Toker03376dc2014-07-07 09:02:20 +00001220 QualType T = AtIndexGetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001221 if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1222 (!arrayRef && !T->isObjCObjectPointerType())) {
1223 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1224 arrayRef ? diag::err_objc_subscript_index_type
1225 : diag::err_objc_subscript_key_type) << T;
Alp Toker03376dc2014-07-07 09:02:20 +00001226 S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001227 diag::note_parameter_type) << T;
1228 return false;
1229 }
Alp Toker314cc812014-01-25 16:55:45 +00001230 QualType R = AtIndexGetter->getReturnType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001231 if (!R->isObjCObjectPointerType()) {
1232 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1233 diag::err_objc_indexing_method_result_type) << R << arrayRef;
1234 S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1235 AtIndexGetter->getDeclName();
1236 }
1237 }
1238 return true;
1239}
1240
1241bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1242 if (AtIndexSetter)
1243 return true;
1244
1245 Expr *BaseExpr = RefExpr->getBaseExpr();
1246 QualType BaseT = BaseExpr->getType();
1247
1248 QualType ResultType;
1249 if (const ObjCObjectPointerType *PTy =
1250 BaseT->getAs<ObjCObjectPointerType>()) {
1251 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001252 }
1253
1254 Sema::ObjCSubscriptKind Res =
1255 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001256 if (Res == Sema::OS_Error) {
1257 if (S.getLangOpts().ObjCAutoRefCount)
1258 CheckKeyForObjCARCConversion(S, ResultType,
1259 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001260 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001261 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001262 bool arrayRef = (Res == Sema::OS_Array);
1263
1264 if (ResultType.isNull()) {
1265 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1266 << BaseExpr->getType() << arrayRef;
1267 return false;
1268 }
1269
1270 if (!arrayRef) {
1271 // dictionary subscripting.
1272 // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1273 IdentifierInfo *KeyIdents[] = {
1274 &S.Context.Idents.get("setObject"),
1275 &S.Context.Idents.get("forKeyedSubscript")
1276 };
1277 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1278 }
1279 else {
1280 // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1281 IdentifierInfo *KeyIdents[] = {
1282 &S.Context.Idents.get("setObject"),
1283 &S.Context.Idents.get("atIndexedSubscript")
1284 };
1285 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1286 }
1287 AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
1288 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001289
David Blaikiebbafb8a2012-03-11 07:00:24 +00001290 if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001291 TypeSourceInfo *ReturnTInfo = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001292 QualType ReturnType = S.Context.VoidTy;
Alp Toker314cc812014-01-25 16:55:45 +00001293 AtIndexSetter = ObjCMethodDecl::Create(
1294 S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector,
1295 ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(),
1296 true /*Instance*/, false /*isVariadic*/,
1297 /*isPropertyAccessor=*/false,
1298 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1299 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001300 SmallVector<ParmVarDecl *, 2> Params;
1301 ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter,
1302 SourceLocation(), SourceLocation(),
1303 &S.Context.Idents.get("object"),
1304 S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001305 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001306 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001307 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001308 Params.push_back(object);
1309 ParmVarDecl *key = ParmVarDecl::Create(S.Context, AtIndexSetter,
1310 SourceLocation(), SourceLocation(),
1311 arrayRef ? &S.Context.Idents.get("index")
1312 : &S.Context.Idents.get("key"),
1313 arrayRef ? S.Context.UnsignedLongTy
1314 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001315 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001316 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001317 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001318 Params.push_back(key);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001319 AtIndexSetter->setMethodParams(S.Context, Params, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001320 }
1321
1322 if (!AtIndexSetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001323 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001324 S.Diag(BaseExpr->getExprLoc(),
1325 diag::err_objc_subscript_method_not_found)
1326 << BaseExpr->getType() << 1 << arrayRef;
1327 return false;
1328 }
1329 AtIndexSetter =
1330 S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector,
1331 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001332 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001333 }
1334
1335 bool err = false;
1336 if (AtIndexSetter && arrayRef) {
Alp Toker03376dc2014-07-07 09:02:20 +00001337 QualType T = AtIndexSetter->parameters()[1]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001338 if (!T->isIntegralOrEnumerationType()) {
1339 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1340 diag::err_objc_subscript_index_type) << T;
Alp Toker03376dc2014-07-07 09:02:20 +00001341 S.Diag(AtIndexSetter->parameters()[1]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001342 diag::note_parameter_type) << T;
1343 err = true;
1344 }
Alp Toker03376dc2014-07-07 09:02:20 +00001345 T = AtIndexSetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001346 if (!T->isObjCObjectPointerType()) {
1347 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1348 diag::err_objc_subscript_object_type) << T << arrayRef;
Alp Toker03376dc2014-07-07 09:02:20 +00001349 S.Diag(AtIndexSetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001350 diag::note_parameter_type) << T;
1351 err = true;
1352 }
1353 }
1354 else if (AtIndexSetter && !arrayRef)
1355 for (unsigned i=0; i <2; i++) {
Alp Toker03376dc2014-07-07 09:02:20 +00001356 QualType T = AtIndexSetter->parameters()[i]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001357 if (!T->isObjCObjectPointerType()) {
1358 if (i == 1)
1359 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1360 diag::err_objc_subscript_key_type) << T;
1361 else
1362 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1363 diag::err_objc_subscript_dic_object_type) << T;
Alp Toker03376dc2014-07-07 09:02:20 +00001364 S.Diag(AtIndexSetter->parameters()[i]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001365 diag::note_parameter_type) << T;
1366 err = true;
1367 }
1368 }
1369
1370 return !err;
1371}
1372
1373// Get the object at "Index" position in the container.
1374// [BaseExpr objectAtIndexedSubscript : IndexExpr];
1375ExprResult ObjCSubscriptOpBuilder::buildGet() {
1376 if (!findAtIndexGetter())
1377 return ExprError();
1378
1379 QualType receiverType = InstanceBase->getType();
1380
1381 // Build a message-send.
1382 ExprResult msg;
1383 Expr *Index = InstanceKey;
1384
1385 // Arguments.
1386 Expr *args[] = { Index };
1387 assert(InstanceBase);
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001388 if (AtIndexGetter)
1389 S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001390 msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1391 GenericLoc,
1392 AtIndexGetterSelector, AtIndexGetter,
1393 MultiExprArg(args, 1));
1394 return msg;
1395}
1396
1397/// Store into the container the "op" object at "Index"'ed location
1398/// by building this messaging expression:
1399/// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00001400/// \param captureSetValueAsResult If true, capture the actual
Ted Kremeneke65b0862012-03-06 20:05:56 +00001401/// value being set as the value of the property operation.
1402ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
1403 bool captureSetValueAsResult) {
1404 if (!findAtIndexSetter())
1405 return ExprError();
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001406 if (AtIndexSetter)
1407 S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001408 QualType receiverType = InstanceBase->getType();
1409 Expr *Index = InstanceKey;
1410
1411 // Arguments.
1412 Expr *args[] = { op, Index };
1413
1414 // Build a message-send.
1415 ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1416 GenericLoc,
1417 AtIndexSetterSelector,
1418 AtIndexSetter,
1419 MultiExprArg(args, 2));
1420
1421 if (!msg.isInvalid() && captureSetValueAsResult) {
1422 ObjCMessageExpr *msgExpr =
1423 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
1424 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +00001425 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +00001426 msgExpr->setArg(0, captureValueAsResult(arg));
Ted Kremeneke65b0862012-03-06 20:05:56 +00001427 }
1428
1429 return msg;
1430}
1431
John McCallfe96e0b2011-11-06 09:01:30 +00001432//===----------------------------------------------------------------------===//
John McCall5e77d762013-04-16 07:28:30 +00001433// MSVC __declspec(property) references
1434//===----------------------------------------------------------------------===//
1435
Alexey Bataevf7630272015-11-25 12:01:00 +00001436MSPropertyRefExpr *
1437MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) {
1438 CallArgs.insert(CallArgs.begin(), E->getIdx());
1439 Expr *Base = E->getBase()->IgnoreParens();
1440 while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Base)) {
1441 CallArgs.insert(CallArgs.begin(), MSPropSubscript->getIdx());
1442 Base = MSPropSubscript->getBase()->IgnoreParens();
1443 }
1444 return cast<MSPropertyRefExpr>(Base);
1445}
1446
John McCall5e77d762013-04-16 07:28:30 +00001447Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Alexey Bataev69103472015-10-14 04:05:42 +00001448 InstanceBase = capture(RefExpr->getBaseExpr());
Aaron Ballman72f65632017-11-03 20:09:17 +00001449 llvm::for_each(CallArgs, [this](Expr *&Arg) { Arg = capture(Arg); });
Alexey Bataevf7630272015-11-25 12:01:00 +00001450 syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1451 switch (Idx) {
1452 case 0:
1453 return InstanceBase;
1454 default:
1455 assert(Idx <= CallArgs.size());
1456 return CallArgs[Idx - 1];
1457 }
1458 }).rebuild(syntacticBase);
John McCall5e77d762013-04-16 07:28:30 +00001459
1460 return syntacticBase;
1461}
1462
1463ExprResult MSPropertyOpBuilder::buildGet() {
1464 if (!RefExpr->getPropertyDecl()->hasGetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001465 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001466 << 0 /* getter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001467 return ExprError();
1468 }
1469
1470 UnqualifiedId GetterName;
1471 IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId();
1472 GetterName.setIdentifier(II, RefExpr->getMemberLoc());
1473 CXXScopeSpec SS;
1474 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001475 ExprResult GetterExpr =
1476 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1477 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1478 SourceLocation(), GetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001479 if (GetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001480 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001481 diag::err_cannot_find_suitable_accessor) << 0 /* getter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001482 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001483 return ExprError();
1484 }
1485
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001486 return S.ActOnCallExpr(S.getCurScope(), GetterExpr.get(),
Alexey Bataevf7630272015-11-25 12:01:00 +00001487 RefExpr->getSourceRange().getBegin(), CallArgs,
John McCall5e77d762013-04-16 07:28:30 +00001488 RefExpr->getSourceRange().getEnd());
1489}
1490
1491ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl,
1492 bool captureSetValueAsResult) {
1493 if (!RefExpr->getPropertyDecl()->hasSetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001494 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001495 << 1 /* setter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001496 return ExprError();
1497 }
1498
1499 UnqualifiedId SetterName;
1500 IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId();
1501 SetterName.setIdentifier(II, RefExpr->getMemberLoc());
1502 CXXScopeSpec SS;
1503 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001504 ExprResult SetterExpr =
1505 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1506 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1507 SourceLocation(), SetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001508 if (SetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001509 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001510 diag::err_cannot_find_suitable_accessor) << 1 /* setter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001511 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001512 return ExprError();
1513 }
1514
Alexey Bataevf7630272015-11-25 12:01:00 +00001515 SmallVector<Expr*, 4> ArgExprs;
1516 ArgExprs.append(CallArgs.begin(), CallArgs.end());
John McCall5e77d762013-04-16 07:28:30 +00001517 ArgExprs.push_back(op);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001518 return S.ActOnCallExpr(S.getCurScope(), SetterExpr.get(),
John McCall5e77d762013-04-16 07:28:30 +00001519 RefExpr->getSourceRange().getBegin(), ArgExprs,
1520 op->getSourceRange().getEnd());
1521}
1522
1523//===----------------------------------------------------------------------===//
John McCallfe96e0b2011-11-06 09:01:30 +00001524// General Sema routines.
1525//===----------------------------------------------------------------------===//
1526
1527ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
1528 Expr *opaqueRef = E->IgnoreParens();
1529 if (ObjCPropertyRefExpr *refExpr
1530 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
1531 ObjCPropertyOpBuilder builder(*this, refExpr);
1532 return builder.buildRValueOperation(E);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001533 }
1534 else if (ObjCSubscriptRefExpr *refExpr
1535 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
1536 ObjCSubscriptOpBuilder builder(*this, refExpr);
1537 return builder.buildRValueOperation(E);
John McCall5e77d762013-04-16 07:28:30 +00001538 } else if (MSPropertyRefExpr *refExpr
1539 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
1540 MSPropertyOpBuilder builder(*this, refExpr);
1541 return builder.buildRValueOperation(E);
Alexey Bataevf7630272015-11-25 12:01:00 +00001542 } else if (MSPropertySubscriptExpr *RefExpr =
1543 dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1544 MSPropertyOpBuilder Builder(*this, RefExpr);
1545 return Builder.buildRValueOperation(E);
John McCallfe96e0b2011-11-06 09:01:30 +00001546 } else {
1547 llvm_unreachable("unknown pseudo-object kind!");
1548 }
1549}
1550
1551/// Check an increment or decrement of a pseudo-object expression.
1552ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc,
1553 UnaryOperatorKind opcode, Expr *op) {
1554 // Do nothing if the operand is dependent.
1555 if (op->isTypeDependent())
1556 return new (Context) UnaryOperator(op, opcode, Context.DependentTy,
Aaron Ballmana5038552018-01-09 13:07:03 +00001557 VK_RValue, OK_Ordinary, opcLoc, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001558
1559 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1560 Expr *opaqueRef = op->IgnoreParens();
1561 if (ObjCPropertyRefExpr *refExpr
1562 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
1563 ObjCPropertyOpBuilder builder(*this, refExpr);
1564 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001565 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1566 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1567 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001568 } else if (MSPropertyRefExpr *refExpr
1569 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
1570 MSPropertyOpBuilder builder(*this, refExpr);
1571 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001572 } else if (MSPropertySubscriptExpr *RefExpr
1573 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1574 MSPropertyOpBuilder Builder(*this, RefExpr);
1575 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001576 } else {
1577 llvm_unreachable("unknown pseudo-object kind!");
1578 }
1579}
1580
1581ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1582 BinaryOperatorKind opcode,
1583 Expr *LHS, Expr *RHS) {
1584 // Do nothing if either argument is dependent.
1585 if (LHS->isTypeDependent() || RHS->isTypeDependent())
1586 return new (Context) BinaryOperator(LHS, RHS, opcode, Context.DependentTy,
Adam Nemet484aa452017-03-27 19:17:25 +00001587 VK_RValue, OK_Ordinary, opcLoc,
1588 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +00001589
1590 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001591 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1592 ExprResult result = CheckPlaceholderExpr(RHS);
1593 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001594 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001595 }
1596
1597 Expr *opaqueRef = LHS->IgnoreParens();
1598 if (ObjCPropertyRefExpr *refExpr
1599 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
1600 ObjCPropertyOpBuilder builder(*this, refExpr);
1601 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001602 } else if (ObjCSubscriptRefExpr *refExpr
1603 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
1604 ObjCSubscriptOpBuilder builder(*this, refExpr);
1605 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001606 } else if (MSPropertyRefExpr *refExpr
1607 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001608 MSPropertyOpBuilder builder(*this, refExpr);
1609 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1610 } else if (MSPropertySubscriptExpr *RefExpr
1611 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1612 MSPropertyOpBuilder Builder(*this, RefExpr);
1613 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001614 } else {
1615 llvm_unreachable("unknown pseudo-object kind!");
1616 }
1617}
John McCalle9290822011-11-30 04:42:31 +00001618
1619/// Given a pseudo-object reference, rebuild it without the opaque
1620/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1621/// This should never operate in-place.
1622static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001623 return Rebuilder(S,
1624 [=](Expr *E, unsigned) -> Expr * {
1625 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1626 })
1627 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001628}
1629
1630/// Given a pseudo-object expression, recreate what it looks like
1631/// syntactically without the attendant OpaqueValueExprs.
1632///
1633/// This is a hack which should be removed when TreeTransform is
1634/// capable of rebuilding a tree without stripping implicit
1635/// operations.
1636Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Aaron Ballmana5038552018-01-09 13:07:03 +00001637 Expr *syntax = E->getSyntacticForm();
1638 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1639 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1640 return new (Context) UnaryOperator(
1641 op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1642 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1643 } else if (CompoundAssignOperator *cop
1644 = dyn_cast<CompoundAssignOperator>(syntax)) {
1645 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001646 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
1647 return new (Context) CompoundAssignOperator(lhs, rhs, cop->getOpcode(),
1648 cop->getType(),
1649 cop->getValueKind(),
1650 cop->getObjectKind(),
1651 cop->getComputationLHSType(),
1652 cop->getComputationResultType(),
Adam Nemet484aa452017-03-27 19:17:25 +00001653 cop->getOperatorLoc(),
1654 FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001655 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1656 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1657 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
1658 return new (Context) BinaryOperator(lhs, rhs, bop->getOpcode(),
1659 bop->getType(), bop->getValueKind(),
1660 bop->getObjectKind(),
Adam Nemet484aa452017-03-27 19:17:25 +00001661 bop->getOperatorLoc(), FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001662 } else {
1663 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1664 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1665 }
1666}