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