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