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