blob: 1e2ba97edcc4239785b179855f0625315165e996 [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
143 SmallVector<Expr*, 8> assocs(numAssocs);
144 SmallVector<TypeSourceInfo*, 8> assocTypes(numAssocs);
145
146 for (unsigned i = 0; i != numAssocs; ++i) {
147 Expr *assoc = gse->getAssocExpr(i);
148 if (i == resultIndex) assoc = rebuild(assoc);
149 assocs[i] = assoc;
150 assocTypes[i] = gse->getAssocTypeSourceInfo(i);
151 }
152
153 return new (S.Context) GenericSelectionExpr(S.Context,
154 gse->getGenericLoc(),
155 gse->getControllingExpr(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000156 assocTypes,
157 assocs,
John McCallfe96e0b2011-11-06 09:01:30 +0000158 gse->getDefaultLoc(),
159 gse->getRParenLoc(),
160 gse->containsUnexpandedParameterPack(),
161 resultIndex);
162 }
163
Eli Friedman75807f22013-07-20 00:40:58 +0000164 if (ChooseExpr *ce = dyn_cast<ChooseExpr>(e)) {
165 assert(!ce->isConditionDependent());
166
167 Expr *LHS = ce->getLHS(), *RHS = ce->getRHS();
168 Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS;
169 rebuiltExpr = rebuild(rebuiltExpr);
170
171 return new (S.Context) ChooseExpr(ce->getBuiltinLoc(),
172 ce->getCond(),
173 LHS, RHS,
174 rebuiltExpr->getType(),
175 rebuiltExpr->getValueKind(),
176 rebuiltExpr->getObjectKind(),
177 ce->getRParenLoc(),
178 ce->isConditionTrue(),
179 rebuiltExpr->isTypeDependent(),
180 rebuiltExpr->isValueDependent());
181 }
182
John McCallfe96e0b2011-11-06 09:01:30 +0000183 llvm_unreachable("bad expression to rebuild!");
184 }
185 };
186
John McCallfe96e0b2011-11-06 09:01:30 +0000187 class PseudoOpBuilder {
188 public:
189 Sema &S;
190 unsigned ResultIndex;
191 SourceLocation GenericLoc;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000192 bool IsUnique;
John McCallfe96e0b2011-11-06 09:01:30 +0000193 SmallVector<Expr *, 4> Semantics;
194
Akira Hatanaka797afe32018-03-20 01:47:58 +0000195 PseudoOpBuilder(Sema &S, SourceLocation genericLoc, bool IsUnique)
John McCallfe96e0b2011-11-06 09:01:30 +0000196 : S(S), ResultIndex(PseudoObjectExpr::NoResult),
Akira Hatanaka797afe32018-03-20 01:47:58 +0000197 GenericLoc(genericLoc), IsUnique(IsUnique) {}
John McCallfe96e0b2011-11-06 09:01:30 +0000198
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);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000211 // An OVE is not unique if it is used as the result expression.
212 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
213 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000214 }
215
216 ExprResult buildRValueOperation(Expr *op);
217 ExprResult buildAssignmentOperation(Scope *Sc,
218 SourceLocation opLoc,
219 BinaryOperatorKind opcode,
220 Expr *LHS, Expr *RHS);
221 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
222 UnaryOperatorKind opcode,
223 Expr *op);
224
Jordan Rosed3934582012-09-28 22:21:30 +0000225 virtual ExprResult complete(Expr *syntacticForm);
John McCallfe96e0b2011-11-06 09:01:30 +0000226
227 OpaqueValueExpr *capture(Expr *op);
228 OpaqueValueExpr *captureValueAsResult(Expr *op);
229
230 void setResultToLastSemantic() {
231 assert(ResultIndex == PseudoObjectExpr::NoResult);
232 ResultIndex = Semantics.size() - 1;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000233 // An OVE is not unique if it is used as the result expression.
234 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantics.back()))
235 OVE->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000236 }
237
238 /// Return true if assignments have a non-void result.
Alexey Bataev60520e22015-12-10 04:38:18 +0000239 static bool CanCaptureValue(Expr *exp) {
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000240 if (exp->isGLValue())
241 return true;
242 QualType ty = exp->getType();
Eli Friedman00fa4292012-11-13 23:16:33 +0000243 assert(!ty->isIncompleteType());
244 assert(!ty->isDependentType());
245
246 if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl())
247 return ClassDecl->isTriviallyCopyable();
248 return true;
249 }
John McCallfe96e0b2011-11-06 09:01:30 +0000250
251 virtual Expr *rebuildAndCaptureObject(Expr *) = 0;
252 virtual ExprResult buildGet() = 0;
253 virtual ExprResult buildSet(Expr *, SourceLocation,
254 bool captureSetValueAsResult) = 0;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000255 /// Should the result of an assignment be the formal result of the
Alexey Bataev60520e22015-12-10 04:38:18 +0000256 /// setter call or the value that was passed to the setter?
257 ///
258 /// Different pseudo-object language features use different language rules
259 /// for this.
260 /// The default is to use the set value. Currently, this affects the
261 /// behavior of simple assignments, compound assignments, and prefix
262 /// increment and decrement.
263 /// Postfix increment and decrement always use the getter result as the
264 /// expression result.
265 ///
266 /// If this method returns true, and the set value isn't capturable for
267 /// some reason, the result of the expression will be void.
268 virtual bool captureSetValueAsResult() const { return true; }
John McCallfe96e0b2011-11-06 09:01:30 +0000269 };
270
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +0000271 /// A PseudoOpBuilder for Objective-C \@properties.
John McCallfe96e0b2011-11-06 09:01:30 +0000272 class ObjCPropertyOpBuilder : public PseudoOpBuilder {
273 ObjCPropertyRefExpr *RefExpr;
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000274 ObjCPropertyRefExpr *SyntacticRefExpr;
John McCallfe96e0b2011-11-06 09:01:30 +0000275 OpaqueValueExpr *InstanceReceiver;
276 ObjCMethodDecl *Getter;
277
278 ObjCMethodDecl *Setter;
279 Selector SetterSelector;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000280 Selector GetterSelector;
John McCallfe96e0b2011-11-06 09:01:30 +0000281
282 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000283 ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr, bool IsUnique)
284 : PseudoOpBuilder(S, refExpr->getLocation(), IsUnique),
285 RefExpr(refExpr), SyntacticRefExpr(nullptr),
286 InstanceReceiver(nullptr), Getter(nullptr), Setter(nullptr) {
John McCallfe96e0b2011-11-06 09:01:30 +0000287 }
288
289 ExprResult buildRValueOperation(Expr *op);
290 ExprResult buildAssignmentOperation(Scope *Sc,
291 SourceLocation opLoc,
292 BinaryOperatorKind opcode,
293 Expr *LHS, Expr *RHS);
294 ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
295 UnaryOperatorKind opcode,
296 Expr *op);
297
298 bool tryBuildGetOfReference(Expr *op, ExprResult &result);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000299 bool findSetter(bool warn=true);
John McCallfe96e0b2011-11-06 09:01:30 +0000300 bool findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000301 void DiagnoseUnsupportedPropertyUse();
John McCallfe96e0b2011-11-06 09:01:30 +0000302
Craig Toppere14c0f82014-03-12 04:55:44 +0000303 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
304 ExprResult buildGet() override;
305 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
306 ExprResult complete(Expr *SyntacticForm) override;
Jordan Rosed3934582012-09-28 22:21:30 +0000307
308 bool isWeakProperty() const;
John McCallfe96e0b2011-11-06 09:01:30 +0000309 };
Ted Kremeneke65b0862012-03-06 20:05:56 +0000310
311 /// A PseudoOpBuilder for Objective-C array/dictionary indexing.
312 class ObjCSubscriptOpBuilder : public PseudoOpBuilder {
313 ObjCSubscriptRefExpr *RefExpr;
314 OpaqueValueExpr *InstanceBase;
315 OpaqueValueExpr *InstanceKey;
316 ObjCMethodDecl *AtIndexGetter;
317 Selector AtIndexGetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000318
Ted Kremeneke65b0862012-03-06 20:05:56 +0000319 ObjCMethodDecl *AtIndexSetter;
320 Selector AtIndexSetterSelector;
Fangrui Song6907ce22018-07-30 19:24:48 +0000321
Ted Kremeneke65b0862012-03-06 20:05:56 +0000322 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000323 ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr, bool IsUnique)
324 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
325 RefExpr(refExpr), InstanceBase(nullptr), InstanceKey(nullptr),
326 AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
Craig Topperc3ec1492014-05-26 06:22:03 +0000327
Ted Kremeneke65b0862012-03-06 20:05:56 +0000328 ExprResult buildRValueOperation(Expr *op);
329 ExprResult buildAssignmentOperation(Scope *Sc,
330 SourceLocation opLoc,
331 BinaryOperatorKind opcode,
332 Expr *LHS, Expr *RHS);
Craig Toppere14c0f82014-03-12 04:55:44 +0000333 Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
334
Ted Kremeneke65b0862012-03-06 20:05:56 +0000335 bool findAtIndexGetter();
336 bool findAtIndexSetter();
Craig Toppere14c0f82014-03-12 04:55:44 +0000337
338 ExprResult buildGet() override;
339 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Ted Kremeneke65b0862012-03-06 20:05:56 +0000340 };
341
John McCall5e77d762013-04-16 07:28:30 +0000342 class MSPropertyOpBuilder : public PseudoOpBuilder {
343 MSPropertyRefExpr *RefExpr;
Alexey Bataev69103472015-10-14 04:05:42 +0000344 OpaqueValueExpr *InstanceBase;
Alexey Bataevf7630272015-11-25 12:01:00 +0000345 SmallVector<Expr *, 4> CallArgs;
346
347 MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
John McCall5e77d762013-04-16 07:28:30 +0000348
349 public:
Akira Hatanaka797afe32018-03-20 01:47:58 +0000350 MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr, bool IsUnique)
351 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
352 RefExpr(refExpr), InstanceBase(nullptr) {}
353 MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr, bool IsUnique)
354 : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique),
Alexey Bataevf7630272015-11-25 12:01:00 +0000355 InstanceBase(nullptr) {
356 RefExpr = getBaseMSProperty(refExpr);
357 }
John McCall5e77d762013-04-16 07:28:30 +0000358
Craig Toppere14c0f82014-03-12 04:55:44 +0000359 Expr *rebuildAndCaptureObject(Expr *) override;
360 ExprResult buildGet() override;
361 ExprResult buildSet(Expr *op, SourceLocation, bool) override;
Alexey Bataev60520e22015-12-10 04:38:18 +0000362 bool captureSetValueAsResult() const override { return false; }
John McCall5e77d762013-04-16 07:28:30 +0000363 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000364}
John McCallfe96e0b2011-11-06 09:01:30 +0000365
366/// Capture the given expression in an OpaqueValueExpr.
367OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
368 // Make a new OVE whose source is the given expression.
Fangrui Song6907ce22018-07-30 19:24:48 +0000369 OpaqueValueExpr *captured =
John McCallfe96e0b2011-11-06 09:01:30 +0000370 new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
Douglas Gregor2d5aea02012-02-23 22:17:26 +0000371 e->getValueKind(), e->getObjectKind(),
372 e);
Akira Hatanaka797afe32018-03-20 01:47:58 +0000373 if (IsUnique)
374 captured->setIsUnique(true);
375
John McCallfe96e0b2011-11-06 09:01:30 +0000376 // Make sure we bind that in the semantics.
377 addSemanticExpr(captured);
378 return captured;
379}
380
381/// Capture the given expression as the result of this pseudo-object
382/// operation. This routine is safe against expressions which may
383/// already be captured.
384///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000385/// \returns the captured expression, which will be the
John McCallfe96e0b2011-11-06 09:01:30 +0000386/// same as the input if the input was already captured
387OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
388 assert(ResultIndex == PseudoObjectExpr::NoResult);
389
390 // If the expression hasn't already been captured, just capture it
Fangrui Song6907ce22018-07-30 19:24:48 +0000391 // and set the new semantic
John McCallfe96e0b2011-11-06 09:01:30 +0000392 if (!isa<OpaqueValueExpr>(e)) {
393 OpaqueValueExpr *cap = capture(e);
394 setResultToLastSemantic();
395 return cap;
396 }
397
398 // Otherwise, it must already be one of our semantic expressions;
399 // set ResultIndex to its index.
400 unsigned index = 0;
401 for (;; ++index) {
402 assert(index < Semantics.size() &&
403 "captured expression not found in semantics!");
404 if (e == Semantics[index]) break;
405 }
406 ResultIndex = index;
Akira Hatanaka797afe32018-03-20 01:47:58 +0000407 // An OVE is not unique if it is used as the result expression.
408 cast<OpaqueValueExpr>(e)->setIsUnique(false);
John McCallfe96e0b2011-11-06 09:01:30 +0000409 return cast<OpaqueValueExpr>(e);
410}
411
412/// The routine which creates the final PseudoObjectExpr.
413ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
414 return PseudoObjectExpr::Create(S.Context, syntactic,
415 Semantics, ResultIndex);
416}
417
418/// The main skeleton for building an r-value operation.
419ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
420 Expr *syntacticBase = rebuildAndCaptureObject(op);
421
422 ExprResult getExpr = buildGet();
423 if (getExpr.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000424 addResultSemanticExpr(getExpr.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000425
426 return complete(syntacticBase);
427}
428
429/// The basic skeleton for building a simple or compound
430/// assignment operation.
431ExprResult
432PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
433 BinaryOperatorKind opcode,
434 Expr *LHS, Expr *RHS) {
435 assert(BinaryOperator::isAssignmentOp(opcode));
436
437 Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
438 OpaqueValueExpr *capturedRHS = capture(RHS);
439
John McCallee04aeb2015-08-22 00:35:27 +0000440 // In some very specific cases, semantic analysis of the RHS as an
441 // expression may require it to be rewritten. In these cases, we
442 // cannot safely keep the OVE around. Fortunately, we don't really
443 // need to: we don't use this particular OVE in multiple places, and
444 // no clients rely that closely on matching up expressions in the
445 // semantic expression with expressions from the syntactic form.
446 Expr *semanticRHS = capturedRHS;
447 if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
448 semanticRHS = RHS;
449 Semantics.pop_back();
450 }
451
John McCallfe96e0b2011-11-06 09:01:30 +0000452 Expr *syntactic;
453
454 ExprResult result;
455 if (opcode == BO_Assign) {
John McCallee04aeb2015-08-22 00:35:27 +0000456 result = semanticRHS;
John McCallfe96e0b2011-11-06 09:01:30 +0000457 syntactic = new (S.Context) BinaryOperator(syntacticLHS, capturedRHS,
458 opcode, capturedRHS->getType(),
459 capturedRHS->getValueKind(),
Adam Nemet484aa452017-03-27 19:17:25 +0000460 OK_Ordinary, opcLoc,
461 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000462 } else {
463 ExprResult opLHS = buildGet();
464 if (opLHS.isInvalid()) return ExprError();
465
466 // Build an ordinary, non-compound operation.
467 BinaryOperatorKind nonCompound =
468 BinaryOperator::getOpForCompoundAssignment(opcode);
John McCallee04aeb2015-08-22 00:35:27 +0000469 result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
John McCallfe96e0b2011-11-06 09:01:30 +0000470 if (result.isInvalid()) return ExprError();
471
472 syntactic =
473 new (S.Context) CompoundAssignOperator(syntacticLHS, capturedRHS, opcode,
474 result.get()->getType(),
475 result.get()->getValueKind(),
476 OK_Ordinary,
477 opLHS.get()->getType(),
478 result.get()->getType(),
Adam Nemet484aa452017-03-27 19:17:25 +0000479 opcLoc, FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +0000480 }
481
482 // The result of the assignment, if not void, is the value set into
483 // the l-value.
Alexey Bataev60520e22015-12-10 04:38:18 +0000484 result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000485 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000486 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000487 if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
488 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
489 setResultToLastSemantic();
John McCallfe96e0b2011-11-06 09:01:30 +0000490
491 return complete(syntactic);
492}
493
494/// The basic skeleton for building an increment or decrement
495/// operation.
496ExprResult
497PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
498 UnaryOperatorKind opcode,
499 Expr *op) {
500 assert(UnaryOperator::isIncrementDecrementOp(opcode));
501
502 Expr *syntacticOp = rebuildAndCaptureObject(op);
503
504 // Load the value.
505 ExprResult result = buildGet();
506 if (result.isInvalid()) return ExprError();
507
508 QualType resultType = result.get()->getType();
509
510 // That's the postfix result.
John McCall0d9dd732013-04-16 22:32:04 +0000511 if (UnaryOperator::isPostfix(opcode) &&
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000512 (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000513 result = capture(result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000514 setResultToLastSemantic();
515 }
516
517 // Add or subtract a literal 1.
518 llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
519 Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
520 GenericLoc);
521
522 if (UnaryOperator::isIncrementOp(opcode)) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000523 result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000524 } else {
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000525 result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
John McCallfe96e0b2011-11-06 09:01:30 +0000526 }
527 if (result.isInvalid()) return ExprError();
528
529 // Store that back into the result. The value stored is the result
530 // of a prefix operation.
Alexey Bataev60520e22015-12-10 04:38:18 +0000531 result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
532 captureSetValueAsResult());
John McCallfe96e0b2011-11-06 09:01:30 +0000533 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000534 addSemanticExpr(result.get());
Alexey Bataev60520e22015-12-10 04:38:18 +0000535 if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
536 !result.get()->getType()->isVoidType() &&
Malcolm Parsonsfab36802018-04-16 08:31:08 +0000537 (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
538 setResultToLastSemantic();
539
540 UnaryOperator *syntactic = new (S.Context) UnaryOperator(
541 syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
542 !resultType->isDependentType()
543 ? S.Context.getTypeSize(resultType) >=
544 S.Context.getTypeSize(S.Context.IntTy)
545 : false);
546 return complete(syntactic);
547}
548
John McCallfe96e0b2011-11-06 09:01:30 +0000549
550//===----------------------------------------------------------------------===//
551// Objective-C @property and implicit property references
552//===----------------------------------------------------------------------===//
553
554/// Look up a method in the receiver type of an Objective-C property
555/// reference.
John McCall526ab472011-10-25 17:37:35 +0000556static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel,
557 const ObjCPropertyRefExpr *PRE) {
John McCall526ab472011-10-25 17:37:35 +0000558 if (PRE->isObjectReceiver()) {
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000559 const ObjCObjectPointerType *PT =
560 PRE->getBase()->getType()->castAs<ObjCObjectPointerType>();
John McCallfe96e0b2011-11-06 09:01:30 +0000561
562 // Special case for 'self' in class method implementations.
563 if (PT->isObjCClassType() &&
564 S.isSelfExpr(const_cast<Expr*>(PRE->getBase()))) {
565 // This cast is safe because isSelfExpr is only true within
566 // methods.
567 ObjCMethodDecl *method =
568 cast<ObjCMethodDecl>(S.CurContext->getNonClosureAncestor());
569 return S.LookupMethodInObjectType(sel,
570 S.Context.getObjCInterfaceType(method->getClassInterface()),
571 /*instance*/ false);
572 }
573
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000574 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
John McCall526ab472011-10-25 17:37:35 +0000575 }
576
Benjamin Kramer8dc57602011-10-28 13:21:18 +0000577 if (PRE->isSuperReceiver()) {
578 if (const ObjCObjectPointerType *PT =
579 PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>())
580 return S.LookupMethodInObjectType(sel, PT->getPointeeType(), true);
581
582 return S.LookupMethodInObjectType(sel, PRE->getSuperReceiverType(), false);
583 }
584
585 assert(PRE->isClassReceiver() && "Invalid expression");
586 QualType IT = S.Context.getObjCInterfaceType(PRE->getClassReceiver());
587 return S.LookupMethodInObjectType(sel, IT, false);
John McCall526ab472011-10-25 17:37:35 +0000588}
589
Jordan Rosed3934582012-09-28 22:21:30 +0000590bool ObjCPropertyOpBuilder::isWeakProperty() const {
591 QualType T;
592 if (RefExpr->isExplicitProperty()) {
593 const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty();
594 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
Bob Wilsonf4f54e32016-05-25 05:41:57 +0000595 return true;
Jordan Rosed3934582012-09-28 22:21:30 +0000596
597 T = Prop->getType();
598 } else if (Getter) {
Alp Toker314cc812014-01-25 16:55:45 +0000599 T = Getter->getReturnType();
Jordan Rosed3934582012-09-28 22:21:30 +0000600 } else {
601 return false;
602 }
603
604 return T.getObjCLifetime() == Qualifiers::OCL_Weak;
605}
606
John McCallfe96e0b2011-11-06 09:01:30 +0000607bool ObjCPropertyOpBuilder::findGetter() {
608 if (Getter) return true;
John McCall526ab472011-10-25 17:37:35 +0000609
John McCallcfef5462011-11-07 22:49:50 +0000610 // For implicit properties, just trust the lookup we already did.
611 if (RefExpr->isImplicitProperty()) {
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000612 if ((Getter = RefExpr->getImplicitPropertyGetter())) {
613 GetterSelector = Getter->getSelector();
614 return true;
615 }
616 else {
617 // Must build the getter selector the hard way.
618 ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter();
619 assert(setter && "both setter and getter are null - cannot happen");
Fangrui Song6907ce22018-07-30 19:24:48 +0000620 IdentifierInfo *setterName =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000621 setter->getSelector().getIdentifierInfoForSlot(0);
Alp Toker541d5072014-06-07 23:30:53 +0000622 IdentifierInfo *getterName =
623 &S.Context.Idents.get(setterName->getName().substr(3));
Fangrui Song6907ce22018-07-30 19:24:48 +0000624 GetterSelector =
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000625 S.PP.getSelectorTable().getNullarySelector(getterName);
626 return false;
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000627 }
John McCallcfef5462011-11-07 22:49:50 +0000628 }
629
630 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
631 Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
Craig Topperc3ec1492014-05-26 06:22:03 +0000632 return (Getter != nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000633}
634
635/// Try to find the most accurate setter declaration for the property
636/// reference.
637///
Fangrui Song6907ce22018-07-30 19:24:48 +0000638/// \return true if a setter was found, in which case Setter
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000639bool ObjCPropertyOpBuilder::findSetter(bool warn) {
John McCallfe96e0b2011-11-06 09:01:30 +0000640 // For implicit properties, just trust the lookup we already did.
641 if (RefExpr->isImplicitProperty()) {
642 if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) {
643 Setter = setter;
644 SetterSelector = setter->getSelector();
645 return true;
John McCall526ab472011-10-25 17:37:35 +0000646 } else {
John McCallfe96e0b2011-11-06 09:01:30 +0000647 IdentifierInfo *getterName =
648 RefExpr->getImplicitPropertyGetter()->getSelector()
649 .getIdentifierInfoForSlot(0);
650 SetterSelector =
Adrian Prantla4ce9062013-06-07 22:29:12 +0000651 SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
652 S.PP.getSelectorTable(),
653 getterName);
John McCallfe96e0b2011-11-06 09:01:30 +0000654 return false;
John McCall526ab472011-10-25 17:37:35 +0000655 }
John McCallfe96e0b2011-11-06 09:01:30 +0000656 }
657
658 // For explicit properties, this is more involved.
659 ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
660 SetterSelector = prop->getSetterName();
661
662 // Do a normal method lookup first.
663 if (ObjCMethodDecl *setter =
664 LookupMethodInReceiverType(S, SetterSelector, RefExpr)) {
Jordan Rosed01e83a2012-10-10 16:42:25 +0000665 if (setter->isPropertyAccessor() && warn)
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000666 if (const ObjCInterfaceDecl *IFace =
667 dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) {
Craig Topperbf3e3272014-08-30 16:55:52 +0000668 StringRef thisPropertyName = prop->getName();
Jordan Rosea7d03842013-02-08 22:30:41 +0000669 // Try flipping the case of the first character.
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000670 char front = thisPropertyName.front();
Jordan Rosea7d03842013-02-08 22:30:41 +0000671 front = isLowercase(front) ? toUppercase(front) : toLowercase(front);
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000672 SmallString<100> PropertyName = thisPropertyName;
673 PropertyName[0] = front;
674 IdentifierInfo *AltMember = &S.PP.getIdentifierTable().get(PropertyName);
Manman Ren5b786402016-01-28 18:49:28 +0000675 if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration(
676 AltMember, prop->getQueryKind()))
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000677 if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) {
Richard Smithf8812672016-12-02 22:38:31 +0000678 S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000679 << prop << prop1 << setter->getSelector();
Fariborz Jahanian3f88afa2012-05-24 22:48:38 +0000680 S.Diag(prop->getLocation(), diag::note_property_declare);
681 S.Diag(prop1->getLocation(), diag::note_property_declare);
682 }
683 }
John McCallfe96e0b2011-11-06 09:01:30 +0000684 Setter = setter;
685 return true;
686 }
687
688 // That can fail in the somewhat crazy situation that we're
689 // type-checking a message send within the @interface declaration
690 // that declared the @property. But it's not clear that that's
691 // valuable to support.
692
693 return false;
694}
695
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000696void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() {
Fariborz Jahanian55513282014-05-28 18:12:10 +0000697 if (S.getCurLexicalContext()->isObjCContainer() &&
698 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl &&
699 S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) {
700 if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) {
701 S.Diag(RefExpr->getLocation(),
702 diag::err_property_function_in_objc_container);
703 S.Diag(prop->getLocation(), diag::note_property_declare);
Fariborz Jahanian55513282014-05-28 18:12:10 +0000704 }
705 }
Fariborz Jahanian55513282014-05-28 18:12:10 +0000706}
707
John McCallfe96e0b2011-11-06 09:01:30 +0000708/// Capture the base object of an Objective-C property expression.
709Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000710 assert(InstanceReceiver == nullptr);
John McCallfe96e0b2011-11-06 09:01:30 +0000711
712 // If we have a base, capture it in an OVE and rebuild the syntactic
713 // form to use the OVE as its base.
714 if (RefExpr->isObjectReceiver()) {
715 InstanceReceiver = capture(RefExpr->getBase());
Alexey Bataevf7630272015-11-25 12:01:00 +0000716 syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * {
717 return InstanceReceiver;
718 }).rebuild(syntacticBase);
John McCallfe96e0b2011-11-06 09:01:30 +0000719 }
720
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000721 if (ObjCPropertyRefExpr *
722 refE = dyn_cast<ObjCPropertyRefExpr>(syntacticBase->IgnoreParens()))
723 SyntacticRefExpr = refE;
724
John McCallfe96e0b2011-11-06 09:01:30 +0000725 return syntacticBase;
726}
727
728/// Load from an Objective-C property reference.
729ExprResult ObjCPropertyOpBuilder::buildGet() {
730 findGetter();
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000731 if (!Getter) {
732 DiagnoseUnsupportedPropertyUse();
733 return ExprError();
734 }
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000735
736 if (SyntacticRefExpr)
737 SyntacticRefExpr->setIsMessagingGetter();
738
Douglas Gregore83b9562015-07-07 03:57:53 +0000739 QualType receiverType = RefExpr->getReceiverType(S.Context);
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000740 if (!Getter->isImplicit())
741 S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true);
John McCallfe96e0b2011-11-06 09:01:30 +0000742 // Build a message-send.
743 ExprResult msg;
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000744 if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
745 RefExpr->isObjectReceiver()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000746 assert(InstanceReceiver || RefExpr->isSuperReceiver());
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000747 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
748 GenericLoc, Getter->getSelector(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000749 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000750 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000751 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
Dmitri Gribenko78852e92013-05-05 20:40:26 +0000752 GenericLoc, Getter->getSelector(),
753 Getter, None);
John McCallfe96e0b2011-11-06 09:01:30 +0000754 }
755 return msg;
756}
John McCall526ab472011-10-25 17:37:35 +0000757
John McCallfe96e0b2011-11-06 09:01:30 +0000758/// Store to an Objective-C property reference.
759///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +0000760/// \param captureSetValueAsResult If true, capture the actual
John McCallfe96e0b2011-11-06 09:01:30 +0000761/// value being set as the value of the property operation.
762ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
763 bool captureSetValueAsResult) {
Olivier Goffartf6fabcc2014-08-04 17:28:11 +0000764 if (!findSetter(false)) {
765 DiagnoseUnsupportedPropertyUse();
766 return ExprError();
767 }
John McCallfe96e0b2011-11-06 09:01:30 +0000768
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +0000769 if (SyntacticRefExpr)
770 SyntacticRefExpr->setIsMessagingSetter();
771
Douglas Gregore83b9562015-07-07 03:57:53 +0000772 QualType receiverType = RefExpr->getReceiverType(S.Context);
John McCallfe96e0b2011-11-06 09:01:30 +0000773
774 // Use assignment constraints when possible; they give us better
775 // diagnostics. "When possible" basically means anything except a
776 // C++ class type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000777 if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000778 QualType paramType = (*Setter->param_begin())->getType()
779 .substObjCMemberType(
780 receiverType,
781 Setter->getDeclContext(),
782 ObjCSubstitutionContext::Parameter);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000783 if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000784 ExprResult opResult = op;
785 Sema::AssignConvertType assignResult
786 = S.CheckSingleAssignmentConstraints(paramType, opResult);
Richard Smithe15a3702016-10-06 23:12:58 +0000787 if (opResult.isInvalid() ||
788 S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType,
John McCallfe96e0b2011-11-06 09:01:30 +0000789 op->getType(), opResult.get(),
790 Sema::AA_Assigning))
791 return ExprError();
792
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000793 op = opResult.get();
John McCallfe96e0b2011-11-06 09:01:30 +0000794 assert(op && "successful assignment left argument invalid?");
John McCall526ab472011-10-25 17:37:35 +0000795 }
796 }
797
John McCallfe96e0b2011-11-06 09:01:30 +0000798 // Arguments.
799 Expr *args[] = { op };
John McCall526ab472011-10-25 17:37:35 +0000800
John McCallfe96e0b2011-11-06 09:01:30 +0000801 // Build a message-send.
802 ExprResult msg;
Fariborz Jahanian89ea9612014-06-16 17:25:41 +0000803 if (!Setter->isImplicit())
804 S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true);
Fariborz Jahanian29cdbc62014-04-21 20:22:17 +0000805 if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) ||
806 RefExpr->isObjectReceiver()) {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000807 msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType,
808 GenericLoc, SetterSelector, Setter,
809 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000810 } else {
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +0000811 msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(),
812 GenericLoc,
813 SetterSelector, Setter,
814 MultiExprArg(args, 1));
John McCallfe96e0b2011-11-06 09:01:30 +0000815 }
816
817 if (!msg.isInvalid() && captureSetValueAsResult) {
818 ObjCMessageExpr *msgExpr =
819 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
820 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +0000821 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +0000822 msgExpr->setArg(0, captureValueAsResult(arg));
John McCallfe96e0b2011-11-06 09:01:30 +0000823 }
824
825 return msg;
John McCall526ab472011-10-25 17:37:35 +0000826}
827
John McCallfe96e0b2011-11-06 09:01:30 +0000828/// @property-specific behavior for doing lvalue-to-rvalue conversion.
829ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) {
830 // Explicit properties always have getters, but implicit ones don't.
831 // Check that before proceeding.
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000832 if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) {
John McCallfe96e0b2011-11-06 09:01:30 +0000833 S.Diag(RefExpr->getLocation(), diag::err_getter_not_found)
Eli Friedmanfd41aee2012-11-29 03:13:49 +0000834 << RefExpr->getSourceRange();
John McCall526ab472011-10-25 17:37:35 +0000835 return ExprError();
836 }
837
John McCallfe96e0b2011-11-06 09:01:30 +0000838 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
John McCall526ab472011-10-25 17:37:35 +0000839 if (result.isInvalid()) return ExprError();
840
John McCallfe96e0b2011-11-06 09:01:30 +0000841 if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType())
842 S.DiagnosePropertyAccessorMismatch(RefExpr->getExplicitProperty(),
843 Getter, RefExpr->getLocation());
844
845 // As a special case, if the method returns 'id', try to get
846 // a better type from the property.
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000847 if (RefExpr->isExplicitProperty() && result.get()->isRValue()) {
Douglas Gregore83b9562015-07-07 03:57:53 +0000848 QualType receiverType = RefExpr->getReceiverType(S.Context);
849 QualType propType = RefExpr->getExplicitProperty()
850 ->getUsageType(receiverType);
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000851 if (result.get()->getType()->isObjCIdType()) {
852 if (const ObjCObjectPointerType *ptr
853 = propType->getAs<ObjCObjectPointerType>()) {
854 if (!ptr->isObjCIdType())
855 result = S.ImpCastExprToType(result.get(), propType, CK_BitCast);
856 }
857 }
Brian Kelleycafd9122017-03-29 17:55:11 +0000858 if (propType.getObjCLifetime() == Qualifiers::OCL_Weak &&
859 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
860 RefExpr->getLocation()))
861 S.getCurFunction()->markSafeWeakUse(RefExpr);
John McCallfe96e0b2011-11-06 09:01:30 +0000862 }
863
John McCall526ab472011-10-25 17:37:35 +0000864 return result;
865}
866
John McCallfe96e0b2011-11-06 09:01:30 +0000867/// Try to build this as a call to a getter that returns a reference.
868///
869/// \return true if it was possible, whether or not it actually
870/// succeeded
871bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op,
872 ExprResult &result) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000873 if (!S.getLangOpts().CPlusPlus) return false;
John McCallfe96e0b2011-11-06 09:01:30 +0000874
875 findGetter();
Olivier Goffart4c182c82014-08-04 17:28:05 +0000876 if (!Getter) {
877 // The property has no setter and no getter! This can happen if the type is
878 // invalid. Error have already been reported.
879 result = ExprError();
880 return true;
881 }
John McCallfe96e0b2011-11-06 09:01:30 +0000882
883 // Only do this if the getter returns an l-value reference type.
Alp Toker314cc812014-01-25 16:55:45 +0000884 QualType resultType = Getter->getReturnType();
John McCallfe96e0b2011-11-06 09:01:30 +0000885 if (!resultType->isLValueReferenceType()) return false;
886
887 result = buildRValueOperation(op);
888 return true;
889}
890
891/// @property-specific behavior for doing assignments.
892ExprResult
893ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc,
894 SourceLocation opcLoc,
895 BinaryOperatorKind opcode,
896 Expr *LHS, Expr *RHS) {
John McCall526ab472011-10-25 17:37:35 +0000897 assert(BinaryOperator::isAssignmentOp(opcode));
John McCall526ab472011-10-25 17:37:35 +0000898
899 // If there's no setter, we have no choice but to try to assign to
900 // the result of the getter.
John McCallfe96e0b2011-11-06 09:01:30 +0000901 if (!findSetter()) {
902 ExprResult result;
903 if (tryBuildGetOfReference(LHS, result)) {
904 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000905 return S.BuildBinOp(Sc, opcLoc, opcode, result.get(), RHS);
John McCall526ab472011-10-25 17:37:35 +0000906 }
907
908 // Otherwise, it's an error.
John McCallfe96e0b2011-11-06 09:01:30 +0000909 S.Diag(opcLoc, diag::err_nosetter_property_assignment)
910 << unsigned(RefExpr->isImplicitProperty())
911 << SetterSelector
John McCall526ab472011-10-25 17:37:35 +0000912 << LHS->getSourceRange() << RHS->getSourceRange();
913 return ExprError();
914 }
915
916 // If there is a setter, we definitely want to use it.
917
John McCallfe96e0b2011-11-06 09:01:30 +0000918 // Verify that we can do a compound assignment.
919 if (opcode != BO_Assign && !findGetter()) {
920 S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment)
John McCall526ab472011-10-25 17:37:35 +0000921 << LHS->getSourceRange() << RHS->getSourceRange();
922 return ExprError();
923 }
924
John McCallfe96e0b2011-11-06 09:01:30 +0000925 ExprResult result =
926 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
John McCall526ab472011-10-25 17:37:35 +0000927 if (result.isInvalid()) return ExprError();
928
John McCallfe96e0b2011-11-06 09:01:30 +0000929 // Various warnings about property assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000930 if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) {
John McCallfe96e0b2011-11-06 09:01:30 +0000931 S.checkRetainCycles(InstanceReceiver->getSourceExpr(), RHS);
932 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
933 }
934
John McCall526ab472011-10-25 17:37:35 +0000935 return result;
936}
John McCallfe96e0b2011-11-06 09:01:30 +0000937
938/// @property-specific behavior for doing increments and decrements.
939ExprResult
940ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
941 UnaryOperatorKind opcode,
942 Expr *op) {
943 // If there's no setter, we have no choice but to try to assign to
944 // the result of the getter.
945 if (!findSetter()) {
946 ExprResult result;
947 if (tryBuildGetOfReference(op, result)) {
948 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000949 return S.BuildUnaryOp(Sc, opcLoc, opcode, result.get());
John McCallfe96e0b2011-11-06 09:01:30 +0000950 }
951
952 // Otherwise, it's an error.
953 S.Diag(opcLoc, diag::err_nosetter_property_incdec)
954 << unsigned(RefExpr->isImplicitProperty())
955 << unsigned(UnaryOperator::isDecrementOp(opcode))
956 << SetterSelector
957 << op->getSourceRange();
958 return ExprError();
959 }
960
961 // If there is a setter, we definitely want to use it.
962
963 // We also need a getter.
964 if (!findGetter()) {
965 assert(RefExpr->isImplicitProperty());
966 S.Diag(opcLoc, diag::err_nogetter_property_incdec)
967 << unsigned(UnaryOperator::isDecrementOp(opcode))
Fariborz Jahanianb525b522012-04-18 19:13:23 +0000968 << GetterSelector
John McCallfe96e0b2011-11-06 09:01:30 +0000969 << op->getSourceRange();
970 return ExprError();
971 }
972
973 return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op);
974}
975
Jordan Rosed3934582012-09-28 22:21:30 +0000976ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) {
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000977 if (isWeakProperty() && !S.isUnevaluatedContext() &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000978 !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000979 SyntacticForm->getBeginLoc()))
Reid Kleckner04f9bca2018-03-07 22:48:35 +0000980 S.getCurFunction()->recordUseOfWeak(SyntacticRefExpr,
981 SyntacticRefExpr->isMessagingGetter());
Jordan Rosed3934582012-09-28 22:21:30 +0000982
983 return PseudoOpBuilder::complete(SyntacticForm);
984}
985
Ted Kremeneke65b0862012-03-06 20:05:56 +0000986// ObjCSubscript build stuff.
987//
988
Fangrui Song6907ce22018-07-30 19:24:48 +0000989/// objective-c subscripting-specific behavior for doing lvalue-to-rvalue
Ted Kremeneke65b0862012-03-06 20:05:56 +0000990/// conversion.
Fangrui Song6907ce22018-07-30 19:24:48 +0000991/// FIXME. Remove this routine if it is proven that no additional
Ted Kremeneke65b0862012-03-06 20:05:56 +0000992/// specifity is needed.
993ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) {
994 ExprResult result = PseudoOpBuilder::buildRValueOperation(op);
995 if (result.isInvalid()) return ExprError();
996 return result;
997}
998
999/// objective-c subscripting-specific behavior for doing assignments.
1000ExprResult
1001ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc,
1002 SourceLocation opcLoc,
1003 BinaryOperatorKind opcode,
1004 Expr *LHS, Expr *RHS) {
1005 assert(BinaryOperator::isAssignmentOp(opcode));
1006 // There must be a method to do the Index'ed assignment.
1007 if (!findAtIndexSetter())
1008 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001009
Ted Kremeneke65b0862012-03-06 20:05:56 +00001010 // Verify that we can do a compound assignment.
1011 if (opcode != BO_Assign && !findAtIndexGetter())
1012 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Ted Kremeneke65b0862012-03-06 20:05:56 +00001014 ExprResult result =
1015 PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS);
1016 if (result.isInvalid()) return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001017
Ted Kremeneke65b0862012-03-06 20:05:56 +00001018 // Various warnings about objc Index'ed assignments in ARC.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001019 if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001020 S.checkRetainCycles(InstanceBase->getSourceExpr(), RHS);
1021 S.checkUnsafeExprAssigns(opcLoc, LHS, RHS);
1022 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001023
Ted Kremeneke65b0862012-03-06 20:05:56 +00001024 return result;
1025}
1026
1027/// Capture the base object of an Objective-C Index'ed expression.
1028Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001029 assert(InstanceBase == nullptr);
1030
Ted Kremeneke65b0862012-03-06 20:05:56 +00001031 // Capture base expression in an OVE and rebuild the syntactic
1032 // form to use the OVE as its base expression.
1033 InstanceBase = capture(RefExpr->getBaseExpr());
1034 InstanceKey = capture(RefExpr->getKeyExpr());
Alexey Bataevf7630272015-11-25 12:01:00 +00001035
Ted Kremeneke65b0862012-03-06 20:05:56 +00001036 syntacticBase =
Alexey Bataevf7630272015-11-25 12:01:00 +00001037 Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1038 switch (Idx) {
1039 case 0:
1040 return InstanceBase;
1041 case 1:
1042 return InstanceKey;
1043 default:
1044 llvm_unreachable("Unexpected index for ObjCSubscriptExpr");
1045 }
1046 }).rebuild(syntacticBase);
1047
Ted Kremeneke65b0862012-03-06 20:05:56 +00001048 return syntacticBase;
1049}
1050
Fangrui Song6907ce22018-07-30 19:24:48 +00001051/// CheckSubscriptingKind - This routine decide what type
Ted Kremeneke65b0862012-03-06 20:05:56 +00001052/// of indexing represented by "FromE" is being done.
Fangrui Song6907ce22018-07-30 19:24:48 +00001053Sema::ObjCSubscriptKind
Ted Kremeneke65b0862012-03-06 20:05:56 +00001054 Sema::CheckSubscriptingKind(Expr *FromE) {
1055 // If the expression already has integral or enumeration type, we're golden.
1056 QualType T = FromE->getType();
1057 if (T->isIntegralOrEnumerationType())
1058 return OS_Array;
Fangrui Song6907ce22018-07-30 19:24:48 +00001059
Ted Kremeneke65b0862012-03-06 20:05:56 +00001060 // If we don't have a class type in C++, there's no way we can get an
1061 // expression of integral or enumeration type.
1062 const RecordType *RecordTy = T->getAs<RecordType>();
Fariborz Jahaniand13951f2014-09-10 20:55:31 +00001063 if (!RecordTy &&
1064 (T->isObjCObjectPointerType() || T->isVoidPointerType()))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001065 // All other scalar cases are assumed to be dictionary indexing which
1066 // caller handles, with diagnostics if needed.
1067 return OS_Dictionary;
Fangrui Song6907ce22018-07-30 19:24:48 +00001068 if (!getLangOpts().CPlusPlus ||
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001069 !RecordTy || RecordTy->isIncompleteType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001070 // No indexing can be done. Issue diagnostics and quit.
Fariborz Jahanianba0afde2012-03-28 17:56:49 +00001071 const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
1072 if (isa<StringLiteral>(IndexExpr))
1073 Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
1074 << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
1075 else
1076 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1077 << T;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001078 return OS_Error;
1079 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001080
Ted Kremeneke65b0862012-03-06 20:05:56 +00001081 // We must have a complete class type.
Fangrui Song6907ce22018-07-30 19:24:48 +00001082 if (RequireCompleteType(FromE->getExprLoc(), T,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001083 diag::err_objc_index_incomplete_class_type, FromE))
Ted Kremeneke65b0862012-03-06 20:05:56 +00001084 return OS_Error;
Fangrui Song6907ce22018-07-30 19:24:48 +00001085
Ted Kremeneke65b0862012-03-06 20:05:56 +00001086 // Look for a conversion to an integral, enumeration type, or
1087 // objective-C pointer type.
Ted Kremeneke65b0862012-03-06 20:05:56 +00001088 int NoIntegrals=0, NoObjCIdPointers=0;
1089 SmallVector<CXXConversionDecl *, 4> ConversionDecls;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001090
1091 for (NamedDecl *D : cast<CXXRecordDecl>(RecordTy->getDecl())
1092 ->getVisibleConversionFunctions()) {
1093 if (CXXConversionDecl *Conversion =
1094 dyn_cast<CXXConversionDecl>(D->getUnderlyingDecl())) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001095 QualType CT = Conversion->getConversionType().getNonReferenceType();
1096 if (CT->isIntegralOrEnumerationType()) {
1097 ++NoIntegrals;
1098 ConversionDecls.push_back(Conversion);
1099 }
1100 else if (CT->isObjCIdType() ||CT->isBlockPointerType()) {
1101 ++NoObjCIdPointers;
1102 ConversionDecls.push_back(Conversion);
1103 }
1104 }
1105 }
1106 if (NoIntegrals ==1 && NoObjCIdPointers == 0)
1107 return OS_Array;
1108 if (NoIntegrals == 0 && NoObjCIdPointers == 1)
1109 return OS_Dictionary;
1110 if (NoIntegrals == 0 && NoObjCIdPointers == 0) {
1111 // No conversion function was found. Issue diagnostic and return.
1112 Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
1113 << FromE->getType();
1114 return OS_Error;
1115 }
1116 Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion)
1117 << FromE->getType();
1118 for (unsigned int i = 0; i < ConversionDecls.size(); i++)
Richard Smith01d96982016-12-02 23:00:28 +00001119 Diag(ConversionDecls[i]->getLocation(),
1120 diag::note_conv_function_declared_at);
1121
Ted Kremeneke65b0862012-03-06 20:05:56 +00001122 return OS_Error;
1123}
1124
Fariborz Jahanian90804912012-08-02 18:03:58 +00001125/// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF
1126/// objects used as dictionary subscript key objects.
Fangrui Song6907ce22018-07-30 19:24:48 +00001127static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001128 Expr *Key) {
1129 if (ContainerT.isNull())
1130 return;
1131 // dictionary subscripting.
1132 // - (id)objectForKeyedSubscript:(id)key;
1133 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001134 &S.Context.Idents.get("objectForKeyedSubscript")
Fariborz Jahanian90804912012-08-02 18:03:58 +00001135 };
1136 Selector GetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
Fangrui Song6907ce22018-07-30 19:24:48 +00001137 ObjCMethodDecl *Getter = S.LookupMethodInObjectType(GetterSelector, ContainerT,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001138 true /*instance*/);
1139 if (!Getter)
1140 return;
Alp Toker03376dc2014-07-07 09:02:20 +00001141 QualType T = Getter->parameters()[0]->getType();
Brian Kelley11352a82017-03-29 18:09:02 +00001142 S.CheckObjCConversion(Key->getSourceRange(), T, Key,
1143 Sema::CCK_ImplicitConversion);
Fariborz Jahanian90804912012-08-02 18:03:58 +00001144}
1145
Ted Kremeneke65b0862012-03-06 20:05:56 +00001146bool ObjCSubscriptOpBuilder::findAtIndexGetter() {
1147 if (AtIndexGetter)
1148 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001149
Ted Kremeneke65b0862012-03-06 20:05:56 +00001150 Expr *BaseExpr = RefExpr->getBaseExpr();
1151 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001152
Ted Kremeneke65b0862012-03-06 20:05:56 +00001153 QualType ResultType;
1154 if (const ObjCObjectPointerType *PTy =
1155 BaseT->getAs<ObjCObjectPointerType>()) {
1156 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001157 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001158 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001159 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001160 if (Res == Sema::OS_Error) {
1161 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001162 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001163 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001164 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001165 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001166 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001167
Ted Kremeneke65b0862012-03-06 20:05:56 +00001168 if (ResultType.isNull()) {
1169 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1170 << BaseExpr->getType() << arrayRef;
1171 return false;
1172 }
1173 if (!arrayRef) {
1174 // dictionary subscripting.
1175 // - (id)objectForKeyedSubscript:(id)key;
1176 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001177 &S.Context.Idents.get("objectForKeyedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001178 };
1179 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1180 }
1181 else {
1182 // - (id)objectAtIndexedSubscript:(size_t)index;
1183 IdentifierInfo *KeyIdents[] = {
Fangrui Song6907ce22018-07-30 19:24:48 +00001184 &S.Context.Idents.get("objectAtIndexedSubscript")
Ted Kremeneke65b0862012-03-06 20:05:56 +00001185 };
Fangrui Song6907ce22018-07-30 19:24:48 +00001186
Ted Kremeneke65b0862012-03-06 20:05:56 +00001187 AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents);
1188 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001189
1190 AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001191 true /*instance*/);
Fangrui Song6907ce22018-07-30 19:24:48 +00001192
David Blaikiebbafb8a2012-03-11 07:00:24 +00001193 if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001194 AtIndexGetter = ObjCMethodDecl::Create(S.Context, SourceLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001195 SourceLocation(), AtIndexGetterSelector,
1196 S.Context.getObjCIdType() /*ReturnType*/,
Craig Topperc3ec1492014-05-26 06:22:03 +00001197 nullptr /*TypeSourceInfo */,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001198 S.Context.getTranslationUnitDecl(),
1199 true /*Instance*/, false/*isVariadic*/,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001200 /*isPropertyAccessor=*/false,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001201 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1202 ObjCMethodDecl::Required,
1203 false);
1204 ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1205 SourceLocation(), SourceLocation(),
1206 arrayRef ? &S.Context.Idents.get("index")
1207 : &S.Context.Idents.get("key"),
1208 arrayRef ? S.Context.UnsignedLongTy
1209 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001210 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001211 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001212 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001213 AtIndexGetter->setMethodParams(S.Context, Argument, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001214 }
1215
1216 if (!AtIndexGetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001217 if (!BaseT->isObjCIdType()) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001218 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1219 << BaseExpr->getType() << 0 << arrayRef;
1220 return false;
1221 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001222 AtIndexGetter =
1223 S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1224 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001225 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001226 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001227
Ted Kremeneke65b0862012-03-06 20:05:56 +00001228 if (AtIndexGetter) {
Alp Toker03376dc2014-07-07 09:02:20 +00001229 QualType T = AtIndexGetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001230 if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1231 (!arrayRef && !T->isObjCObjectPointerType())) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001232 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001233 arrayRef ? diag::err_objc_subscript_index_type
1234 : diag::err_objc_subscript_key_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001235 S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001236 diag::note_parameter_type) << T;
1237 return false;
1238 }
Alp Toker314cc812014-01-25 16:55:45 +00001239 QualType R = AtIndexGetter->getReturnType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001240 if (!R->isObjCObjectPointerType()) {
1241 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1242 diag::err_objc_indexing_method_result_type) << R << arrayRef;
1243 S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1244 AtIndexGetter->getDeclName();
1245 }
1246 }
1247 return true;
1248}
1249
1250bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1251 if (AtIndexSetter)
1252 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001253
Ted Kremeneke65b0862012-03-06 20:05:56 +00001254 Expr *BaseExpr = RefExpr->getBaseExpr();
1255 QualType BaseT = BaseExpr->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001256
Ted Kremeneke65b0862012-03-06 20:05:56 +00001257 QualType ResultType;
1258 if (const ObjCObjectPointerType *PTy =
1259 BaseT->getAs<ObjCObjectPointerType>()) {
1260 ResultType = PTy->getPointeeType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001261 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001262
1263 Sema::ObjCSubscriptKind Res =
Ted Kremeneke65b0862012-03-06 20:05:56 +00001264 S.CheckSubscriptingKind(RefExpr->getKeyExpr());
Fariborz Jahanian90804912012-08-02 18:03:58 +00001265 if (Res == Sema::OS_Error) {
1266 if (S.getLangOpts().ObjCAutoRefCount)
Fangrui Song6907ce22018-07-30 19:24:48 +00001267 CheckKeyForObjCARCConversion(S, ResultType,
Fariborz Jahanian90804912012-08-02 18:03:58 +00001268 RefExpr->getKeyExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001269 return false;
Fariborz Jahanian90804912012-08-02 18:03:58 +00001270 }
Ted Kremeneke65b0862012-03-06 20:05:56 +00001271 bool arrayRef = (Res == Sema::OS_Array);
Fangrui Song6907ce22018-07-30 19:24:48 +00001272
Ted Kremeneke65b0862012-03-06 20:05:56 +00001273 if (ResultType.isNull()) {
1274 S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1275 << BaseExpr->getType() << arrayRef;
1276 return false;
1277 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001278
Ted Kremeneke65b0862012-03-06 20:05:56 +00001279 if (!arrayRef) {
1280 // dictionary subscripting.
1281 // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1282 IdentifierInfo *KeyIdents[] = {
1283 &S.Context.Idents.get("setObject"),
1284 &S.Context.Idents.get("forKeyedSubscript")
1285 };
1286 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1287 }
1288 else {
1289 // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1290 IdentifierInfo *KeyIdents[] = {
1291 &S.Context.Idents.get("setObject"),
1292 &S.Context.Idents.get("atIndexedSubscript")
1293 };
1294 AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1295 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001296 AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001297 true /*instance*/);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001298
David Blaikiebbafb8a2012-03-11 07:00:24 +00001299 if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001300 TypeSourceInfo *ReturnTInfo = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00001301 QualType ReturnType = S.Context.VoidTy;
Alp Toker314cc812014-01-25 16:55:45 +00001302 AtIndexSetter = ObjCMethodDecl::Create(
1303 S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector,
1304 ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(),
1305 true /*Instance*/, false /*isVariadic*/,
1306 /*isPropertyAccessor=*/false,
1307 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1308 ObjCMethodDecl::Required, false);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001309 SmallVector<ParmVarDecl *, 2> Params;
1310 ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter,
1311 SourceLocation(), SourceLocation(),
1312 &S.Context.Idents.get("object"),
1313 S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001314 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001315 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001316 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001317 Params.push_back(object);
1318 ParmVarDecl *key = ParmVarDecl::Create(S.Context, AtIndexSetter,
1319 SourceLocation(), SourceLocation(),
1320 arrayRef ? &S.Context.Idents.get("index")
1321 : &S.Context.Idents.get("key"),
1322 arrayRef ? S.Context.UnsignedLongTy
1323 : S.Context.getObjCIdType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001324 /*TInfo=*/nullptr,
Ted Kremeneke65b0862012-03-06 20:05:56 +00001325 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00001326 nullptr);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001327 Params.push_back(key);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001328 AtIndexSetter->setMethodParams(S.Context, Params, None);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001329 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001330
Ted Kremeneke65b0862012-03-06 20:05:56 +00001331 if (!AtIndexSetter) {
Alex Lorenz4b9f80c2017-07-11 10:18:35 +00001332 if (!BaseT->isObjCIdType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001333 S.Diag(BaseExpr->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001334 diag::err_objc_subscript_method_not_found)
1335 << BaseExpr->getType() << 1 << arrayRef;
1336 return false;
1337 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001338 AtIndexSetter =
1339 S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector,
1340 RefExpr->getSourceRange(),
Fariborz Jahanian890803f2015-04-15 17:26:21 +00001341 true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001342 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001343
Ted Kremeneke65b0862012-03-06 20:05:56 +00001344 bool err = false;
1345 if (AtIndexSetter && arrayRef) {
Alp Toker03376dc2014-07-07 09:02:20 +00001346 QualType T = AtIndexSetter->parameters()[1]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001347 if (!T->isIntegralOrEnumerationType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001348 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001349 diag::err_objc_subscript_index_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001350 S.Diag(AtIndexSetter->parameters()[1]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001351 diag::note_parameter_type) << T;
1352 err = true;
1353 }
Alp Toker03376dc2014-07-07 09:02:20 +00001354 T = AtIndexSetter->parameters()[0]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001355 if (!T->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001356 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001357 diag::err_objc_subscript_object_type) << T << arrayRef;
Fangrui Song6907ce22018-07-30 19:24:48 +00001358 S.Diag(AtIndexSetter->parameters()[0]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001359 diag::note_parameter_type) << T;
1360 err = true;
1361 }
1362 }
1363 else if (AtIndexSetter && !arrayRef)
1364 for (unsigned i=0; i <2; i++) {
Alp Toker03376dc2014-07-07 09:02:20 +00001365 QualType T = AtIndexSetter->parameters()[i]->getType();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001366 if (!T->isObjCObjectPointerType()) {
1367 if (i == 1)
1368 S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1369 diag::err_objc_subscript_key_type) << T;
1370 else
1371 S.Diag(RefExpr->getBaseExpr()->getExprLoc(),
1372 diag::err_objc_subscript_dic_object_type) << T;
Fangrui Song6907ce22018-07-30 19:24:48 +00001373 S.Diag(AtIndexSetter->parameters()[i]->getLocation(),
Ted Kremeneke65b0862012-03-06 20:05:56 +00001374 diag::note_parameter_type) << T;
1375 err = true;
1376 }
1377 }
1378
1379 return !err;
1380}
1381
1382// Get the object at "Index" position in the container.
1383// [BaseExpr objectAtIndexedSubscript : IndexExpr];
1384ExprResult ObjCSubscriptOpBuilder::buildGet() {
1385 if (!findAtIndexGetter())
1386 return ExprError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001387
Ted Kremeneke65b0862012-03-06 20:05:56 +00001388 QualType receiverType = InstanceBase->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00001389
Ted Kremeneke65b0862012-03-06 20:05:56 +00001390 // Build a message-send.
1391 ExprResult msg;
1392 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001393
Ted Kremeneke65b0862012-03-06 20:05:56 +00001394 // Arguments.
1395 Expr *args[] = { Index };
1396 assert(InstanceBase);
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001397 if (AtIndexGetter)
1398 S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001399 msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1400 GenericLoc,
1401 AtIndexGetterSelector, AtIndexGetter,
1402 MultiExprArg(args, 1));
1403 return msg;
1404}
1405
1406/// Store into the container the "op" object at "Index"'ed location
1407/// by building this messaging expression:
1408/// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00001409/// \param captureSetValueAsResult If true, capture the actual
Ted Kremeneke65b0862012-03-06 20:05:56 +00001410/// value being set as the value of the property operation.
1411ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc,
1412 bool captureSetValueAsResult) {
1413 if (!findAtIndexSetter())
1414 return ExprError();
Fariborz Jahanian3d576402014-06-10 19:02:48 +00001415 if (AtIndexSetter)
1416 S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001417 QualType receiverType = InstanceBase->getType();
1418 Expr *Index = InstanceKey;
Fangrui Song6907ce22018-07-30 19:24:48 +00001419
Ted Kremeneke65b0862012-03-06 20:05:56 +00001420 // Arguments.
1421 Expr *args[] = { op, Index };
Fangrui Song6907ce22018-07-30 19:24:48 +00001422
Ted Kremeneke65b0862012-03-06 20:05:56 +00001423 // Build a message-send.
1424 ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType,
1425 GenericLoc,
1426 AtIndexSetterSelector,
1427 AtIndexSetter,
1428 MultiExprArg(args, 2));
Fangrui Song6907ce22018-07-30 19:24:48 +00001429
Ted Kremeneke65b0862012-03-06 20:05:56 +00001430 if (!msg.isInvalid() && captureSetValueAsResult) {
1431 ObjCMessageExpr *msgExpr =
1432 cast<ObjCMessageExpr>(msg.get()->IgnoreImplicit());
1433 Expr *arg = msgExpr->getArg(0);
Fariborz Jahanian15dde892014-03-06 00:34:05 +00001434 if (CanCaptureValue(arg))
Eli Friedman00fa4292012-11-13 23:16:33 +00001435 msgExpr->setArg(0, captureValueAsResult(arg));
Ted Kremeneke65b0862012-03-06 20:05:56 +00001436 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001437
Ted Kremeneke65b0862012-03-06 20:05:56 +00001438 return msg;
1439}
1440
John McCallfe96e0b2011-11-06 09:01:30 +00001441//===----------------------------------------------------------------------===//
John McCall5e77d762013-04-16 07:28:30 +00001442// MSVC __declspec(property) references
1443//===----------------------------------------------------------------------===//
1444
Alexey Bataevf7630272015-11-25 12:01:00 +00001445MSPropertyRefExpr *
1446MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) {
1447 CallArgs.insert(CallArgs.begin(), E->getIdx());
1448 Expr *Base = E->getBase()->IgnoreParens();
1449 while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Base)) {
1450 CallArgs.insert(CallArgs.begin(), MSPropSubscript->getIdx());
1451 Base = MSPropSubscript->getBase()->IgnoreParens();
1452 }
1453 return cast<MSPropertyRefExpr>(Base);
1454}
1455
John McCall5e77d762013-04-16 07:28:30 +00001456Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) {
Alexey Bataev69103472015-10-14 04:05:42 +00001457 InstanceBase = capture(RefExpr->getBaseExpr());
Aaron Ballman72f65632017-11-03 20:09:17 +00001458 llvm::for_each(CallArgs, [this](Expr *&Arg) { Arg = capture(Arg); });
Alexey Bataevf7630272015-11-25 12:01:00 +00001459 syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * {
1460 switch (Idx) {
1461 case 0:
1462 return InstanceBase;
1463 default:
1464 assert(Idx <= CallArgs.size());
1465 return CallArgs[Idx - 1];
1466 }
1467 }).rebuild(syntacticBase);
John McCall5e77d762013-04-16 07:28:30 +00001468
1469 return syntacticBase;
1470}
1471
1472ExprResult MSPropertyOpBuilder::buildGet() {
1473 if (!RefExpr->getPropertyDecl()->hasGetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001474 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001475 << 0 /* getter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001476 return ExprError();
1477 }
1478
1479 UnqualifiedId GetterName;
1480 IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId();
1481 GetterName.setIdentifier(II, RefExpr->getMemberLoc());
1482 CXXScopeSpec SS;
1483 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001484 ExprResult GetterExpr =
1485 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1486 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1487 SourceLocation(), GetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001488 if (GetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001489 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001490 diag::err_cannot_find_suitable_accessor) << 0 /* getter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001491 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001492 return ExprError();
1493 }
1494
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001495 return S.ActOnCallExpr(S.getCurScope(), GetterExpr.get(),
Alexey Bataevf7630272015-11-25 12:01:00 +00001496 RefExpr->getSourceRange().getBegin(), CallArgs,
John McCall5e77d762013-04-16 07:28:30 +00001497 RefExpr->getSourceRange().getEnd());
1498}
1499
1500ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl,
1501 bool captureSetValueAsResult) {
1502 if (!RefExpr->getPropertyDecl()->hasSetter()) {
Aaron Ballman213cf412013-12-26 16:35:04 +00001503 S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property)
Aaron Ballman1bda4592014-01-03 01:09:27 +00001504 << 1 /* setter */ << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001505 return ExprError();
1506 }
1507
1508 UnqualifiedId SetterName;
1509 IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId();
1510 SetterName.setIdentifier(II, RefExpr->getMemberLoc());
1511 CXXScopeSpec SS;
1512 SS.Adopt(RefExpr->getQualifierLoc());
Alexey Bataev69103472015-10-14 04:05:42 +00001513 ExprResult SetterExpr =
1514 S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(),
1515 RefExpr->isArrow() ? tok::arrow : tok::period, SS,
1516 SourceLocation(), SetterName, nullptr);
John McCall5e77d762013-04-16 07:28:30 +00001517 if (SetterExpr.isInvalid()) {
Aaron Ballman9e35bfe2013-12-26 15:46:38 +00001518 S.Diag(RefExpr->getMemberLoc(),
Richard Smithf8812672016-12-02 22:38:31 +00001519 diag::err_cannot_find_suitable_accessor) << 1 /* setter */
Aaron Ballman1bda4592014-01-03 01:09:27 +00001520 << RefExpr->getPropertyDecl();
John McCall5e77d762013-04-16 07:28:30 +00001521 return ExprError();
1522 }
1523
Alexey Bataevf7630272015-11-25 12:01:00 +00001524 SmallVector<Expr*, 4> ArgExprs;
1525 ArgExprs.append(CallArgs.begin(), CallArgs.end());
John McCall5e77d762013-04-16 07:28:30 +00001526 ArgExprs.push_back(op);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001527 return S.ActOnCallExpr(S.getCurScope(), SetterExpr.get(),
John McCall5e77d762013-04-16 07:28:30 +00001528 RefExpr->getSourceRange().getBegin(), ArgExprs,
1529 op->getSourceRange().getEnd());
1530}
1531
1532//===----------------------------------------------------------------------===//
John McCallfe96e0b2011-11-06 09:01:30 +00001533// General Sema routines.
1534//===----------------------------------------------------------------------===//
1535
1536ExprResult Sema::checkPseudoObjectRValue(Expr *E) {
1537 Expr *opaqueRef = E->IgnoreParens();
1538 if (ObjCPropertyRefExpr *refExpr
1539 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001540 ObjCPropertyOpBuilder builder(*this, refExpr, true);
John McCallfe96e0b2011-11-06 09:01:30 +00001541 return builder.buildRValueOperation(E);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001542 }
1543 else if (ObjCSubscriptRefExpr *refExpr
1544 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001545 ObjCSubscriptOpBuilder builder(*this, refExpr, true);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001546 return builder.buildRValueOperation(E);
John McCall5e77d762013-04-16 07:28:30 +00001547 } else if (MSPropertyRefExpr *refExpr
1548 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001549 MSPropertyOpBuilder builder(*this, refExpr, true);
John McCall5e77d762013-04-16 07:28:30 +00001550 return builder.buildRValueOperation(E);
Alexey Bataevf7630272015-11-25 12:01:00 +00001551 } else if (MSPropertySubscriptExpr *RefExpr =
1552 dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001553 MSPropertyOpBuilder Builder(*this, RefExpr, true);
Alexey Bataevf7630272015-11-25 12:01:00 +00001554 return Builder.buildRValueOperation(E);
John McCallfe96e0b2011-11-06 09:01:30 +00001555 } else {
1556 llvm_unreachable("unknown pseudo-object kind!");
1557 }
1558}
1559
1560/// Check an increment or decrement of a pseudo-object expression.
1561ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc,
1562 UnaryOperatorKind opcode, Expr *op) {
1563 // Do nothing if the operand is dependent.
1564 if (op->isTypeDependent())
1565 return new (Context) UnaryOperator(op, opcode, Context.DependentTy,
Aaron Ballmana5038552018-01-09 13:07:03 +00001566 VK_RValue, OK_Ordinary, opcLoc, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001567
1568 assert(UnaryOperator::isIncrementDecrementOp(opcode));
1569 Expr *opaqueRef = op->IgnoreParens();
1570 if (ObjCPropertyRefExpr *refExpr
1571 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001572 ObjCPropertyOpBuilder builder(*this, refExpr, false);
John McCallfe96e0b2011-11-06 09:01:30 +00001573 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001574 } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1575 Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1576 return ExprError();
John McCall5e77d762013-04-16 07:28:30 +00001577 } else if (MSPropertyRefExpr *refExpr
1578 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001579 MSPropertyOpBuilder builder(*this, refExpr, false);
John McCall5e77d762013-04-16 07:28:30 +00001580 return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
Alexey Bataevf7630272015-11-25 12:01:00 +00001581 } else if (MSPropertySubscriptExpr *RefExpr
1582 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001583 MSPropertyOpBuilder Builder(*this, RefExpr, false);
Alexey Bataevf7630272015-11-25 12:01:00 +00001584 return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
John McCallfe96e0b2011-11-06 09:01:30 +00001585 } else {
1586 llvm_unreachable("unknown pseudo-object kind!");
1587 }
1588}
1589
1590ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc,
1591 BinaryOperatorKind opcode,
1592 Expr *LHS, Expr *RHS) {
1593 // Do nothing if either argument is dependent.
1594 if (LHS->isTypeDependent() || RHS->isTypeDependent())
1595 return new (Context) BinaryOperator(LHS, RHS, opcode, Context.DependentTy,
Adam Nemet484aa452017-03-27 19:17:25 +00001596 VK_RValue, OK_Ordinary, opcLoc,
1597 FPOptions());
John McCallfe96e0b2011-11-06 09:01:30 +00001598
1599 // Filter out non-overload placeholder types in the RHS.
John McCalld5c98ae2011-11-15 01:35:18 +00001600 if (RHS->getType()->isNonOverloadPlaceholderType()) {
1601 ExprResult result = CheckPlaceholderExpr(RHS);
1602 if (result.isInvalid()) return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001603 RHS = result.get();
John McCallfe96e0b2011-11-06 09:01:30 +00001604 }
1605
Akira Hatanaka797afe32018-03-20 01:47:58 +00001606 bool IsSimpleAssign = opcode == BO_Assign;
John McCallfe96e0b2011-11-06 09:01:30 +00001607 Expr *opaqueRef = LHS->IgnoreParens();
1608 if (ObjCPropertyRefExpr *refExpr
1609 = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001610 ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
John McCallfe96e0b2011-11-06 09:01:30 +00001611 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001612 } else if (ObjCSubscriptRefExpr *refExpr
1613 = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001614 ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001615 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCall5e77d762013-04-16 07:28:30 +00001616 } else if (MSPropertyRefExpr *refExpr
1617 = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001618 MSPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001619 return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1620 } else if (MSPropertySubscriptExpr *RefExpr
1621 = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
Akira Hatanaka797afe32018-03-20 01:47:58 +00001622 MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign);
Alexey Bataevf7630272015-11-25 12:01:00 +00001623 return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
John McCallfe96e0b2011-11-06 09:01:30 +00001624 } else {
1625 llvm_unreachable("unknown pseudo-object kind!");
1626 }
1627}
John McCalle9290822011-11-30 04:42:31 +00001628
1629/// Given a pseudo-object reference, rebuild it without the opaque
1630/// values. Basically, undo the behavior of rebuildAndCaptureObject.
1631/// This should never operate in-place.
1632static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) {
Alexey Bataevf7630272015-11-25 12:01:00 +00001633 return Rebuilder(S,
1634 [=](Expr *E, unsigned) -> Expr * {
1635 return cast<OpaqueValueExpr>(E)->getSourceExpr();
1636 })
1637 .rebuild(E);
John McCalle9290822011-11-30 04:42:31 +00001638}
1639
1640/// Given a pseudo-object expression, recreate what it looks like
1641/// syntactically without the attendant OpaqueValueExprs.
1642///
1643/// This is a hack which should be removed when TreeTransform is
1644/// capable of rebuilding a tree without stripping implicit
1645/// operations.
1646Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) {
Malcolm Parsonsfab36802018-04-16 08:31:08 +00001647 Expr *syntax = E->getSyntacticForm();
1648 if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1649 Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1650 return new (Context) UnaryOperator(
1651 op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1652 uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1653 } else if (CompoundAssignOperator *cop
1654 = dyn_cast<CompoundAssignOperator>(syntax)) {
1655 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
John McCalle9290822011-11-30 04:42:31 +00001656 Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
1657 return new (Context) CompoundAssignOperator(lhs, rhs, cop->getOpcode(),
1658 cop->getType(),
1659 cop->getValueKind(),
1660 cop->getObjectKind(),
1661 cop->getComputationLHSType(),
1662 cop->getComputationResultType(),
Adam Nemet484aa452017-03-27 19:17:25 +00001663 cop->getOperatorLoc(),
1664 FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001665 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1666 Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1667 Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
1668 return new (Context) BinaryOperator(lhs, rhs, bop->getOpcode(),
1669 bop->getType(), bop->getValueKind(),
1670 bop->getObjectKind(),
Adam Nemet484aa452017-03-27 19:17:25 +00001671 bop->getOperatorLoc(), FPOptions());
John McCalle9290822011-11-30 04:42:31 +00001672 } else {
1673 assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1674 return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1675 }
1676}