clang  7.0.0svn
SemaPseudoObject.cpp
Go to the documentation of this file.
1 //===--- SemaPseudoObject.cpp - Semantic Analysis for Pseudo-Objects ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements semantic analysis for expressions involving
11 // pseudo-object references. Pseudo-objects are conceptual objects
12 // whose storage is entirely abstract and all accesses to which are
13 // translated through some sort of abstraction barrier.
14 //
15 // For example, Objective-C objects can have "properties", either
16 // declared or undeclared. A property may be accessed by writing
17 // expr.prop
18 // where 'expr' is an r-value of Objective-C pointer type and 'prop'
19 // is the name of the property. If this expression is used in a context
20 // needing an r-value, it is treated as if it were a message-send
21 // of the associated 'getter' selector, typically:
22 // [expr prop]
23 // If it is used as the LHS of a simple assignment, it is treated
24 // as a message-send of the associated 'setter' selector, typically:
25 // [expr setProp: RHS]
26 // If it is used as the LHS of a compound assignment, or the operand
27 // of a unary increment or decrement, both are required; for example,
28 // 'expr.prop *= 100' would be translated to:
29 // [expr setProp: [expr prop] * 100]
30 //
31 //===----------------------------------------------------------------------===//
32 
34 #include "clang/AST/ExprCXX.h"
35 #include "clang/AST/ExprObjC.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Lex/Preprocessor.h"
39 #include "clang/Sema/ScopeInfo.h"
40 #include "llvm/ADT/SmallString.h"
41 
42 using namespace clang;
43 using namespace sema;
44 
45 namespace {
46  // Basically just a very focused copy of TreeTransform.
47  struct Rebuilder {
48  Sema &S;
49  unsigned MSPropertySubscriptCount;
50  typedef llvm::function_ref<Expr *(Expr *, unsigned)> SpecificRebuilderRefTy;
51  const SpecificRebuilderRefTy &SpecificCallback;
52  Rebuilder(Sema &S, const SpecificRebuilderRefTy &SpecificCallback)
53  : S(S), MSPropertySubscriptCount(0),
54  SpecificCallback(SpecificCallback) {}
55 
56  Expr *rebuildObjCPropertyRefExpr(ObjCPropertyRefExpr *refExpr) {
57  // Fortunately, the constraint that we're rebuilding something
58  // with a base limits the number of cases here.
59  if (refExpr->isClassReceiver() || refExpr->isSuperReceiver())
60  return refExpr;
61 
62  if (refExpr->isExplicitProperty()) {
63  return new (S.Context) ObjCPropertyRefExpr(
64  refExpr->getExplicitProperty(), refExpr->getType(),
65  refExpr->getValueKind(), refExpr->getObjectKind(),
66  refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
67  }
68  return new (S.Context) ObjCPropertyRefExpr(
69  refExpr->getImplicitPropertyGetter(),
70  refExpr->getImplicitPropertySetter(), refExpr->getType(),
71  refExpr->getValueKind(), refExpr->getObjectKind(),
72  refExpr->getLocation(), SpecificCallback(refExpr->getBase(), 0));
73  }
74  Expr *rebuildObjCSubscriptRefExpr(ObjCSubscriptRefExpr *refExpr) {
75  assert(refExpr->getBaseExpr());
76  assert(refExpr->getKeyExpr());
77 
78  return new (S.Context) ObjCSubscriptRefExpr(
79  SpecificCallback(refExpr->getBaseExpr(), 0),
80  SpecificCallback(refExpr->getKeyExpr(), 1), refExpr->getType(),
81  refExpr->getValueKind(), refExpr->getObjectKind(),
82  refExpr->getAtIndexMethodDecl(), refExpr->setAtIndexMethodDecl(),
83  refExpr->getRBracket());
84  }
85  Expr *rebuildMSPropertyRefExpr(MSPropertyRefExpr *refExpr) {
86  assert(refExpr->getBaseExpr());
87 
88  return new (S.Context) MSPropertyRefExpr(
89  SpecificCallback(refExpr->getBaseExpr(), 0),
90  refExpr->getPropertyDecl(), refExpr->isArrow(), refExpr->getType(),
91  refExpr->getValueKind(), refExpr->getQualifierLoc(),
92  refExpr->getMemberLoc());
93  }
94  Expr *rebuildMSPropertySubscriptExpr(MSPropertySubscriptExpr *refExpr) {
95  assert(refExpr->getBase());
96  assert(refExpr->getIdx());
97 
98  auto *NewBase = rebuild(refExpr->getBase());
99  ++MSPropertySubscriptCount;
100  return new (S.Context) MSPropertySubscriptExpr(
101  NewBase,
102  SpecificCallback(refExpr->getIdx(), MSPropertySubscriptCount),
103  refExpr->getType(), refExpr->getValueKind(), refExpr->getObjectKind(),
104  refExpr->getRBracketLoc());
105  }
106 
107  Expr *rebuild(Expr *e) {
108  // Fast path: nothing to look through.
109  if (auto *PRE = dyn_cast<ObjCPropertyRefExpr>(e))
110  return rebuildObjCPropertyRefExpr(PRE);
111  if (auto *SRE = dyn_cast<ObjCSubscriptRefExpr>(e))
112  return rebuildObjCSubscriptRefExpr(SRE);
113  if (auto *MSPRE = dyn_cast<MSPropertyRefExpr>(e))
114  return rebuildMSPropertyRefExpr(MSPRE);
115  if (auto *MSPSE = dyn_cast<MSPropertySubscriptExpr>(e))
116  return rebuildMSPropertySubscriptExpr(MSPSE);
117 
118  // Otherwise, we should look through and rebuild anything that
119  // IgnoreParens would.
120 
121  if (ParenExpr *parens = dyn_cast<ParenExpr>(e)) {
122  e = rebuild(parens->getSubExpr());
123  return new (S.Context) ParenExpr(parens->getLParen(),
124  parens->getRParen(),
125  e);
126  }
127 
128  if (UnaryOperator *uop = dyn_cast<UnaryOperator>(e)) {
129  assert(uop->getOpcode() == UO_Extension);
130  e = rebuild(uop->getSubExpr());
131  return new (S.Context) UnaryOperator(e, uop->getOpcode(),
132  uop->getType(),
133  uop->getValueKind(),
134  uop->getObjectKind(),
135  uop->getOperatorLoc(),
136  uop->canOverflow());
137  }
138 
139  if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
140  assert(!gse->isResultDependent());
141  unsigned resultIndex = gse->getResultIndex();
142  unsigned numAssocs = gse->getNumAssocs();
143 
144  SmallVector<Expr*, 8> assocs(numAssocs);
145  SmallVector<TypeSourceInfo*, 8> assocTypes(numAssocs);
146 
147  for (unsigned i = 0; i != numAssocs; ++i) {
148  Expr *assoc = gse->getAssocExpr(i);
149  if (i == resultIndex) assoc = rebuild(assoc);
150  assocs[i] = assoc;
151  assocTypes[i] = gse->getAssocTypeSourceInfo(i);
152  }
153 
154  return new (S.Context) GenericSelectionExpr(S.Context,
155  gse->getGenericLoc(),
156  gse->getControllingExpr(),
157  assocTypes,
158  assocs,
159  gse->getDefaultLoc(),
160  gse->getRParenLoc(),
161  gse->containsUnexpandedParameterPack(),
162  resultIndex);
163  }
164 
165  if (ChooseExpr *ce = dyn_cast<ChooseExpr>(e)) {
166  assert(!ce->isConditionDependent());
167 
168  Expr *LHS = ce->getLHS(), *RHS = ce->getRHS();
169  Expr *&rebuiltExpr = ce->isConditionTrue() ? LHS : RHS;
170  rebuiltExpr = rebuild(rebuiltExpr);
171 
172  return new (S.Context) ChooseExpr(ce->getBuiltinLoc(),
173  ce->getCond(),
174  LHS, RHS,
175  rebuiltExpr->getType(),
176  rebuiltExpr->getValueKind(),
177  rebuiltExpr->getObjectKind(),
178  ce->getRParenLoc(),
179  ce->isConditionTrue(),
180  rebuiltExpr->isTypeDependent(),
181  rebuiltExpr->isValueDependent());
182  }
183 
184  llvm_unreachable("bad expression to rebuild!");
185  }
186  };
187 
188  class PseudoOpBuilder {
189  public:
190  Sema &S;
191  unsigned ResultIndex;
192  SourceLocation GenericLoc;
193  SmallVector<Expr *, 4> Semantics;
194 
195  PseudoOpBuilder(Sema &S, SourceLocation genericLoc)
196  : S(S), ResultIndex(PseudoObjectExpr::NoResult),
197  GenericLoc(genericLoc) {}
198 
199  virtual ~PseudoOpBuilder() {}
200 
201  /// Add a normal semantic expression.
202  void addSemanticExpr(Expr *semantic) {
203  Semantics.push_back(semantic);
204  }
205 
206  /// Add the 'result' semantic expression.
207  void addResultSemanticExpr(Expr *resultExpr) {
208  assert(ResultIndex == PseudoObjectExpr::NoResult);
209  ResultIndex = Semantics.size();
210  Semantics.push_back(resultExpr);
211  }
212 
213  ExprResult buildRValueOperation(Expr *op);
214  ExprResult buildAssignmentOperation(Scope *Sc,
215  SourceLocation opLoc,
216  BinaryOperatorKind opcode,
217  Expr *LHS, Expr *RHS);
218  ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
219  UnaryOperatorKind opcode,
220  Expr *op);
221 
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  }
231 
232  /// Return true if assignments have a non-void result.
233  static bool CanCaptureValue(Expr *exp) {
234  if (exp->isGLValue())
235  return true;
236  QualType ty = exp->getType();
237  assert(!ty->isIncompleteType());
238  assert(!ty->isDependentType());
239 
240  if (const CXXRecordDecl *ClassDecl = ty->getAsCXXRecordDecl())
241  return ClassDecl->isTriviallyCopyable();
242  return true;
243  }
244 
245  virtual Expr *rebuildAndCaptureObject(Expr *) = 0;
246  virtual ExprResult buildGet() = 0;
247  virtual ExprResult buildSet(Expr *, SourceLocation,
248  bool captureSetValueAsResult) = 0;
249  /// \brief Should the result of an assignment be the formal result of the
250  /// setter call or the value that was passed to the setter?
251  ///
252  /// Different pseudo-object language features use different language rules
253  /// for this.
254  /// The default is to use the set value. Currently, this affects the
255  /// behavior of simple assignments, compound assignments, and prefix
256  /// increment and decrement.
257  /// Postfix increment and decrement always use the getter result as the
258  /// expression result.
259  ///
260  /// If this method returns true, and the set value isn't capturable for
261  /// some reason, the result of the expression will be void.
262  virtual bool captureSetValueAsResult() const { return true; }
263  };
264 
265  /// A PseudoOpBuilder for Objective-C \@properties.
266  class ObjCPropertyOpBuilder : public PseudoOpBuilder {
267  ObjCPropertyRefExpr *RefExpr;
268  ObjCPropertyRefExpr *SyntacticRefExpr;
269  OpaqueValueExpr *InstanceReceiver;
270  ObjCMethodDecl *Getter;
271 
272  ObjCMethodDecl *Setter;
273  Selector SetterSelector;
274  Selector GetterSelector;
275 
276  public:
277  ObjCPropertyOpBuilder(Sema &S, ObjCPropertyRefExpr *refExpr) :
278  PseudoOpBuilder(S, refExpr->getLocation()), RefExpr(refExpr),
279  SyntacticRefExpr(nullptr), InstanceReceiver(nullptr), Getter(nullptr),
280  Setter(nullptr) {
281  }
282 
283  ExprResult buildRValueOperation(Expr *op);
284  ExprResult buildAssignmentOperation(Scope *Sc,
285  SourceLocation opLoc,
286  BinaryOperatorKind opcode,
287  Expr *LHS, Expr *RHS);
288  ExprResult buildIncDecOperation(Scope *Sc, SourceLocation opLoc,
289  UnaryOperatorKind opcode,
290  Expr *op);
291 
292  bool tryBuildGetOfReference(Expr *op, ExprResult &result);
293  bool findSetter(bool warn=true);
294  bool findGetter();
295  void DiagnoseUnsupportedPropertyUse();
296 
297  Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
298  ExprResult buildGet() override;
299  ExprResult buildSet(Expr *op, SourceLocation, bool) override;
300  ExprResult complete(Expr *SyntacticForm) override;
301 
302  bool isWeakProperty() const;
303  };
304 
305  /// A PseudoOpBuilder for Objective-C array/dictionary indexing.
306  class ObjCSubscriptOpBuilder : public PseudoOpBuilder {
307  ObjCSubscriptRefExpr *RefExpr;
308  OpaqueValueExpr *InstanceBase;
309  OpaqueValueExpr *InstanceKey;
310  ObjCMethodDecl *AtIndexGetter;
311  Selector AtIndexGetterSelector;
312 
313  ObjCMethodDecl *AtIndexSetter;
314  Selector AtIndexSetterSelector;
315 
316  public:
317  ObjCSubscriptOpBuilder(Sema &S, ObjCSubscriptRefExpr *refExpr) :
318  PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
319  RefExpr(refExpr),
320  InstanceBase(nullptr), InstanceKey(nullptr),
321  AtIndexGetter(nullptr), AtIndexSetter(nullptr) {}
322 
323  ExprResult buildRValueOperation(Expr *op);
324  ExprResult buildAssignmentOperation(Scope *Sc,
325  SourceLocation opLoc,
326  BinaryOperatorKind opcode,
327  Expr *LHS, Expr *RHS);
328  Expr *rebuildAndCaptureObject(Expr *syntacticBase) override;
329 
330  bool findAtIndexGetter();
331  bool findAtIndexSetter();
332 
333  ExprResult buildGet() override;
334  ExprResult buildSet(Expr *op, SourceLocation, bool) override;
335  };
336 
337  class MSPropertyOpBuilder : public PseudoOpBuilder {
338  MSPropertyRefExpr *RefExpr;
339  OpaqueValueExpr *InstanceBase;
340  SmallVector<Expr *, 4> CallArgs;
341 
342  MSPropertyRefExpr *getBaseMSProperty(MSPropertySubscriptExpr *E);
343 
344  public:
345  MSPropertyOpBuilder(Sema &S, MSPropertyRefExpr *refExpr) :
346  PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
347  RefExpr(refExpr), InstanceBase(nullptr) {}
348  MSPropertyOpBuilder(Sema &S, MSPropertySubscriptExpr *refExpr)
349  : PseudoOpBuilder(S, refExpr->getSourceRange().getBegin()),
350  InstanceBase(nullptr) {
351  RefExpr = getBaseMSProperty(refExpr);
352  }
353 
354  Expr *rebuildAndCaptureObject(Expr *) override;
355  ExprResult buildGet() override;
356  ExprResult buildSet(Expr *op, SourceLocation, bool) override;
357  bool captureSetValueAsResult() const override { return false; }
358  };
359 }
360 
361 /// Capture the given expression in an OpaqueValueExpr.
362 OpaqueValueExpr *PseudoOpBuilder::capture(Expr *e) {
363  // Make a new OVE whose source is the given expression.
364  OpaqueValueExpr *captured =
365  new (S.Context) OpaqueValueExpr(GenericLoc, e->getType(),
366  e->getValueKind(), e->getObjectKind(),
367  e);
368 
369  // Make sure we bind that in the semantics.
370  addSemanticExpr(captured);
371  return captured;
372 }
373 
374 /// Capture the given expression as the result of this pseudo-object
375 /// operation. This routine is safe against expressions which may
376 /// already be captured.
377 ///
378 /// \returns the captured expression, which will be the
379 /// same as the input if the input was already captured
380 OpaqueValueExpr *PseudoOpBuilder::captureValueAsResult(Expr *e) {
381  assert(ResultIndex == PseudoObjectExpr::NoResult);
382 
383  // If the expression hasn't already been captured, just capture it
384  // and set the new semantic
385  if (!isa<OpaqueValueExpr>(e)) {
386  OpaqueValueExpr *cap = capture(e);
387  setResultToLastSemantic();
388  return cap;
389  }
390 
391  // Otherwise, it must already be one of our semantic expressions;
392  // set ResultIndex to its index.
393  unsigned index = 0;
394  for (;; ++index) {
395  assert(index < Semantics.size() &&
396  "captured expression not found in semantics!");
397  if (e == Semantics[index]) break;
398  }
399  ResultIndex = index;
400  return cast<OpaqueValueExpr>(e);
401 }
402 
403 /// The routine which creates the final PseudoObjectExpr.
404 ExprResult PseudoOpBuilder::complete(Expr *syntactic) {
405  return PseudoObjectExpr::Create(S.Context, syntactic,
406  Semantics, ResultIndex);
407 }
408 
409 /// The main skeleton for building an r-value operation.
410 ExprResult PseudoOpBuilder::buildRValueOperation(Expr *op) {
411  Expr *syntacticBase = rebuildAndCaptureObject(op);
412 
413  ExprResult getExpr = buildGet();
414  if (getExpr.isInvalid()) return ExprError();
415  addResultSemanticExpr(getExpr.get());
416 
417  return complete(syntacticBase);
418 }
419 
420 /// The basic skeleton for building a simple or compound
421 /// assignment operation.
423 PseudoOpBuilder::buildAssignmentOperation(Scope *Sc, SourceLocation opcLoc,
424  BinaryOperatorKind opcode,
425  Expr *LHS, Expr *RHS) {
426  assert(BinaryOperator::isAssignmentOp(opcode));
427 
428  Expr *syntacticLHS = rebuildAndCaptureObject(LHS);
429  OpaqueValueExpr *capturedRHS = capture(RHS);
430 
431  // In some very specific cases, semantic analysis of the RHS as an
432  // expression may require it to be rewritten. In these cases, we
433  // cannot safely keep the OVE around. Fortunately, we don't really
434  // need to: we don't use this particular OVE in multiple places, and
435  // no clients rely that closely on matching up expressions in the
436  // semantic expression with expressions from the syntactic form.
437  Expr *semanticRHS = capturedRHS;
438  if (RHS->hasPlaceholderType() || isa<InitListExpr>(RHS)) {
439  semanticRHS = RHS;
440  Semantics.pop_back();
441  }
442 
443  Expr *syntactic;
444 
445  ExprResult result;
446  if (opcode == BO_Assign) {
447  result = semanticRHS;
448  syntactic = new (S.Context) BinaryOperator(syntacticLHS, capturedRHS,
449  opcode, capturedRHS->getType(),
450  capturedRHS->getValueKind(),
451  OK_Ordinary, opcLoc,
452  FPOptions());
453  } else {
454  ExprResult opLHS = buildGet();
455  if (opLHS.isInvalid()) return ExprError();
456 
457  // Build an ordinary, non-compound operation.
458  BinaryOperatorKind nonCompound =
460  result = S.BuildBinOp(Sc, opcLoc, nonCompound, opLHS.get(), semanticRHS);
461  if (result.isInvalid()) return ExprError();
462 
463  syntactic =
464  new (S.Context) CompoundAssignOperator(syntacticLHS, capturedRHS, opcode,
465  result.get()->getType(),
466  result.get()->getValueKind(),
467  OK_Ordinary,
468  opLHS.get()->getType(),
469  result.get()->getType(),
470  opcLoc, FPOptions());
471  }
472 
473  // The result of the assignment, if not void, is the value set into
474  // the l-value.
475  result = buildSet(result.get(), opcLoc, captureSetValueAsResult());
476  if (result.isInvalid()) return ExprError();
477  addSemanticExpr(result.get());
478  if (!captureSetValueAsResult() && !result.get()->getType()->isVoidType() &&
479  (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
480  setResultToLastSemantic();
481 
482  return complete(syntactic);
483 }
484 
485 /// The basic skeleton for building an increment or decrement
486 /// operation.
488 PseudoOpBuilder::buildIncDecOperation(Scope *Sc, SourceLocation opcLoc,
489  UnaryOperatorKind opcode,
490  Expr *op) {
492 
493  Expr *syntacticOp = rebuildAndCaptureObject(op);
494 
495  // Load the value.
496  ExprResult result = buildGet();
497  if (result.isInvalid()) return ExprError();
498 
499  QualType resultType = result.get()->getType();
500 
501  // That's the postfix result.
502  if (UnaryOperator::isPostfix(opcode) &&
503  (result.get()->isTypeDependent() || CanCaptureValue(result.get()))) {
504  result = capture(result.get());
505  setResultToLastSemantic();
506  }
507 
508  // Add or subtract a literal 1.
509  llvm::APInt oneV(S.Context.getTypeSize(S.Context.IntTy), 1);
510  Expr *one = IntegerLiteral::Create(S.Context, oneV, S.Context.IntTy,
511  GenericLoc);
512 
513  if (UnaryOperator::isIncrementOp(opcode)) {
514  result = S.BuildBinOp(Sc, opcLoc, BO_Add, result.get(), one);
515  } else {
516  result = S.BuildBinOp(Sc, opcLoc, BO_Sub, result.get(), one);
517  }
518  if (result.isInvalid()) return ExprError();
519 
520  // Store that back into the result. The value stored is the result
521  // of a prefix operation.
522  result = buildSet(result.get(), opcLoc, UnaryOperator::isPrefix(opcode) &&
523  captureSetValueAsResult());
524  if (result.isInvalid()) return ExprError();
525  addSemanticExpr(result.get());
526  if (UnaryOperator::isPrefix(opcode) && !captureSetValueAsResult() &&
527  !result.get()->getType()->isVoidType() &&
528  (result.get()->isTypeDependent() || CanCaptureValue(result.get())))
529  setResultToLastSemantic();
530 
531  UnaryOperator *syntactic = new (S.Context) UnaryOperator(
532  syntacticOp, opcode, resultType, VK_LValue, OK_Ordinary, opcLoc,
533  !resultType->isDependentType()
534  ? S.Context.getTypeSize(resultType) >=
536  : 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() &&
969  !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak,
970  SyntacticForm->getLocStart()))
971  S.recordUseOfEvaluatedWeak(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(S.Context, SourceLocation(),
1186  SourceLocation(), AtIndexGetterSelector,
1187  S.Context.getObjCIdType() /*ReturnType*/,
1188  nullptr /*TypeSourceInfo */,
1190  true /*Instance*/, false/*isVariadic*/,
1191  /*isPropertyAccessor=*/false,
1192  /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
1194  false);
1195  ParmVarDecl *Argument = ParmVarDecl::Create(S.Context, AtIndexGetter,
1197  arrayRef ? &S.Context.Idents.get("index")
1198  : &S.Context.Idents.get("key"),
1199  arrayRef ? S.Context.UnsignedLongTy
1200  : S.Context.getObjCIdType(),
1201  /*TInfo=*/nullptr,
1202  SC_None,
1203  nullptr);
1204  AtIndexGetter->setMethodParams(S.Context, Argument, None);
1205  }
1206 
1207  if (!AtIndexGetter) {
1208  if (!BaseT->isObjCIdType()) {
1209  S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
1210  << BaseExpr->getType() << 0 << arrayRef;
1211  return false;
1212  }
1213  AtIndexGetter =
1214  S.LookupInstanceMethodInGlobalPool(AtIndexGetterSelector,
1215  RefExpr->getSourceRange(),
1216  true);
1217  }
1218 
1219  if (AtIndexGetter) {
1220  QualType T = AtIndexGetter->parameters()[0]->getType();
1221  if ((arrayRef && !T->isIntegralOrEnumerationType()) ||
1222  (!arrayRef && !T->isObjCObjectPointerType())) {
1223  S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1224  arrayRef ? diag::err_objc_subscript_index_type
1225  : diag::err_objc_subscript_key_type) << T;
1226  S.Diag(AtIndexGetter->parameters()[0]->getLocation(),
1227  diag::note_parameter_type) << T;
1228  return false;
1229  }
1230  QualType R = AtIndexGetter->getReturnType();
1231  if (!R->isObjCObjectPointerType()) {
1232  S.Diag(RefExpr->getKeyExpr()->getExprLoc(),
1233  diag::err_objc_indexing_method_result_type) << R << arrayRef;
1234  S.Diag(AtIndexGetter->getLocation(), diag::note_method_declared_at) <<
1235  AtIndexGetter->getDeclName();
1236  }
1237  }
1238  return true;
1239 }
1240 
1241 bool ObjCSubscriptOpBuilder::findAtIndexSetter() {
1242  if (AtIndexSetter)
1243  return true;
1244 
1245  Expr *BaseExpr = RefExpr->getBaseExpr();
1246  QualType BaseT = BaseExpr->getType();
1247 
1248  QualType ResultType;
1249  if (const ObjCObjectPointerType *PTy =
1250  BaseT->getAs<ObjCObjectPointerType>()) {
1251  ResultType = PTy->getPointeeType();
1252  }
1253 
1255  S.CheckSubscriptingKind(RefExpr->getKeyExpr());
1256  if (Res == Sema::OS_Error) {
1257  if (S.getLangOpts().ObjCAutoRefCount)
1258  CheckKeyForObjCARCConversion(S, ResultType,
1259  RefExpr->getKeyExpr());
1260  return false;
1261  }
1262  bool arrayRef = (Res == Sema::OS_Array);
1263 
1264  if (ResultType.isNull()) {
1265  S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_base_type)
1266  << BaseExpr->getType() << arrayRef;
1267  return false;
1268  }
1269 
1270  if (!arrayRef) {
1271  // dictionary subscripting.
1272  // - (void)setObject:(id)object forKeyedSubscript:(id)key;
1273  IdentifierInfo *KeyIdents[] = {
1274  &S.Context.Idents.get("setObject"),
1275  &S.Context.Idents.get("forKeyedSubscript")
1276  };
1277  AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1278  }
1279  else {
1280  // - (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
1281  IdentifierInfo *KeyIdents[] = {
1282  &S.Context.Idents.get("setObject"),
1283  &S.Context.Idents.get("atIndexedSubscript")
1284  };
1285  AtIndexSetterSelector = S.Context.Selectors.getSelector(2, KeyIdents);
1286  }
1287  AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType,
1288  true /*instance*/);
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  /*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.ActOnCallExpr(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.ActOnCallExpr(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);
1532  return builder.buildRValueOperation(E);
1533  }
1534  else if (ObjCSubscriptRefExpr *refExpr
1535  = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
1536  ObjCSubscriptOpBuilder builder(*this, refExpr);
1537  return builder.buildRValueOperation(E);
1538  } else if (MSPropertyRefExpr *refExpr
1539  = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
1540  MSPropertyOpBuilder builder(*this, refExpr);
1541  return builder.buildRValueOperation(E);
1542  } else if (MSPropertySubscriptExpr *RefExpr =
1543  dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1544  MSPropertyOpBuilder Builder(*this, RefExpr);
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 new (Context) UnaryOperator(op, opcode, Context.DependentTy,
1557  VK_RValue, OK_Ordinary, opcLoc, false);
1558 
1559  assert(UnaryOperator::isIncrementDecrementOp(opcode));
1560  Expr *opaqueRef = op->IgnoreParens();
1561  if (ObjCPropertyRefExpr *refExpr
1562  = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
1563  ObjCPropertyOpBuilder builder(*this, refExpr);
1564  return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
1565  } else if (isa<ObjCSubscriptRefExpr>(opaqueRef)) {
1566  Diag(opcLoc, diag::err_illegal_container_subscripting_op);
1567  return ExprError();
1568  } else if (MSPropertyRefExpr *refExpr
1569  = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
1570  MSPropertyOpBuilder builder(*this, refExpr);
1571  return builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
1572  } else if (MSPropertySubscriptExpr *RefExpr
1573  = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1574  MSPropertyOpBuilder Builder(*this, RefExpr);
1575  return Builder.buildIncDecOperation(Sc, opcLoc, opcode, op);
1576  } else {
1577  llvm_unreachable("unknown pseudo-object kind!");
1578  }
1579 }
1580 
1582  BinaryOperatorKind opcode,
1583  Expr *LHS, Expr *RHS) {
1584  // Do nothing if either argument is dependent.
1585  if (LHS->isTypeDependent() || RHS->isTypeDependent())
1586  return new (Context) BinaryOperator(LHS, RHS, opcode, Context.DependentTy,
1587  VK_RValue, OK_Ordinary, opcLoc,
1588  FPOptions());
1589 
1590  // Filter out non-overload placeholder types in the RHS.
1591  if (RHS->getType()->isNonOverloadPlaceholderType()) {
1592  ExprResult result = CheckPlaceholderExpr(RHS);
1593  if (result.isInvalid()) return ExprError();
1594  RHS = result.get();
1595  }
1596 
1597  Expr *opaqueRef = LHS->IgnoreParens();
1598  if (ObjCPropertyRefExpr *refExpr
1599  = dyn_cast<ObjCPropertyRefExpr>(opaqueRef)) {
1600  ObjCPropertyOpBuilder builder(*this, refExpr);
1601  return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1602  } else if (ObjCSubscriptRefExpr *refExpr
1603  = dyn_cast<ObjCSubscriptRefExpr>(opaqueRef)) {
1604  ObjCSubscriptOpBuilder builder(*this, refExpr);
1605  return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1606  } else if (MSPropertyRefExpr *refExpr
1607  = dyn_cast<MSPropertyRefExpr>(opaqueRef)) {
1608  MSPropertyOpBuilder builder(*this, refExpr);
1609  return builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1610  } else if (MSPropertySubscriptExpr *RefExpr
1611  = dyn_cast<MSPropertySubscriptExpr>(opaqueRef)) {
1612  MSPropertyOpBuilder Builder(*this, RefExpr);
1613  return Builder.buildAssignmentOperation(S, opcLoc, opcode, LHS, RHS);
1614  } else {
1615  llvm_unreachable("unknown pseudo-object kind!");
1616  }
1617 }
1618 
1619 /// Given a pseudo-object reference, rebuild it without the opaque
1620 /// values. Basically, undo the behavior of rebuildAndCaptureObject.
1621 /// This should never operate in-place.
1623  return Rebuilder(S,
1624  [=](Expr *E, unsigned) -> Expr * {
1625  return cast<OpaqueValueExpr>(E)->getSourceExpr();
1626  })
1627  .rebuild(E);
1628 }
1629 
1630 /// Given a pseudo-object expression, recreate what it looks like
1631 /// syntactically without the attendant OpaqueValueExprs.
1632 ///
1633 /// This is a hack which should be removed when TreeTransform is
1634 /// capable of rebuilding a tree without stripping implicit
1635 /// operations.
1637  Expr *syntax = E->getSyntacticForm();
1638  if (UnaryOperator *uop = dyn_cast<UnaryOperator>(syntax)) {
1639  Expr *op = stripOpaqueValuesFromPseudoObjectRef(*this, uop->getSubExpr());
1640  return new (Context) UnaryOperator(
1641  op, uop->getOpcode(), uop->getType(), uop->getValueKind(),
1642  uop->getObjectKind(), uop->getOperatorLoc(), uop->canOverflow());
1643  } else if (CompoundAssignOperator *cop
1644  = dyn_cast<CompoundAssignOperator>(syntax)) {
1645  Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, cop->getLHS());
1646  Expr *rhs = cast<OpaqueValueExpr>(cop->getRHS())->getSourceExpr();
1647  return new (Context) CompoundAssignOperator(lhs, rhs, cop->getOpcode(),
1648  cop->getType(),
1649  cop->getValueKind(),
1650  cop->getObjectKind(),
1651  cop->getComputationLHSType(),
1652  cop->getComputationResultType(),
1653  cop->getOperatorLoc(),
1654  FPOptions());
1655  } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(syntax)) {
1656  Expr *lhs = stripOpaqueValuesFromPseudoObjectRef(*this, bop->getLHS());
1657  Expr *rhs = cast<OpaqueValueExpr>(bop->getRHS())->getSourceExpr();
1658  return new (Context) BinaryOperator(lhs, rhs, bop->getOpcode(),
1659  bop->getType(), bop->getValueKind(),
1660  bop->getObjectKind(),
1661  bop->getOperatorLoc(), FPOptions());
1662  } else {
1663  assert(syntax->hasPlaceholderType(BuiltinType::PseudoObject));
1664  return stripOpaqueValuesFromPseudoObjectRef(*this, syntax);
1665  }
1666 }
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:577
ObjCPropertyQueryKind getQueryKind() const
Definition: DeclObjC.h:898
bool isIncrementOp() const
Definition: Expr.h:1776
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:5026
Smart pointer class that efficiently represents Objective-C method names.
QualType getObjCIdType() const
Represents the Objective-CC id type.
Definition: ASTContext.h:1811
SelectorTable & getSelectorTable()
Definition: Preprocessor.h:825
A (possibly-)qualified type.
Definition: Type.h:653
bool isBlockPointerType() const
Definition: Type.h:5956
static Opcode getOpForCompoundAssignment(Opcode Opc)
Definition: Expr.h:3132
ObjCMethodDecl * getAtIndexMethodDecl() const
Definition: ExprObjC.h:853
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1101
bool isSuperReceiver() const
Definition: ExprObjC.h:739
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:456
ObjCMethodDecl * setAtIndexMethodDecl() const
Definition: ExprObjC.h:857
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:472
bool isRecordType() const
Definition: Type.h:6021
ObjCSubscriptKind
Definition: Sema.h:2676
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition: Sema.h:1271
ObjCMethodDecl * getImplicitPropertySetter() const
Definition: ExprObjC.h:680
AssignConvertType
AssignConvertType - All of the &#39;assignment&#39; semantic checks return this enum to indicate whether the ...
Definition: Sema.h:9389
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:1665
bool isObjCContainer() const
Definition: DeclBase.h:1372
ObjCSubscriptRefExpr - used for array and dictionary subscripting.
Definition: ExprObjC.h:802
A container of type source information.
Definition: Decl.h:86
Floating point control options.
Definition: LangOptions.h:225
MS property subscript expression.
Definition: ExprCXX.h:797
void Adopt(NestedNameSpecifierLoc Other)
Adopt an existing nested-name-specifier (with source-range information).
Definition: DeclSpec.cpp:125
DiagnosticsEngine & Diags
Definition: Sema.h:319
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6317
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:734
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:12451
Represents a parameter to a function.
Definition: Decl.h:1515
Defines the clang::Expr interface and subclasses for C++ expressions.
ObjCPropertyDecl * getExplicitProperty() const
Definition: ExprObjC.h:670
ExprResult ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, MultiExprArg ArgExprs, SourceLocation RParenLoc, Expr *ExecConfig=nullptr, bool IsExecConfig=false)
ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Definition: SemaExpr.cpp:5220
One of these records is kept for each identifier that is lexed.
bool isExplicitProperty() const
Definition: ExprObjC.h:668
bool isObjCIdType() const
Definition: Type.h:6074
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:3124
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6231
IdentifierTable & Idents
Definition: ASTContext.h:537
An r-value expression (a pr-value in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:107
bool isGLValue() const
Definition: Expr.h:252
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:850
BinaryOperatorKind
Represents a C++ unqualified-id that has been parsed.
Definition: DeclSpec.h:910
bool isPostfix() const
Definition: Expr.h:1771
Selector getNullarySelector(IdentifierInfo *ID)
PtrTy get() const
Definition: Ownership.h:174
Expr * getBaseExpr() const
Definition: ExprObjC.h:847
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:405
PropertyAttributeKind getPropertyAttributes() const
Definition: DeclObjC.h:857
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2998
bool isClassReceiver() const
Definition: ExprObjC.h:740
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:39
bool isArrow() const
Definition: ExprCXX.h:781
Represents a C++ nested-name-specifier or a global scope specifier.
Definition: DeclSpec.h:63
Preprocessor & PP
Definition: Sema.h:316
const LangOptions & getLangOpts() const
Definition: Sema.h:1194
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:748
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:167
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:7928
An ordinary object is located at an address in memory.
Definition: Specifiers.h:123
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
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:275
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1590
SourceLocation getRBracket() const
Definition: ExprObjC.h:838
bool isNonOverloadPlaceholderType() const
Test for a placeholder type other than Overload; see BuiltinType::isNonOverloadPlaceholderType.
Definition: Type.h:6169
SourceLocation getRBracketLoc() const
Definition: ExprCXX.h:835
Scope * getCurScope() const
Retrieve the parser&#39;s current scope.
Definition: Sema.h:10564
Expr - This represents one expression.
Definition: Expr.h:106
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 isImplicitlyDeclared=false, bool isDefined=false, ImplementationControl impControl=None, bool HasRelatedResultType=false)
Definition: DeclObjC.cpp:772
Selector getSetterName() const
Definition: DeclObjC.h:931
const FunctionProtoType * T
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6380
Defines the clang::Preprocessor interface.
Decl * getNonClosureAncestor()
Find the nearest non-closure ancestor of this context, i.e.
Definition: DeclBase.cpp:976
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:13314
QualType getType() const
Definition: Expr.h:128
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:1305
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:903
ObjCMethodDecl * getImplicitPropertyGetter() const
Definition: ExprObjC.h:675
bool isInvalid() const
Definition: Ownership.h:170
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:1717
A member reference to an MSPropertyDecl.
Definition: ExprCXX.h:728
Selector getSelector() const
Definition: DeclObjC.h:359
Represents a C++ conversion function within a class.
Definition: DeclCXX.h:2737
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:719
QualType getType() const
Definition: DeclObjC.h:846
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: ExprObjC.h:1341
bool isDecrementOp() const
Definition: Expr.h:1783
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition: Expr.h:412
bool isVoidPointerType() const
Definition: Type.cpp:426
RecordDecl * getDecl() const
Definition: Type.h:3992
Decl::Kind getDeclKind() const
Definition: DeclBase.h:1328
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:868
LLVM_READONLY char toLowercase(char c)
Converts the given ASCII character to its lowercase equivalent.
Definition: CharInfo.h:165
SelectorTable & Selectors
Definition: ASTContext.h:538
ActionResult - This structure is used while parsing/acting on expressions, stmts, etc...
Definition: Ownership.h:157
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:4982
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:277
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:823
CanQualType VoidTy
Definition: ASTContext.h:996
bool isValueDependent() const
isValueDependent - Determines whether this expression is value-dependent (C++ [temp.dep.constexpr]).
Definition: Expr.h:149
bool isObjCObjectPointerType() const
Definition: Type.h:6045
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:746
#define exp(__x)
Definition: tgmath.h:447
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:100
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:3204
StringRef getName() const
Return the actual identifier string.
Represents a C11 generic selection.
Definition: Expr.h:4697
SourceLocation getMemberLoc() const
Definition: ExprCXX.h:782
static ParmVarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
Definition: Decl.cpp:2456
Dataflow Directional Tag Classes.
void recordUseOfEvaluatedWeak(const ExprT *E, bool IsRead=true)
Definition: Sema.h:1338
UnaryOperatorKind
SourceLocation getLocation() const
Definition: ExprObjC.h:726
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:467
MSPropertyDecl * getPropertyDecl() const
Definition: ExprCXX.h:780
SourceRange getSourceRange(const SourceRange &Range)
Returns the SourceRange of a SourceRange.
Definition: FixIt.h:34
bool isObjCClassType() const
True if this is equivalent to the &#39;Class&#39; type, i.e.
Definition: Type.h:5514
bool DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, const ObjCInterfaceDecl *UnknownObjCClass=nullptr, bool ObjCPropertyAccess=false, bool AvoidPartialAvailabilityChecks=false)
Determine whether the use of this declaration is valid, and emit any corresponding diagnostics...
Definition: SemaExpr.cpp:204
static PseudoObjectExpr * Create(const ASTContext &Context, Expr *syntactic, ArrayRef< Expr *> semantic, unsigned resultIndex)
Definition: Expr.cpp:3920
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:783
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2552
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:171
Represents a pointer to an Objective C object.
Definition: Type.h:5446
ObjCMethodDecl * LookupInstanceMethodInGlobalPool(Selector Sel, SourceRange R, bool receiverIdOrClass=false)
LookupInstanceMethodInGlobalPool - Returns the method and warns if there are multiple signatures...
Definition: Sema.h:3608
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3982
CanQualType UnsignedLongTy
Definition: ASTContext.h:1005
Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV)
Can create any sort of selector.
CanQualType DependentTy
Definition: ASTContext.h:1013
DeclContext * getCurLexicalContext() const
Definition: Sema.h:10575
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:92
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2018
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
Definition: Type.cpp:1986
An implicit conversion.
Definition: Sema.h:9267
bool isLValueReferenceType() const
Definition: Type.h:5964
Reading or writing from this object requires a barrier call.
Definition: Type.h:183
QualType getSuperReceiverType() const
Definition: ExprObjC.h:730
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:989
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
sema::FunctionScopeInfo * getCurFunction() const
Definition: Sema.h:1321
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:3677
The parameter type of a method or function.
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:329
bool isPrefix() const
Definition: Expr.h:1770
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:265
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:275
ExprResult ExprError()
Definition: Ownership.h:283
LLVM_READONLY char toUppercase(char c)
Converts the given ASCII character to its uppercase equivalent.
Definition: CharInfo.h:174
bool DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *PD, ObjCMethodDecl *Getter, SourceLocation Loc)
CanQualType IntTy
Definition: ASTContext.h:1004
bool isObjectReceiver() const
Definition: ExprObjC.h:738
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:1864
bool isIgnored(unsigned DiagID, SourceLocation Loc) const
Determine whether the diagnostic is known to be ignored.
Definition: Diagnostic.h:818
bool isIncrementDecrementOp() const
Definition: Expr.h:1788
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:111
const Expr * getBase() const
Definition: ExprObjC.h:719
ASTContext & Context
Definition: Sema.h:317
This represents a decl that may have a name.
Definition: Decl.h:248
void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc)
Specify that this unqualified-id was parsed as an identifier.
Definition: DeclSpec.h:999
ExprResult BuildBinOp(Scope *S, SourceLocation OpLoc, BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr)
Definition: SemaExpr.cpp:12087
Selector getGetterName() const
Definition: DeclObjC.h:923
SourceLocation getLocStart() const LLVM_READONLY
Definition: Stmt.cpp:277
void setArg(unsigned Arg, Expr *ArgExpr)
setArg - Set the specified argument.
Definition: ExprObjC.h:1351
Expr * getBaseExpr() const
Definition: ExprCXX.h:779
SourceLocation getLocation() const
Definition: DeclBase.h:416
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:405
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2434
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1075
QualType getPointeeType() const
Gets the type pointed to by this ObjC pointer.
Definition: Type.h:5462