SemaPseudoObject.cpp 63 KB

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