1//===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
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 provides an abstract class for Objective-C code generation. Concrete
10// subclasses of this implement code generation for specific Objective-C
11// runtime libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
16#define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
17#include "CGBuilder.h"
18#include "CGCall.h"
19#include "CGCleanup.h"
20#include "CGValue.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/Basic/IdentifierTable.h" // Selector
23#include "llvm/ADT/UniqueVector.h"
24
25namespace llvm {
26class Constant;
27class Function;
28class Module;
29class StructLayout;
30class StructType;
31class Type;
32class Value;
33} // namespace llvm
34
35namespace clang {
36namespace CodeGen {
37class CGFunctionInfo;
38class CodeGenFunction;
39} // namespace CodeGen
40
41class FieldDecl;
42class ObjCAtTryStmt;
43class ObjCAtThrowStmt;
44class ObjCAtSynchronizedStmt;
45class ObjCContainerDecl;
46class ObjCCategoryImplDecl;
47class ObjCImplementationDecl;
48class ObjCInterfaceDecl;
49class ObjCMessageExpr;
50class ObjCMethodDecl;
51class ObjCProtocolDecl;
52class Selector;
53class ObjCIvarDecl;
54class ObjCStringLiteral;
55class BlockDeclRefExpr;
56
57namespace CodeGen {
58class CodeGenModule;
59class CGBlockInfo;
60
61// FIXME: Several methods should be pure virtual but aren't to avoid the
62// partially-implemented subclass breaking.
63
64/// Implements runtime-specific code generation functions.
65class CGObjCRuntime {
66protected:
67 CodeGen::CodeGenModule &CGM;
68 CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
69
70 // Utility functions for unified ivar access. These need to
71 // eventually be folded into other places (the structure layout
72 // code).
73
74 /// Compute an offset to the given ivar, suitable for passing to
75 /// EmitValueForIvarAtOffset. Note that the correct handling of
76 /// bit-fields is carefully coordinated by these two, use caution!
77 ///
78 /// The latter overload is suitable for computing the offset of a
79 /// sythesized ivar.
80 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
81 const ObjCInterfaceDecl *OID,
82 const ObjCIvarDecl *Ivar);
83 uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
84 const ObjCImplementationDecl *OID,
85 const ObjCIvarDecl *Ivar);
86
87 LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
88 const ObjCInterfaceDecl *OID,
89 llvm::Value *BaseValue,
90 const ObjCIvarDecl *Ivar,
91 unsigned CVRQualifiers, llvm::Value *Offset);
92 /// Emits a try / catch statement. This function is intended to be called by
93 /// subclasses, and provides a generic mechanism for generating these, which
94 /// should be usable by all runtimes. The caller must provide the functions
95 /// to call when entering and exiting a \@catch() block, and the function
96 /// used to rethrow exceptions. If the begin and end catch functions are
97 /// NULL, then the function assumes that the EH personality function provides
98 /// the thrown object directly.
99 void EmitTryCatchStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S,
100 llvm::FunctionCallee beginCatchFn,
101 llvm::FunctionCallee endCatchFn,
102 llvm::FunctionCallee exceptionRethrowFn);
103
104 void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn,
105 const VarDecl *paramDecl);
106
107 /// Emits an \@synchronize() statement, using the \p syncEnterFn and
108 /// \p syncExitFn arguments as the functions called to lock and unlock
109 /// the object. This function can be called by subclasses that use
110 /// zero-cost exception handling.
111 void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
112 const ObjCAtSynchronizedStmt &S,
113 llvm::FunctionCallee syncEnterFn,
114 llvm::FunctionCallee syncExitFn);
115
116public:
117 virtual ~CGObjCRuntime();
118
119 std::string getSymbolNameForMethod(const ObjCMethodDecl *method,
120 bool includeCategoryName = true,
121 bool useDirectABI = false);
122
123 /// Generate the function required to register all Objective-C components in
124 /// this compilation unit with the runtime library.
125 virtual llvm::Function *ModuleInitFunction() = 0;
126
127 /// Get a selector for the specified name and type values.
128 /// The result should have the LLVM type for ASTContext::getObjCSelType().
129 virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0;
130
131 /// Get the address of a selector for the specified name and type values.
132 /// This is a rarely-used language extension, but sadly it exists.
133 ///
134 /// The result should have the LLVM type for a pointer to
135 /// ASTContext::getObjCSelType().
136 virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0;
137
138 /// Get a typed selector.
139 virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
140 const ObjCMethodDecl *Method) = 0;
141
142 /// Get the type constant to catch for the given ObjC pointer type.
143 /// This is used externally to implement catching ObjC types in C++.
144 /// Runtimes which don't support this should add the appropriate
145 /// error to Sema.
146 virtual llvm::Constant *GetEHType(QualType T) = 0;
147
148 virtual CatchTypeInfo getCatchAllTypeInfo() { return {.RTTI: nullptr, .Flags: 0}; }
149
150 /// Generate a constant string object.
151 virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0;
152 virtual ConstantAddress GenerateConstantNumber(const bool Value,
153 const QualType &Ty) = 0;
154 virtual ConstantAddress GenerateConstantNumber(const llvm::APSInt &Value,
155 const QualType &Ty) = 0;
156 virtual ConstantAddress GenerateConstantNumber(const llvm::APFloat &Value,
157 const QualType &Ty) = 0;
158 virtual ConstantAddress
159 GenerateConstantArray(const ArrayRef<llvm::Constant *> &Objects) = 0;
160 virtual ConstantAddress GenerateConstantDictionary(
161 const ObjCDictionaryLiteral *E,
162 ArrayRef<std::pair<llvm::Constant *, llvm::Constant *>>
163 KeysAndObjects) = 0;
164
165 /// Generate a category. A category contains a list of methods (and
166 /// accompanying metadata) and a list of protocols.
167 virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
168
169 /// Generate a class structure for this class.
170 virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
171
172 /// Register an class alias.
173 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
174
175 /// Generate an Objective-C message send operation.
176 ///
177 /// \param Method - The method being called, this may be null if synthesizing
178 /// a property setter or getter.
179 virtual CodeGen::RValue
180 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot,
181 QualType ResultType, Selector Sel, llvm::Value *Receiver,
182 const CallArgList &CallArgs,
183 const ObjCInterfaceDecl *Class = nullptr,
184 const ObjCMethodDecl *Method = nullptr) = 0;
185
186 /// Generate an Objective-C message send operation.
187 ///
188 /// This variant allows for the call to be substituted with an optimized
189 /// variant.
190 CodeGen::RValue GeneratePossiblySpecializedMessageSend(
191 CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType,
192 Selector Sel, llvm::Value *Receiver, const CallArgList &Args,
193 const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method,
194 bool isClassMessage);
195
196 /// Generate an Objective-C message send operation to the super
197 /// class initiated in a method for Class and with the given Self
198 /// object.
199 ///
200 /// \param Method - The method being called, this may be null if synthesizing
201 /// a property setter or getter.
202 virtual CodeGen::RValue GenerateMessageSendSuper(
203 CodeGen::CodeGenFunction &CGF, ReturnValueSlot ReturnSlot,
204 QualType ResultType, Selector Sel, const ObjCInterfaceDecl *Class,
205 bool isCategoryImpl, llvm::Value *Self, bool IsClassMessage,
206 const CallArgList &CallArgs, const ObjCMethodDecl *Method = nullptr) = 0;
207
208 /// Walk the list of protocol references from a class, category or
209 /// protocol to traverse the DAG formed from it's inheritance hierarchy. Find
210 /// the list of protocols that ends each walk at either a runtime
211 /// protocol or a non-runtime protocol with no parents. For the common case of
212 /// just a list of standard runtime protocols this just returns the same list
213 /// that was passed in.
214 std::vector<const ObjCProtocolDecl *>
215 GetRuntimeProtocolList(ObjCProtocolDecl::protocol_iterator begin,
216 ObjCProtocolDecl::protocol_iterator end);
217
218 /// Emit the code to return the named protocol as an object, as in a
219 /// \@protocol expression.
220 virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
221 const ObjCProtocolDecl *OPD) = 0;
222
223 /// Generate the named protocol. Protocols contain method metadata but no
224 /// implementations.
225 virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
226
227 /// GetOrEmitProtocol - Get the protocol object for the given
228 /// declaration, emitting it if necessary. The return value has type
229 /// ProtocolPtrTy.
230 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
231
232 /// Generate a function preamble for a method with the specified
233 /// types.
234
235 // FIXME: Current this just generates the Function definition, but really this
236 // should also be generating the loads of the parameters, as the runtime
237 // should have full control over how parameters are passed.
238 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
239 const ObjCContainerDecl *CD) = 0;
240
241 /// Generates precondition checks for direct Objective-C Methods.
242 /// This includes [self self] for class methods and nil checks.
243 virtual void GenerateDirectMethodsPreconditionCheck(
244 CodeGenFunction &CGF, llvm::Function *Fn, const ObjCMethodDecl *OMD,
245 const ObjCContainerDecl *CD) = 0;
246
247 /// Generates prologue for direct Objective-C Methods.
248 virtual void GenerateDirectMethodPrologue(CodeGenFunction &CGF,
249 llvm::Function *Fn,
250 const ObjCMethodDecl *OMD,
251 const ObjCContainerDecl *CD) = 0;
252
253 /// Return the runtime function for getting properties.
254 virtual llvm::FunctionCallee GetPropertyGetFunction() = 0;
255
256 /// Return the runtime function for setting properties.
257 virtual llvm::FunctionCallee GetPropertySetFunction() = 0;
258
259 /// Return the runtime function for optimized setting properties.
260 virtual llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
261 bool copy) = 0;
262
263 // API for atomic copying of qualified aggregates in getter.
264 virtual llvm::FunctionCallee GetGetStructFunction() = 0;
265 // API for atomic copying of qualified aggregates in setter.
266 virtual llvm::FunctionCallee GetSetStructFunction() = 0;
267 /// API for atomic copying of qualified aggregates with non-trivial copy
268 /// assignment (c++) in setter.
269 virtual llvm::FunctionCallee GetCppAtomicObjectSetFunction() = 0;
270 /// API for atomic copying of qualified aggregates with non-trivial copy
271 /// assignment (c++) in getter.
272 virtual llvm::FunctionCallee GetCppAtomicObjectGetFunction() = 0;
273
274 /// GetClass - Return a reference to the class for the given
275 /// interface decl.
276 virtual llvm::Value *GetClass(CodeGenFunction &CGF,
277 const ObjCInterfaceDecl *OID) = 0;
278
279 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
280 llvm_unreachable("autoreleasepool unsupported in this ABI");
281 }
282
283 /// EnumerationMutationFunction - Return the function that's called by the
284 /// compiler when a mutation is detected during foreach iteration.
285 virtual llvm::FunctionCallee EnumerationMutationFunction() = 0;
286
287 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
288 const ObjCAtSynchronizedStmt &S) = 0;
289 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
290 const ObjCAtTryStmt &S) = 0;
291 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
292 const ObjCAtThrowStmt &S,
293 bool ClearInsertionPoint = true) = 0;
294 virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
295 Address AddrWeakObj) = 0;
296 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
297 llvm::Value *src, Address dest) = 0;
298 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
299 llvm::Value *src, Address dest,
300 bool threadlocal = false) = 0;
301 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
302 llvm::Value *src, Address dest,
303 llvm::Value *ivarOffset) = 0;
304 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
305 llvm::Value *src, Address dest) = 0;
306
307 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
308 QualType ObjectTy, llvm::Value *BaseValue,
309 const ObjCIvarDecl *Ivar,
310 unsigned CVRQualifiers) = 0;
311 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
312 const ObjCInterfaceDecl *Interface,
313 const ObjCIvarDecl *Ivar) = 0;
314 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
315 Address DestPtr, Address SrcPtr,
316 llvm::Value *Size) = 0;
317 virtual llvm::Constant *
318 BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
319 const CodeGen::CGBlockInfo &blockInfo) = 0;
320 virtual llvm::Constant *
321 BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
322 const CodeGen::CGBlockInfo &blockInfo) = 0;
323 virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM,
324 const CGBlockInfo &blockInfo) {
325 return {};
326 }
327
328 /// Returns an i8* which points to the byref layout information.
329 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
330 QualType T) = 0;
331
332 struct MessageSendInfo {
333 const CGFunctionInfo &CallInfo;
334 llvm::PointerType *MessengerType;
335
336 MessageSendInfo(const CGFunctionInfo &callInfo,
337 llvm::PointerType *messengerType)
338 : CallInfo(callInfo), MessengerType(messengerType) {}
339 };
340
341 MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
342 QualType resultType,
343 CallArgList &callArgs);
344
345 bool canMessageReceiverBeNull(CodeGenFunction &CGF,
346 const ObjCMethodDecl *method, bool isSuper,
347 const ObjCInterfaceDecl *classReceiver,
348 llvm::Value *receiver);
349
350 /// Check if a class object can be unrealized (not yet initialized).
351 /// Returns true if the class may be unrealized, false if provably realized.
352 ///
353 /// TODO: Returns false if:
354 /// - An instance method on the same class was called in a dominating path
355 /// - The class was explicitly realized earlier in control flow
356 /// - Note: [Parent foo] does NOT realize Child (inheritance care needed)
357 bool canClassObjectBeUnrealized(const ObjCInterfaceDecl *ClassDecl,
358 CodeGenFunction &CGF) const;
359
360 static bool isWeakLinkedClass(const ObjCInterfaceDecl *cls);
361
362 /// Destroy the callee-destroyed arguments of the given method,
363 /// if it has any. Used for nil-receiver paths in message sends.
364 /// Never does anything if the method does not satisfy
365 /// hasParamDestroyedInCallee().
366 ///
367 /// \param callArgs - just the formal arguments, not including implicit
368 /// arguments such as self and cmd
369 static void destroyCalleeDestroyedArguments(CodeGenFunction &CGF,
370 const ObjCMethodDecl *method,
371 const CallArgList &callArgs);
372
373 // FIXME: This probably shouldn't be here, but the code to compute
374 // it is here.
375 unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
376 const ObjCInterfaceDecl *ID,
377 const ObjCIvarDecl *Ivar);
378};
379
380/// Creates an instance of an Objective-C runtime class.
381// TODO: This should include some way of selecting which runtime to target.
382CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
383CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
384} // namespace CodeGen
385} // namespace clang
386#endif
387