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