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