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