1 | //===--- SemaPseudoObject.cpp - Semantic Analysis for Pseudo-Objects ------===// |
---|---|
2 | // |
3 | // 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 |
6 | // |
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" |
33 | #include "clang/AST/ExprCXX.h" |
34 | #include "clang/AST/ExprObjC.h" |
35 | #include "clang/Basic/CharInfo.h" |
36 | #include "clang/Lex/Preprocessor.h" |
37 | #include "clang/Sema/Initialization.h" |
38 | #include "clang/Sema/ScopeInfo.h" |
39 | #include "llvm/ADT/SmallString.h" |
40 | |
41 | using namespace clang; |
42 | using namespace sema; |
43 | |
44 | namespace { |
45 | // Basically just a very focused copy of TreeTransform. |
46 | struct Rebuilder { |
47 | Sema &S; |
48 | 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) {} |
54 | |
55 | 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(e: 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 | } |
105 | |
106 | Expr *rebuild(Expr *e) { |
107 | // Fast path: nothing to look through. |
108 | if (auto *PRE = dyn_cast<ObjCPropertyRefExpr>(Val: e)) |
109 | return rebuildObjCPropertyRefExpr(refExpr: PRE); |
110 | if (auto *SRE = dyn_cast<ObjCSubscriptRefExpr>(Val: e)) |
111 | return rebuildObjCSubscriptRefExpr(refExpr: SRE); |
112 | if (auto *MSPRE = dyn_cast<MSPropertyRefExpr>(Val: e)) |
113 | return rebuildMSPropertyRefExpr(refExpr: MSPRE); |
114 | if (auto *MSPSE = dyn_cast<MSPropertySubscriptExpr>(Val: e)) |
115 | return rebuildMSPropertySubscriptExpr(refExpr: MSPSE); |
116 | |
117 | // Otherwise, we should look through and rebuild anything that |
118 | // IgnoreParens would. |
119 | |
120 | if (ParenExpr *parens = dyn_cast<ParenExpr>(Val: e)) { |
121 | e = rebuild(e: parens->getSubExpr()); |
122 | return new (S.Context) ParenExpr(parens->getLParen(), |
123 | parens->getRParen(), |
124 | e); |
125 | } |
126 | |
127 | if (UnaryOperator *uop = dyn_cast<UnaryOperator>(Val: e)) { |
128 | assert(uop->getOpcode() == UO_Extension); |
129 | e = rebuild(e: uop->getSubExpr()); |
130 | return UnaryOperator::Create( |
131 | C: S.Context, input: e, opc: uop->getOpcode(), type: uop->getType(), VK: uop->getValueKind(), |
132 | OK: uop->getObjectKind(), l: uop->getOperatorLoc(), CanOverflow: uop->canOverflow(), |
133 | FPFeatures: S.CurFPFeatureOverrides()); |
134 | } |
135 | |
136 | if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(Val: e)) { |
137 | assert(!gse->isResultDependent()); |
138 | unsigned resultIndex = gse->getResultIndex(); |
139 | unsigned numAssocs = gse->getNumAssocs(); |
140 | |
141 | SmallVector<Expr *, 8> assocExprs; |
142 | SmallVector<TypeSourceInfo *, 8> assocTypes; |
143 | assocExprs.reserve(N: numAssocs); |
144 | assocTypes.reserve(N: numAssocs); |
145 | |
146 | for (const GenericSelectionExpr::Association assoc : |
147 | gse->associations()) { |
148 | Expr *assocExpr = assoc.getAssociationExpr(); |
149 | if (assoc.isSelected()) |
150 | assocExpr = rebuild(e: assocExpr); |
151 | assocExprs.push_back(Elt: assocExpr); |
152 | assocTypes.push_back(Elt: assoc.getTypeSourceInfo()); |
153 | } |
154 | |
155 | if (gse->isExprPredicate()) |
156 | return GenericSelectionExpr::Create( |
157 | S.Context, gse->getGenericLoc(), gse->getControllingExpr(), |
158 | assocTypes, assocExprs, gse->getDefaultLoc(), gse->getRParenLoc(), |
159 | gse->containsUnexpandedParameterPack(), resultIndex); |
160 | return GenericSelectionExpr::Create( |
161 | S.Context, gse->getGenericLoc(), gse->getControllingType(), |
162 | assocTypes, assocExprs, gse->getDefaultLoc(), gse->getRParenLoc(), |
163 | gse->containsUnexpandedParameterPack(), resultIndex); |
164 | } |
165 | |
166 | if (ChooseExpr *ce = dyn_cast<ChooseExpr>(Val: e)) { |
167 | assert(!ce->isConditionDependent()); |
168 | |
169 | Expr *LHS = ce->getLHS(), *RHS = ce->getRHS(); |
170 | Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS; |
171 | rebuiltExpr = rebuild(e: rebuiltExpr); |
172 | |
173 | return new (S.Context) |
174 | ChooseExpr(ce->getBuiltinLoc(), ce->getCond(), LHS, RHS, |
175 | rebuiltExpr->getType(), rebuiltExpr->getValueKind(), |
176 | rebuiltExpr->getObjectKind(), ce->getRParenLoc(), |
177 | ce->isConditionTrue()); |
178 | } |
179 | |
180 | llvm_unreachable("bad expression to rebuild!"); |
181 | } |
182 | }; |
183 | |
184 | class PseudoOpBuilder { |
185 | public: |
186 | Sema &S; |
187 | unsigned ResultIndex; |
188 | SourceLocation GenericLoc; |
189 | bool IsUnique; |
190 | SmallVector<Expr *, 4> Semantics; |
191 | |
192 | PseudoOpBuilder(Sema &S, SourceLocation genericLoc, bool IsUnique) |
193 | : S(S), ResultIndex(PseudoObjectExpr::NoResult), |
194 | GenericLoc(genericLoc), IsUnique(IsUnique) {} |
195 | |
196 | virtual ~PseudoOpBuilder() {} |
197 | |
198 | /// Add a normal semantic expression. |
199 | void addSemanticExpr(Expr *semantic) { |
200 | Semantics.push_back(Elt: semantic); |
201 | } |
202 | |
203 | /// Add the 'result' semantic expression. |
204 | void addResultSemanticExpr(Expr *resultExpr) { |
205 | assert(ResultIndex == PseudoObjectExpr::NoResult); |
206 | ResultIndex = Semantics.size(); |
207 | Semantics.push_back(Elt: resultExpr); |
208 | // An OVE is not unique if it is used as the result expression. |
209 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: Semantics.back())) |
210 | OVE->setIsUnique(false); |
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 | |
222 | virtual ExprResult complete(Expr *syntacticForm); |
223 | |
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 | // An OVE is not unique if it is used as the result expression. |
231 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: Semantics.back())) |
232 | OVE->setIsUnique(false); |
233 | } |
234 | |
235 | /// Return true if assignments have a non-void result. |
236 | static bool CanCaptureValue(Expr *exp) { |
237 | if (exp->isGLValue()) |
238 | return true; |
239 | QualType ty = exp->getType(); |
240 | assert(!ty->isIncompleteType()); |
241 | assert(!ty->isDependentType()); |
242 | |
243 | if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl()) |
244 | return ClassDecl->isTriviallyCopyable(); |
245 | return true; |
246 | } |
247 | |
248 | virtual Expr *rebuildAndCaptureObject(Expr *) = 0; |
249 | virtual ExprResult buildGet() = 0; |
250 | virtual ExprResult buildSet(Expr *, SourceLocation, |
251 | bool captureSetValueAsResult) = 0; |
252 | /// Should the result of an assignment be the formal result of the |
253 | /// setter call or the value that was passed to the setter? |
254 | /// |
255 | /// Different pseudo-object language features use different language rules |
256 | /// for this. |
257 | /// The default is to use the set value. Currently, this affects the |
258 | /// behavior of simple assignments, compound assignments, and prefix |
259 | /// increment and decrement. |
260 | /// Postfix increment and decrement always use the getter result as the |
261 | /// expression result. |
262 | /// |
263 | /// If this method returns true, and the set value isn't capturable for |
264 | /// some reason, the result of the expression will be void. |
265 | virtual bool captureSetValueAsResult() const { return true; } |
266 | }; |
267 | |
268 | /// A PseudoOpBuilder for Objective-C \@properties. |
269 | class ObjCPropertyOpBuilder : public PseudoOpBuilder { |
270 | ObjCPropertyRefExpr *RefExpr; |
271 | ObjCPropertyRefExpr *SyntacticRefExpr; |
272 | OpaqueValueExpr *InstanceReceiver; |
273 | ObjCMethodDecl *Getter; |
274 | |
275 | ObjCMethodDecl *Setter; |
276 | Selector SetterSelector; |
277 | Selector GetterSelector; |
278 | |
279 | public: |
280 | ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr, bool IsUnique) |
281 | : PseudoOpBuilder(S, refExpr->getLocation(), IsUnique), |
282 | RefExpr(refExpr), SyntacticRefExpr(nullptr), |
283 | InstanceReceiver(nullptr), Getter(nullptr), Setter(nullptr) { |
284 | } |
285 | |
286 | ExprResult buildRValueOperation(Expr *op); |
287 | ExprResult buildAssignmentOperation(Scope *Sc, |
288 | SourceLocation opLoc, |
289 | BinaryOperatorKind opcode, |
290 | Expr *LHS, Expr *RHS); |
291 | ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc, |
292 | UnaryOperatorKind opcode, |
293 | Expr *op); |
294 | |
295 | bool tryBuildGetOfReference(Expr *op, ExprResult &result); |
296 | bool findSetter(bool warn=true); |
297 | bool findGetter(); |
298 | void DiagnoseUnsupportedPropertyUse(); |
299 | |
300 | Expr *rebuildAndCaptureObject(Expr *syntacticBase) override; |
301 | ExprResult buildGet() override; |
302 | ExprResult buildSet(Expr *op, SourceLocation, bool) override; |
303 | ExprResult complete(Expr *SyntacticForm) override; |
304 | |
305 | bool isWeakProperty() const; |
306 | }; |
307 | |
308 | /// A PseudoOpBuilder for Objective-C array/dictionary indexing. |
309 | class ObjCSubscriptOpBuilder : public PseudoOpBuilder { |
310 | ObjCSubscriptRefExpr *RefExpr; |
311 | OpaqueValueExpr *InstanceBase; |
312 | OpaqueValueExpr *InstanceKey; |
313 | ObjCMethodDecl *AtIndexGetter; |
314 | Selector AtIndexGetterSelector; |
315 | |
316 | ObjCMethodDecl *AtIndexSetter; |
317 | Selector AtIndexSetterSelector; |
318 | |
319 | public: |
320 | ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr, bool IsUnique) |
321 | : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique), |
322 | RefExpr(refExpr), InstanceBase(nullptr), InstanceKey(nullptr), |
323 | AtIndexGetter(nullptr), AtIndexSetter(nullptr) {} |
324 | |
325 | ExprResult buildRValueOperation(Expr *op); |
326 | ExprResult buildAssignmentOperation(Scope *Sc, |
327 | SourceLocation opLoc, |
328 | BinaryOperatorKind opcode, |
329 | Expr *LHS, Expr *RHS); |
330 | Expr *rebuildAndCaptureObject(Expr *syntacticBase) override; |
331 | |
332 | bool findAtIndexGetter(); |
333 | bool findAtIndexSetter(); |
334 | |
335 | ExprResult buildGet() override; |
336 | ExprResult buildSet(Expr *op, SourceLocation, bool) override; |
337 | }; |
338 | |
339 | class MSPropertyOpBuilder : public PseudoOpBuilder { |
340 | MSPropertyRefExpr *RefExpr; |
341 | OpaqueValueExpr *InstanceBase; |
342 | SmallVector<Expr *, 4> CallArgs; |
343 | |
344 | MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E); |
345 | |
346 | public: |
347 | MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr, bool IsUnique) |
348 | : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique), |
349 | RefExpr(refExpr), InstanceBase(nullptr) {} |
350 | MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr, bool IsUnique) |
351 | : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin(), IsUnique), |
352 | InstanceBase(nullptr) { |
353 | RefExpr = getBaseMSProperty(E: refExpr); |
354 | } |
355 | |
356 | Expr *rebuildAndCaptureObject(Expr *) override; |
357 | ExprResult buildGet() override; |
358 | ExprResult buildSet(Expr *op, SourceLocation, bool) override; |
359 | bool captureSetValueAsResult() const override { return false; } |
360 | }; |
361 | } |
362 | |
363 | /// Capture the given expression in an OpaqueValueExpr. |
364 | OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) { |
365 | // Make a new OVE whose source is the given expression. |
366 | OpaqueValueExpr *captured = |
367 | new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(), |
368 | e->getValueKind(), e->getObjectKind(), |
369 | e); |
370 | if (IsUnique) |
371 | captured->setIsUnique(true); |
372 | |
373 | // Make sure we bind that in the semantics. |
374 | addSemanticExpr(captured); |
375 | return captured; |
376 | } |
377 | |
378 | /// Capture the given expression as the result of this pseudo-object |
379 | /// operation. This routine is safe against expressions which may |
380 | /// already be captured. |
381 | /// |
382 | /// \returns the captured expression, which will be the |
383 | /// same as the input if the input was already captured |
384 | OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) { |
385 | assert(ResultIndex == PseudoObjectExpr::NoResult); |
386 | |
387 | // If the expression hasn't already been captured, just capture it |
388 | // and set the new semantic |
389 | if (!isa<OpaqueValueExpr>(Val: e)) { |
390 | OpaqueValueExpr *cap = capture(e); |
391 | setResultToLastSemantic(); |
392 | return cap; |
393 | } |
394 | |
395 | // Otherwise, it must already be one of our semantic expressions; |
396 | // set ResultIndex to its index. |
397 | unsigned index = 0; |
398 | for (;; ++index) { |
399 | assert(index < Semantics.size() && |
400 | "captured expression not found in semantics!"); |
401 | if (e == Semantics[index]) break; |
402 | } |
403 | ResultIndex = index; |
404 | // An OVE is not unique if it is used as the result expression. |
405 | cast<OpaqueValueExpr>(Val: e)->setIsUnique(false); |
406 | return cast<OpaqueValueExpr>(Val: e); |
407 | } |
408 | |
409 | /// The routine which creates the final PseudoObjectExpr. |
410 | ExprResult PseudoOpBuilder::complete(Expr *syntactic) { |
411 | return PseudoObjectExpr::Create(Context: S.Context, syntactic, |
412 | semantic: Semantics, resultIndex: ResultIndex); |
413 | } |
414 | |
415 | /// The main skeleton for building an r-value operation. |
416 | ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) { |
417 | Expr *syntacticBase = rebuildAndCaptureObject(op); |
418 | |
419 | ExprResult getExpr = buildGet(); |
420 | if (getExpr.isInvalid()) return ExprError(); |
421 | addResultSemanticExpr(resultExpr: getExpr.get()); |
422 | |
423 | return complete(syntactic: syntacticBase); |
424 | } |
425 | |
426 | /// The basic skeleton for building a simple or compound |
427 | /// assignment operation. |
428 | ExprResult |
429 | PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc, |
430 | BinaryOperatorKind opcode, |
431 | Expr *LHS, Expr *RHS) { |
432 | assert(BinaryOperator::isAssignmentOp(opcode)); |
433 | |
434 | Expr *syntacticLHS = rebuildAndCaptureObject(LHS); |
435 | OpaqueValueExpr *capturedRHS = capture(e: RHS); |
436 | |
437 | // In some very specific cases, semantic analysis of the RHS as an |
438 | // expression may require it to be rewritten. In these cases, we |
439 | // cannot safely keep the OVE around. Fortunately, we don't really |
440 | // need to: we don't use this particular OVE in multiple places, and |
441 | // no clients rely that closely on matching up expressions in the |
442 | // semantic expression with expressions from the syntactic form. |
443 | Expr *semanticRHS = capturedRHS; |
444 | if (RHS->hasPlaceholderType() || isa<InitListExpr>(Val: RHS)) { |
445 | semanticRHS = RHS; |
446 | Semantics.pop_back(); |
447 | } |
448 | |
449 | Expr *syntactic; |
450 | |
451 | ExprResult result; |
452 | if (opcode == BO_Assign) { |
453 | result = semanticRHS; |
454 | syntactic = BinaryOperator::Create(C: S.Context, lhs: syntacticLHS, rhs: capturedRHS, |
455 | opc: opcode, ResTy: capturedRHS->getType(), |
456 | VK: capturedRHS->getValueKind(), OK: OK_Ordinary, |
457 | opLoc: opcLoc, FPFeatures: S.CurFPFeatureOverrides()); |
458 | |
459 | } else { |
460 | ExprResult opLHS = buildGet(); |
461 | if (opLHS.isInvalid()) return ExprError(); |
462 | |
463 | // Build an ordinary, non-compound operation. |
464 | BinaryOperatorKind nonCompound = |
465 | BinaryOperator::getOpForCompoundAssignment(Opc: opcode); |
466 | result = S.BuildBinOp(S: Sc, OpLoc: opcLoc, Opc: nonCompound, LHSExpr: opLHS.get(), RHSExpr: semanticRHS); |
467 | if (result.isInvalid()) return ExprError(); |
468 | |
469 | syntactic = CompoundAssignOperator::Create( |
470 | S.Context, syntacticLHS, capturedRHS, opcode, result.get()->getType(), |
471 | result.get()->getValueKind(), OK_Ordinary, opcLoc, |
472 | S.CurFPFeatureOverrides(), opLHS.get()->getType(), |
473 | result.get()->getType()); |
474 | } |
475 | |
476 | // The result of the assignment, if not void, is the value set into |
477 | // the l-value. |
478 | result = buildSet(result.get(), opcLoc, captureSetValueAsResult: captureSetValueAsResult()); |
479 | if (result.isInvalid()) return ExprError(); |
480 | addSemanticExpr(semantic: result.get()); |
481 | if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() && |
482 | (result.get()->isTypeDependent() || CanCaptureValue(exp: result.get()))) |
483 | setResultToLastSemantic(); |
484 | |
485 | return complete(syntactic); |
486 | } |
487 | |
488 | /// The basic skeleton for building an increment or decrement |
489 | /// operation. |
490 | ExprResult |
491 | PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc, |
492 | UnaryOperatorKind opcode, |
493 | Expr *op) { |
494 | assert(UnaryOperator::isIncrementDecrementOp(opcode)); |
495 | |
496 | Expr *syntacticOp = rebuildAndCaptureObject(op); |
497 | |
498 | // Load the value. |
499 | ExprResult result = buildGet(); |
500 | if (result.isInvalid()) return ExprError(); |
501 | |
502 | QualType resultType = result.get()->getType(); |
503 | |
504 | // That's the postfix result. |
505 | if (UnaryOperator::isPostfix(Op: opcode) && |
506 | (result.get()->isTypeDependent() || CanCaptureValue(exp: result.get()))) { |
507 | result = capture(e: result.get()); |
508 | setResultToLastSemantic(); |
509 | } |
510 | |
511 | // Add or subtract a literal 1. |
512 | llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1); |
513 | Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy, |
514 | GenericLoc); |
515 | |
516 | if (UnaryOperator::isIncrementOp(Op: opcode)) { |
517 | result = S.BuildBinOp(S: Sc, OpLoc: opcLoc, Opc: BO_Add, LHSExpr: result.get(), RHSExpr: one); |
518 | } else { |
519 | result = S.BuildBinOp(S: Sc, OpLoc: opcLoc, Opc: BO_Sub, LHSExpr: result.get(), RHSExpr: one); |
520 | } |
521 | if (result.isInvalid()) return ExprError(); |
522 | |
523 | // Store that back into the result. The value stored is the result |
524 | // of a prefix operation. |
525 | result = buildSet(result.get(), opcLoc, captureSetValueAsResult: UnaryOperator::isPrefix(Op: opcode) && |
526 | captureSetValueAsResult()); |
527 | if (result.isInvalid()) return ExprError(); |
528 | addSemanticExpr(semantic: result.get()); |
529 | if (UnaryOperator::isPrefix(Op: opcode) && !captureSetValueAsResult() && |
530 | !result.get()->getType()->isVoidType() && |
531 | (result.get()->isTypeDependent() || CanCaptureValue(exp: result.get()))) |
532 | setResultToLastSemantic(); |
533 | |
534 | UnaryOperator *syntactic = |
535 | UnaryOperator::Create(C: S.Context, input: syntacticOp, opc: opcode, type: resultType, |
536 | VK: VK_LValue, OK: OK_Ordinary, l: opcLoc, |
537 | CanOverflow: !resultType->isDependentType() |
538 | ? S.Context.getTypeSize(T: resultType) >= |
539 | S.Context.getTypeSize(S.Context.IntTy) |
540 | : false, |
541 | FPFeatures: S.CurFPFeatureOverrides()); |
542 | return complete(syntactic); |
543 | } |
544 | |
545 | |
546 | //===----------------------------------------------------------------------===// |
547 | // Objective-C @property and implicit property references |
548 | //===----------------------------------------------------------------------===// |
549 | |
550 | /// Look up a method in the receiver type of an Objective-C property |
551 | /// reference. |
552 | static ObjCMethodDecl *LookupMethodInReceiverType(Sema &S, Selector sel, |
553 | const ObjCPropertyRefExpr *PRE) { |
554 | if (PRE->isObjectReceiver()) { |
555 | const ObjCObjectPointerType *PT = |
556 | PRE->getBase()->getType()->castAs<ObjCObjectPointerType>(); |
557 | |
558 | // Special case for 'self' in class method implementations. |
559 | if (PT->isObjCClassType() && |
560 | S.isSelfExpr(RExpr: const_cast<Expr*>(PRE->getBase()))) { |
561 | // This cast is safe because isSelfExpr is only true within |
562 | // methods. |
563 | ObjCMethodDecl *method = |
564 | cast<ObjCMethodDecl>(Val: S.CurContext->getNonClosureAncestor()); |
565 | return S.LookupMethodInObjectType(Sel: sel, |
566 | Ty: S.Context.getObjCInterfaceType(Decl: method->getClassInterface()), |
567 | /*instance*/ IsInstance: false); |
568 | } |
569 | |
570 | return S.LookupMethodInObjectType(Sel: sel, Ty: PT->getPointeeType(), IsInstance: true); |
571 | } |
572 | |
573 | if (PRE->isSuperReceiver()) { |
574 | if (const ObjCObjectPointerType *PT = |
575 | PRE->getSuperReceiverType()->getAs<ObjCObjectPointerType>()) |
576 | return S.LookupMethodInObjectType(Sel: sel, Ty: PT->getPointeeType(), IsInstance: true); |
577 | |
578 | return S.LookupMethodInObjectType(Sel: sel, Ty: PRE->getSuperReceiverType(), IsInstance: false); |
579 | } |
580 | |
581 | assert(PRE->isClassReceiver() && "Invalid expression"); |
582 | QualType IT = S.Context.getObjCInterfaceType(Decl: PRE->getClassReceiver()); |
583 | return S.LookupMethodInObjectType(Sel: sel, Ty: IT, IsInstance: false); |
584 | } |
585 | |
586 | bool ObjCPropertyOpBuilder::isWeakProperty() const { |
587 | QualType T; |
588 | if (RefExpr->isExplicitProperty()) { |
589 | const ObjCPropertyDecl *Prop = RefExpr->getExplicitProperty(); |
590 | if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) |
591 | return true; |
592 | |
593 | T = Prop->getType(); |
594 | } else if (Getter) { |
595 | T = Getter->getReturnType(); |
596 | } else { |
597 | return false; |
598 | } |
599 | |
600 | return T.getObjCLifetime() == Qualifiers::OCL_Weak; |
601 | } |
602 | |
603 | bool ObjCPropertyOpBuilder::findGetter() { |
604 | if (Getter) return true; |
605 | |
606 | // For implicit properties, just trust the lookup we already did. |
607 | if (RefExpr->isImplicitProperty()) { |
608 | if ((Getter = RefExpr->getImplicitPropertyGetter())) { |
609 | GetterSelector = Getter->getSelector(); |
610 | return true; |
611 | } |
612 | else { |
613 | // Must build the getter selector the hard way. |
614 | ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter(); |
615 | assert(setter && "both setter and getter are null - cannot happen"); |
616 | const IdentifierInfo *setterName = |
617 | setter->getSelector().getIdentifierInfoForSlot(argIndex: 0); |
618 | const IdentifierInfo *getterName = |
619 | &S.Context.Idents.get(Name: setterName->getName().substr(Start: 3)); |
620 | GetterSelector = |
621 | S.PP.getSelectorTable().getNullarySelector(getterName); |
622 | return false; |
623 | } |
624 | } |
625 | |
626 | ObjCPropertyDecl *prop = RefExpr->getExplicitProperty(); |
627 | Getter = LookupMethodInReceiverType(S, sel: prop->getGetterName(), PRE: RefExpr); |
628 | return (Getter != nullptr); |
629 | } |
630 | |
631 | /// Try to find the most accurate setter declaration for the property |
632 | /// reference. |
633 | /// |
634 | /// \return true if a setter was found, in which case Setter |
635 | bool ObjCPropertyOpBuilder::findSetter(bool warn) { |
636 | // For implicit properties, just trust the lookup we already did. |
637 | if (RefExpr->isImplicitProperty()) { |
638 | if (ObjCMethodDecl *setter = RefExpr->getImplicitPropertySetter()) { |
639 | Setter = setter; |
640 | SetterSelector = setter->getSelector(); |
641 | return true; |
642 | } else { |
643 | const IdentifierInfo *getterName = RefExpr->getImplicitPropertyGetter() |
644 | ->getSelector() |
645 | .getIdentifierInfoForSlot(argIndex: 0); |
646 | SetterSelector = |
647 | SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(), |
648 | S.PP.getSelectorTable(), |
649 | getterName); |
650 | return false; |
651 | } |
652 | } |
653 | |
654 | // For explicit properties, this is more involved. |
655 | ObjCPropertyDecl *prop = RefExpr->getExplicitProperty(); |
656 | SetterSelector = prop->getSetterName(); |
657 | |
658 | // Do a normal method lookup first. |
659 | if (ObjCMethodDecl *setter = |
660 | LookupMethodInReceiverType(S, SetterSelector, RefExpr)) { |
661 | if (setter->isPropertyAccessor() && warn) |
662 | if (const ObjCInterfaceDecl *IFace = |
663 | dyn_cast<ObjCInterfaceDecl>(setter->getDeclContext())) { |
664 | StringRef thisPropertyName = prop->getName(); |
665 | // Try flipping the case of the first character. |
666 | char front = thisPropertyName.front(); |
667 | front = isLowercase(c: front) ? toUppercase(c: front) : toLowercase(c: front); |
668 | SmallString<100> PropertyName = thisPropertyName; |
669 | PropertyName[0] = front; |
670 | const IdentifierInfo *AltMember = |
671 | &S.PP.getIdentifierTable().get(Name: PropertyName); |
672 | if (ObjCPropertyDecl *prop1 = IFace->FindPropertyDeclaration( |
673 | AltMember, prop->getQueryKind())) |
674 | if (prop != prop1 && (prop1->getSetterMethodDecl() == setter)) { |
675 | S.Diag(RefExpr->getExprLoc(), diag::err_property_setter_ambiguous_use) |
676 | << prop << prop1 << setter->getSelector(); |
677 | S.Diag(prop->getLocation(), diag::note_property_declare); |
678 | S.Diag(prop1->getLocation(), diag::note_property_declare); |
679 | } |
680 | } |
681 | Setter = setter; |
682 | return true; |
683 | } |
684 | |
685 | // That can fail in the somewhat crazy situation that we're |
686 | // type-checking a message send within the @interface declaration |
687 | // that declared the @property. But it's not clear that that's |
688 | // valuable to support. |
689 | |
690 | return false; |
691 | } |
692 | |
693 | void ObjCPropertyOpBuilder::DiagnoseUnsupportedPropertyUse() { |
694 | if (S.getCurLexicalContext()->isObjCContainer() && |
695 | S.getCurLexicalContext()->getDeclKind() != Decl::ObjCCategoryImpl && |
696 | S.getCurLexicalContext()->getDeclKind() != Decl::ObjCImplementation) { |
697 | if (ObjCPropertyDecl *prop = RefExpr->getExplicitProperty()) { |
698 | S.Diag(RefExpr->getLocation(), |
699 | diag::err_property_function_in_objc_container); |
700 | S.Diag(prop->getLocation(), diag::note_property_declare); |
701 | } |
702 | } |
703 | } |
704 | |
705 | /// Capture the base object of an Objective-C property expression. |
706 | Expr *ObjCPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) { |
707 | assert(InstanceReceiver == nullptr); |
708 | |
709 | // If we have a base, capture it in an OVE and rebuild the syntactic |
710 | // form to use the OVE as its base. |
711 | if (RefExpr->isObjectReceiver()) { |
712 | InstanceReceiver = capture(RefExpr->getBase()); |
713 | syntacticBase = Rebuilder(S, [=](Expr *, unsigned) -> Expr * { |
714 | return InstanceReceiver; |
715 | }).rebuild(e: syntacticBase); |
716 | } |
717 | |
718 | if (ObjCPropertyRefExpr * |
719 | refE = dyn_cast<ObjCPropertyRefExpr>(Val: syntacticBase->IgnoreParens())) |
720 | SyntacticRefExpr = refE; |
721 | |
722 | return syntacticBase; |
723 | } |
724 | |
725 | /// Load from an Objective-C property reference. |
726 | ExprResult ObjCPropertyOpBuilder::buildGet() { |
727 | findGetter(); |
728 | if (!Getter) { |
729 | DiagnoseUnsupportedPropertyUse(); |
730 | return ExprError(); |
731 | } |
732 | |
733 | if (SyntacticRefExpr) |
734 | SyntacticRefExpr->setIsMessagingGetter(); |
735 | |
736 | QualType receiverType = RefExpr->getReceiverType(ctx: S.Context); |
737 | if (!Getter->isImplicit()) |
738 | S.DiagnoseUseOfDecl(Getter, GenericLoc, nullptr, true); |
739 | // Build a message-send. |
740 | ExprResult msg; |
741 | if ((Getter->isInstanceMethod() && !RefExpr->isClassReceiver()) || |
742 | RefExpr->isObjectReceiver()) { |
743 | assert(InstanceReceiver || RefExpr->isSuperReceiver()); |
744 | msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType, |
745 | GenericLoc, Getter->getSelector(), |
746 | Getter, std::nullopt); |
747 | } else { |
748 | msg = S.BuildClassMessageImplicit(ReceiverType: receiverType, isSuperReceiver: RefExpr->isSuperReceiver(), |
749 | Loc: GenericLoc, Sel: Getter->getSelector(), Method: Getter, |
750 | Args: std::nullopt); |
751 | } |
752 | return msg; |
753 | } |
754 | |
755 | /// Store to an Objective-C property reference. |
756 | /// |
757 | /// \param captureSetValueAsResult If true, capture the actual |
758 | /// value being set as the value of the property operation. |
759 | ExprResult ObjCPropertyOpBuilder::buildSet(Expr *op, SourceLocation opcLoc, |
760 | bool captureSetValueAsResult) { |
761 | if (!findSetter(warn: false)) { |
762 | DiagnoseUnsupportedPropertyUse(); |
763 | return ExprError(); |
764 | } |
765 | |
766 | if (SyntacticRefExpr) |
767 | SyntacticRefExpr->setIsMessagingSetter(); |
768 | |
769 | QualType receiverType = RefExpr->getReceiverType(ctx: S.Context); |
770 | |
771 | // Use assignment constraints when possible; they give us better |
772 | // diagnostics. "When possible" basically means anything except a |
773 | // C++ class type. |
774 | if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) { |
775 | QualType paramType = (*Setter->param_begin())->getType() |
776 | .substObjCMemberType( |
777 | receiverType, |
778 | Setter->getDeclContext(), |
779 | ObjCSubstitutionContext::Parameter); |
780 | if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) { |
781 | ExprResult opResult = op; |
782 | Sema::AssignConvertType assignResult |
783 | = S.CheckSingleAssignmentConstraints(LHSType: paramType, RHS&: opResult); |
784 | if (opResult.isInvalid() || |
785 | S.DiagnoseAssignmentResult(ConvTy: assignResult, Loc: opcLoc, DstType: paramType, |
786 | SrcType: op->getType(), SrcExpr: opResult.get(), |
787 | Action: Sema::AA_Assigning)) |
788 | return ExprError(); |
789 | |
790 | op = opResult.get(); |
791 | assert(op && "successful assignment left argument invalid?"); |
792 | } |
793 | } |
794 | |
795 | // Arguments. |
796 | Expr *args[] = { op }; |
797 | |
798 | // Build a message-send. |
799 | ExprResult msg; |
800 | if (!Setter->isImplicit()) |
801 | S.DiagnoseUseOfDecl(Setter, GenericLoc, nullptr, true); |
802 | if ((Setter->isInstanceMethod() && !RefExpr->isClassReceiver()) || |
803 | RefExpr->isObjectReceiver()) { |
804 | msg = S.BuildInstanceMessageImplicit(InstanceReceiver, receiverType, |
805 | GenericLoc, SetterSelector, Setter, |
806 | MultiExprArg(args, 1)); |
807 | } else { |
808 | msg = S.BuildClassMessageImplicit(receiverType, RefExpr->isSuperReceiver(), |
809 | GenericLoc, |
810 | SetterSelector, Setter, |
811 | MultiExprArg(args, 1)); |
812 | } |
813 | |
814 | if (!msg.isInvalid() && captureSetValueAsResult) { |
815 | ObjCMessageExpr *msgExpr = |
816 | cast<ObjCMessageExpr>(Val: msg.get()->IgnoreImplicit()); |
817 | Expr *arg = msgExpr->getArg(Arg: 0); |
818 | if (CanCaptureValue(exp: arg)) |
819 | msgExpr->setArg(Arg: 0, ArgExpr: captureValueAsResult(arg)); |
820 | } |
821 | |
822 | return msg; |
823 | } |
824 | |
825 | /// @property-specific behavior for doing lvalue-to-rvalue conversion. |
826 | ExprResult ObjCPropertyOpBuilder::buildRValueOperation(Expr *op) { |
827 | // Explicit properties always have getters, but implicit ones don't. |
828 | // Check that before proceeding. |
829 | if (RefExpr->isImplicitProperty() && !RefExpr->getImplicitPropertyGetter()) { |
830 | S.Diag(RefExpr->getLocation(), diag::err_getter_not_found) |
831 | << RefExpr->getSourceRange(); |
832 | return ExprError(); |
833 | } |
834 | |
835 | ExprResult result = PseudoOpBuilder::buildRValueOperation(op); |
836 | if (result.isInvalid()) return ExprError(); |
837 | |
838 | if (RefExpr->isExplicitProperty() && !Getter->hasRelatedResultType()) |
839 | S.DiagnosePropertyAccessorMismatch(PD: RefExpr->getExplicitProperty(), |
840 | Getter, Loc: RefExpr->getLocation()); |
841 | |
842 | // As a special case, if the method returns 'id', try to get |
843 | // a better type from the property. |
844 | if (RefExpr->isExplicitProperty() && result.get()->isPRValue()) { |
845 | QualType receiverType = RefExpr->getReceiverType(ctx: S.Context); |
846 | QualType propType = RefExpr->getExplicitProperty() |
847 | ->getUsageType(objectType: receiverType); |
848 | if (result.get()->getType()->isObjCIdType()) { |
849 | if (const ObjCObjectPointerType *ptr |
850 | = propType->getAs<ObjCObjectPointerType>()) { |
851 | if (!ptr->isObjCIdType()) |
852 | result = S.ImpCastExprToType(E: result.get(), Type: propType, CK: CK_BitCast); |
853 | } |
854 | } |
855 | if (propType.getObjCLifetime() == Qualifiers::OCL_Weak && |
856 | !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, |
857 | RefExpr->getLocation())) |
858 | S.getCurFunction()->markSafeWeakUse(RefExpr); |
859 | } |
860 | |
861 | return result; |
862 | } |
863 | |
864 | /// Try to build this as a call to a getter that returns a reference. |
865 | /// |
866 | /// \return true if it was possible, whether or not it actually |
867 | /// succeeded |
868 | bool ObjCPropertyOpBuilder::tryBuildGetOfReference(Expr *op, |
869 | ExprResult &result) { |
870 | if (!S.getLangOpts().CPlusPlus) return false; |
871 | |
872 | findGetter(); |
873 | if (!Getter) { |
874 | // The property has no setter and no getter! This can happen if the type is |
875 | // invalid. Error have already been reported. |
876 | result = ExprError(); |
877 | return true; |
878 | } |
879 | |
880 | // Only do this if the getter returns an l-value reference type. |
881 | QualType resultType = Getter->getReturnType(); |
882 | if (!resultType->isLValueReferenceType()) return false; |
883 | |
884 | result = buildRValueOperation(op); |
885 | return true; |
886 | } |
887 | |
888 | /// @property-specific behavior for doing assignments. |
889 | ExprResult |
890 | ObjCPropertyOpBuilder::buildAssignmentOperation(Scope *Sc, |
891 | SourceLocation opcLoc, |
892 | BinaryOperatorKind opcode, |
893 | Expr *LHS, Expr *RHS) { |
894 | assert(BinaryOperator::isAssignmentOp(opcode)); |
895 | |
896 | // If there's no setter, we have no choice but to try to assign to |
897 | // the result of the getter. |
898 | if (!findSetter()) { |
899 | ExprResult result; |
900 | if (tryBuildGetOfReference(op: LHS, result)) { |
901 | if (result.isInvalid()) return ExprError(); |
902 | return S.BuildBinOp(S: Sc, OpLoc: opcLoc, Opc: opcode, LHSExpr: result.get(), RHSExpr: RHS); |
903 | } |
904 | |
905 | // Otherwise, it's an error. |
906 | S.Diag(opcLoc, diag::err_nosetter_property_assignment) |
907 | << unsigned(RefExpr->isImplicitProperty()) |
908 | << SetterSelector |
909 | << LHS->getSourceRange() << RHS->getSourceRange(); |
910 | return ExprError(); |
911 | } |
912 | |
913 | // If there is a setter, we definitely want to use it. |
914 | |
915 | // Verify that we can do a compound assignment. |
916 | if (opcode != BO_Assign && !findGetter()) { |
917 | S.Diag(opcLoc, diag::err_nogetter_property_compound_assignment) |
918 | << LHS->getSourceRange() << RHS->getSourceRange(); |
919 | return ExprError(); |
920 | } |
921 | |
922 | ExprResult result = |
923 | PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS); |
924 | if (result.isInvalid()) return ExprError(); |
925 | |
926 | // Various warnings about property assignments in ARC. |
927 | if (S.getLangOpts().ObjCAutoRefCount && InstanceReceiver) { |
928 | S.checkRetainCycles(receiver: InstanceReceiver->getSourceExpr(), argument: RHS); |
929 | S.checkUnsafeExprAssigns(Loc: opcLoc, LHS, RHS); |
930 | } |
931 | |
932 | return result; |
933 | } |
934 | |
935 | /// @property-specific behavior for doing increments and decrements. |
936 | ExprResult |
937 | ObjCPropertyOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc, |
938 | UnaryOperatorKind opcode, |
939 | Expr *op) { |
940 | // If there's no setter, we have no choice but to try to assign to |
941 | // the result of the getter. |
942 | if (!findSetter()) { |
943 | ExprResult result; |
944 | if (tryBuildGetOfReference(op, result)) { |
945 | if (result.isInvalid()) return ExprError(); |
946 | return S.BuildUnaryOp(S: Sc, OpLoc: opcLoc, Opc: opcode, Input: result.get()); |
947 | } |
948 | |
949 | // Otherwise, it's an error. |
950 | S.Diag(opcLoc, diag::err_nosetter_property_incdec) |
951 | << unsigned(RefExpr->isImplicitProperty()) |
952 | << unsigned(UnaryOperator::isDecrementOp(opcode)) |
953 | << SetterSelector |
954 | << op->getSourceRange(); |
955 | return ExprError(); |
956 | } |
957 | |
958 | // If there is a setter, we definitely want to use it. |
959 | |
960 | // We also need a getter. |
961 | if (!findGetter()) { |
962 | assert(RefExpr->isImplicitProperty()); |
963 | S.Diag(opcLoc, diag::err_nogetter_property_incdec) |
964 | << unsigned(UnaryOperator::isDecrementOp(opcode)) |
965 | << GetterSelector |
966 | << op->getSourceRange(); |
967 | return ExprError(); |
968 | } |
969 | |
970 | return PseudoOpBuilder::buildIncDecOperation(Sc, opcLoc, opcode, op); |
971 | } |
972 | |
973 | ExprResult ObjCPropertyOpBuilder::complete(Expr *SyntacticForm) { |
974 | if (isWeakProperty() && !S.isUnevaluatedContext() && |
975 | !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, |
976 | SyntacticForm->getBeginLoc())) |
977 | S.getCurFunction()->recordUseOfWeak(E: SyntacticRefExpr, |
978 | IsRead: SyntacticRefExpr->isMessagingGetter()); |
979 | |
980 | return PseudoOpBuilder::complete(SyntacticForm); |
981 | } |
982 | |
983 | // ObjCSubscript build stuff. |
984 | // |
985 | |
986 | /// objective-c subscripting-specific behavior for doing lvalue-to-rvalue |
987 | /// conversion. |
988 | /// FIXME. Remove this routine if it is proven that no additional |
989 | /// specifity is needed. |
990 | ExprResult ObjCSubscriptOpBuilder::buildRValueOperation(Expr *op) { |
991 | ExprResult result = PseudoOpBuilder::buildRValueOperation(op); |
992 | if (result.isInvalid()) return ExprError(); |
993 | return result; |
994 | } |
995 | |
996 | /// objective-c subscripting-specific behavior for doing assignments. |
997 | ExprResult |
998 | ObjCSubscriptOpBuilder::buildAssignmentOperation(Scope *Sc, |
999 | SourceLocation opcLoc, |
1000 | BinaryOperatorKind opcode, |
1001 | Expr *LHS, Expr *RHS) { |
1002 | assert(BinaryOperator::isAssignmentOp(opcode)); |
1003 | // There must be a method to do the Index'ed assignment. |
1004 | if (!findAtIndexSetter()) |
1005 | return ExprError(); |
1006 | |
1007 | // Verify that we can do a compound assignment. |
1008 | if (opcode != BO_Assign && !findAtIndexGetter()) |
1009 | return ExprError(); |
1010 | |
1011 | ExprResult result = |
1012 | PseudoOpBuilder::buildAssignmentOperation(Sc, opcLoc, opcode, LHS, RHS); |
1013 | if (result.isInvalid()) return ExprError(); |
1014 | |
1015 | // Various warnings about objc Index'ed assignments in ARC. |
1016 | if (S.getLangOpts().ObjCAutoRefCount && InstanceBase) { |
1017 | S.checkRetainCycles(receiver: InstanceBase->getSourceExpr(), argument: RHS); |
1018 | S.checkUnsafeExprAssigns(Loc: opcLoc, LHS, RHS); |
1019 | } |
1020 | |
1021 | return result; |
1022 | } |
1023 | |
1024 | /// Capture the base object of an Objective-C Index'ed expression. |
1025 | Expr *ObjCSubscriptOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) { |
1026 | assert(InstanceBase == nullptr); |
1027 | |
1028 | // Capture base expression in an OVE and rebuild the syntactic |
1029 | // form to use the OVE as its base expression. |
1030 | InstanceBase = capture(RefExpr->getBaseExpr()); |
1031 | InstanceKey = capture(RefExpr->getKeyExpr()); |
1032 | |
1033 | syntacticBase = |
1034 | Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * { |
1035 | switch (Idx) { |
1036 | case 0: |
1037 | return InstanceBase; |
1038 | case 1: |
1039 | return InstanceKey; |
1040 | default: |
1041 | llvm_unreachable("Unexpected index for ObjCSubscriptExpr"); |
1042 | } |
1043 | }).rebuild(e: syntacticBase); |
1044 | |
1045 | return syntacticBase; |
1046 | } |
1047 | |
1048 | /// CheckSubscriptingKind - This routine decide what type |
1049 | /// of indexing represented by "FromE" is being done. |
1050 | Sema::ObjCSubscriptKind |
1051 | Sema::CheckSubscriptingKind(Expr *FromE) { |
1052 | // If the expression already has integral or enumeration type, we're golden. |
1053 | QualType T = FromE->getType(); |
1054 | if (T->isIntegralOrEnumerationType()) |
1055 | return OS_Array; |
1056 | |
1057 | // If we don't have a class type in C++, there's no way we can get an |
1058 | // expression of integral or enumeration type. |
1059 | const RecordType *RecordTy = T->getAs<RecordType>(); |
1060 | if (!RecordTy && |
1061 | (T->isObjCObjectPointerType() || T->isVoidPointerType())) |
1062 | // All other scalar cases are assumed to be dictionary indexing which |
1063 | // caller handles, with diagnostics if needed. |
1064 | return OS_Dictionary; |
1065 | if (!getLangOpts().CPlusPlus || |
1066 | !RecordTy || RecordTy->isIncompleteType()) { |
1067 | // No indexing can be done. Issue diagnostics and quit. |
1068 | const Expr *IndexExpr = FromE->IgnoreParenImpCasts(); |
1069 | if (isa<StringLiteral>(IndexExpr)) |
1070 | Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer) |
1071 | << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@"); |
1072 | else |
1073 | Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion) |
1074 | << T; |
1075 | return OS_Error; |
1076 | } |
1077 | |
1078 | // We must have a complete class type. |
1079 | if (RequireCompleteType(FromE->getExprLoc(), T, |
1080 | diag::err_objc_index_incomplete_class_type, FromE)) |
1081 | return OS_Error; |
1082 | |
1083 | // Look for a conversion to an integral, enumeration type, or |
1084 | // objective-C pointer type. |
1085 | int NoIntegrals=0, NoObjCIdPointers=0; |
1086 | SmallVector<CXXConversionDecl *, 4> ConversionDecls; |
1087 | |
1088 | for (NamedDecl *D : cast<CXXRecordDecl>(Val: RecordTy->getDecl()) |
1089 | ->getVisibleConversionFunctions()) { |
1090 | if (CXXConversionDecl *Conversion = |
1091 | dyn_cast<CXXConversionDecl>(Val: D->getUnderlyingDecl())) { |
1092 | QualType CT = Conversion->getConversionType().getNonReferenceType(); |
1093 | if (CT->isIntegralOrEnumerationType()) { |
1094 | ++NoIntegrals; |
1095 | ConversionDecls.push_back(Elt: Conversion); |
1096 | } |
1097 | else if (CT->isObjCIdType() ||CT->isBlockPointerType()) { |
1098 | ++NoObjCIdPointers; |
1099 | ConversionDecls.push_back(Elt: Conversion); |
1100 | } |
1101 | } |
1102 | } |
1103 | if (NoIntegrals ==1 && NoObjCIdPointers == 0) |
1104 | return OS_Array; |
1105 | if (NoIntegrals == 0 && NoObjCIdPointers == 1) |
1106 | return OS_Dictionary; |
1107 | if (NoIntegrals == 0 && NoObjCIdPointers == 0) { |
1108 | // No conversion function was found. Issue diagnostic and return. |
1109 | Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion) |
1110 | << FromE->getType(); |
1111 | return OS_Error; |
1112 | } |
1113 | Diag(FromE->getExprLoc(), diag::err_objc_multiple_subscript_type_conversion) |
1114 | << FromE->getType(); |
1115 | for (unsigned int i = 0; i < ConversionDecls.size(); i++) |
1116 | Diag(ConversionDecls[i]->getLocation(), |
1117 | diag::note_conv_function_declared_at); |
1118 | |
1119 | return OS_Error; |
1120 | } |
1121 | |
1122 | /// CheckKeyForObjCARCConversion - This routine suggests bridge casting of CF |
1123 | /// objects used as dictionary subscript key objects. |
1124 | static void CheckKeyForObjCARCConversion(Sema &S, QualType ContainerT, |
1125 | Expr *Key) { |
1126 | if (ContainerT.isNull()) |
1127 | return; |
1128 | // dictionary subscripting. |
1129 | // - (id)objectForKeyedSubscript:(id)key; |
1130 | const IdentifierInfo *KeyIdents[] = { |
1131 | &S.Context.Idents.get(Name: "objectForKeyedSubscript")}; |
1132 | Selector GetterSelector = S.Context.Selectors.getSelector(NumArgs: 1, IIV: KeyIdents); |
1133 | ObjCMethodDecl *Getter = S.LookupMethodInObjectType(Sel: GetterSelector, Ty: ContainerT, |
1134 | IsInstance: true /*instance*/); |
1135 | if (!Getter) |
1136 | return; |
1137 | QualType T = Getter->parameters()[0]->getType(); |
1138 | S.CheckObjCConversion(castRange: Key->getSourceRange(), castType: T, op&: Key, |
1139 | CCK: CheckedConversionKind::Implicit); |
1140 | } |
1141 | |
1142 | bool ObjCSubscriptOpBuilder::findAtIndexGetter() { |
1143 | if (AtIndexGetter) |
1144 | return true; |
1145 | |
1146 | Expr *BaseExpr = RefExpr->getBaseExpr(); |
1147 | QualType BaseT = BaseExpr->getType(); |
1148 | |
1149 | QualType ResultType; |
1150 | if (const ObjCObjectPointerType *PTy = |
1151 | BaseT->getAs<ObjCObjectPointerType>()) { |
1152 | ResultType = PTy->getPointeeType(); |
1153 | } |
1154 | Sema::ObjCSubscriptKind Res = |
1155 | S.CheckSubscriptingKind(FromE: RefExpr->getKeyExpr()); |
1156 | if (Res == Sema::OS_Error) { |
1157 | if (S.getLangOpts().ObjCAutoRefCount) |
1158 | CheckKeyForObjCARCConversion(S, ContainerT: ResultType, |
1159 | Key: RefExpr->getKeyExpr()); |
1160 | return false; |
1161 | } |
1162 | bool arrayRef = (Res == Sema::OS_Array); |
1163 | |
1164 | if (ResultType.isNull()) { |
1165 | S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type) |
1166 | << BaseExpr->getType() << arrayRef; |
1167 | return false; |
1168 | } |
1169 | if (!arrayRef) { |
1170 | // dictionary subscripting. |
1171 | // - (id)objectForKeyedSubscript:(id)key; |
1172 | const IdentifierInfo *KeyIdents[] = { |
1173 | &S.Context.Idents.get(Name: "objectForKeyedSubscript")}; |
1174 | AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents); |
1175 | } |
1176 | else { |
1177 | // - (id)objectAtIndexedSubscript:(size_t)index; |
1178 | const IdentifierInfo *KeyIdents[] = { |
1179 | &S.Context.Idents.get(Name: "objectAtIndexedSubscript")}; |
1180 | |
1181 | AtIndexGetterSelector = S.Context.Selectors.getSelector(1, KeyIdents); |
1182 | } |
1183 | |
1184 | AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType, |
1185 | true /*instance*/); |
1186 | |
1187 | if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) { |
1188 | AtIndexGetter = ObjCMethodDecl::Create( |
1189 | S.Context, SourceLocation(), SourceLocation(), AtIndexGetterSelector, |
1190 | S.Context.getObjCIdType() /*ReturnType*/, nullptr /*TypeSourceInfo */, |
1191 | S.Context.getTranslationUnitDecl(), true /*Instance*/, |
1192 | false /*isVariadic*/, |
1193 | /*isPropertyAccessor=*/false, |
1194 | /*isSynthesizedAccessorStub=*/false, |
1195 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
1196 | ObjCImplementationControl::Required, false); |
1197 | ParmVarDecl *Argument = ParmVarDecl::Create(C&: S.Context, DC: AtIndexGetter, |
1198 | StartLoc: SourceLocation(), IdLoc: SourceLocation(), |
1199 | Id: arrayRef ? &S.Context.Idents.get(Name: "index") |
1200 | : &S.Context.Idents.get(Name: "key"), |
1201 | T: arrayRef ? S.Context.UnsignedLongTy |
1202 | : S.Context.getObjCIdType(), |
1203 | /*TInfo=*/nullptr, |
1204 | S: SC_None, |
1205 | DefArg: nullptr); |
1206 | AtIndexGetter->setMethodParams(C&: S.Context, Params: Argument, SelLocs: std::nullopt); |
1207 | } |
1208 | |
1209 | if (!AtIndexGetter) { |
1210 | if (!BaseT->isObjCIdType()) { |
1211 | S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found) |
1212 | << BaseExpr->getType() << 0 << arrayRef; |
1213 | return false; |
1214 | } |
1215 | AtIndexGetter = |
1216 | S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector, |
1217 | RefExpr->getSourceRange(), |
1218 | true); |
1219 | } |
1220 | |
1221 | if (AtIndexGetter) { |
1222 | QualType T = AtIndexGetter->parameters()[0]->getType(); |
1223 | if ((arrayRef && !T->isIntegralOrEnumerationType()) || |
1224 | (!arrayRef && !T->isObjCObjectPointerType())) { |
1225 | S.Diag(RefExpr->getKeyExpr()->getExprLoc(), |
1226 | arrayRef ? diag::err_objc_subscript_index_type |
1227 | : diag::err_objc_subscript_key_type) << T; |
1228 | S.Diag(AtIndexGetter->parameters()[0]->getLocation(), |
1229 | diag::note_parameter_type) << T; |
1230 | return false; |
1231 | } |
1232 | QualType R = AtIndexGetter->getReturnType(); |
1233 | if (!R->isObjCObjectPointerType()) { |
1234 | S.Diag(RefExpr->getKeyExpr()->getExprLoc(), |
1235 | diag::err_objc_indexing_method_result_type) << R << arrayRef; |
1236 | S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) << |
1237 | AtIndexGetter->getDeclName(); |
1238 | } |
1239 | } |
1240 | return true; |
1241 | } |
1242 | |
1243 | bool ObjCSubscriptOpBuilder::findAtIndexSetter() { |
1244 | if (AtIndexSetter) |
1245 | return true; |
1246 | |
1247 | Expr *BaseExpr = RefExpr->getBaseExpr(); |
1248 | QualType BaseT = BaseExpr->getType(); |
1249 | |
1250 | QualType ResultType; |
1251 | if (const ObjCObjectPointerType *PTy = |
1252 | BaseT->getAs<ObjCObjectPointerType>()) { |
1253 | ResultType = PTy->getPointeeType(); |
1254 | } |
1255 | |
1256 | Sema::ObjCSubscriptKind Res = |
1257 | S.CheckSubscriptingKind(FromE: RefExpr->getKeyExpr()); |
1258 | if (Res == Sema::OS_Error) { |
1259 | if (S.getLangOpts().ObjCAutoRefCount) |
1260 | CheckKeyForObjCARCConversion(S, ContainerT: ResultType, |
1261 | Key: RefExpr->getKeyExpr()); |
1262 | return false; |
1263 | } |
1264 | bool arrayRef = (Res == Sema::OS_Array); |
1265 | |
1266 | if (ResultType.isNull()) { |
1267 | S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type) |
1268 | << BaseExpr->getType() << arrayRef; |
1269 | return false; |
1270 | } |
1271 | |
1272 | if (!arrayRef) { |
1273 | // dictionary subscripting. |
1274 | // - (void)setObject:(id)object forKeyedSubscript:(id)key; |
1275 | const IdentifierInfo *KeyIdents[] = { |
1276 | &S.Context.Idents.get(Name: "setObject"), |
1277 | &S.Context.Idents.get(Name: "forKeyedSubscript")}; |
1278 | AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents); |
1279 | } |
1280 | else { |
1281 | // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index; |
1282 | const IdentifierInfo *KeyIdents[] = { |
1283 | &S.Context.Idents.get(Name: "setObject"), |
1284 | &S.Context.Idents.get(Name: "atIndexedSubscript")}; |
1285 | AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents); |
1286 | } |
1287 | AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType, |
1288 | true /*instance*/); |
1289 | |
1290 | if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) { |
1291 | TypeSourceInfo *ReturnTInfo = nullptr; |
1292 | QualType ReturnType = S.Context.VoidTy; |
1293 | AtIndexSetter = ObjCMethodDecl::Create( |
1294 | S.Context, SourceLocation(), SourceLocation(), AtIndexSetterSelector, |
1295 | ReturnType, ReturnTInfo, S.Context.getTranslationUnitDecl(), |
1296 | true /*Instance*/, false /*isVariadic*/, |
1297 | /*isPropertyAccessor=*/false, |
1298 | /*isSynthesizedAccessorStub=*/false, |
1299 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
1300 | ObjCImplementationControl::Required, false); |
1301 | SmallVector<ParmVarDecl *, 2> Params; |
1302 | ParmVarDecl *object = ParmVarDecl::Create(S.Context, AtIndexSetter, |
1303 | SourceLocation(), SourceLocation(), |
1304 | &S.Context.Idents.get(Name: "object"), |
1305 | S.Context.getObjCIdType(), |
1306 | /*TInfo=*/nullptr, |
1307 | SC_None, |
1308 | nullptr); |
1309 | Params.push_back(Elt: object); |
1310 | ParmVarDecl *key = ParmVarDecl::Create(C&: S.Context, DC: AtIndexSetter, |
1311 | StartLoc: SourceLocation(), IdLoc: SourceLocation(), |
1312 | Id: arrayRef ? &S.Context.Idents.get(Name: "index") |
1313 | : &S.Context.Idents.get(Name: "key"), |
1314 | T: arrayRef ? S.Context.UnsignedLongTy |
1315 | : S.Context.getObjCIdType(), |
1316 | /*TInfo=*/nullptr, |
1317 | S: SC_None, |
1318 | DefArg: nullptr); |
1319 | Params.push_back(Elt: key); |
1320 | AtIndexSetter->setMethodParams(C&: S.Context, Params, SelLocs: std::nullopt); |
1321 | } |
1322 | |
1323 | if (!AtIndexSetter) { |
1324 | if (!BaseT->isObjCIdType()) { |
1325 | S.Diag(BaseExpr->getExprLoc(), |
1326 | diag::err_objc_subscript_method_not_found) |
1327 | << BaseExpr->getType() << 1 << arrayRef; |
1328 | return false; |
1329 | } |
1330 | AtIndexSetter = |
1331 | S.LookupInstanceMethodInGlobalPool(AtIndexSetterSelector, |
1332 | RefExpr->getSourceRange(), |
1333 | true); |
1334 | } |
1335 | |
1336 | bool err = false; |
1337 | if (AtIndexSetter && arrayRef) { |
1338 | QualType T = AtIndexSetter->parameters()[1]->getType(); |
1339 | if (!T->isIntegralOrEnumerationType()) { |
1340 | S.Diag(RefExpr->getKeyExpr()->getExprLoc(), |
1341 | diag::err_objc_subscript_index_type) << T; |
1342 | S.Diag(AtIndexSetter->parameters()[1]->getLocation(), |
1343 | diag::note_parameter_type) << T; |
1344 | err = true; |
1345 | } |
1346 | T = AtIndexSetter->parameters()[0]->getType(); |
1347 | if (!T->isObjCObjectPointerType()) { |
1348 | S.Diag(RefExpr->getBaseExpr()->getExprLoc(), |
1349 | diag::err_objc_subscript_object_type) << T << arrayRef; |
1350 | S.Diag(AtIndexSetter->parameters()[0]->getLocation(), |
1351 | diag::note_parameter_type) << T; |
1352 | err = true; |
1353 | } |
1354 | } |
1355 | else if (AtIndexSetter && !arrayRef) |
1356 | for (unsigned i=0; i <2; i++) { |
1357 | QualType T = AtIndexSetter->parameters()[i]->getType(); |
1358 | if (!T->isObjCObjectPointerType()) { |
1359 | if (i == 1) |
1360 | S.Diag(RefExpr->getKeyExpr()->getExprLoc(), |
1361 | diag::err_objc_subscript_key_type) << T; |
1362 | else |
1363 | S.Diag(RefExpr->getBaseExpr()->getExprLoc(), |
1364 | diag::err_objc_subscript_dic_object_type) << T; |
1365 | S.Diag(AtIndexSetter->parameters()[i]->getLocation(), |
1366 | diag::note_parameter_type) << T; |
1367 | err = true; |
1368 | } |
1369 | } |
1370 | |
1371 | return !err; |
1372 | } |
1373 | |
1374 | // Get the object at "Index" position in the container. |
1375 | // [BaseExpr objectAtIndexedSubscript : IndexExpr]; |
1376 | ExprResult ObjCSubscriptOpBuilder::buildGet() { |
1377 | if (!findAtIndexGetter()) |
1378 | return ExprError(); |
1379 | |
1380 | QualType receiverType = InstanceBase->getType(); |
1381 | |
1382 | // Build a message-send. |
1383 | ExprResult msg; |
1384 | Expr *Index = InstanceKey; |
1385 | |
1386 | // Arguments. |
1387 | Expr *args[] = { Index }; |
1388 | assert(InstanceBase); |
1389 | if (AtIndexGetter) |
1390 | S.DiagnoseUseOfDecl(AtIndexGetter, GenericLoc); |
1391 | msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType, |
1392 | GenericLoc, |
1393 | AtIndexGetterSelector, AtIndexGetter, |
1394 | MultiExprArg(args, 1)); |
1395 | return msg; |
1396 | } |
1397 | |
1398 | /// Store into the container the "op" object at "Index"'ed location |
1399 | /// by building this messaging expression: |
1400 | /// - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index; |
1401 | /// \param captureSetValueAsResult If true, capture the actual |
1402 | /// value being set as the value of the property operation. |
1403 | ExprResult ObjCSubscriptOpBuilder::buildSet(Expr *op, SourceLocation opcLoc, |
1404 | bool captureSetValueAsResult) { |
1405 | if (!findAtIndexSetter()) |
1406 | return ExprError(); |
1407 | if (AtIndexSetter) |
1408 | S.DiagnoseUseOfDecl(AtIndexSetter, GenericLoc); |
1409 | QualType receiverType = InstanceBase->getType(); |
1410 | Expr *Index = InstanceKey; |
1411 | |
1412 | // Arguments. |
1413 | Expr *args[] = { op, Index }; |
1414 | |
1415 | // Build a message-send. |
1416 | ExprResult msg = S.BuildInstanceMessageImplicit(InstanceBase, receiverType, |
1417 | GenericLoc, |
1418 | AtIndexSetterSelector, |
1419 | AtIndexSetter, |
1420 | MultiExprArg(args, 2)); |
1421 | |
1422 | if (!msg.isInvalid() && captureSetValueAsResult) { |
1423 | ObjCMessageExpr *msgExpr = |
1424 | cast<ObjCMessageExpr>(Val: msg.get()->IgnoreImplicit()); |
1425 | Expr *arg = msgExpr->getArg(Arg: 0); |
1426 | if (CanCaptureValue(exp: arg)) |
1427 | msgExpr->setArg(Arg: 0, ArgExpr: captureValueAsResult(arg)); |
1428 | } |
1429 | |
1430 | return msg; |
1431 | } |
1432 | |
1433 | //===----------------------------------------------------------------------===// |
1434 | // MSVC __declspec(property) references |
1435 | //===----------------------------------------------------------------------===// |
1436 | |
1437 | MSPropertyRefExpr * |
1438 | MSPropertyOpBuilder::getBaseMSProperty(MSPropertySubscriptExpr *E) { |
1439 | CallArgs.insert(I: CallArgs.begin(), Elt: E->getIdx()); |
1440 | Expr *Base = E->getBase()->IgnoreParens(); |
1441 | while (auto *MSPropSubscript = dyn_cast<MSPropertySubscriptExpr>(Val: Base)) { |
1442 | CallArgs.insert(I: CallArgs.begin(), Elt: MSPropSubscript->getIdx()); |
1443 | Base = MSPropSubscript->getBase()->IgnoreParens(); |
1444 | } |
1445 | return cast<MSPropertyRefExpr>(Val: Base); |
1446 | } |
1447 | |
1448 | Expr *MSPropertyOpBuilder::rebuildAndCaptureObject(Expr *syntacticBase) { |
1449 | InstanceBase = capture(e: RefExpr->getBaseExpr()); |
1450 | for (Expr *&Arg : CallArgs) |
1451 | Arg = capture(e: Arg); |
1452 | syntacticBase = Rebuilder(S, [=](Expr *, unsigned Idx) -> Expr * { |
1453 | switch (Idx) { |
1454 | case 0: |
1455 | return InstanceBase; |
1456 | default: |
1457 | assert(Idx <= CallArgs.size()); |
1458 | return CallArgs[Idx - 1]; |
1459 | } |
1460 | }).rebuild(e: syntacticBase); |
1461 | |
1462 | return syntacticBase; |
1463 | } |
1464 | |
1465 | ExprResult MSPropertyOpBuilder::buildGet() { |
1466 | if (!RefExpr->getPropertyDecl()->hasGetter()) { |
1467 | S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property) |
1468 | << 0 /* getter */ << RefExpr->getPropertyDecl(); |
1469 | return ExprError(); |
1470 | } |
1471 | |
1472 | UnqualifiedId GetterName; |
1473 | const IdentifierInfo *II = RefExpr->getPropertyDecl()->getGetterId(); |
1474 | GetterName.setIdentifier(Id: II, IdLoc: RefExpr->getMemberLoc()); |
1475 | CXXScopeSpec SS; |
1476 | SS.Adopt(Other: RefExpr->getQualifierLoc()); |
1477 | ExprResult GetterExpr = |
1478 | S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(), |
1479 | RefExpr->isArrow() ? tok::arrow : tok::period, SS, |
1480 | SourceLocation(), GetterName, nullptr); |
1481 | if (GetterExpr.isInvalid()) { |
1482 | S.Diag(RefExpr->getMemberLoc(), |
1483 | diag::err_cannot_find_suitable_accessor) << 0 /* getter */ |
1484 | << RefExpr->getPropertyDecl(); |
1485 | return ExprError(); |
1486 | } |
1487 | |
1488 | return S.BuildCallExpr(S: S.getCurScope(), Fn: GetterExpr.get(), |
1489 | LParenLoc: RefExpr->getSourceRange().getBegin(), ArgExprs: CallArgs, |
1490 | RParenLoc: RefExpr->getSourceRange().getEnd()); |
1491 | } |
1492 | |
1493 | ExprResult MSPropertyOpBuilder::buildSet(Expr *op, SourceLocation sl, |
1494 | bool captureSetValueAsResult) { |
1495 | if (!RefExpr->getPropertyDecl()->hasSetter()) { |
1496 | S.Diag(RefExpr->getMemberLoc(), diag::err_no_accessor_for_property) |
1497 | << 1 /* setter */ << RefExpr->getPropertyDecl(); |
1498 | return ExprError(); |
1499 | } |
1500 | |
1501 | UnqualifiedId SetterName; |
1502 | const IdentifierInfo *II = RefExpr->getPropertyDecl()->getSetterId(); |
1503 | SetterName.setIdentifier(Id: II, IdLoc: RefExpr->getMemberLoc()); |
1504 | CXXScopeSpec SS; |
1505 | SS.Adopt(Other: RefExpr->getQualifierLoc()); |
1506 | ExprResult SetterExpr = |
1507 | S.ActOnMemberAccessExpr(S.getCurScope(), InstanceBase, SourceLocation(), |
1508 | RefExpr->isArrow() ? tok::arrow : tok::period, SS, |
1509 | SourceLocation(), SetterName, nullptr); |
1510 | if (SetterExpr.isInvalid()) { |
1511 | S.Diag(RefExpr->getMemberLoc(), |
1512 | diag::err_cannot_find_suitable_accessor) << 1 /* setter */ |
1513 | << RefExpr->getPropertyDecl(); |
1514 | return ExprError(); |
1515 | } |
1516 | |
1517 | SmallVector<Expr*, 4> ArgExprs; |
1518 | ArgExprs.append(in_start: CallArgs.begin(), in_end: CallArgs.end()); |
1519 | ArgExprs.push_back(Elt: op); |
1520 | return S.BuildCallExpr(S: S.getCurScope(), Fn: SetterExpr.get(), |
1521 | LParenLoc: RefExpr->getSourceRange().getBegin(), ArgExprs, |
1522 | RParenLoc: op->getSourceRange().getEnd()); |
1523 | } |
1524 | |
1525 | //===----------------------------------------------------------------------===// |
1526 | // General Sema routines. |
1527 | //===----------------------------------------------------------------------===// |
1528 | |
1529 | ExprResult Sema::checkPseudoObjectRValue(Expr *E) { |
1530 | Expr *opaqueRef = E->IgnoreParens(); |
1531 | if (ObjCPropertyRefExpr *refExpr |
1532 | = dyn_cast<ObjCPropertyRefExpr>(Val: opaqueRef)) { |
1533 | ObjCPropertyOpBuilder builder(*this, refExpr, true); |
1534 | return builder.buildRValueOperation(op: E); |
1535 | } |
1536 | else if (ObjCSubscriptRefExpr *refExpr |
1537 | = dyn_cast<ObjCSubscriptRefExpr>(Val: opaqueRef)) { |
1538 | ObjCSubscriptOpBuilder builder(*this, refExpr, true); |
1539 | return builder.buildRValueOperation(op: E); |
1540 | } else if (MSPropertyRefExpr *refExpr |
1541 | = dyn_cast<MSPropertyRefExpr>(Val: opaqueRef)) { |
1542 | MSPropertyOpBuilder builder(*this, refExpr, true); |
1543 | return builder.buildRValueOperation(op: E); |
1544 | } else if (MSPropertySubscriptExpr *RefExpr = |
1545 | dyn_cast<MSPropertySubscriptExpr>(Val: opaqueRef)) { |
1546 | MSPropertyOpBuilder Builder(*this, RefExpr, true); |
1547 | return Builder.buildRValueOperation(op: E); |
1548 | } else { |
1549 | llvm_unreachable("unknown pseudo-object kind!"); |
1550 | } |
1551 | } |
1552 | |
1553 | /// Check an increment or decrement of a pseudo-object expression. |
1554 | ExprResult Sema::checkPseudoObjectIncDec(Scope *Sc, SourceLocation opcLoc, |
1555 | UnaryOperatorKind opcode, Expr *op) { |
1556 | // Do nothing if the operand is dependent. |
1557 | if (op->isTypeDependent()) |
1558 | return UnaryOperator::Create(C: Context, input: op, opc: opcode, type: Context.DependentTy, |
1559 | VK: VK_PRValue, OK: OK_Ordinary, l: opcLoc, CanOverflow: false, |
1560 | FPFeatures: CurFPFeatureOverrides()); |
1561 | |
1562 | assert(UnaryOperator::isIncrementDecrementOp(opcode)); |
1563 | Expr *opaqueRef = op->IgnoreParens(); |
1564 | if (ObjCPropertyRefExpr *refExpr |
1565 | = dyn_cast<ObjCPropertyRefExpr>(Val: opaqueRef)) { |
1566 | ObjCPropertyOpBuilder builder(*this, refExpr, false); |
1567 | return builder.buildIncDecOperation(Sc, opcLoc, opcode, op); |
1568 | } else if (isa<ObjCSubscriptRefExpr>(Val: opaqueRef)) { |
1569 | Diag(opcLoc, diag::err_illegal_container_subscripting_op); |
1570 | return ExprError(); |
1571 | } else if (MSPropertyRefExpr *refExpr |
1572 | = dyn_cast<MSPropertyRefExpr>(Val: opaqueRef)) { |
1573 | MSPropertyOpBuilder builder(*this, refExpr, false); |
1574 | return builder.buildIncDecOperation(Sc, opcLoc, opcode, op); |
1575 | } else if (MSPropertySubscriptExpr *RefExpr |
1576 | = dyn_cast<MSPropertySubscriptExpr>(Val: opaqueRef)) { |
1577 | MSPropertyOpBuilder Builder(*this, RefExpr, false); |
1578 | return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op); |
1579 | } else { |
1580 | llvm_unreachable("unknown pseudo-object kind!"); |
1581 | } |
1582 | } |
1583 | |
1584 | ExprResult Sema::checkPseudoObjectAssignment(Scope *S, SourceLocation opcLoc, |
1585 | BinaryOperatorKind opcode, |
1586 | Expr *LHS, Expr *RHS) { |
1587 | // Do nothing if either argument is dependent. |
1588 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
1589 | return BinaryOperator::Create(C: Context, lhs: LHS, rhs: RHS, opc: opcode, |
1590 | ResTy: Context.DependentTy, VK: VK_PRValue, OK: OK_Ordinary, |
1591 | opLoc: opcLoc, FPFeatures: CurFPFeatureOverrides()); |
1592 | |
1593 | // Filter out non-overload placeholder types in the RHS. |
1594 | if (RHS->getType()->isNonOverloadPlaceholderType()) { |
1595 | ExprResult result = CheckPlaceholderExpr(E: RHS); |
1596 | if (result.isInvalid()) return ExprError(); |
1597 | RHS = result.get(); |
1598 | } |
1599 | |
1600 | bool IsSimpleAssign = opcode == BO_Assign; |
1601 | Expr *opaqueRef = LHS->IgnoreParens(); |
1602 | if (ObjCPropertyRefExpr *refExpr |
1603 | = dyn_cast<ObjCPropertyRefExpr>(Val: opaqueRef)) { |
1604 | ObjCPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign); |
1605 | return builder.buildAssignmentOperation(Sc: S, opcLoc, opcode, LHS, RHS); |
1606 | } else if (ObjCSubscriptRefExpr *refExpr |
1607 | = dyn_cast<ObjCSubscriptRefExpr>(Val: opaqueRef)) { |
1608 | ObjCSubscriptOpBuilder builder(*this, refExpr, IsSimpleAssign); |
1609 | return builder.buildAssignmentOperation(Sc: S, opcLoc, opcode, LHS, RHS); |
1610 | } else if (MSPropertyRefExpr *refExpr |
1611 | = dyn_cast<MSPropertyRefExpr>(Val: opaqueRef)) { |
1612 | MSPropertyOpBuilder builder(*this, refExpr, IsSimpleAssign); |
1613 | return builder.buildAssignmentOperation(Sc: S, opcLoc, opcode, LHS, RHS); |
1614 | } else if (MSPropertySubscriptExpr *RefExpr |
1615 | = dyn_cast<MSPropertySubscriptExpr>(Val: opaqueRef)) { |
1616 | MSPropertyOpBuilder Builder(*this, RefExpr, IsSimpleAssign); |
1617 | return Builder.buildAssignmentOperation(Sc: S, opcLoc, opcode, LHS, RHS); |
1618 | } else { |
1619 | llvm_unreachable("unknown pseudo-object kind!"); |
1620 | } |
1621 | } |
1622 | |
1623 | /// Given a pseudo-object reference, rebuild it without the opaque |
1624 | /// values. Basically, undo the behavior of rebuildAndCaptureObject. |
1625 | /// This should never operate in-place. |
1626 | static Expr *stripOpaqueValuesFromPseudoObjectRef(Sema &S, Expr *E) { |
1627 | return Rebuilder(S, |
1628 | [=](Expr *E, unsigned) -> Expr * { |
1629 | return cast<OpaqueValueExpr>(Val: E)->getSourceExpr(); |
1630 | }) |
1631 | .rebuild(e: E); |
1632 | } |
1633 | |
1634 | /// Given a pseudo-object expression, recreate what it looks like |
1635 | /// syntactically without the attendant OpaqueValueExprs. |
1636 | /// |
1637 | /// This is a hack which should be removed when TreeTransform is |
1638 | /// capable of rebuilding a tree without stripping implicit |
1639 | /// operations. |
1640 | Expr *Sema::recreateSyntacticForm(PseudoObjectExpr *E) { |
1641 | Expr *syntax = E->getSyntacticForm(); |
1642 | if (UnaryOperator *uop = dyn_cast<UnaryOperator>(Val: syntax)) { |
1643 | Expr *op = stripOpaqueValuesFromPseudoObjectRef(S&: *this, E: uop->getSubExpr()); |
1644 | return UnaryOperator::Create(C: Context, input: op, opc: uop->getOpcode(), type: uop->getType(), |
1645 | VK: uop->getValueKind(), OK: uop->getObjectKind(), |
1646 | l: uop->getOperatorLoc(), CanOverflow: uop->canOverflow(), |
1647 | FPFeatures: CurFPFeatureOverrides()); |
1648 | } else if (CompoundAssignOperator *cop |
1649 | = dyn_cast<CompoundAssignOperator>(Val: syntax)) { |
1650 | Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS()); |
1651 | Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr(); |
1652 | return CompoundAssignOperator::Create( |
1653 | C: Context, lhs, rhs, opc: cop->getOpcode(), ResTy: cop->getType(), |
1654 | VK: cop->getValueKind(), OK: cop->getObjectKind(), opLoc: cop->getOperatorLoc(), |
1655 | FPFeatures: CurFPFeatureOverrides(), CompLHSType: cop->getComputationLHSType(), |
1656 | CompResultType: cop->getComputationResultType()); |
1657 | |
1658 | } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(Val: syntax)) { |
1659 | Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(S&: *this, E: bop->getLHS()); |
1660 | Expr *rhs = cast<OpaqueValueExpr>(Val: bop->getRHS())->getSourceExpr(); |
1661 | return BinaryOperator::Create(C: Context, lhs, rhs, opc: bop->getOpcode(), |
1662 | ResTy: bop->getType(), VK: bop->getValueKind(), |
1663 | OK: bop->getObjectKind(), opLoc: bop->getOperatorLoc(), |
1664 | FPFeatures: CurFPFeatureOverrides()); |
1665 | |
1666 | } else if (isa<CallExpr>(Val: syntax)) { |
1667 | return syntax; |
1668 | } else { |
1669 | assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject)); |
1670 | return stripOpaqueValuesFromPseudoObjectRef(S&: *this, E: syntax); |
1671 | } |
1672 | } |
1673 |
Definitions
- Rebuilder
- Rebuilder
- rebuildObjCPropertyRefExpr
- rebuildObjCSubscriptRefExpr
- rebuildMSPropertyRefExpr
- rebuildMSPropertySubscriptExpr
- rebuild
- PseudoOpBuilder
- PseudoOpBuilder
- ~PseudoOpBuilder
- addSemanticExpr
- addResultSemanticExpr
- setResultToLastSemantic
- CanCaptureValue
- captureSetValueAsResult
- ObjCPropertyOpBuilder
- ObjCPropertyOpBuilder
- ObjCSubscriptOpBuilder
- ObjCSubscriptOpBuilder
- MSPropertyOpBuilder
- MSPropertyOpBuilder
- MSPropertyOpBuilder
- captureSetValueAsResult
- capture
- captureValueAsResult
- complete
- buildRValueOperation
- buildAssignmentOperation
- buildIncDecOperation
- LookupMethodInReceiverType
- isWeakProperty
- findGetter
- findSetter
- DiagnoseUnsupportedPropertyUse
- rebuildAndCaptureObject
- buildGet
- buildSet
- buildRValueOperation
- tryBuildGetOfReference
- buildAssignmentOperation
- buildIncDecOperation
- complete
- buildRValueOperation
- buildAssignmentOperation
- rebuildAndCaptureObject
- CheckSubscriptingKind
- CheckKeyForObjCARCConversion
- findAtIndexGetter
- findAtIndexSetter
- buildGet
- buildSet
- getBaseMSProperty
- rebuildAndCaptureObject
- buildGet
- buildSet
- checkPseudoObjectRValue
- checkPseudoObjectIncDec
- checkPseudoObjectAssignment
- stripOpaqueValuesFromPseudoObjectRef
Learn to use CMake with our Intro Training
Find out more