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