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