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"
17#include "clang/AST/Type.h"
18#include "clang/AST/TypeLoc.h"
19#include "llvm/Support/ErrorHandling.h"
20#include <algorithm>
21#include <cassert>
22#include <cstdint>
23
24using namespace clang;
25
26ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
27 ObjCMethodDecl *Method, SourceRange SR)
28 : Expr(ObjCArrayLiteralClass, T, VK_PRValue, OK_Ordinary),
29 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
30 Expr **SaveElements = getElements();
31 for (unsigned I = 0, N = Elements.size(); I != N; ++I)
32 SaveElements[I] = Elements[I];
33
34 setDependence(computeDependence(this));
35}
36
38 ArrayRef<Expr *> Elements,
39 QualType T, ObjCMethodDecl *Method,
40 SourceRange SR) {
41 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size()));
42 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
43}
44
46 unsigned NumElements) {
47 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
48 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
49}
50
51ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
52 bool HasPackExpansions, QualType T,
53 ObjCMethodDecl *method,
54 SourceRange SR)
55 : Expr(ObjCDictionaryLiteralClass, T, VK_PRValue, OK_Ordinary),
56 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
57 DictWithObjectsMethod(method) {
58 KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
59 ExpansionData *Expansions =
60 HasPackExpansions ? getTrailingObjects<ExpansionData>() : nullptr;
61 for (unsigned I = 0; I < NumElements; I++) {
62 KeyValues[I].Key = VK[I].Key;
63 KeyValues[I].Value = VK[I].Value;
64 if (Expansions) {
65 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
66 if (VK[I].NumExpansions)
67 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
68 else
69 Expansions[I].NumExpansionsPlusOne = 0;
70 }
71 }
73}
74
78 bool HasPackExpansions, QualType T,
79 ObjCMethodDecl *method, SourceRange SR) {
80 void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
81 VK.size(), HasPackExpansions ? VK.size() : 0));
82 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
83}
84
86ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
87 bool HasPackExpansions) {
88 void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
89 NumElements, HasPackExpansions ? NumElements : 0));
90 return new (Mem)
91 ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
92}
93
95 if (isClassReceiver())
97
98 if (isSuperReceiver())
99 return getSuperReceiverType();
100
101 return getBase()->getType();
102}
103
104ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
105 SourceLocation LBracLoc,
106 SourceLocation SuperLoc, bool IsInstanceSuper,
107 QualType SuperType, Selector Sel,
109 SelectorLocationsKind SelLocsK,
110 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
111 SourceLocation RBracLoc, bool isImplicit)
112 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
113 SelectorOrMethod(
114 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
115 Kind(IsInstanceSuper ? SuperInstance : SuperClass),
116 HasMethod(Method != nullptr), IsDelegateInitCall(false),
117 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
118 RBracLoc(RBracLoc) {
119 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
120 setReceiverPointer(SuperType.getAsOpaquePtr());
122}
123
124ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
125 SourceLocation LBracLoc,
126 TypeSourceInfo *Receiver, Selector Sel,
128 SelectorLocationsKind SelLocsK,
129 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
130 SourceLocation RBracLoc, bool isImplicit)
131 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
132 SelectorOrMethod(
133 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
134 Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
135 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
136 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
137 setReceiverPointer(Receiver);
139}
140
141ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
142 SourceLocation LBracLoc, Expr *Receiver,
144 SelectorLocationsKind SelLocsK,
145 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
146 SourceLocation RBracLoc, bool isImplicit)
147 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary),
148 SelectorOrMethod(
149 reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
150 Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
151 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
152 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
153 setReceiverPointer(Receiver);
155}
156
157void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
159 SelectorLocationsKind SelLocsK) {
160 setNumArgs(Args.size());
161 Expr **MyArgs = getArgs();
162 for (unsigned I = 0; I != Args.size(); ++I)
163 MyArgs[I] = Args[I];
164
165 SelLocsKind = SelLocsK;
166 if (!isImplicit()) {
167 if (SelLocsK == SelLoc_NonStandard)
168 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
169 }
170}
171
174 SourceLocation LBracLoc, SourceLocation SuperLoc,
175 bool IsInstanceSuper, QualType SuperType, Selector Sel,
177 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
178 SourceLocation RBracLoc, bool isImplicit) {
179 assert((!SelLocs.empty() || isImplicit) &&
180 "No selector locs for non-implicit message");
181 ObjCMessageExpr *Mem;
183 if (isImplicit)
184 Mem = alloc(Context, Args.size(), 0);
185 else
186 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
187 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
188 SuperType, Sel, SelLocs, SelLocsK, Method,
189 Args, RBracLoc, isImplicit);
190}
191
194 SourceLocation LBracLoc, TypeSourceInfo *Receiver,
196 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
197 SourceLocation RBracLoc, bool isImplicit) {
198 assert((!SelLocs.empty() || isImplicit) &&
199 "No selector locs for non-implicit message");
200 ObjCMessageExpr *Mem;
202 if (isImplicit)
203 Mem = alloc(Context, Args.size(), 0);
204 else
205 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
206 return new (Mem)
207 ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
208 Args, RBracLoc, isImplicit);
209}
210
213 SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
215 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
216 SourceLocation RBracLoc, bool isImplicit) {
217 assert((!SelLocs.empty() || isImplicit) &&
218 "No selector locs for non-implicit message");
219 ObjCMessageExpr *Mem;
221 if (isImplicit)
222 Mem = alloc(Context, Args.size(), 0);
223 else
224 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
225 return new (Mem)
226 ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
227 Args, RBracLoc, isImplicit);
228}
229
231 unsigned NumArgs,
232 unsigned NumStoredSelLocs) {
233 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
234 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
235}
236
237ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
238 ArrayRef<Expr *> Args,
239 SourceLocation RBraceLoc,
241 Selector Sel,
242 SelectorLocationsKind &SelLocsK) {
243 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
244 unsigned NumStoredSelLocs =
245 (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() : 0;
246 return alloc(C, Args.size(), NumStoredSelLocs);
247}
248
249ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
250 unsigned NumStoredSelLocs) {
251 return (ObjCMessageExpr *)C.Allocate(
252 totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
253 alignof(ObjCMessageExpr));
254}
255
257 SmallVectorImpl<SourceLocation> &SelLocs) const {
258 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
259 SelLocs.push_back(getSelectorLoc(i));
260}
261
262
264 if (const ObjCMethodDecl *MD = getMethodDecl()) {
265 QualType QT = MD->getReturnType();
266 if (QT == Ctx.getObjCInstanceType()) {
267 // instancetype corresponds to expression types.
268 return getType();
269 }
270 return QT;
271 }
272 return Ctx.getReferenceQualifiedType(this);
273}
274
276 switch (getReceiverKind()) {
277 case Instance:
279
280 case Class:
282
283 case SuperInstance:
284 case SuperClass:
285 return getSuperLoc();
286 }
287
288 llvm_unreachable("Invalid ReceiverKind!");
289}
290
292 if (HasMethod)
293 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
294 ->getSelector();
295 return Selector(SelectorOrMethod);
296}
297
299 switch (getReceiverKind()) {
300 case Instance:
301 return getInstanceReceiver()->getType();
302 case Class:
303 return getClassReceiver();
304 case SuperInstance:
305 case SuperClass:
306 return getSuperType();
307 }
308
309 llvm_unreachable("unexpected receiver kind");
310}
311
314
316 return Ptr->getInterfaceDecl();
317
318 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
319 return Ty->getInterface();
320
321 return nullptr;
322}
323
325 Stmt **begin;
326 if (getReceiverKind() == Instance)
327 begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
328 else
329 begin = reinterpret_cast<Stmt **>(getArgs());
330 return child_range(begin,
331 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
332}
333
335 auto Children = const_cast<ObjCMessageExpr *>(this)->children();
336 return const_child_range(Children.begin(), Children.end());
337}
338
340 switch (getBridgeKind()) {
341 case OBC_Bridge:
342 return "__bridge";
344 return "__bridge_transfer";
346 return "__bridge_retained";
347 }
348
349 llvm_unreachable("Invalid BridgeKind!");
350}
Defines the clang::ASTContext interface.
SourceRange Range
Definition: SemaObjC.cpp:758
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:188
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:2077
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:45
static ObjCArrayLiteral * Create(const ASTContext &C, ArrayRef< Expr * > Elements, QualType T, ObjCMethodDecl *Method, SourceRange SR)
Definition: ExprObjC.cpp:37
StringRef getBridgeKindName() const
Retrieve the kind of bridge being performed as a string.
Definition: ExprObjC.cpp:339
ObjCBridgeCastKind getBridgeKind() const
Determine which kind of bridge is being performed via this cast.
Definition: ExprObjC.h:1658
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:76
static ObjCDictionaryLiteral * CreateEmpty(const ASTContext &C, unsigned NumElements, bool HasPackExpansions)
Definition: ExprObjC.cpp:86
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:941
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:173
void getSelectorLocs(SmallVectorImpl< SourceLocation > &SelLocs) const
Definition: ExprObjC.cpp:256
bool isImplicit() const
Indicates whether the message send was implicitly generated by the implementation.
Definition: ExprObjC.h:1226
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:230
Expr ** getArgs()
Retrieve the arguments to this message, not including the receiver.
Definition: ExprObjC.h:1382
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
Definition: ExprObjC.h:1256
QualType getCallReturnType(ASTContext &Ctx) const
Definition: ExprObjC.cpp:263
SourceLocation getSuperLoc() const
Retrieve the location of the 'super' keyword for a class or instance message to 'super',...
Definition: ExprObjC.h:1297
Selector getSelector() const
Definition: ExprObjC.cpp:291
@ SuperInstance
The receiver is the instance of the superclass object.
Definition: ExprObjC.h:955
@ Instance
The receiver is an object instance.
Definition: ExprObjC.h:949
@ SuperClass
The receiver is a superclass.
Definition: ExprObjC.h:952
@ Class
The receiver is a class.
Definition: ExprObjC.h:946
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:1284
QualType getClassReceiver() const
Returns the type of a class message send, or NULL if the message is not a class message.
Definition: ExprObjC.h:1275
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:312
QualType getSuperType() const
Retrieve the type referred to by 'super'.
Definition: ExprObjC.h:1332
const ObjCMethodDecl * getMethodDecl() const
Definition: ExprObjC.h:1352
SourceRange getReceiverRange() const
Source range of the receiver.
Definition: ExprObjC.cpp:275
unsigned getNumSelectorLocs() const
Definition: ExprObjC.h:1433
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
Definition: ExprObjC.h:1230
child_range children()
Definition: ExprObjC.cpp:324
QualType getReceiverType() const
Retrieve the receiver type to which this message is being directed.
Definition: ExprObjC.cpp:298
SourceLocation getSelectorLoc(unsigned Index) const
Definition: ExprObjC.h:1421
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
Definition: ExprObjC.h:1378
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
Represents a pointer to an Objective C object.
Definition: Type.h:7580
Represents a class type in Objective C.
Definition: Type.h:7326
const Expr * getBase() const
Definition: ExprObjC.h:755
QualType getSuperReceiverType() const
Definition: ExprObjC.h:762
ObjCInterfaceDecl * getClassReceiver() const
Definition: ExprObjC.h:766
QualType getReceiverType(const ASTContext &ctx) const
Determine the type of the base, regardless of the kind of receiver.
Definition: ExprObjC.cpp:94
bool isClassReceiver() const
Definition: ExprObjC.h:772
bool isSuperReceiver() const
Definition: ExprObjC.h:771
A (possibly-)qualified type.
Definition: Type.h:929
void * getAsOpaquePtr() const
Definition: Type.h:976
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:333
llvm::iterator_range< child_iterator > child_range
Definition: Stmt.h:1469
llvm::iterator_range< const_child_iterator > const_child_range
Definition: Stmt.h:1470
SourceRange getSourceRange() const LLVM_READONLY
Get the full source range.
Definition: TypeLoc.h:153
A container of type source information.
Definition: Type.h:7902
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:8731
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:1320