ExprObjC.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //===- ExprObjC.cpp - (ObjC) Expression AST Node Implementation -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the subclesses of Expr class declared in ExprObjC.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ExprObjC.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/SelectorLocationsKind.h"
  15. #include "clang/AST/Type.h"
  16. #include "clang/AST/TypeLoc.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include <algorithm>
  20. #include <cassert>
  21. #include <cstdint>
  22. using namespace clang;
  23. ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
  24. ObjCMethodDecl *Method, SourceRange SR)
  25. : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
  26. false, false),
  27. NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
  28. Expr **SaveElements = getElements();
  29. for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
  30. if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
  31. ExprBits.ValueDependent = true;
  32. if (Elements[I]->isInstantiationDependent())
  33. ExprBits.InstantiationDependent = true;
  34. if (Elements[I]->containsUnexpandedParameterPack())
  35. ExprBits.ContainsUnexpandedParameterPack = true;
  36. SaveElements[I] = Elements[I];
  37. }
  38. }
  39. ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
  40. ArrayRef<Expr *> Elements,
  41. QualType T, ObjCMethodDecl *Method,
  42. SourceRange SR) {
  43. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size()));
  44. return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
  45. }
  46. ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
  47. unsigned NumElements) {
  48. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
  49. return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
  50. }
  51. ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
  52. bool HasPackExpansions, QualType T,
  53. ObjCMethodDecl *method,
  54. SourceRange SR)
  55. : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
  56. false, false),
  57. NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
  58. DictWithObjectsMethod(method) {
  59. KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
  60. ExpansionData *Expansions =
  61. HasPackExpansions ? getTrailingObjects<ExpansionData>() : nullptr;
  62. for (unsigned I = 0; I < NumElements; I++) {
  63. if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
  64. VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
  65. ExprBits.ValueDependent = true;
  66. if (VK[I].Key->isInstantiationDependent() ||
  67. VK[I].Value->isInstantiationDependent())
  68. ExprBits.InstantiationDependent = true;
  69. if (VK[I].EllipsisLoc.isInvalid() &&
  70. (VK[I].Key->containsUnexpandedParameterPack() ||
  71. VK[I].Value->containsUnexpandedParameterPack()))
  72. ExprBits.ContainsUnexpandedParameterPack = true;
  73. KeyValues[I].Key = VK[I].Key;
  74. KeyValues[I].Value = VK[I].Value;
  75. if (Expansions) {
  76. Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
  77. if (VK[I].NumExpansions)
  78. Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
  79. else
  80. Expansions[I].NumExpansionsPlusOne = 0;
  81. }
  82. }
  83. }
  84. ObjCDictionaryLiteral *
  85. ObjCDictionaryLiteral::Create(const ASTContext &C,
  86. ArrayRef<ObjCDictionaryElement> VK,
  87. bool HasPackExpansions, QualType T,
  88. ObjCMethodDecl *method, SourceRange SR) {
  89. void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
  90. VK.size(), HasPackExpansions ? VK.size() : 0));
  91. return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
  92. }
  93. ObjCDictionaryLiteral *
  94. ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
  95. bool HasPackExpansions) {
  96. void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
  97. NumElements, HasPackExpansions ? NumElements : 0));
  98. return new (Mem)
  99. ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
  100. }
  101. QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const {
  102. if (isClassReceiver())
  103. return ctx.getObjCInterfaceType(getClassReceiver());
  104. if (isSuperReceiver())
  105. return getSuperReceiverType();
  106. return getBase()->getType();
  107. }
  108. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  109. SourceLocation LBracLoc,
  110. SourceLocation SuperLoc, bool IsInstanceSuper,
  111. QualType SuperType, Selector Sel,
  112. ArrayRef<SourceLocation> SelLocs,
  113. SelectorLocationsKind SelLocsK,
  114. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  115. SourceLocation RBracLoc, bool isImplicit)
  116. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
  117. /*TypeDependent=*/false, /*ValueDependent=*/false,
  118. /*InstantiationDependent=*/false,
  119. /*ContainsUnexpandedParameterPack=*/false),
  120. SelectorOrMethod(
  121. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  122. Kind(IsInstanceSuper ? SuperInstance : SuperClass),
  123. HasMethod(Method != nullptr), IsDelegateInitCall(false),
  124. IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
  125. RBracLoc(RBracLoc) {
  126. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  127. setReceiverPointer(SuperType.getAsOpaquePtr());
  128. }
  129. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  130. SourceLocation LBracLoc,
  131. TypeSourceInfo *Receiver, Selector Sel,
  132. ArrayRef<SourceLocation> SelLocs,
  133. SelectorLocationsKind SelLocsK,
  134. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  135. SourceLocation RBracLoc, bool isImplicit)
  136. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
  137. T->isDependentType(), T->isInstantiationDependentType(),
  138. T->containsUnexpandedParameterPack()),
  139. SelectorOrMethod(
  140. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  141. Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
  142. IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
  143. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  144. setReceiverPointer(Receiver);
  145. }
  146. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  147. SourceLocation LBracLoc, Expr *Receiver,
  148. Selector Sel, ArrayRef<SourceLocation> SelLocs,
  149. SelectorLocationsKind SelLocsK,
  150. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  151. SourceLocation RBracLoc, bool isImplicit)
  152. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
  153. Receiver->isTypeDependent(), Receiver->isTypeDependent(),
  154. Receiver->isInstantiationDependent(),
  155. Receiver->containsUnexpandedParameterPack()),
  156. SelectorOrMethod(
  157. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  158. Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
  159. IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
  160. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  161. setReceiverPointer(Receiver);
  162. }
  163. void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
  164. ArrayRef<SourceLocation> SelLocs,
  165. SelectorLocationsKind SelLocsK) {
  166. setNumArgs(Args.size());
  167. Expr **MyArgs = getArgs();
  168. for (unsigned I = 0; I != Args.size(); ++I) {
  169. if (Args[I]->isTypeDependent())
  170. ExprBits.TypeDependent = true;
  171. if (Args[I]->isValueDependent())
  172. ExprBits.ValueDependent = true;
  173. if (Args[I]->isInstantiationDependent())
  174. ExprBits.InstantiationDependent = true;
  175. if (Args[I]->containsUnexpandedParameterPack())
  176. ExprBits.ContainsUnexpandedParameterPack = true;
  177. MyArgs[I] = Args[I];
  178. }
  179. SelLocsKind = SelLocsK;
  180. if (!isImplicit()) {
  181. if (SelLocsK == SelLoc_NonStandard)
  182. std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
  183. }
  184. }
  185. ObjCMessageExpr *
  186. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  187. SourceLocation LBracLoc, SourceLocation SuperLoc,
  188. bool IsInstanceSuper, QualType SuperType, Selector Sel,
  189. ArrayRef<SourceLocation> SelLocs,
  190. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  191. SourceLocation RBracLoc, bool isImplicit) {
  192. assert((!SelLocs.empty() || isImplicit) &&
  193. "No selector locs for non-implicit message");
  194. ObjCMessageExpr *Mem;
  195. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  196. if (isImplicit)
  197. Mem = alloc(Context, Args.size(), 0);
  198. else
  199. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  200. return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
  201. SuperType, Sel, SelLocs, SelLocsK, Method,
  202. Args, RBracLoc, isImplicit);
  203. }
  204. ObjCMessageExpr *
  205. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  206. SourceLocation LBracLoc, TypeSourceInfo *Receiver,
  207. Selector Sel, ArrayRef<SourceLocation> SelLocs,
  208. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  209. SourceLocation RBracLoc, bool isImplicit) {
  210. assert((!SelLocs.empty() || isImplicit) &&
  211. "No selector locs for non-implicit message");
  212. ObjCMessageExpr *Mem;
  213. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  214. if (isImplicit)
  215. Mem = alloc(Context, Args.size(), 0);
  216. else
  217. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  218. return new (Mem)
  219. ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
  220. Args, RBracLoc, isImplicit);
  221. }
  222. ObjCMessageExpr *
  223. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  224. SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
  225. ArrayRef<SourceLocation> SelLocs,
  226. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  227. SourceLocation RBracLoc, bool isImplicit) {
  228. assert((!SelLocs.empty() || isImplicit) &&
  229. "No selector locs for non-implicit message");
  230. ObjCMessageExpr *Mem;
  231. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  232. if (isImplicit)
  233. Mem = alloc(Context, Args.size(), 0);
  234. else
  235. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  236. return new (Mem)
  237. ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
  238. Args, RBracLoc, isImplicit);
  239. }
  240. ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
  241. unsigned NumArgs,
  242. unsigned NumStoredSelLocs) {
  243. ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
  244. return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
  245. }
  246. ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
  247. ArrayRef<Expr *> Args,
  248. SourceLocation RBraceLoc,
  249. ArrayRef<SourceLocation> SelLocs,
  250. Selector Sel,
  251. SelectorLocationsKind &SelLocsK) {
  252. SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
  253. unsigned NumStoredSelLocs =
  254. (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() : 0;
  255. return alloc(C, Args.size(), NumStoredSelLocs);
  256. }
  257. ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
  258. unsigned NumStoredSelLocs) {
  259. return (ObjCMessageExpr *)C.Allocate(
  260. totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
  261. alignof(ObjCMessageExpr));
  262. }
  263. void ObjCMessageExpr::getSelectorLocs(
  264. SmallVectorImpl<SourceLocation> &SelLocs) const {
  265. for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
  266. SelLocs.push_back(getSelectorLoc(i));
  267. }
  268. QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
  269. if (const ObjCMethodDecl *MD = getMethodDecl()) {
  270. QualType QT = MD->getReturnType();
  271. if (QT == Ctx.getObjCInstanceType()) {
  272. // instancetype corresponds to expression types.
  273. return getType();
  274. }
  275. return QT;
  276. }
  277. // Expression type might be different from an expected call return type,
  278. // as expression type would never be a reference even if call returns a
  279. // reference. Reconstruct the original expression type.
  280. QualType QT = getType();
  281. switch (getValueKind()) {
  282. case VK_LValue:
  283. return Ctx.getLValueReferenceType(QT);
  284. case VK_XValue:
  285. return Ctx.getRValueReferenceType(QT);
  286. case VK_RValue:
  287. return QT;
  288. }
  289. llvm_unreachable("Unsupported ExprValueKind");
  290. }
  291. SourceRange ObjCMessageExpr::getReceiverRange() const {
  292. switch (getReceiverKind()) {
  293. case Instance:
  294. return getInstanceReceiver()->getSourceRange();
  295. case Class:
  296. return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
  297. case SuperInstance:
  298. case SuperClass:
  299. return getSuperLoc();
  300. }
  301. llvm_unreachable("Invalid ReceiverKind!");
  302. }
  303. Selector ObjCMessageExpr::getSelector() const {
  304. if (HasMethod)
  305. return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
  306. ->getSelector();
  307. return Selector(SelectorOrMethod);
  308. }
  309. QualType ObjCMessageExpr::getReceiverType() const {
  310. switch (getReceiverKind()) {
  311. case Instance:
  312. return getInstanceReceiver()->getType();
  313. case Class:
  314. return getClassReceiver();
  315. case SuperInstance:
  316. case SuperClass:
  317. return getSuperType();
  318. }
  319. llvm_unreachable("unexpected receiver kind");
  320. }
  321. ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
  322. QualType T = getReceiverType();
  323. if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
  324. return Ptr->getInterfaceDecl();
  325. if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
  326. return Ty->getInterface();
  327. return nullptr;
  328. }
  329. Stmt::child_range ObjCMessageExpr::children() {
  330. Stmt **begin;
  331. if (getReceiverKind() == Instance)
  332. begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
  333. else
  334. begin = reinterpret_cast<Stmt **>(getArgs());
  335. return child_range(begin,
  336. reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
  337. }
  338. Stmt::const_child_range ObjCMessageExpr::children() const {
  339. auto Children = const_cast<ObjCMessageExpr *>(this)->children();
  340. return const_child_range(Children.begin(), Children.end());
  341. }
  342. StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
  343. switch (getBridgeKind()) {
  344. case OBC_Bridge:
  345. return "__bridge";
  346. case OBC_BridgeTransfer:
  347. return "__bridge_transfer";
  348. case OBC_BridgeRetained:
  349. return "__bridge_retained";
  350. }
  351. llvm_unreachable("Invalid BridgeKind!");
  352. }