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