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