ExprObjC.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/ComputeDependence.h"
  15. #include "clang/AST/DependenceFlags.h"
  16. #include "clang/AST/SelectorLocationsKind.h"
  17. #include "clang/AST/Type.h"
  18. #include "clang/AST/TypeLoc.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include <algorithm>
  22. #include <cassert>
  23. #include <cstdint>
  24. using namespace clang;
  25. ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
  26. ObjCMethodDecl *Method, SourceRange SR)
  27. : Expr(ObjCArrayLiteralClass, T, VK_PRValue, OK_Ordinary),
  28. NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
  29. Expr **SaveElements = getElements();
  30. for (unsigned I = 0, N = Elements.size(); I != N; ++I)
  31. SaveElements[I] = Elements[I];
  32. setDependence(computeDependence(this));
  33. }
  34. ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
  35. ArrayRef<Expr *> Elements,
  36. QualType T, ObjCMethodDecl *Method,
  37. SourceRange SR) {
  38. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size()));
  39. return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
  40. }
  41. ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
  42. unsigned NumElements) {
  43. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
  44. return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
  45. }
  46. ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
  47. bool HasPackExpansions, QualType T,
  48. ObjCMethodDecl *method,
  49. SourceRange SR)
  50. : Expr(ObjCDictionaryLiteralClass, T, VK_PRValue, OK_Ordinary),
  51. NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
  52. DictWithObjectsMethod(method) {
  53. KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
  54. ExpansionData *Expansions =
  55. HasPackExpansions ? getTrailingObjects<ExpansionData>() : nullptr;
  56. for (unsigned I = 0; I < NumElements; I++) {
  57. KeyValues[I].Key = VK[I].Key;
  58. KeyValues[I].Value = VK[I].Value;
  59. if (Expansions) {
  60. Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
  61. if (VK[I].NumExpansions)
  62. Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
  63. else
  64. Expansions[I].NumExpansionsPlusOne = 0;
  65. }
  66. }
  67. setDependence(computeDependence(this));
  68. }
  69. ObjCDictionaryLiteral *
  70. ObjCDictionaryLiteral::Create(const ASTContext &C,
  71. ArrayRef<ObjCDictionaryElement> VK,
  72. bool HasPackExpansions, QualType T,
  73. ObjCMethodDecl *method, SourceRange SR) {
  74. void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
  75. VK.size(), HasPackExpansions ? VK.size() : 0));
  76. return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
  77. }
  78. ObjCDictionaryLiteral *
  79. ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
  80. bool HasPackExpansions) {
  81. void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
  82. NumElements, HasPackExpansions ? NumElements : 0));
  83. return new (Mem)
  84. ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
  85. }
  86. QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const {
  87. if (isClassReceiver())
  88. return ctx.getObjCInterfaceType(getClassReceiver());
  89. if (isSuperReceiver())
  90. return getSuperReceiverType();
  91. return getBase()->getType();
  92. }
  93. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  94. SourceLocation LBracLoc,
  95. SourceLocation SuperLoc, bool IsInstanceSuper,
  96. QualType SuperType, Selector Sel,
  97. ArrayRef<SourceLocation> SelLocs,
  98. SelectorLocationsKind SelLocsK,
  99. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  100. SourceLocation RBracLoc, bool isImplicit)
  101. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
  102. SelectorOrMethod(
  103. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  104. Kind(IsInstanceSuper ? SuperInstance : SuperClass),
  105. HasMethod(Method != nullptr), IsDelegateInitCall(false),
  106. IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
  107. RBracLoc(RBracLoc) {
  108. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  109. setReceiverPointer(SuperType.getAsOpaquePtr());
  110. setDependence(computeDependence(this));
  111. }
  112. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  113. SourceLocation LBracLoc,
  114. TypeSourceInfo *Receiver, Selector Sel,
  115. ArrayRef<SourceLocation> SelLocs,
  116. SelectorLocationsKind SelLocsK,
  117. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  118. SourceLocation RBracLoc, bool isImplicit)
  119. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
  120. SelectorOrMethod(
  121. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  122. Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
  123. IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
  124. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  125. setReceiverPointer(Receiver);
  126. setDependence(computeDependence(this));
  127. }
  128. ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
  129. SourceLocation LBracLoc, Expr *Receiver,
  130. Selector Sel, ArrayRef<SourceLocation> SelLocs,
  131. SelectorLocationsKind SelLocsK,
  132. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  133. SourceLocation RBracLoc, bool isImplicit)
  134. : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
  135. SelectorOrMethod(
  136. reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
  137. Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
  138. IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
  139. initArgsAndSelLocs(Args, SelLocs, SelLocsK);
  140. setReceiverPointer(Receiver);
  141. setDependence(computeDependence(this));
  142. }
  143. void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
  144. ArrayRef<SourceLocation> SelLocs,
  145. SelectorLocationsKind SelLocsK) {
  146. setNumArgs(Args.size());
  147. Expr **MyArgs = getArgs();
  148. for (unsigned I = 0; I != Args.size(); ++I)
  149. MyArgs[I] = Args[I];
  150. SelLocsKind = SelLocsK;
  151. if (!isImplicit()) {
  152. if (SelLocsK == SelLoc_NonStandard)
  153. std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
  154. }
  155. }
  156. ObjCMessageExpr *
  157. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  158. SourceLocation LBracLoc, SourceLocation SuperLoc,
  159. bool IsInstanceSuper, QualType SuperType, Selector Sel,
  160. ArrayRef<SourceLocation> SelLocs,
  161. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  162. SourceLocation RBracLoc, bool isImplicit) {
  163. assert((!SelLocs.empty() || isImplicit) &&
  164. "No selector locs for non-implicit message");
  165. ObjCMessageExpr *Mem;
  166. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  167. if (isImplicit)
  168. Mem = alloc(Context, Args.size(), 0);
  169. else
  170. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  171. return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
  172. SuperType, Sel, SelLocs, SelLocsK, Method,
  173. Args, RBracLoc, isImplicit);
  174. }
  175. ObjCMessageExpr *
  176. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  177. SourceLocation LBracLoc, TypeSourceInfo *Receiver,
  178. Selector Sel, ArrayRef<SourceLocation> SelLocs,
  179. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  180. SourceLocation RBracLoc, bool isImplicit) {
  181. assert((!SelLocs.empty() || isImplicit) &&
  182. "No selector locs for non-implicit message");
  183. ObjCMessageExpr *Mem;
  184. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  185. if (isImplicit)
  186. Mem = alloc(Context, Args.size(), 0);
  187. else
  188. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  189. return new (Mem)
  190. ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
  191. Args, RBracLoc, isImplicit);
  192. }
  193. ObjCMessageExpr *
  194. ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
  195. SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
  196. ArrayRef<SourceLocation> SelLocs,
  197. ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
  198. SourceLocation RBracLoc, bool isImplicit) {
  199. assert((!SelLocs.empty() || isImplicit) &&
  200. "No selector locs for non-implicit message");
  201. ObjCMessageExpr *Mem;
  202. SelectorLocationsKind SelLocsK = SelectorLocationsKind();
  203. if (isImplicit)
  204. Mem = alloc(Context, Args.size(), 0);
  205. else
  206. Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
  207. return new (Mem)
  208. ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
  209. Args, RBracLoc, isImplicit);
  210. }
  211. ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
  212. unsigned NumArgs,
  213. unsigned NumStoredSelLocs) {
  214. ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
  215. return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
  216. }
  217. ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
  218. ArrayRef<Expr *> Args,
  219. SourceLocation RBraceLoc,
  220. ArrayRef<SourceLocation> SelLocs,
  221. Selector Sel,
  222. SelectorLocationsKind &SelLocsK) {
  223. SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
  224. unsigned NumStoredSelLocs =
  225. (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() : 0;
  226. return alloc(C, Args.size(), NumStoredSelLocs);
  227. }
  228. ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
  229. unsigned NumStoredSelLocs) {
  230. return (ObjCMessageExpr *)C.Allocate(
  231. totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
  232. alignof(ObjCMessageExpr));
  233. }
  234. void ObjCMessageExpr::getSelectorLocs(
  235. SmallVectorImpl<SourceLocation> &SelLocs) const {
  236. for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
  237. SelLocs.push_back(getSelectorLoc(i));
  238. }
  239. QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
  240. if (const ObjCMethodDecl *MD = getMethodDecl()) {
  241. QualType QT = MD->getReturnType();
  242. if (QT == Ctx.getObjCInstanceType()) {
  243. // instancetype corresponds to expression types.
  244. return getType();
  245. }
  246. return QT;
  247. }
  248. return Ctx.getReferenceQualifiedType(this);
  249. }
  250. SourceRange ObjCMessageExpr::getReceiverRange() const {
  251. switch (getReceiverKind()) {
  252. case Instance:
  253. return getInstanceReceiver()->getSourceRange();
  254. case Class:
  255. return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
  256. case SuperInstance:
  257. case SuperClass:
  258. return getSuperLoc();
  259. }
  260. llvm_unreachable("Invalid ReceiverKind!");
  261. }
  262. Selector ObjCMessageExpr::getSelector() const {
  263. if (HasMethod)
  264. return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
  265. ->getSelector();
  266. return Selector(SelectorOrMethod);
  267. }
  268. QualType ObjCMessageExpr::getReceiverType() const {
  269. switch (getReceiverKind()) {
  270. case Instance:
  271. return getInstanceReceiver()->getType();
  272. case Class:
  273. return getClassReceiver();
  274. case SuperInstance:
  275. case SuperClass:
  276. return getSuperType();
  277. }
  278. llvm_unreachable("unexpected receiver kind");
  279. }
  280. ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
  281. QualType T = getReceiverType();
  282. if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
  283. return Ptr->getInterfaceDecl();
  284. if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
  285. return Ty->getInterface();
  286. return nullptr;
  287. }
  288. Stmt::child_range ObjCMessageExpr::children() {
  289. Stmt **begin;
  290. if (getReceiverKind() == Instance)
  291. begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
  292. else
  293. begin = reinterpret_cast<Stmt **>(getArgs());
  294. return child_range(begin,
  295. reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
  296. }
  297. Stmt::const_child_range ObjCMessageExpr::children() const {
  298. auto Children = const_cast<ObjCMessageExpr *>(this)->children();
  299. return const_child_range(Children.begin(), Children.end());
  300. }
  301. StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
  302. switch (getBridgeKind()) {
  303. case OBC_Bridge:
  304. return "__bridge";
  305. case OBC_BridgeTransfer:
  306. return "__bridge_transfer";
  307. case OBC_BridgeRetained:
  308. return "__bridge_retained";
  309. }
  310. llvm_unreachable("Invalid BridgeKind!");
  311. }