clang 20.0.0git
ExprObjC.cpp
Go to the documentation of this file.
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"
18#include "clang/AST/Type.h"
19#include "clang/AST/TypeLoc.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/ErrorHandling.h"
22#include <algorithm>
23#include <cassert>
24#include <cstdint>
25
26using namespace clang;
27
28ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
29 ObjCMethodDecl *Method, SourceRange SR)
30 : Expr(ObjCArrayLiteralClass, T, VK_PRValue, OK_Ordinary),
31 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
32 Expr **SaveElements = getElements();
33 for (unsigned I = 0, N = Elements.size(); I != N; ++I)
34 SaveElements[I] = Elements[I];
35
36 setDependence(computeDependence(this));
37}
38
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
48 unsigned NumElements) {
49 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
50 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
51}
52
53ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
54 bool HasPackExpansions, QualType T,
55 ObjCMethodDecl *method,
56 SourceRange SR)
57 : Expr(ObjCDictionaryLiteralClass, T, VK_PRValue, OK_Ordinary),
58 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
59 DictWithObjectsMethod(method) {
60 KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
61 ExpansionData *Expansions =
62 HasPackExpansions ? getTrailingObjects<ExpansionData>() : nullptr;
63 for (unsigned I = 0; I < NumElements; I++) {
64 KeyValues[I].Key = VK[I].Key;
65 KeyValues[I].Value = VK[I].Value;
66 if (Expansions) {
67 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
68 if (VK[I].NumExpansions)
69 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
70 else
71 Expansions[I].NumExpansionsPlusOne = 0;
72 }
73 }
75}
76
80 bool HasPackExpansions, QualType T,
81 ObjCMethodDecl *method, SourceRange SR) {
82 void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
83 VK.size(), HasPackExpansions ? VK.size() : 0));
84 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
85}
86
88ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
89 bool HasPackExpansions) {
90 void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
91 NumElements, HasPackExpansions ? NumElements : 0));
92 return new (Mem)
93 ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
94}
95
97 if (isClassReceiver())
99
100 if (isSuperReceiver())
101 return getSuperReceiverType();
102
103 return getBase()->getType();
104}
105
106ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
107 SourceLocation LBracLoc,
108 SourceLocation SuperLoc, bool IsInstanceSuper,
109 QualType SuperType, Selector Sel,
111 SelectorLocationsKind SelLocsK,
112 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
113 SourceLocation RBracLoc, bool isImplicit)
114 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
115 SelectorOrMethod(
116 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
117 Kind(IsInstanceSuper ? SuperInstance : SuperClass),
118 HasMethod(Method != nullptr), IsDelegateInitCall(false),
119 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
120 RBracLoc(RBracLoc) {
121 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
122 setReceiverPointer(SuperType.getAsOpaquePtr());
124}
125
126ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
127 SourceLocation LBracLoc,
128 TypeSourceInfo *Receiver, Selector Sel,
130 SelectorLocationsKind SelLocsK,
131 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
132 SourceLocation RBracLoc, bool isImplicit)
133 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
134 SelectorOrMethod(
135 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
136 Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
137 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
138 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
139 setReceiverPointer(Receiver);
141}
142
143ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
144 SourceLocation LBracLoc, Expr *Receiver,
146 SelectorLocationsKind SelLocsK,
147 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
148 SourceLocation RBracLoc, bool isImplicit)
149 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
150 SelectorOrMethod(
151 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
152 Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
153 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
154 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
155 setReceiverPointer(Receiver);
157}
158
159void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
161 SelectorLocationsKind SelLocsK) {
162 setNumArgs(Args.size());
163 Expr **MyArgs = getArgs();
164 for (unsigned I = 0; I != Args.size(); ++I)
165 MyArgs[I] = Args[I];
166
167 SelLocsKind = SelLocsK;
168 if (!isImplicit()) {
169 if (SelLocsK == SelLoc_NonStandard)
170 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
171 }
172}
173
176 SourceLocation LBracLoc, SourceLocation SuperLoc,
177 bool IsInstanceSuper, QualType SuperType, Selector Sel,
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;
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) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
190 SuperType, Sel, SelLocs, SelLocsK, Method,
191 Args, RBracLoc, isImplicit);
192}
193
196 SourceLocation LBracLoc, TypeSourceInfo *Receiver,
198 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
199 SourceLocation RBracLoc, bool isImplicit) {
200 assert((!SelLocs.empty() || isImplicit) &&
201 "No selector locs for non-implicit message");
202 ObjCMessageExpr *Mem;
204 if (isImplicit)
205 Mem = alloc(Context, Args.size(), 0);
206 else
207 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
208 return new (Mem)
209 ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
210 Args, RBracLoc, isImplicit);
211}
212
215 SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
217 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
218 SourceLocation RBracLoc, bool isImplicit) {
219 assert((!SelLocs.empty() || isImplicit) &&
220 "No selector locs for non-implicit message");
221 ObjCMessageExpr *Mem;
223 if (isImplicit)
224 Mem = alloc(Context, Args.size(), 0);
225 else
226 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
227 return new (Mem)
228 ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
229 Args, RBracLoc, isImplicit);
230}
231
233 unsigned NumArgs,
234 unsigned NumStoredSelLocs) {
235 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
236 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
237}
238
239ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
240 ArrayRef<Expr *> Args,
241 SourceLocation RBraceLoc,
243 Selector Sel,
244 SelectorLocationsKind &SelLocsK) {
245 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
246 unsigned NumStoredSelLocs =
247 (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() : 0;
248 return alloc(C, Args.size(), NumStoredSelLocs);
249}
250
251ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
252 unsigned NumStoredSelLocs) {
253 return (ObjCMessageExpr *)C.Allocate(
254 totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
255 alignof(ObjCMessageExpr));
256}
257
259 SmallVectorImpl<SourceLocation> &SelLocs) const {
260 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
261 SelLocs.push_back(getSelectorLoc(i));
262}
263
264
266 if (const ObjCMethodDecl *MD = getMethodDecl()) {
267 QualType QT = MD->getReturnType();
268 if (QT == Ctx.getObjCInstanceType()) {
269 // instancetype corresponds to expression types.
270 return getType();
271 }
272 return QT;
273 }
274 return Ctx.getReferenceQualifiedType(this);
275}
276
278 switch (getReceiverKind()) {
279 case Instance:
281
282 case Class:
284
285 case SuperInstance:
286 case SuperClass:
287 return getSuperLoc();
288 }
289
290 llvm_unreachable("Invalid ReceiverKind!");
291}
292
294 if (HasMethod)
295 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
296 ->getSelector();
297 return Selector(SelectorOrMethod);
298}
299
301 switch (getReceiverKind()) {
302 case Instance:
303 return getInstanceReceiver()->getType();
304 case Class:
305 return getClassReceiver();
306 case SuperInstance:
307 case SuperClass:
308 return getSuperType();
309 }
310
311 llvm_unreachable("unexpected receiver kind");
312}
313
316
318 return Ptr->getInterfaceDecl();
319
320 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
321 return Ty->getInterface();
322
323 return nullptr;
324}
325
327 Stmt **begin;
328 if (getReceiverKind() == Instance)
329 begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
330 else
331 begin = reinterpret_cast<Stmt **>(getArgs());
332 return child_range(begin,
333 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
334}
335
337 auto Children = const_cast<ObjCMessageExpr *>(this)->children();
338 return const_child_range(Children.begin(), Children.end());
339}
340
342 switch (getBridgeKind()) {
343 case OBC_Bridge:
344 return "__bridge";
346 return "__bridge_transfer";
348 return "__bridge_retained";
349 }
350
351 llvm_unreachable("Invalid BridgeKind!");
352}
Defines the clang::ASTContext interface.
SourceRange Range
Definition: SemaObjC.cpp:757
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, ObjCInterfaceDecl *PrevDecl=nullptr) const
getObjCInterfaceType - Return the unique reference to the type for the specified ObjC interface decl.
QualType getReferenceQualifiedType(const Expr *e) const
getReferenceQualifiedType - Given an expr, will return the type for that expression,...
QualType getObjCInstanceType()
Retrieve the Objective-C "instancetype" type, if already known; otherwise, returns a NULL type;.
Definition: ASTContext.h:1998
This represents one expression.
Definition: Expr.h:110
QualType getType() const
Definition: Expr.h:142
void setDependence(ExprDependence Deps)
Each concrete expr subclass is expected to compute its dependence and call this in the constructor.
Definition: Expr.h:135
ObjCArrayLiteral - used for objective-c array containers; as in: @["Hello", NSApp,...
Definition: ExprObjC.h:191
static ObjCArrayLiteral * CreateEmpty(const ASTContext &C, unsigned NumElements)
Definition: ExprObjC.cpp:47
static ObjCArrayLiteral * Create(const ASTContext &C, ArrayRef< Expr * > Elements, QualType T, ObjCMethodDecl *Method, SourceRange SR)
Definition: ExprObjC.cpp:39
StringRef getBridgeKindName() const
Retrieve the kind of bridge being performed as a string.
Definition: ExprObjC.cpp:341
ObjCBridgeCastKind getBridgeKind() const
Determine which kind of bridge is being performed via this cast.
Definition: ExprObjC.h:1662
ObjCDictionaryLiteral - AST node to represent objective-c dictionary literals; as in:"name" : NSUserN...
Definition: ExprObjC.h:309
static ObjCDictionaryLiteral * Create(const ASTContext &C, ArrayRef< ObjCDictionaryElement > VK, bool HasPackExpansions, QualType T, ObjCMethodDecl *method, SourceRange SR)
Definition: ExprObjC.cpp:78
static ObjCDictionaryLiteral * CreateEmpty(const ASTContext &C, unsigned NumElements, bool HasPackExpansions)
Definition: ExprObjC.cpp:88
Represents an ObjC class declaration.
Definition: DeclObjC.h:1153
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:945
static ObjCMessageExpr * Create(const ASTContext &Context, QualType T, ExprValueKind VK, SourceLocation LBracLoc, SourceLocation SuperLoc, bool IsInstanceSuper, QualType SuperType, Selector Sel, ArrayRef< SourceLocation > SelLocs, ObjCMethodDecl *Method, ArrayRef< Expr * > Args, SourceLocation RBracLoc, bool isImplicit)
Create a message send to super.
Definition: ExprObjC.cpp:175
void getSelectorLocs(SmallVectorImpl< SourceLocation > &SelLocs) const
Definition: ExprObjC.cpp:258
bool isImplicit() const
Indicates whether the message send was implicitly generated by the implementation.
Definition: ExprObjC.h:1230
static ObjCMessageExpr * CreateEmpty(const ASTContext &Context, unsigned NumArgs, unsigned NumStoredSelLocs)
Create an empty Objective-C message expression, to be filled in by subsequent calls.
Definition: ExprObjC.cpp:232
Expr ** getArgs()
Retrieve the arguments to this message, not including the receiver.
Definition: ExprObjC.h:1386
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1260
QualType getCallReturnType(ASTContext &Ctx) const
Definition: ExprObjC.cpp:265
SourceLocation getSuperLoc() const
Retrieve the location of the 'super' keyword for a class or instance message to 'super',...
Definition: ExprObjC.h:1301
Selector getSelector() const
Definition: ExprObjC.cpp:293
@ SuperInstance
The receiver is the instance of the superclass object.
Definition: ExprObjC.h:959
@ Instance
The receiver is an object instance.
Definition: ExprObjC.h:953
@ SuperClass
The receiver is a superclass.
Definition: ExprObjC.h:956
@ Class
The receiver is a class.
Definition: ExprObjC.h:950
TypeSourceInfo * getClassReceiverTypeInfo() const
Returns a type-source information of a class message send, or nullptr if the message is not a class m...
Definition: ExprObjC.h:1288
QualType getClassReceiver() const
Returns the type of a class message send, or NULL if the message is not a class message.
Definition: ExprObjC.h:1279
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:314
QualType getSuperType() const
Retrieve the type referred to by 'super'.
Definition: ExprObjC.h:1336
const ObjCMethodDecl * getMethodDecl() const
Definition: ExprObjC.h:1356
SourceRange getReceiverRange() const
Source range of the receiver.
Definition: ExprObjC.cpp:277
unsigned getNumSelectorLocs() const
Definition: ExprObjC.h:1437
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
Definition: ExprObjC.h:1234
child_range children()
Definition: ExprObjC.cpp:326
QualType getReceiverType() const
Retrieve the receiver type to which this message is being directed.
Definition: ExprObjC.cpp:300
SourceLocation getSelectorLoc(unsigned Index) const
Definition: ExprObjC.h:1425
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
Definition: ExprObjC.h:1382
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
Represents a pointer to an Objective C object.
Definition: Type.h:7399
Represents a class type in Objective C.
Definition: Type.h:7145
const Expr * getBase() const
Definition: ExprObjC.h:755
QualType getSuperReceiverType() const
Definition: ExprObjC.h:766
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:770
QualType getReceiverType(const ASTContext &ctx) const
Determine the type of the base, regardless of the kind of receiver.
Definition: ExprObjC.cpp:96
bool isClassReceiver() const
Definition: ExprObjC.h:776
bool isSuperReceiver() const
Definition: ExprObjC.h:775
A (possibly-)qualified type.
Definition: Type.h:941
void * getAsOpaquePtr() const
Definition: Type.h:988
Smart pointer class that efficiently represents Objective-C method names.
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:326
llvm::iterator_range< child_iterator > child_range
Definition: Stmt.h:1447
llvm::iterator_range< const_child_iterator > const_child_range
Definition: Stmt.h:1448
SourceRange getSourceRange() const LLVM_READONLY
Get the full source range.
Definition: TypeLoc.h:153
A container of type source information.
Definition: Type.h:7721
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:256
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
The JSON file list parser is used to communicate input to InstallAPI.
SelectorLocationsKind
Whether all locations of the selector identifiers are in a "standard" position.
@ SelLoc_NonStandard
Non-standard.
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition: Specifiers.h:151
ExprDependence computeDependence(FullExpr *E)
SelectorLocationsKind hasStandardSelectorLocs(Selector Sel, ArrayRef< SourceLocation > SelLocs, ArrayRef< Expr * > Args, SourceLocation EndLoc)
Returns true if all SelLocs are in a "standard" location.
@ OBC_Bridge
Bridging via __bridge, which does nothing but reinterpret the bits.
@ OBC_BridgeTransfer
Bridging via __bridge_transfer, which transfers ownership of an Objective-C pointer into ARC.
@ OBC_BridgeRetained
Bridging via __bridge_retain, which makes an ARC object available as a +1 C pointer.
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition: Specifiers.h:132
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:135
const FunctionProtoType * T
@ Class
The "class" keyword introduces the elaborated-type-specifier.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
#define false
Definition: stdbool.h:26
A placeholder type used to construct an empty shell of a type, that will be filled in later (e....
Definition: Stmt.h:1298