1e5dd7070Spatrick //===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This provides Objective-C code generation targeting the GNU runtime. The
10e5dd7070Spatrick // class in this file generates structures used by the GNU Objective-C runtime
11e5dd7070Spatrick // library. These structures are defined in objc/objc.h and objc/objc-api.h in
12e5dd7070Spatrick // the GNU runtime distribution.
13e5dd7070Spatrick //
14e5dd7070Spatrick //===----------------------------------------------------------------------===//
15e5dd7070Spatrick
16e5dd7070Spatrick #include "CGCXXABI.h"
17e5dd7070Spatrick #include "CGCleanup.h"
18e5dd7070Spatrick #include "CGObjCRuntime.h"
19e5dd7070Spatrick #include "CodeGenFunction.h"
20e5dd7070Spatrick #include "CodeGenModule.h"
21e5dd7070Spatrick #include "clang/AST/ASTContext.h"
22e5dd7070Spatrick #include "clang/AST/Attr.h"
23e5dd7070Spatrick #include "clang/AST/Decl.h"
24e5dd7070Spatrick #include "clang/AST/DeclObjC.h"
25e5dd7070Spatrick #include "clang/AST/RecordLayout.h"
26e5dd7070Spatrick #include "clang/AST/StmtObjC.h"
27e5dd7070Spatrick #include "clang/Basic/FileManager.h"
28e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
29e5dd7070Spatrick #include "clang/CodeGen/ConstantInitBuilder.h"
30e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
31e5dd7070Spatrick #include "llvm/ADT/StringMap.h"
32e5dd7070Spatrick #include "llvm/IR/DataLayout.h"
33e5dd7070Spatrick #include "llvm/IR/Intrinsics.h"
34e5dd7070Spatrick #include "llvm/IR/LLVMContext.h"
35e5dd7070Spatrick #include "llvm/IR/Module.h"
36e5dd7070Spatrick #include "llvm/Support/Compiler.h"
37e5dd7070Spatrick #include "llvm/Support/ConvertUTF.h"
38e5dd7070Spatrick #include <cctype>
39e5dd7070Spatrick
40e5dd7070Spatrick using namespace clang;
41e5dd7070Spatrick using namespace CodeGen;
42e5dd7070Spatrick
43e5dd7070Spatrick namespace {
44e5dd7070Spatrick
45e5dd7070Spatrick /// Class that lazily initialises the runtime function. Avoids inserting the
46e5dd7070Spatrick /// types and the function declaration into a module if they're not used, and
47e5dd7070Spatrick /// avoids constructing the type more than once if it's used more than once.
48e5dd7070Spatrick class LazyRuntimeFunction {
49e5dd7070Spatrick CodeGenModule *CGM;
50e5dd7070Spatrick llvm::FunctionType *FTy;
51e5dd7070Spatrick const char *FunctionName;
52e5dd7070Spatrick llvm::FunctionCallee Function;
53e5dd7070Spatrick
54e5dd7070Spatrick public:
55e5dd7070Spatrick /// Constructor leaves this class uninitialized, because it is intended to
56e5dd7070Spatrick /// be used as a field in another class and not all of the types that are
57e5dd7070Spatrick /// used as arguments will necessarily be available at construction time.
LazyRuntimeFunction()58e5dd7070Spatrick LazyRuntimeFunction()
59e5dd7070Spatrick : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
60e5dd7070Spatrick
61e5dd7070Spatrick /// Initialises the lazy function with the name, return type, and the types
62e5dd7070Spatrick /// of the arguments.
63e5dd7070Spatrick template <typename... Tys>
init(CodeGenModule * Mod,const char * name,llvm::Type * RetTy,Tys * ...Types)64e5dd7070Spatrick void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy,
65e5dd7070Spatrick Tys *... Types) {
66e5dd7070Spatrick CGM = Mod;
67e5dd7070Spatrick FunctionName = name;
68e5dd7070Spatrick Function = nullptr;
69e5dd7070Spatrick if(sizeof...(Tys)) {
70e5dd7070Spatrick SmallVector<llvm::Type *, 8> ArgTys({Types...});
71e5dd7070Spatrick FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
72e5dd7070Spatrick }
73e5dd7070Spatrick else {
74*12c85518Srobert FTy = llvm::FunctionType::get(RetTy, std::nullopt, false);
75e5dd7070Spatrick }
76e5dd7070Spatrick }
77e5dd7070Spatrick
getType()78e5dd7070Spatrick llvm::FunctionType *getType() { return FTy; }
79e5dd7070Spatrick
80e5dd7070Spatrick /// Overloaded cast operator, allows the class to be implicitly cast to an
81e5dd7070Spatrick /// LLVM constant.
operator llvm::FunctionCallee()82e5dd7070Spatrick operator llvm::FunctionCallee() {
83e5dd7070Spatrick if (!Function) {
84e5dd7070Spatrick if (!FunctionName)
85e5dd7070Spatrick return nullptr;
86e5dd7070Spatrick Function = CGM->CreateRuntimeFunction(FTy, FunctionName);
87e5dd7070Spatrick }
88e5dd7070Spatrick return Function;
89e5dd7070Spatrick }
90e5dd7070Spatrick };
91e5dd7070Spatrick
92e5dd7070Spatrick
93e5dd7070Spatrick /// GNU Objective-C runtime code generation. This class implements the parts of
94e5dd7070Spatrick /// Objective-C support that are specific to the GNU family of runtimes (GCC,
95e5dd7070Spatrick /// GNUstep and ObjFW).
96e5dd7070Spatrick class CGObjCGNU : public CGObjCRuntime {
97e5dd7070Spatrick protected:
98e5dd7070Spatrick /// The LLVM module into which output is inserted
99e5dd7070Spatrick llvm::Module &TheModule;
100e5dd7070Spatrick /// strut objc_super. Used for sending messages to super. This structure
101e5dd7070Spatrick /// contains the receiver (object) and the expected class.
102e5dd7070Spatrick llvm::StructType *ObjCSuperTy;
103e5dd7070Spatrick /// struct objc_super*. The type of the argument to the superclass message
104e5dd7070Spatrick /// lookup functions.
105e5dd7070Spatrick llvm::PointerType *PtrToObjCSuperTy;
106e5dd7070Spatrick /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring
107e5dd7070Spatrick /// SEL is included in a header somewhere, in which case it will be whatever
108e5dd7070Spatrick /// type is declared in that header, most likely {i8*, i8*}.
109e5dd7070Spatrick llvm::PointerType *SelectorTy;
110*12c85518Srobert /// Element type of SelectorTy.
111*12c85518Srobert llvm::Type *SelectorElemTy;
112e5dd7070Spatrick /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the
113e5dd7070Spatrick /// places where it's used
114e5dd7070Spatrick llvm::IntegerType *Int8Ty;
115e5dd7070Spatrick /// Pointer to i8 - LLVM type of char*, for all of the places where the
116e5dd7070Spatrick /// runtime needs to deal with C strings.
117e5dd7070Spatrick llvm::PointerType *PtrToInt8Ty;
118e5dd7070Spatrick /// struct objc_protocol type
119e5dd7070Spatrick llvm::StructType *ProtocolTy;
120e5dd7070Spatrick /// Protocol * type.
121e5dd7070Spatrick llvm::PointerType *ProtocolPtrTy;
122e5dd7070Spatrick /// Instance Method Pointer type. This is a pointer to a function that takes,
123e5dd7070Spatrick /// at a minimum, an object and a selector, and is the generic type for
124e5dd7070Spatrick /// Objective-C methods. Due to differences between variadic / non-variadic
125e5dd7070Spatrick /// calling conventions, it must always be cast to the correct type before
126e5dd7070Spatrick /// actually being used.
127e5dd7070Spatrick llvm::PointerType *IMPTy;
128e5dd7070Spatrick /// Type of an untyped Objective-C object. Clang treats id as a built-in type
129e5dd7070Spatrick /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
130e5dd7070Spatrick /// but if the runtime header declaring it is included then it may be a
131e5dd7070Spatrick /// pointer to a structure.
132e5dd7070Spatrick llvm::PointerType *IdTy;
133*12c85518Srobert /// Element type of IdTy.
134*12c85518Srobert llvm::Type *IdElemTy;
135e5dd7070Spatrick /// Pointer to a pointer to an Objective-C object. Used in the new ABI
136e5dd7070Spatrick /// message lookup function and some GC-related functions.
137e5dd7070Spatrick llvm::PointerType *PtrToIdTy;
138e5dd7070Spatrick /// The clang type of id. Used when using the clang CGCall infrastructure to
139e5dd7070Spatrick /// call Objective-C methods.
140e5dd7070Spatrick CanQualType ASTIdTy;
141e5dd7070Spatrick /// LLVM type for C int type.
142e5dd7070Spatrick llvm::IntegerType *IntTy;
143e5dd7070Spatrick /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is
144e5dd7070Spatrick /// used in the code to document the difference between i8* meaning a pointer
145e5dd7070Spatrick /// to a C string and i8* meaning a pointer to some opaque type.
146e5dd7070Spatrick llvm::PointerType *PtrTy;
147e5dd7070Spatrick /// LLVM type for C long type. The runtime uses this in a lot of places where
148e5dd7070Spatrick /// it should be using intptr_t, but we can't fix this without breaking
149e5dd7070Spatrick /// compatibility with GCC...
150e5dd7070Spatrick llvm::IntegerType *LongTy;
151e5dd7070Spatrick /// LLVM type for C size_t. Used in various runtime data structures.
152e5dd7070Spatrick llvm::IntegerType *SizeTy;
153e5dd7070Spatrick /// LLVM type for C intptr_t.
154e5dd7070Spatrick llvm::IntegerType *IntPtrTy;
155e5dd7070Spatrick /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions.
156e5dd7070Spatrick llvm::IntegerType *PtrDiffTy;
157e5dd7070Spatrick /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance
158e5dd7070Spatrick /// variables.
159e5dd7070Spatrick llvm::PointerType *PtrToIntTy;
160e5dd7070Spatrick /// LLVM type for Objective-C BOOL type.
161e5dd7070Spatrick llvm::Type *BoolTy;
162e5dd7070Spatrick /// 32-bit integer type, to save us needing to look it up every time it's used.
163e5dd7070Spatrick llvm::IntegerType *Int32Ty;
164e5dd7070Spatrick /// 64-bit integer type, to save us needing to look it up every time it's used.
165e5dd7070Spatrick llvm::IntegerType *Int64Ty;
166e5dd7070Spatrick /// The type of struct objc_property.
167e5dd7070Spatrick llvm::StructType *PropertyMetadataTy;
168e5dd7070Spatrick /// Metadata kind used to tie method lookups to message sends. The GNUstep
169e5dd7070Spatrick /// runtime provides some LLVM passes that can use this to do things like
170e5dd7070Spatrick /// automatic IMP caching and speculative inlining.
171e5dd7070Spatrick unsigned msgSendMDKind;
172e5dd7070Spatrick /// Does the current target use SEH-based exceptions? False implies
173e5dd7070Spatrick /// Itanium-style DWARF unwinding.
174e5dd7070Spatrick bool usesSEHExceptions;
175e5dd7070Spatrick
176e5dd7070Spatrick /// Helper to check if we are targeting a specific runtime version or later.
isRuntime(ObjCRuntime::Kind kind,unsigned major,unsigned minor=0)177e5dd7070Spatrick bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
178e5dd7070Spatrick const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
179e5dd7070Spatrick return (R.getKind() == kind) &&
180e5dd7070Spatrick (R.getVersion() >= VersionTuple(major, minor));
181e5dd7070Spatrick }
182e5dd7070Spatrick
ManglePublicSymbol(StringRef Name)183e5dd7070Spatrick std::string ManglePublicSymbol(StringRef Name) {
184e5dd7070Spatrick return (StringRef(CGM.getTriple().isOSBinFormatCOFF() ? "$_" : "._") + Name).str();
185e5dd7070Spatrick }
186e5dd7070Spatrick
SymbolForProtocol(Twine Name)187e5dd7070Spatrick std::string SymbolForProtocol(Twine Name) {
188e5dd7070Spatrick return (ManglePublicSymbol("OBJC_PROTOCOL_") + Name).str();
189e5dd7070Spatrick }
190e5dd7070Spatrick
SymbolForProtocolRef(StringRef Name)191e5dd7070Spatrick std::string SymbolForProtocolRef(StringRef Name) {
192e5dd7070Spatrick return (ManglePublicSymbol("OBJC_REF_PROTOCOL_") + Name).str();
193e5dd7070Spatrick }
194e5dd7070Spatrick
195e5dd7070Spatrick
196e5dd7070Spatrick /// Helper function that generates a constant string and returns a pointer to
197e5dd7070Spatrick /// the start of the string. The result of this function can be used anywhere
198e5dd7070Spatrick /// where the C code specifies const char*.
MakeConstantString(StringRef Str,const char * Name="")199e5dd7070Spatrick llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") {
200ec727ea7Spatrick ConstantAddress Array =
201ec727ea7Spatrick CGM.GetAddrOfConstantCString(std::string(Str), Name);
202e5dd7070Spatrick return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(),
203e5dd7070Spatrick Array.getPointer(), Zeros);
204e5dd7070Spatrick }
205e5dd7070Spatrick
206e5dd7070Spatrick /// Emits a linkonce_odr string, whose name is the prefix followed by the
207e5dd7070Spatrick /// string value. This allows the linker to combine the strings between
208e5dd7070Spatrick /// different modules. Used for EH typeinfo names, selector strings, and a
209e5dd7070Spatrick /// few other things.
ExportUniqueString(const std::string & Str,const std::string & prefix,bool Private=false)210e5dd7070Spatrick llvm::Constant *ExportUniqueString(const std::string &Str,
211e5dd7070Spatrick const std::string &prefix,
212e5dd7070Spatrick bool Private=false) {
213e5dd7070Spatrick std::string name = prefix + Str;
214e5dd7070Spatrick auto *ConstStr = TheModule.getGlobalVariable(name);
215e5dd7070Spatrick if (!ConstStr) {
216e5dd7070Spatrick llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
217e5dd7070Spatrick auto *GV = new llvm::GlobalVariable(TheModule, value->getType(), true,
218e5dd7070Spatrick llvm::GlobalValue::LinkOnceODRLinkage, value, name);
219e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(name));
220e5dd7070Spatrick if (Private)
221e5dd7070Spatrick GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
222e5dd7070Spatrick ConstStr = GV;
223e5dd7070Spatrick }
224e5dd7070Spatrick return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(),
225e5dd7070Spatrick ConstStr, Zeros);
226e5dd7070Spatrick }
227e5dd7070Spatrick
228e5dd7070Spatrick /// Returns a property name and encoding string.
MakePropertyEncodingString(const ObjCPropertyDecl * PD,const Decl * Container)229e5dd7070Spatrick llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
230e5dd7070Spatrick const Decl *Container) {
231e5dd7070Spatrick assert(!isRuntime(ObjCRuntime::GNUstep, 2));
232e5dd7070Spatrick if (isRuntime(ObjCRuntime::GNUstep, 1, 6)) {
233e5dd7070Spatrick std::string NameAndAttributes;
234e5dd7070Spatrick std::string TypeStr =
235e5dd7070Spatrick CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container);
236e5dd7070Spatrick NameAndAttributes += '\0';
237e5dd7070Spatrick NameAndAttributes += TypeStr.length() + 3;
238e5dd7070Spatrick NameAndAttributes += TypeStr;
239e5dd7070Spatrick NameAndAttributes += '\0';
240e5dd7070Spatrick NameAndAttributes += PD->getNameAsString();
241e5dd7070Spatrick return MakeConstantString(NameAndAttributes);
242e5dd7070Spatrick }
243e5dd7070Spatrick return MakeConstantString(PD->getNameAsString());
244e5dd7070Spatrick }
245e5dd7070Spatrick
246e5dd7070Spatrick /// Push the property attributes into two structure fields.
PushPropertyAttributes(ConstantStructBuilder & Fields,const ObjCPropertyDecl * property,bool isSynthesized=true,bool isDynamic=true)247e5dd7070Spatrick void PushPropertyAttributes(ConstantStructBuilder &Fields,
248e5dd7070Spatrick const ObjCPropertyDecl *property, bool isSynthesized=true, bool
249e5dd7070Spatrick isDynamic=true) {
250e5dd7070Spatrick int attrs = property->getPropertyAttributes();
251e5dd7070Spatrick // For read-only properties, clear the copy and retain flags
252ec727ea7Spatrick if (attrs & ObjCPropertyAttribute::kind_readonly) {
253ec727ea7Spatrick attrs &= ~ObjCPropertyAttribute::kind_copy;
254ec727ea7Spatrick attrs &= ~ObjCPropertyAttribute::kind_retain;
255ec727ea7Spatrick attrs &= ~ObjCPropertyAttribute::kind_weak;
256ec727ea7Spatrick attrs &= ~ObjCPropertyAttribute::kind_strong;
257e5dd7070Spatrick }
258e5dd7070Spatrick // The first flags field has the same attribute values as clang uses internally
259e5dd7070Spatrick Fields.addInt(Int8Ty, attrs & 0xff);
260e5dd7070Spatrick attrs >>= 8;
261e5dd7070Spatrick attrs <<= 2;
262e5dd7070Spatrick // For protocol properties, synthesized and dynamic have no meaning, so we
263e5dd7070Spatrick // reuse these flags to indicate that this is a protocol property (both set
264e5dd7070Spatrick // has no meaning, as a property can't be both synthesized and dynamic)
265e5dd7070Spatrick attrs |= isSynthesized ? (1<<0) : 0;
266e5dd7070Spatrick attrs |= isDynamic ? (1<<1) : 0;
267e5dd7070Spatrick // The second field is the next four fields left shifted by two, with the
268e5dd7070Spatrick // low bit set to indicate whether the field is synthesized or dynamic.
269e5dd7070Spatrick Fields.addInt(Int8Ty, attrs & 0xff);
270e5dd7070Spatrick // Two padding fields
271e5dd7070Spatrick Fields.addInt(Int8Ty, 0);
272e5dd7070Spatrick Fields.addInt(Int8Ty, 0);
273e5dd7070Spatrick }
274e5dd7070Spatrick
275e5dd7070Spatrick virtual llvm::Constant *GenerateCategoryProtocolList(const
276e5dd7070Spatrick ObjCCategoryDecl *OCD);
PushPropertyListHeader(ConstantStructBuilder & Fields,int count)277e5dd7070Spatrick virtual ConstantArrayBuilder PushPropertyListHeader(ConstantStructBuilder &Fields,
278e5dd7070Spatrick int count) {
279e5dd7070Spatrick // int count;
280e5dd7070Spatrick Fields.addInt(IntTy, count);
281e5dd7070Spatrick // int size; (only in GNUstep v2 ABI.
282e5dd7070Spatrick if (isRuntime(ObjCRuntime::GNUstep, 2)) {
283e5dd7070Spatrick llvm::DataLayout td(&TheModule);
284e5dd7070Spatrick Fields.addInt(IntTy, td.getTypeSizeInBits(PropertyMetadataTy) /
285e5dd7070Spatrick CGM.getContext().getCharWidth());
286e5dd7070Spatrick }
287e5dd7070Spatrick // struct objc_property_list *next;
288e5dd7070Spatrick Fields.add(NULLPtr);
289e5dd7070Spatrick // struct objc_property properties[]
290e5dd7070Spatrick return Fields.beginArray(PropertyMetadataTy);
291e5dd7070Spatrick }
PushProperty(ConstantArrayBuilder & PropertiesArray,const ObjCPropertyDecl * property,const Decl * OCD,bool isSynthesized=true,bool isDynamic=true)292e5dd7070Spatrick virtual void PushProperty(ConstantArrayBuilder &PropertiesArray,
293e5dd7070Spatrick const ObjCPropertyDecl *property,
294e5dd7070Spatrick const Decl *OCD,
295e5dd7070Spatrick bool isSynthesized=true, bool
296e5dd7070Spatrick isDynamic=true) {
297e5dd7070Spatrick auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy);
298e5dd7070Spatrick ASTContext &Context = CGM.getContext();
299e5dd7070Spatrick Fields.add(MakePropertyEncodingString(property, OCD));
300e5dd7070Spatrick PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
301e5dd7070Spatrick auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
302e5dd7070Spatrick if (accessor) {
303e5dd7070Spatrick std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
304e5dd7070Spatrick llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
305e5dd7070Spatrick Fields.add(MakeConstantString(accessor->getSelector().getAsString()));
306e5dd7070Spatrick Fields.add(TypeEncoding);
307e5dd7070Spatrick } else {
308e5dd7070Spatrick Fields.add(NULLPtr);
309e5dd7070Spatrick Fields.add(NULLPtr);
310e5dd7070Spatrick }
311e5dd7070Spatrick };
312e5dd7070Spatrick addPropertyMethod(property->getGetterMethodDecl());
313e5dd7070Spatrick addPropertyMethod(property->getSetterMethodDecl());
314e5dd7070Spatrick Fields.finishAndAddTo(PropertiesArray);
315e5dd7070Spatrick }
316e5dd7070Spatrick
317e5dd7070Spatrick /// Ensures that the value has the required type, by inserting a bitcast if
318e5dd7070Spatrick /// required. This function lets us avoid inserting bitcasts that are
319e5dd7070Spatrick /// redundant.
EnforceType(CGBuilderTy & B,llvm::Value * V,llvm::Type * Ty)320e5dd7070Spatrick llvm::Value *EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
321*12c85518Srobert if (V->getType() == Ty)
322*12c85518Srobert return V;
323e5dd7070Spatrick return B.CreateBitCast(V, Ty);
324e5dd7070Spatrick }
325e5dd7070Spatrick
326e5dd7070Spatrick // Some zeros used for GEPs in lots of places.
327e5dd7070Spatrick llvm::Constant *Zeros[2];
328e5dd7070Spatrick /// Null pointer value. Mainly used as a terminator in various arrays.
329e5dd7070Spatrick llvm::Constant *NULLPtr;
330e5dd7070Spatrick /// LLVM context.
331e5dd7070Spatrick llvm::LLVMContext &VMContext;
332e5dd7070Spatrick
333e5dd7070Spatrick protected:
334e5dd7070Spatrick
335e5dd7070Spatrick /// Placeholder for the class. Lots of things refer to the class before we've
336e5dd7070Spatrick /// actually emitted it. We use this alias as a placeholder, and then replace
337e5dd7070Spatrick /// it with a pointer to the class structure before finally emitting the
338e5dd7070Spatrick /// module.
339e5dd7070Spatrick llvm::GlobalAlias *ClassPtrAlias;
340e5dd7070Spatrick /// Placeholder for the metaclass. Lots of things refer to the class before
341e5dd7070Spatrick /// we've / actually emitted it. We use this alias as a placeholder, and then
342e5dd7070Spatrick /// replace / it with a pointer to the metaclass structure before finally
343e5dd7070Spatrick /// emitting the / module.
344e5dd7070Spatrick llvm::GlobalAlias *MetaClassPtrAlias;
345e5dd7070Spatrick /// All of the classes that have been generated for this compilation units.
346e5dd7070Spatrick std::vector<llvm::Constant*> Classes;
347e5dd7070Spatrick /// All of the categories that have been generated for this compilation units.
348e5dd7070Spatrick std::vector<llvm::Constant*> Categories;
349e5dd7070Spatrick /// All of the Objective-C constant strings that have been generated for this
350e5dd7070Spatrick /// compilation units.
351e5dd7070Spatrick std::vector<llvm::Constant*> ConstantStrings;
352e5dd7070Spatrick /// Map from string values to Objective-C constant strings in the output.
353e5dd7070Spatrick /// Used to prevent emitting Objective-C strings more than once. This should
354e5dd7070Spatrick /// not be required at all - CodeGenModule should manage this list.
355e5dd7070Spatrick llvm::StringMap<llvm::Constant*> ObjCStrings;
356e5dd7070Spatrick /// All of the protocols that have been declared.
357e5dd7070Spatrick llvm::StringMap<llvm::Constant*> ExistingProtocols;
358e5dd7070Spatrick /// For each variant of a selector, we store the type encoding and a
359e5dd7070Spatrick /// placeholder value. For an untyped selector, the type will be the empty
360e5dd7070Spatrick /// string. Selector references are all done via the module's selector table,
361e5dd7070Spatrick /// so we create an alias as a placeholder and then replace it with the real
362e5dd7070Spatrick /// value later.
363e5dd7070Spatrick typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
364e5dd7070Spatrick /// Type of the selector map. This is roughly equivalent to the structure
365e5dd7070Spatrick /// used in the GNUstep runtime, which maintains a list of all of the valid
366e5dd7070Spatrick /// types for a selector in a table.
367e5dd7070Spatrick typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
368e5dd7070Spatrick SelectorMap;
369e5dd7070Spatrick /// A map from selectors to selector types. This allows us to emit all
370e5dd7070Spatrick /// selectors of the same name and type together.
371e5dd7070Spatrick SelectorMap SelectorTable;
372e5dd7070Spatrick
373e5dd7070Spatrick /// Selectors related to memory management. When compiling in GC mode, we
374e5dd7070Spatrick /// omit these.
375e5dd7070Spatrick Selector RetainSel, ReleaseSel, AutoreleaseSel;
376e5dd7070Spatrick /// Runtime functions used for memory management in GC mode. Note that clang
377e5dd7070Spatrick /// supports code generation for calling these functions, but neither GNU
378e5dd7070Spatrick /// runtime actually supports this API properly yet.
379e5dd7070Spatrick LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
380e5dd7070Spatrick WeakAssignFn, GlobalAssignFn;
381e5dd7070Spatrick
382e5dd7070Spatrick typedef std::pair<std::string, std::string> ClassAliasPair;
383e5dd7070Spatrick /// All classes that have aliases set for them.
384e5dd7070Spatrick std::vector<ClassAliasPair> ClassAliases;
385e5dd7070Spatrick
386e5dd7070Spatrick protected:
387e5dd7070Spatrick /// Function used for throwing Objective-C exceptions.
388e5dd7070Spatrick LazyRuntimeFunction ExceptionThrowFn;
389e5dd7070Spatrick /// Function used for rethrowing exceptions, used at the end of \@finally or
390e5dd7070Spatrick /// \@synchronize blocks.
391e5dd7070Spatrick LazyRuntimeFunction ExceptionReThrowFn;
392e5dd7070Spatrick /// Function called when entering a catch function. This is required for
393e5dd7070Spatrick /// differentiating Objective-C exceptions and foreign exceptions.
394e5dd7070Spatrick LazyRuntimeFunction EnterCatchFn;
395e5dd7070Spatrick /// Function called when exiting from a catch block. Used to do exception
396e5dd7070Spatrick /// cleanup.
397e5dd7070Spatrick LazyRuntimeFunction ExitCatchFn;
398e5dd7070Spatrick /// Function called when entering an \@synchronize block. Acquires the lock.
399e5dd7070Spatrick LazyRuntimeFunction SyncEnterFn;
400e5dd7070Spatrick /// Function called when exiting an \@synchronize block. Releases the lock.
401e5dd7070Spatrick LazyRuntimeFunction SyncExitFn;
402e5dd7070Spatrick
403e5dd7070Spatrick private:
404e5dd7070Spatrick /// Function called if fast enumeration detects that the collection is
405e5dd7070Spatrick /// modified during the update.
406e5dd7070Spatrick LazyRuntimeFunction EnumerationMutationFn;
407e5dd7070Spatrick /// Function for implementing synthesized property getters that return an
408e5dd7070Spatrick /// object.
409e5dd7070Spatrick LazyRuntimeFunction GetPropertyFn;
410e5dd7070Spatrick /// Function for implementing synthesized property setters that return an
411e5dd7070Spatrick /// object.
412e5dd7070Spatrick LazyRuntimeFunction SetPropertyFn;
413e5dd7070Spatrick /// Function used for non-object declared property getters.
414e5dd7070Spatrick LazyRuntimeFunction GetStructPropertyFn;
415e5dd7070Spatrick /// Function used for non-object declared property setters.
416e5dd7070Spatrick LazyRuntimeFunction SetStructPropertyFn;
417e5dd7070Spatrick
418e5dd7070Spatrick protected:
419e5dd7070Spatrick /// The version of the runtime that this class targets. Must match the
420e5dd7070Spatrick /// version in the runtime.
421e5dd7070Spatrick int RuntimeVersion;
422e5dd7070Spatrick /// The version of the protocol class. Used to differentiate between ObjC1
423e5dd7070Spatrick /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional
424e5dd7070Spatrick /// components and can not contain declared properties. We always emit
425e5dd7070Spatrick /// Objective-C 2 property structures, but we have to pretend that they're
426e5dd7070Spatrick /// Objective-C 1 property structures when targeting the GCC runtime or it
427e5dd7070Spatrick /// will abort.
428e5dd7070Spatrick const int ProtocolVersion;
429e5dd7070Spatrick /// The version of the class ABI. This value is used in the class structure
430e5dd7070Spatrick /// and indicates how various fields should be interpreted.
431e5dd7070Spatrick const int ClassABIVersion;
432e5dd7070Spatrick /// Generates an instance variable list structure. This is a structure
433e5dd7070Spatrick /// containing a size and an array of structures containing instance variable
434e5dd7070Spatrick /// metadata. This is used purely for introspection in the fragile ABI. In
435e5dd7070Spatrick /// the non-fragile ABI, it's used for instance variable fixup.
436e5dd7070Spatrick virtual llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
437e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarTypes,
438e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarOffsets,
439e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarAlign,
440e5dd7070Spatrick ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership);
441e5dd7070Spatrick
442e5dd7070Spatrick /// Generates a method list structure. This is a structure containing a size
443e5dd7070Spatrick /// and an array of structures containing method metadata.
444e5dd7070Spatrick ///
445e5dd7070Spatrick /// This structure is used by both classes and categories, and contains a next
446e5dd7070Spatrick /// pointer allowing them to be chained together in a linked list.
447e5dd7070Spatrick llvm::Constant *GenerateMethodList(StringRef ClassName,
448e5dd7070Spatrick StringRef CategoryName,
449e5dd7070Spatrick ArrayRef<const ObjCMethodDecl*> Methods,
450e5dd7070Spatrick bool isClassMethodList);
451e5dd7070Spatrick
452e5dd7070Spatrick /// Emits an empty protocol. This is used for \@protocol() where no protocol
453e5dd7070Spatrick /// is found. The runtime will (hopefully) fix up the pointer to refer to the
454e5dd7070Spatrick /// real protocol.
455e5dd7070Spatrick virtual llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName);
456e5dd7070Spatrick
457e5dd7070Spatrick /// Generates a list of property metadata structures. This follows the same
458e5dd7070Spatrick /// pattern as method and instance variable metadata lists.
459e5dd7070Spatrick llvm::Constant *GeneratePropertyList(const Decl *Container,
460e5dd7070Spatrick const ObjCContainerDecl *OCD,
461e5dd7070Spatrick bool isClassProperty=false,
462e5dd7070Spatrick bool protocolOptionalProperties=false);
463e5dd7070Spatrick
464e5dd7070Spatrick /// Generates a list of referenced protocols. Classes, categories, and
465e5dd7070Spatrick /// protocols all use this structure.
466e5dd7070Spatrick llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
467e5dd7070Spatrick
468e5dd7070Spatrick /// To ensure that all protocols are seen by the runtime, we add a category on
469e5dd7070Spatrick /// a class defined in the runtime, declaring no methods, but adopting the
470e5dd7070Spatrick /// protocols. This is a horribly ugly hack, but it allows us to collect all
471e5dd7070Spatrick /// of the protocols without changing the ABI.
472e5dd7070Spatrick void GenerateProtocolHolderCategory();
473e5dd7070Spatrick
474e5dd7070Spatrick /// Generates a class structure.
475e5dd7070Spatrick llvm::Constant *GenerateClassStructure(
476e5dd7070Spatrick llvm::Constant *MetaClass,
477e5dd7070Spatrick llvm::Constant *SuperClass,
478e5dd7070Spatrick unsigned info,
479e5dd7070Spatrick const char *Name,
480e5dd7070Spatrick llvm::Constant *Version,
481e5dd7070Spatrick llvm::Constant *InstanceSize,
482e5dd7070Spatrick llvm::Constant *IVars,
483e5dd7070Spatrick llvm::Constant *Methods,
484e5dd7070Spatrick llvm::Constant *Protocols,
485e5dd7070Spatrick llvm::Constant *IvarOffsets,
486e5dd7070Spatrick llvm::Constant *Properties,
487e5dd7070Spatrick llvm::Constant *StrongIvarBitmap,
488e5dd7070Spatrick llvm::Constant *WeakIvarBitmap,
489e5dd7070Spatrick bool isMeta=false);
490e5dd7070Spatrick
491e5dd7070Spatrick /// Generates a method list. This is used by protocols to define the required
492e5dd7070Spatrick /// and optional methods.
493e5dd7070Spatrick virtual llvm::Constant *GenerateProtocolMethodList(
494e5dd7070Spatrick ArrayRef<const ObjCMethodDecl*> Methods);
495e5dd7070Spatrick /// Emits optional and required method lists.
496e5dd7070Spatrick template<class T>
EmitProtocolMethodList(T && Methods,llvm::Constant * & Required,llvm::Constant * & Optional)497e5dd7070Spatrick void EmitProtocolMethodList(T &&Methods, llvm::Constant *&Required,
498e5dd7070Spatrick llvm::Constant *&Optional) {
499e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> RequiredMethods;
500e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> OptionalMethods;
501e5dd7070Spatrick for (const auto *I : Methods)
502e5dd7070Spatrick if (I->isOptional())
503e5dd7070Spatrick OptionalMethods.push_back(I);
504e5dd7070Spatrick else
505e5dd7070Spatrick RequiredMethods.push_back(I);
506e5dd7070Spatrick Required = GenerateProtocolMethodList(RequiredMethods);
507e5dd7070Spatrick Optional = GenerateProtocolMethodList(OptionalMethods);
508e5dd7070Spatrick }
509e5dd7070Spatrick
510e5dd7070Spatrick /// Returns a selector with the specified type encoding. An empty string is
511e5dd7070Spatrick /// used to return an untyped selector (with the types field set to NULL).
512e5dd7070Spatrick virtual llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel,
513e5dd7070Spatrick const std::string &TypeEncoding);
514e5dd7070Spatrick
515e5dd7070Spatrick /// Returns the name of ivar offset variables. In the GNUstep v1 ABI, this
516e5dd7070Spatrick /// contains the class and ivar names, in the v2 ABI this contains the type
517e5dd7070Spatrick /// encoding as well.
GetIVarOffsetVariableName(const ObjCInterfaceDecl * ID,const ObjCIvarDecl * Ivar)518e5dd7070Spatrick virtual std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID,
519e5dd7070Spatrick const ObjCIvarDecl *Ivar) {
520e5dd7070Spatrick const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
521e5dd7070Spatrick + '.' + Ivar->getNameAsString();
522e5dd7070Spatrick return Name;
523e5dd7070Spatrick }
524e5dd7070Spatrick /// Returns the variable used to store the offset of an instance variable.
525e5dd7070Spatrick llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
526e5dd7070Spatrick const ObjCIvarDecl *Ivar);
527e5dd7070Spatrick /// Emits a reference to a class. This allows the linker to object if there
528e5dd7070Spatrick /// is no class of the matching name.
529e5dd7070Spatrick void EmitClassRef(const std::string &className);
530e5dd7070Spatrick
531e5dd7070Spatrick /// Emits a pointer to the named class
532e5dd7070Spatrick virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
533e5dd7070Spatrick const std::string &Name, bool isWeak);
534e5dd7070Spatrick
535e5dd7070Spatrick /// Looks up the method for sending a message to the specified object. This
536e5dd7070Spatrick /// mechanism differs between the GCC and GNU runtimes, so this method must be
537e5dd7070Spatrick /// overridden in subclasses.
538e5dd7070Spatrick virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
539e5dd7070Spatrick llvm::Value *&Receiver,
540e5dd7070Spatrick llvm::Value *cmd,
541e5dd7070Spatrick llvm::MDNode *node,
542e5dd7070Spatrick MessageSendInfo &MSI) = 0;
543e5dd7070Spatrick
544e5dd7070Spatrick /// Looks up the method for sending a message to a superclass. This
545e5dd7070Spatrick /// mechanism differs between the GCC and GNU runtimes, so this method must
546e5dd7070Spatrick /// be overridden in subclasses.
547e5dd7070Spatrick virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
548e5dd7070Spatrick Address ObjCSuper,
549e5dd7070Spatrick llvm::Value *cmd,
550e5dd7070Spatrick MessageSendInfo &MSI) = 0;
551e5dd7070Spatrick
552e5dd7070Spatrick /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
553e5dd7070Spatrick /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
554e5dd7070Spatrick /// bits set to their values, LSB first, while larger ones are stored in a
555e5dd7070Spatrick /// structure of this / form:
556e5dd7070Spatrick ///
557e5dd7070Spatrick /// struct { int32_t length; int32_t values[length]; };
558e5dd7070Spatrick ///
559e5dd7070Spatrick /// The values in the array are stored in host-endian format, with the least
560e5dd7070Spatrick /// significant bit being assumed to come first in the bitfield. Therefore,
561e5dd7070Spatrick /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
562e5dd7070Spatrick /// while a bitfield / with the 63rd bit set will be 1<<64.
563e5dd7070Spatrick llvm::Constant *MakeBitField(ArrayRef<bool> bits);
564e5dd7070Spatrick
565e5dd7070Spatrick public:
566e5dd7070Spatrick CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
567e5dd7070Spatrick unsigned protocolClassVersion, unsigned classABI=1);
568e5dd7070Spatrick
569e5dd7070Spatrick ConstantAddress GenerateConstantString(const StringLiteral *) override;
570e5dd7070Spatrick
571e5dd7070Spatrick RValue
572e5dd7070Spatrick GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
573e5dd7070Spatrick QualType ResultType, Selector Sel,
574e5dd7070Spatrick llvm::Value *Receiver, const CallArgList &CallArgs,
575e5dd7070Spatrick const ObjCInterfaceDecl *Class,
576e5dd7070Spatrick const ObjCMethodDecl *Method) override;
577e5dd7070Spatrick RValue
578e5dd7070Spatrick GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
579e5dd7070Spatrick QualType ResultType, Selector Sel,
580e5dd7070Spatrick const ObjCInterfaceDecl *Class,
581e5dd7070Spatrick bool isCategoryImpl, llvm::Value *Receiver,
582e5dd7070Spatrick bool IsClassMessage, const CallArgList &CallArgs,
583e5dd7070Spatrick const ObjCMethodDecl *Method) override;
584e5dd7070Spatrick llvm::Value *GetClass(CodeGenFunction &CGF,
585e5dd7070Spatrick const ObjCInterfaceDecl *OID) override;
586e5dd7070Spatrick llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override;
587e5dd7070Spatrick Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override;
588e5dd7070Spatrick llvm::Value *GetSelector(CodeGenFunction &CGF,
589e5dd7070Spatrick const ObjCMethodDecl *Method) override;
GetConstantSelector(Selector Sel,const std::string & TypeEncoding)590e5dd7070Spatrick virtual llvm::Constant *GetConstantSelector(Selector Sel,
591e5dd7070Spatrick const std::string &TypeEncoding) {
592e5dd7070Spatrick llvm_unreachable("Runtime unable to generate constant selector");
593e5dd7070Spatrick }
GetConstantSelector(const ObjCMethodDecl * M)594e5dd7070Spatrick llvm::Constant *GetConstantSelector(const ObjCMethodDecl *M) {
595e5dd7070Spatrick return GetConstantSelector(M->getSelector(),
596e5dd7070Spatrick CGM.getContext().getObjCEncodingForMethodDecl(M));
597e5dd7070Spatrick }
598e5dd7070Spatrick llvm::Constant *GetEHType(QualType T) override;
599e5dd7070Spatrick
600e5dd7070Spatrick llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
601e5dd7070Spatrick const ObjCContainerDecl *CD) override;
602e5dd7070Spatrick void GenerateDirectMethodPrologue(CodeGenFunction &CGF, llvm::Function *Fn,
603e5dd7070Spatrick const ObjCMethodDecl *OMD,
604e5dd7070Spatrick const ObjCContainerDecl *CD) override;
605e5dd7070Spatrick void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
606e5dd7070Spatrick void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
607e5dd7070Spatrick void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
608e5dd7070Spatrick llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
609e5dd7070Spatrick const ObjCProtocolDecl *PD) override;
610e5dd7070Spatrick void GenerateProtocol(const ObjCProtocolDecl *PD) override;
611ec727ea7Spatrick
612ec727ea7Spatrick virtual llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD);
613ec727ea7Spatrick
GetOrEmitProtocol(const ObjCProtocolDecl * PD)614ec727ea7Spatrick llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) override {
615ec727ea7Spatrick return GenerateProtocolRef(PD);
616ec727ea7Spatrick }
617ec727ea7Spatrick
618e5dd7070Spatrick llvm::Function *ModuleInitFunction() override;
619e5dd7070Spatrick llvm::FunctionCallee GetPropertyGetFunction() override;
620e5dd7070Spatrick llvm::FunctionCallee GetPropertySetFunction() override;
621e5dd7070Spatrick llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
622e5dd7070Spatrick bool copy) override;
623e5dd7070Spatrick llvm::FunctionCallee GetSetStructFunction() override;
624e5dd7070Spatrick llvm::FunctionCallee GetGetStructFunction() override;
625e5dd7070Spatrick llvm::FunctionCallee GetCppAtomicObjectGetFunction() override;
626e5dd7070Spatrick llvm::FunctionCallee GetCppAtomicObjectSetFunction() override;
627e5dd7070Spatrick llvm::FunctionCallee EnumerationMutationFunction() override;
628e5dd7070Spatrick
629e5dd7070Spatrick void EmitTryStmt(CodeGenFunction &CGF,
630e5dd7070Spatrick const ObjCAtTryStmt &S) override;
631e5dd7070Spatrick void EmitSynchronizedStmt(CodeGenFunction &CGF,
632e5dd7070Spatrick const ObjCAtSynchronizedStmt &S) override;
633e5dd7070Spatrick void EmitThrowStmt(CodeGenFunction &CGF,
634e5dd7070Spatrick const ObjCAtThrowStmt &S,
635e5dd7070Spatrick bool ClearInsertionPoint=true) override;
636e5dd7070Spatrick llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
637e5dd7070Spatrick Address AddrWeakObj) override;
638e5dd7070Spatrick void EmitObjCWeakAssign(CodeGenFunction &CGF,
639e5dd7070Spatrick llvm::Value *src, Address dst) override;
640e5dd7070Spatrick void EmitObjCGlobalAssign(CodeGenFunction &CGF,
641e5dd7070Spatrick llvm::Value *src, Address dest,
642e5dd7070Spatrick bool threadlocal=false) override;
643e5dd7070Spatrick void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
644e5dd7070Spatrick Address dest, llvm::Value *ivarOffset) override;
645e5dd7070Spatrick void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
646e5dd7070Spatrick llvm::Value *src, Address dest) override;
647e5dd7070Spatrick void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr,
648e5dd7070Spatrick Address SrcPtr,
649e5dd7070Spatrick llvm::Value *Size) override;
650e5dd7070Spatrick LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
651e5dd7070Spatrick llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
652e5dd7070Spatrick unsigned CVRQualifiers) override;
653e5dd7070Spatrick llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
654e5dd7070Spatrick const ObjCInterfaceDecl *Interface,
655e5dd7070Spatrick const ObjCIvarDecl *Ivar) override;
656e5dd7070Spatrick llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
BuildGCBlockLayout(CodeGenModule & CGM,const CGBlockInfo & blockInfo)657e5dd7070Spatrick llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
658e5dd7070Spatrick const CGBlockInfo &blockInfo) override {
659e5dd7070Spatrick return NULLPtr;
660e5dd7070Spatrick }
BuildRCBlockLayout(CodeGenModule & CGM,const CGBlockInfo & blockInfo)661e5dd7070Spatrick llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
662e5dd7070Spatrick const CGBlockInfo &blockInfo) override {
663e5dd7070Spatrick return NULLPtr;
664e5dd7070Spatrick }
665e5dd7070Spatrick
BuildByrefLayout(CodeGenModule & CGM,QualType T)666e5dd7070Spatrick llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
667e5dd7070Spatrick return NULLPtr;
668e5dd7070Spatrick }
669e5dd7070Spatrick };
670e5dd7070Spatrick
671e5dd7070Spatrick /// Class representing the legacy GCC Objective-C ABI. This is the default when
672e5dd7070Spatrick /// -fobjc-nonfragile-abi is not specified.
673e5dd7070Spatrick ///
674e5dd7070Spatrick /// The GCC ABI target actually generates code that is approximately compatible
675e5dd7070Spatrick /// with the new GNUstep runtime ABI, but refrains from using any features that
676e5dd7070Spatrick /// would not work with the GCC runtime. For example, clang always generates
677e5dd7070Spatrick /// the extended form of the class structure, and the extra fields are simply
678e5dd7070Spatrick /// ignored by GCC libobjc.
679e5dd7070Spatrick class CGObjCGCC : public CGObjCGNU {
680e5dd7070Spatrick /// The GCC ABI message lookup function. Returns an IMP pointing to the
681e5dd7070Spatrick /// method implementation for this message.
682e5dd7070Spatrick LazyRuntimeFunction MsgLookupFn;
683e5dd7070Spatrick /// The GCC ABI superclass message lookup function. Takes a pointer to a
684e5dd7070Spatrick /// structure describing the receiver and the class, and a selector as
685e5dd7070Spatrick /// arguments. Returns the IMP for the corresponding method.
686e5dd7070Spatrick LazyRuntimeFunction MsgLookupSuperFn;
687e5dd7070Spatrick
688e5dd7070Spatrick protected:
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)689e5dd7070Spatrick llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
690e5dd7070Spatrick llvm::Value *cmd, llvm::MDNode *node,
691e5dd7070Spatrick MessageSendInfo &MSI) override {
692e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
693e5dd7070Spatrick llvm::Value *args[] = {
694e5dd7070Spatrick EnforceType(Builder, Receiver, IdTy),
695e5dd7070Spatrick EnforceType(Builder, cmd, SelectorTy) };
696e5dd7070Spatrick llvm::CallBase *imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
697e5dd7070Spatrick imp->setMetadata(msgSendMDKind, node);
698e5dd7070Spatrick return imp;
699e5dd7070Spatrick }
700e5dd7070Spatrick
LookupIMPSuper(CodeGenFunction & CGF,Address ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)701e5dd7070Spatrick llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
702e5dd7070Spatrick llvm::Value *cmd, MessageSendInfo &MSI) override {
703e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
704*12c85518Srobert llvm::Value *lookupArgs[] = {
705*12c85518Srobert EnforceType(Builder, ObjCSuper.getPointer(), PtrToObjCSuperTy), cmd};
706e5dd7070Spatrick return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
707e5dd7070Spatrick }
708e5dd7070Spatrick
709e5dd7070Spatrick public:
CGObjCGCC(CodeGenModule & Mod)710e5dd7070Spatrick CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
711e5dd7070Spatrick // IMP objc_msg_lookup(id, SEL);
712e5dd7070Spatrick MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy);
713e5dd7070Spatrick // IMP objc_msg_lookup_super(struct objc_super*, SEL);
714e5dd7070Spatrick MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
715e5dd7070Spatrick PtrToObjCSuperTy, SelectorTy);
716e5dd7070Spatrick }
717e5dd7070Spatrick };
718e5dd7070Spatrick
719e5dd7070Spatrick /// Class used when targeting the new GNUstep runtime ABI.
720e5dd7070Spatrick class CGObjCGNUstep : public CGObjCGNU {
721e5dd7070Spatrick /// The slot lookup function. Returns a pointer to a cacheable structure
722e5dd7070Spatrick /// that contains (among other things) the IMP.
723e5dd7070Spatrick LazyRuntimeFunction SlotLookupFn;
724e5dd7070Spatrick /// The GNUstep ABI superclass message lookup function. Takes a pointer to
725e5dd7070Spatrick /// a structure describing the receiver and the class, and a selector as
726e5dd7070Spatrick /// arguments. Returns the slot for the corresponding method. Superclass
727e5dd7070Spatrick /// message lookup rarely changes, so this is a good caching opportunity.
728e5dd7070Spatrick LazyRuntimeFunction SlotLookupSuperFn;
729e5dd7070Spatrick /// Specialised function for setting atomic retain properties
730e5dd7070Spatrick LazyRuntimeFunction SetPropertyAtomic;
731e5dd7070Spatrick /// Specialised function for setting atomic copy properties
732e5dd7070Spatrick LazyRuntimeFunction SetPropertyAtomicCopy;
733e5dd7070Spatrick /// Specialised function for setting nonatomic retain properties
734e5dd7070Spatrick LazyRuntimeFunction SetPropertyNonAtomic;
735e5dd7070Spatrick /// Specialised function for setting nonatomic copy properties
736e5dd7070Spatrick LazyRuntimeFunction SetPropertyNonAtomicCopy;
737e5dd7070Spatrick /// Function to perform atomic copies of C++ objects with nontrivial copy
738e5dd7070Spatrick /// constructors from Objective-C ivars.
739e5dd7070Spatrick LazyRuntimeFunction CxxAtomicObjectGetFn;
740e5dd7070Spatrick /// Function to perform atomic copies of C++ objects with nontrivial copy
741e5dd7070Spatrick /// constructors to Objective-C ivars.
742e5dd7070Spatrick LazyRuntimeFunction CxxAtomicObjectSetFn;
743a9ac8606Spatrick /// Type of a slot structure pointer. This is returned by the various
744e5dd7070Spatrick /// lookup functions.
745e5dd7070Spatrick llvm::Type *SlotTy;
746a9ac8606Spatrick /// Type of a slot structure.
747a9ac8606Spatrick llvm::Type *SlotStructTy;
748e5dd7070Spatrick
749e5dd7070Spatrick public:
750e5dd7070Spatrick llvm::Constant *GetEHType(QualType T) override;
751e5dd7070Spatrick
752e5dd7070Spatrick protected:
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)753e5dd7070Spatrick llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
754e5dd7070Spatrick llvm::Value *cmd, llvm::MDNode *node,
755e5dd7070Spatrick MessageSendInfo &MSI) override {
756e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
757e5dd7070Spatrick llvm::FunctionCallee LookupFn = SlotLookupFn;
758e5dd7070Spatrick
759e5dd7070Spatrick // Store the receiver on the stack so that we can reload it later
760e5dd7070Spatrick Address ReceiverPtr =
761e5dd7070Spatrick CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign());
762e5dd7070Spatrick Builder.CreateStore(Receiver, ReceiverPtr);
763e5dd7070Spatrick
764e5dd7070Spatrick llvm::Value *self;
765e5dd7070Spatrick
766e5dd7070Spatrick if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
767e5dd7070Spatrick self = CGF.LoadObjCSelf();
768e5dd7070Spatrick } else {
769e5dd7070Spatrick self = llvm::ConstantPointerNull::get(IdTy);
770e5dd7070Spatrick }
771e5dd7070Spatrick
772e5dd7070Spatrick // The lookup function is guaranteed not to capture the receiver pointer.
773e5dd7070Spatrick if (auto *LookupFn2 = dyn_cast<llvm::Function>(LookupFn.getCallee()))
774e5dd7070Spatrick LookupFn2->addParamAttr(0, llvm::Attribute::NoCapture);
775e5dd7070Spatrick
776e5dd7070Spatrick llvm::Value *args[] = {
777e5dd7070Spatrick EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy),
778e5dd7070Spatrick EnforceType(Builder, cmd, SelectorTy),
779e5dd7070Spatrick EnforceType(Builder, self, IdTy) };
780e5dd7070Spatrick llvm::CallBase *slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
781e5dd7070Spatrick slot->setOnlyReadsMemory();
782e5dd7070Spatrick slot->setMetadata(msgSendMDKind, node);
783e5dd7070Spatrick
784e5dd7070Spatrick // Load the imp from the slot
785e5dd7070Spatrick llvm::Value *imp = Builder.CreateAlignedLoad(
786a9ac8606Spatrick IMPTy, Builder.CreateStructGEP(SlotStructTy, slot, 4),
787a9ac8606Spatrick CGF.getPointerAlign());
788e5dd7070Spatrick
789e5dd7070Spatrick // The lookup function may have changed the receiver, so make sure we use
790e5dd7070Spatrick // the new one.
791e5dd7070Spatrick Receiver = Builder.CreateLoad(ReceiverPtr, true);
792e5dd7070Spatrick return imp;
793e5dd7070Spatrick }
794e5dd7070Spatrick
LookupIMPSuper(CodeGenFunction & CGF,Address ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)795e5dd7070Spatrick llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
796e5dd7070Spatrick llvm::Value *cmd,
797e5dd7070Spatrick MessageSendInfo &MSI) override {
798e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
799e5dd7070Spatrick llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd};
800e5dd7070Spatrick
801e5dd7070Spatrick llvm::CallInst *slot =
802e5dd7070Spatrick CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
803e5dd7070Spatrick slot->setOnlyReadsMemory();
804e5dd7070Spatrick
805a9ac8606Spatrick return Builder.CreateAlignedLoad(
806a9ac8606Spatrick IMPTy, Builder.CreateStructGEP(SlotStructTy, slot, 4),
807e5dd7070Spatrick CGF.getPointerAlign());
808e5dd7070Spatrick }
809e5dd7070Spatrick
810e5dd7070Spatrick public:
CGObjCGNUstep(CodeGenModule & Mod)811e5dd7070Spatrick CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 9, 3, 1) {}
CGObjCGNUstep(CodeGenModule & Mod,unsigned ABI,unsigned ProtocolABI,unsigned ClassABI)812e5dd7070Spatrick CGObjCGNUstep(CodeGenModule &Mod, unsigned ABI, unsigned ProtocolABI,
813e5dd7070Spatrick unsigned ClassABI) :
814e5dd7070Spatrick CGObjCGNU(Mod, ABI, ProtocolABI, ClassABI) {
815e5dd7070Spatrick const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
816e5dd7070Spatrick
817a9ac8606Spatrick SlotStructTy = llvm::StructType::get(PtrTy, PtrTy, PtrTy, IntTy, IMPTy);
818e5dd7070Spatrick SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
819e5dd7070Spatrick // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
820e5dd7070Spatrick SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
821e5dd7070Spatrick SelectorTy, IdTy);
822e5dd7070Spatrick // Slot_t objc_slot_lookup_super(struct objc_super*, SEL);
823e5dd7070Spatrick SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
824e5dd7070Spatrick PtrToObjCSuperTy, SelectorTy);
825e5dd7070Spatrick // If we're in ObjC++ mode, then we want to make
826e5dd7070Spatrick if (usesSEHExceptions) {
827e5dd7070Spatrick llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
828e5dd7070Spatrick // void objc_exception_rethrow(void)
829e5dd7070Spatrick ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
830e5dd7070Spatrick } else if (CGM.getLangOpts().CPlusPlus) {
831e5dd7070Spatrick llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
832e5dd7070Spatrick // void *__cxa_begin_catch(void *e)
833e5dd7070Spatrick EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
834e5dd7070Spatrick // void __cxa_end_catch(void)
835e5dd7070Spatrick ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
836e5dd7070Spatrick // void _Unwind_Resume_or_Rethrow(void*)
837e5dd7070Spatrick ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
838e5dd7070Spatrick PtrTy);
839e5dd7070Spatrick } else if (R.getVersion() >= VersionTuple(1, 7)) {
840e5dd7070Spatrick llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
841e5dd7070Spatrick // id objc_begin_catch(void *e)
842e5dd7070Spatrick EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
843e5dd7070Spatrick // void objc_end_catch(void)
844e5dd7070Spatrick ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy);
845e5dd7070Spatrick // void _Unwind_Resume_or_Rethrow(void*)
846e5dd7070Spatrick ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
847e5dd7070Spatrick }
848e5dd7070Spatrick llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
849e5dd7070Spatrick SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
850e5dd7070Spatrick SelectorTy, IdTy, PtrDiffTy);
851e5dd7070Spatrick SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
852e5dd7070Spatrick IdTy, SelectorTy, IdTy, PtrDiffTy);
853e5dd7070Spatrick SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
854e5dd7070Spatrick IdTy, SelectorTy, IdTy, PtrDiffTy);
855e5dd7070Spatrick SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
856e5dd7070Spatrick VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy);
857e5dd7070Spatrick // void objc_setCppObjectAtomic(void *dest, const void *src, void
858e5dd7070Spatrick // *helper);
859e5dd7070Spatrick CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
860e5dd7070Spatrick PtrTy, PtrTy);
861e5dd7070Spatrick // void objc_getCppObjectAtomic(void *dest, const void *src, void
862e5dd7070Spatrick // *helper);
863e5dd7070Spatrick CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
864e5dd7070Spatrick PtrTy, PtrTy);
865e5dd7070Spatrick }
866e5dd7070Spatrick
GetCppAtomicObjectGetFunction()867e5dd7070Spatrick llvm::FunctionCallee GetCppAtomicObjectGetFunction() override {
868e5dd7070Spatrick // The optimised functions were added in version 1.7 of the GNUstep
869e5dd7070Spatrick // runtime.
870e5dd7070Spatrick assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
871e5dd7070Spatrick VersionTuple(1, 7));
872e5dd7070Spatrick return CxxAtomicObjectGetFn;
873e5dd7070Spatrick }
874e5dd7070Spatrick
GetCppAtomicObjectSetFunction()875e5dd7070Spatrick llvm::FunctionCallee GetCppAtomicObjectSetFunction() override {
876e5dd7070Spatrick // The optimised functions were added in version 1.7 of the GNUstep
877e5dd7070Spatrick // runtime.
878e5dd7070Spatrick assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
879e5dd7070Spatrick VersionTuple(1, 7));
880e5dd7070Spatrick return CxxAtomicObjectSetFn;
881e5dd7070Spatrick }
882e5dd7070Spatrick
GetOptimizedPropertySetFunction(bool atomic,bool copy)883e5dd7070Spatrick llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
884e5dd7070Spatrick bool copy) override {
885e5dd7070Spatrick // The optimised property functions omit the GC check, and so are not
886e5dd7070Spatrick // safe to use in GC mode. The standard functions are fast in GC mode,
887e5dd7070Spatrick // so there is less advantage in using them.
888e5dd7070Spatrick assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
889e5dd7070Spatrick // The optimised functions were added in version 1.7 of the GNUstep
890e5dd7070Spatrick // runtime.
891e5dd7070Spatrick assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
892e5dd7070Spatrick VersionTuple(1, 7));
893e5dd7070Spatrick
894e5dd7070Spatrick if (atomic) {
895e5dd7070Spatrick if (copy) return SetPropertyAtomicCopy;
896e5dd7070Spatrick return SetPropertyAtomic;
897e5dd7070Spatrick }
898e5dd7070Spatrick
899e5dd7070Spatrick return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
900e5dd7070Spatrick }
901e5dd7070Spatrick };
902e5dd7070Spatrick
903e5dd7070Spatrick /// GNUstep Objective-C ABI version 2 implementation.
904e5dd7070Spatrick /// This is the ABI that provides a clean break with the legacy GCC ABI and
905e5dd7070Spatrick /// cleans up a number of things that were added to work around 1980s linkers.
906e5dd7070Spatrick class CGObjCGNUstep2 : public CGObjCGNUstep {
907e5dd7070Spatrick enum SectionKind
908e5dd7070Spatrick {
909e5dd7070Spatrick SelectorSection = 0,
910e5dd7070Spatrick ClassSection,
911e5dd7070Spatrick ClassReferenceSection,
912e5dd7070Spatrick CategorySection,
913e5dd7070Spatrick ProtocolSection,
914e5dd7070Spatrick ProtocolReferenceSection,
915e5dd7070Spatrick ClassAliasSection,
916e5dd7070Spatrick ConstantStringSection
917e5dd7070Spatrick };
918e5dd7070Spatrick static const char *const SectionsBaseNames[8];
919e5dd7070Spatrick static const char *const PECOFFSectionsBaseNames[8];
920e5dd7070Spatrick template<SectionKind K>
sectionName()921e5dd7070Spatrick std::string sectionName() {
922e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
923e5dd7070Spatrick std::string name(PECOFFSectionsBaseNames[K]);
924e5dd7070Spatrick name += "$m";
925e5dd7070Spatrick return name;
926e5dd7070Spatrick }
927e5dd7070Spatrick return SectionsBaseNames[K];
928e5dd7070Spatrick }
929e5dd7070Spatrick /// The GCC ABI superclass message lookup function. Takes a pointer to a
930e5dd7070Spatrick /// structure describing the receiver and the class, and a selector as
931e5dd7070Spatrick /// arguments. Returns the IMP for the corresponding method.
932e5dd7070Spatrick LazyRuntimeFunction MsgLookupSuperFn;
933e5dd7070Spatrick /// A flag indicating if we've emitted at least one protocol.
934e5dd7070Spatrick /// If we haven't, then we need to emit an empty protocol, to ensure that the
935e5dd7070Spatrick /// __start__objc_protocols and __stop__objc_protocols sections exist.
936e5dd7070Spatrick bool EmittedProtocol = false;
937e5dd7070Spatrick /// A flag indicating if we've emitted at least one protocol reference.
938e5dd7070Spatrick /// If we haven't, then we need to emit an empty protocol, to ensure that the
939e5dd7070Spatrick /// __start__objc_protocol_refs and __stop__objc_protocol_refs sections
940e5dd7070Spatrick /// exist.
941e5dd7070Spatrick bool EmittedProtocolRef = false;
942e5dd7070Spatrick /// A flag indicating if we've emitted at least one class.
943e5dd7070Spatrick /// If we haven't, then we need to emit an empty protocol, to ensure that the
944e5dd7070Spatrick /// __start__objc_classes and __stop__objc_classes sections / exist.
945e5dd7070Spatrick bool EmittedClass = false;
946e5dd7070Spatrick /// Generate the name of a symbol for a reference to a class. Accesses to
947e5dd7070Spatrick /// classes should be indirected via this.
948e5dd7070Spatrick
949a9ac8606Spatrick typedef std::pair<std::string, std::pair<llvm::GlobalVariable*, int>>
950a9ac8606Spatrick EarlyInitPair;
951e5dd7070Spatrick std::vector<EarlyInitPair> EarlyInitList;
952e5dd7070Spatrick
SymbolForClassRef(StringRef Name,bool isWeak)953e5dd7070Spatrick std::string SymbolForClassRef(StringRef Name, bool isWeak) {
954e5dd7070Spatrick if (isWeak)
955e5dd7070Spatrick return (ManglePublicSymbol("OBJC_WEAK_REF_CLASS_") + Name).str();
956e5dd7070Spatrick else
957e5dd7070Spatrick return (ManglePublicSymbol("OBJC_REF_CLASS_") + Name).str();
958e5dd7070Spatrick }
959e5dd7070Spatrick /// Generate the name of a class symbol.
SymbolForClass(StringRef Name)960e5dd7070Spatrick std::string SymbolForClass(StringRef Name) {
961e5dd7070Spatrick return (ManglePublicSymbol("OBJC_CLASS_") + Name).str();
962e5dd7070Spatrick }
CallRuntimeFunction(CGBuilderTy & B,StringRef FunctionName,ArrayRef<llvm::Value * > Args)963e5dd7070Spatrick void CallRuntimeFunction(CGBuilderTy &B, StringRef FunctionName,
964e5dd7070Spatrick ArrayRef<llvm::Value*> Args) {
965e5dd7070Spatrick SmallVector<llvm::Type *,8> Types;
966e5dd7070Spatrick for (auto *Arg : Args)
967e5dd7070Spatrick Types.push_back(Arg->getType());
968e5dd7070Spatrick llvm::FunctionType *FT = llvm::FunctionType::get(B.getVoidTy(), Types,
969e5dd7070Spatrick false);
970e5dd7070Spatrick llvm::FunctionCallee Fn = CGM.CreateRuntimeFunction(FT, FunctionName);
971e5dd7070Spatrick B.CreateCall(Fn, Args);
972e5dd7070Spatrick }
973e5dd7070Spatrick
GenerateConstantString(const StringLiteral * SL)974e5dd7070Spatrick ConstantAddress GenerateConstantString(const StringLiteral *SL) override {
975e5dd7070Spatrick
976e5dd7070Spatrick auto Str = SL->getString();
977e5dd7070Spatrick CharUnits Align = CGM.getPointerAlign();
978e5dd7070Spatrick
979e5dd7070Spatrick // Look for an existing one
980e5dd7070Spatrick llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
981e5dd7070Spatrick if (old != ObjCStrings.end())
982*12c85518Srobert return ConstantAddress(old->getValue(), IdElemTy, Align);
983e5dd7070Spatrick
984e5dd7070Spatrick bool isNonASCII = SL->containsNonAscii();
985e5dd7070Spatrick
986e5dd7070Spatrick auto LiteralLength = SL->getLength();
987e5dd7070Spatrick
988*12c85518Srobert if ((CGM.getTarget().getPointerWidth(LangAS::Default) == 64) &&
989e5dd7070Spatrick (LiteralLength < 9) && !isNonASCII) {
990e5dd7070Spatrick // Tiny strings are only used on 64-bit platforms. They store 8 7-bit
991e5dd7070Spatrick // ASCII characters in the high 56 bits, followed by a 4-bit length and a
992e5dd7070Spatrick // 3-bit tag (which is always 4).
993e5dd7070Spatrick uint64_t str = 0;
994e5dd7070Spatrick // Fill in the characters
995e5dd7070Spatrick for (unsigned i=0 ; i<LiteralLength ; i++)
996e5dd7070Spatrick str |= ((uint64_t)SL->getCodeUnit(i)) << ((64 - 4 - 3) - (i*7));
997e5dd7070Spatrick // Fill in the length
998e5dd7070Spatrick str |= LiteralLength << 3;
999e5dd7070Spatrick // Set the tag
1000e5dd7070Spatrick str |= 4;
1001e5dd7070Spatrick auto *ObjCStr = llvm::ConstantExpr::getIntToPtr(
1002e5dd7070Spatrick llvm::ConstantInt::get(Int64Ty, str), IdTy);
1003e5dd7070Spatrick ObjCStrings[Str] = ObjCStr;
1004*12c85518Srobert return ConstantAddress(ObjCStr, IdElemTy, Align);
1005e5dd7070Spatrick }
1006e5dd7070Spatrick
1007e5dd7070Spatrick StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
1008e5dd7070Spatrick
1009e5dd7070Spatrick if (StringClass.empty()) StringClass = "NSConstantString";
1010e5dd7070Spatrick
1011e5dd7070Spatrick std::string Sym = SymbolForClass(StringClass);
1012e5dd7070Spatrick
1013e5dd7070Spatrick llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1014e5dd7070Spatrick
1015e5dd7070Spatrick if (!isa) {
1016e5dd7070Spatrick isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
1017e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, Sym);
1018e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
1019e5dd7070Spatrick cast<llvm::GlobalValue>(isa)->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1020e5dd7070Spatrick }
1021e5dd7070Spatrick } else if (isa->getType() != PtrToIdTy)
1022e5dd7070Spatrick isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1023e5dd7070Spatrick
1024e5dd7070Spatrick // struct
1025e5dd7070Spatrick // {
1026e5dd7070Spatrick // Class isa;
1027e5dd7070Spatrick // uint32_t flags;
1028e5dd7070Spatrick // uint32_t length; // Number of codepoints
1029e5dd7070Spatrick // uint32_t size; // Number of bytes
1030e5dd7070Spatrick // uint32_t hash;
1031e5dd7070Spatrick // const char *data;
1032e5dd7070Spatrick // };
1033e5dd7070Spatrick
1034e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
1035e5dd7070Spatrick auto Fields = Builder.beginStruct();
1036e5dd7070Spatrick if (!CGM.getTriple().isOSBinFormatCOFF()) {
1037e5dd7070Spatrick Fields.add(isa);
1038e5dd7070Spatrick } else {
1039e5dd7070Spatrick Fields.addNullPointer(PtrTy);
1040e5dd7070Spatrick }
1041e5dd7070Spatrick // For now, all non-ASCII strings are represented as UTF-16. As such, the
1042e5dd7070Spatrick // number of bytes is simply double the number of UTF-16 codepoints. In
1043e5dd7070Spatrick // ASCII strings, the number of bytes is equal to the number of non-ASCII
1044e5dd7070Spatrick // codepoints.
1045e5dd7070Spatrick if (isNonASCII) {
1046e5dd7070Spatrick unsigned NumU8CodeUnits = Str.size();
1047e5dd7070Spatrick // A UTF-16 representation of a unicode string contains at most the same
1048e5dd7070Spatrick // number of code units as a UTF-8 representation. Allocate that much
1049e5dd7070Spatrick // space, plus one for the final null character.
1050e5dd7070Spatrick SmallVector<llvm::UTF16, 128> ToBuf(NumU8CodeUnits + 1);
1051e5dd7070Spatrick const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)Str.data();
1052e5dd7070Spatrick llvm::UTF16 *ToPtr = &ToBuf[0];
1053e5dd7070Spatrick (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumU8CodeUnits,
1054e5dd7070Spatrick &ToPtr, ToPtr + NumU8CodeUnits, llvm::strictConversion);
1055e5dd7070Spatrick uint32_t StringLength = ToPtr - &ToBuf[0];
1056e5dd7070Spatrick // Add null terminator
1057e5dd7070Spatrick *ToPtr = 0;
1058e5dd7070Spatrick // Flags: 2 indicates UTF-16 encoding
1059e5dd7070Spatrick Fields.addInt(Int32Ty, 2);
1060e5dd7070Spatrick // Number of UTF-16 codepoints
1061e5dd7070Spatrick Fields.addInt(Int32Ty, StringLength);
1062e5dd7070Spatrick // Number of bytes
1063e5dd7070Spatrick Fields.addInt(Int32Ty, StringLength * 2);
1064e5dd7070Spatrick // Hash. Not currently initialised by the compiler.
1065e5dd7070Spatrick Fields.addInt(Int32Ty, 0);
1066e5dd7070Spatrick // pointer to the data string.
1067*12c85518Srobert auto Arr = llvm::ArrayRef(&ToBuf[0], ToPtr + 1);
1068e5dd7070Spatrick auto *C = llvm::ConstantDataArray::get(VMContext, Arr);
1069e5dd7070Spatrick auto *Buffer = new llvm::GlobalVariable(TheModule, C->getType(),
1070e5dd7070Spatrick /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, C, ".str");
1071e5dd7070Spatrick Buffer->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1072e5dd7070Spatrick Fields.add(Buffer);
1073e5dd7070Spatrick } else {
1074e5dd7070Spatrick // Flags: 0 indicates ASCII encoding
1075e5dd7070Spatrick Fields.addInt(Int32Ty, 0);
1076e5dd7070Spatrick // Number of UTF-16 codepoints, each ASCII byte is a UTF-16 codepoint
1077e5dd7070Spatrick Fields.addInt(Int32Ty, Str.size());
1078e5dd7070Spatrick // Number of bytes
1079e5dd7070Spatrick Fields.addInt(Int32Ty, Str.size());
1080e5dd7070Spatrick // Hash. Not currently initialised by the compiler.
1081e5dd7070Spatrick Fields.addInt(Int32Ty, 0);
1082e5dd7070Spatrick // Data pointer
1083e5dd7070Spatrick Fields.add(MakeConstantString(Str));
1084e5dd7070Spatrick }
1085e5dd7070Spatrick std::string StringName;
1086e5dd7070Spatrick bool isNamed = !isNonASCII;
1087e5dd7070Spatrick if (isNamed) {
1088e5dd7070Spatrick StringName = ".objc_str_";
1089e5dd7070Spatrick for (int i=0,e=Str.size() ; i<e ; ++i) {
1090e5dd7070Spatrick unsigned char c = Str[i];
1091e5dd7070Spatrick if (isalnum(c))
1092e5dd7070Spatrick StringName += c;
1093e5dd7070Spatrick else if (c == ' ')
1094e5dd7070Spatrick StringName += '_';
1095e5dd7070Spatrick else {
1096e5dd7070Spatrick isNamed = false;
1097e5dd7070Spatrick break;
1098e5dd7070Spatrick }
1099e5dd7070Spatrick }
1100e5dd7070Spatrick }
1101a9ac8606Spatrick llvm::GlobalVariable *ObjCStrGV =
1102e5dd7070Spatrick Fields.finishAndCreateGlobal(
1103e5dd7070Spatrick isNamed ? StringRef(StringName) : ".objc_string",
1104e5dd7070Spatrick Align, false, isNamed ? llvm::GlobalValue::LinkOnceODRLinkage
1105e5dd7070Spatrick : llvm::GlobalValue::PrivateLinkage);
1106e5dd7070Spatrick ObjCStrGV->setSection(sectionName<ConstantStringSection>());
1107e5dd7070Spatrick if (isNamed) {
1108e5dd7070Spatrick ObjCStrGV->setComdat(TheModule.getOrInsertComdat(StringName));
1109e5dd7070Spatrick ObjCStrGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1110e5dd7070Spatrick }
1111e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
1112a9ac8606Spatrick std::pair<llvm::GlobalVariable*, int> v{ObjCStrGV, 0};
1113e5dd7070Spatrick EarlyInitList.emplace_back(Sym, v);
1114e5dd7070Spatrick }
1115e5dd7070Spatrick llvm::Constant *ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStrGV, IdTy);
1116e5dd7070Spatrick ObjCStrings[Str] = ObjCStr;
1117e5dd7070Spatrick ConstantStrings.push_back(ObjCStr);
1118*12c85518Srobert return ConstantAddress(ObjCStr, IdElemTy, Align);
1119e5dd7070Spatrick }
1120e5dd7070Spatrick
PushProperty(ConstantArrayBuilder & PropertiesArray,const ObjCPropertyDecl * property,const Decl * OCD,bool isSynthesized=true,bool isDynamic=true)1121e5dd7070Spatrick void PushProperty(ConstantArrayBuilder &PropertiesArray,
1122e5dd7070Spatrick const ObjCPropertyDecl *property,
1123e5dd7070Spatrick const Decl *OCD,
1124e5dd7070Spatrick bool isSynthesized=true, bool
1125e5dd7070Spatrick isDynamic=true) override {
1126e5dd7070Spatrick // struct objc_property
1127e5dd7070Spatrick // {
1128e5dd7070Spatrick // const char *name;
1129e5dd7070Spatrick // const char *attributes;
1130e5dd7070Spatrick // const char *type;
1131e5dd7070Spatrick // SEL getter;
1132e5dd7070Spatrick // SEL setter;
1133e5dd7070Spatrick // };
1134e5dd7070Spatrick auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy);
1135e5dd7070Spatrick ASTContext &Context = CGM.getContext();
1136e5dd7070Spatrick Fields.add(MakeConstantString(property->getNameAsString()));
1137e5dd7070Spatrick std::string TypeStr =
1138e5dd7070Spatrick CGM.getContext().getObjCEncodingForPropertyDecl(property, OCD);
1139e5dd7070Spatrick Fields.add(MakeConstantString(TypeStr));
1140e5dd7070Spatrick std::string typeStr;
1141e5dd7070Spatrick Context.getObjCEncodingForType(property->getType(), typeStr);
1142e5dd7070Spatrick Fields.add(MakeConstantString(typeStr));
1143e5dd7070Spatrick auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
1144e5dd7070Spatrick if (accessor) {
1145e5dd7070Spatrick std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
1146e5dd7070Spatrick Fields.add(GetConstantSelector(accessor->getSelector(), TypeStr));
1147e5dd7070Spatrick } else {
1148e5dd7070Spatrick Fields.add(NULLPtr);
1149e5dd7070Spatrick }
1150e5dd7070Spatrick };
1151e5dd7070Spatrick addPropertyMethod(property->getGetterMethodDecl());
1152e5dd7070Spatrick addPropertyMethod(property->getSetterMethodDecl());
1153e5dd7070Spatrick Fields.finishAndAddTo(PropertiesArray);
1154e5dd7070Spatrick }
1155e5dd7070Spatrick
1156e5dd7070Spatrick llvm::Constant *
GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl * > Methods)1157e5dd7070Spatrick GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl*> Methods) override {
1158e5dd7070Spatrick // struct objc_protocol_method_description
1159e5dd7070Spatrick // {
1160e5dd7070Spatrick // SEL selector;
1161e5dd7070Spatrick // const char *types;
1162e5dd7070Spatrick // };
1163e5dd7070Spatrick llvm::StructType *ObjCMethodDescTy =
1164e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(),
1165e5dd7070Spatrick { PtrToInt8Ty, PtrToInt8Ty });
1166e5dd7070Spatrick ASTContext &Context = CGM.getContext();
1167e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
1168e5dd7070Spatrick // struct objc_protocol_method_description_list
1169e5dd7070Spatrick // {
1170e5dd7070Spatrick // int count;
1171e5dd7070Spatrick // int size;
1172e5dd7070Spatrick // struct objc_protocol_method_description methods[];
1173e5dd7070Spatrick // };
1174e5dd7070Spatrick auto MethodList = Builder.beginStruct();
1175e5dd7070Spatrick // int count;
1176e5dd7070Spatrick MethodList.addInt(IntTy, Methods.size());
1177e5dd7070Spatrick // int size; // sizeof(struct objc_method_description)
1178e5dd7070Spatrick llvm::DataLayout td(&TheModule);
1179e5dd7070Spatrick MethodList.addInt(IntTy, td.getTypeSizeInBits(ObjCMethodDescTy) /
1180e5dd7070Spatrick CGM.getContext().getCharWidth());
1181e5dd7070Spatrick // struct objc_method_description[]
1182e5dd7070Spatrick auto MethodArray = MethodList.beginArray(ObjCMethodDescTy);
1183e5dd7070Spatrick for (auto *M : Methods) {
1184e5dd7070Spatrick auto Method = MethodArray.beginStruct(ObjCMethodDescTy);
1185e5dd7070Spatrick Method.add(CGObjCGNU::GetConstantSelector(M));
1186e5dd7070Spatrick Method.add(GetTypeString(Context.getObjCEncodingForMethodDecl(M, true)));
1187e5dd7070Spatrick Method.finishAndAddTo(MethodArray);
1188e5dd7070Spatrick }
1189e5dd7070Spatrick MethodArray.finishAndAddTo(MethodList);
1190e5dd7070Spatrick return MethodList.finishAndCreateGlobal(".objc_protocol_method_list",
1191e5dd7070Spatrick CGM.getPointerAlign());
1192e5dd7070Spatrick }
GenerateCategoryProtocolList(const ObjCCategoryDecl * OCD)1193e5dd7070Spatrick llvm::Constant *GenerateCategoryProtocolList(const ObjCCategoryDecl *OCD)
1194e5dd7070Spatrick override {
1195a9ac8606Spatrick const auto &ReferencedProtocols = OCD->getReferencedProtocols();
1196a9ac8606Spatrick auto RuntimeProtocols = GetRuntimeProtocolList(ReferencedProtocols.begin(),
1197a9ac8606Spatrick ReferencedProtocols.end());
1198e5dd7070Spatrick SmallVector<llvm::Constant *, 16> Protocols;
1199a9ac8606Spatrick for (const auto *PI : RuntimeProtocols)
1200e5dd7070Spatrick Protocols.push_back(
1201e5dd7070Spatrick llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI),
1202e5dd7070Spatrick ProtocolPtrTy));
1203e5dd7070Spatrick return GenerateProtocolList(Protocols);
1204e5dd7070Spatrick }
1205e5dd7070Spatrick
LookupIMPSuper(CodeGenFunction & CGF,Address ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)1206e5dd7070Spatrick llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
1207e5dd7070Spatrick llvm::Value *cmd, MessageSendInfo &MSI) override {
1208e5dd7070Spatrick // Don't access the slot unless we're trying to cache the result.
1209e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
1210*12c85518Srobert llvm::Value *lookupArgs[] = {CGObjCGNU::EnforceType(Builder,
1211*12c85518Srobert ObjCSuper.getPointer(),
1212*12c85518Srobert PtrToObjCSuperTy),
1213*12c85518Srobert cmd};
1214e5dd7070Spatrick return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
1215e5dd7070Spatrick }
1216e5dd7070Spatrick
GetClassVar(StringRef Name,bool isWeak=false)1217e5dd7070Spatrick llvm::GlobalVariable *GetClassVar(StringRef Name, bool isWeak=false) {
1218e5dd7070Spatrick std::string SymbolName = SymbolForClassRef(Name, isWeak);
1219e5dd7070Spatrick auto *ClassSymbol = TheModule.getNamedGlobal(SymbolName);
1220e5dd7070Spatrick if (ClassSymbol)
1221e5dd7070Spatrick return ClassSymbol;
1222e5dd7070Spatrick ClassSymbol = new llvm::GlobalVariable(TheModule,
1223e5dd7070Spatrick IdTy, false, llvm::GlobalValue::ExternalLinkage,
1224e5dd7070Spatrick nullptr, SymbolName);
1225e5dd7070Spatrick // If this is a weak symbol, then we are creating a valid definition for
1226e5dd7070Spatrick // the symbol, pointing to a weak definition of the real class pointer. If
1227e5dd7070Spatrick // this is not a weak reference, then we are expecting another compilation
1228e5dd7070Spatrick // unit to provide the real indirection symbol.
1229e5dd7070Spatrick if (isWeak)
1230e5dd7070Spatrick ClassSymbol->setInitializer(new llvm::GlobalVariable(TheModule,
1231e5dd7070Spatrick Int8Ty, false, llvm::GlobalValue::ExternalWeakLinkage,
1232e5dd7070Spatrick nullptr, SymbolForClass(Name)));
1233e5dd7070Spatrick else {
1234e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
1235e5dd7070Spatrick IdentifierInfo &II = CGM.getContext().Idents.get(Name);
1236e5dd7070Spatrick TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
1237e5dd7070Spatrick DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
1238e5dd7070Spatrick
1239e5dd7070Spatrick const ObjCInterfaceDecl *OID = nullptr;
1240a9ac8606Spatrick for (const auto *Result : DC->lookup(&II))
1241e5dd7070Spatrick if ((OID = dyn_cast<ObjCInterfaceDecl>(Result)))
1242e5dd7070Spatrick break;
1243e5dd7070Spatrick
1244e5dd7070Spatrick // The first Interface we find may be a @class,
1245e5dd7070Spatrick // which should only be treated as the source of
1246e5dd7070Spatrick // truth in the absence of a true declaration.
1247e5dd7070Spatrick assert(OID && "Failed to find ObjCInterfaceDecl");
1248e5dd7070Spatrick const ObjCInterfaceDecl *OIDDef = OID->getDefinition();
1249e5dd7070Spatrick if (OIDDef != nullptr)
1250e5dd7070Spatrick OID = OIDDef;
1251e5dd7070Spatrick
1252e5dd7070Spatrick auto Storage = llvm::GlobalValue::DefaultStorageClass;
1253e5dd7070Spatrick if (OID->hasAttr<DLLImportAttr>())
1254e5dd7070Spatrick Storage = llvm::GlobalValue::DLLImportStorageClass;
1255e5dd7070Spatrick else if (OID->hasAttr<DLLExportAttr>())
1256e5dd7070Spatrick Storage = llvm::GlobalValue::DLLExportStorageClass;
1257e5dd7070Spatrick
1258e5dd7070Spatrick cast<llvm::GlobalValue>(ClassSymbol)->setDLLStorageClass(Storage);
1259e5dd7070Spatrick }
1260e5dd7070Spatrick }
1261e5dd7070Spatrick assert(ClassSymbol->getName() == SymbolName);
1262e5dd7070Spatrick return ClassSymbol;
1263e5dd7070Spatrick }
GetClassNamed(CodeGenFunction & CGF,const std::string & Name,bool isWeak)1264e5dd7070Spatrick llvm::Value *GetClassNamed(CodeGenFunction &CGF,
1265e5dd7070Spatrick const std::string &Name,
1266e5dd7070Spatrick bool isWeak) override {
1267*12c85518Srobert return CGF.Builder.CreateLoad(
1268*12c85518Srobert Address(GetClassVar(Name, isWeak), IdTy, CGM.getPointerAlign()));
1269e5dd7070Spatrick }
FlagsForOwnership(Qualifiers::ObjCLifetime Ownership)1270e5dd7070Spatrick int32_t FlagsForOwnership(Qualifiers::ObjCLifetime Ownership) {
1271e5dd7070Spatrick // typedef enum {
1272e5dd7070Spatrick // ownership_invalid = 0,
1273e5dd7070Spatrick // ownership_strong = 1,
1274e5dd7070Spatrick // ownership_weak = 2,
1275e5dd7070Spatrick // ownership_unsafe = 3
1276e5dd7070Spatrick // } ivar_ownership;
1277e5dd7070Spatrick int Flag;
1278e5dd7070Spatrick switch (Ownership) {
1279e5dd7070Spatrick case Qualifiers::OCL_Strong:
1280e5dd7070Spatrick Flag = 1;
1281e5dd7070Spatrick break;
1282e5dd7070Spatrick case Qualifiers::OCL_Weak:
1283e5dd7070Spatrick Flag = 2;
1284e5dd7070Spatrick break;
1285e5dd7070Spatrick case Qualifiers::OCL_ExplicitNone:
1286e5dd7070Spatrick Flag = 3;
1287e5dd7070Spatrick break;
1288e5dd7070Spatrick case Qualifiers::OCL_None:
1289e5dd7070Spatrick case Qualifiers::OCL_Autoreleasing:
1290e5dd7070Spatrick assert(Ownership != Qualifiers::OCL_Autoreleasing);
1291e5dd7070Spatrick Flag = 0;
1292e5dd7070Spatrick }
1293e5dd7070Spatrick return Flag;
1294e5dd7070Spatrick }
GenerateIvarList(ArrayRef<llvm::Constant * > IvarNames,ArrayRef<llvm::Constant * > IvarTypes,ArrayRef<llvm::Constant * > IvarOffsets,ArrayRef<llvm::Constant * > IvarAlign,ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership)1295e5dd7070Spatrick llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1296e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarTypes,
1297e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarOffsets,
1298e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarAlign,
1299e5dd7070Spatrick ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership) override {
1300e5dd7070Spatrick llvm_unreachable("Method should not be called!");
1301e5dd7070Spatrick }
1302e5dd7070Spatrick
GenerateEmptyProtocol(StringRef ProtocolName)1303e5dd7070Spatrick llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName) override {
1304e5dd7070Spatrick std::string Name = SymbolForProtocol(ProtocolName);
1305e5dd7070Spatrick auto *GV = TheModule.getGlobalVariable(Name);
1306e5dd7070Spatrick if (!GV) {
1307e5dd7070Spatrick // Emit a placeholder symbol.
1308e5dd7070Spatrick GV = new llvm::GlobalVariable(TheModule, ProtocolTy, false,
1309e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, Name);
1310e5dd7070Spatrick GV->setAlignment(CGM.getPointerAlign().getAsAlign());
1311e5dd7070Spatrick }
1312e5dd7070Spatrick return llvm::ConstantExpr::getBitCast(GV, ProtocolPtrTy);
1313e5dd7070Spatrick }
1314e5dd7070Spatrick
1315e5dd7070Spatrick /// Existing protocol references.
1316e5dd7070Spatrick llvm::StringMap<llvm::Constant*> ExistingProtocolRefs;
1317e5dd7070Spatrick
GenerateProtocolRef(CodeGenFunction & CGF,const ObjCProtocolDecl * PD)1318e5dd7070Spatrick llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
1319e5dd7070Spatrick const ObjCProtocolDecl *PD) override {
1320e5dd7070Spatrick auto Name = PD->getNameAsString();
1321e5dd7070Spatrick auto *&Ref = ExistingProtocolRefs[Name];
1322e5dd7070Spatrick if (!Ref) {
1323e5dd7070Spatrick auto *&Protocol = ExistingProtocols[Name];
1324e5dd7070Spatrick if (!Protocol)
1325e5dd7070Spatrick Protocol = GenerateProtocolRef(PD);
1326e5dd7070Spatrick std::string RefName = SymbolForProtocolRef(Name);
1327e5dd7070Spatrick assert(!TheModule.getGlobalVariable(RefName));
1328e5dd7070Spatrick // Emit a reference symbol.
1329e5dd7070Spatrick auto GV = new llvm::GlobalVariable(TheModule, ProtocolPtrTy,
1330e5dd7070Spatrick false, llvm::GlobalValue::LinkOnceODRLinkage,
1331e5dd7070Spatrick llvm::ConstantExpr::getBitCast(Protocol, ProtocolPtrTy), RefName);
1332e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(RefName));
1333e5dd7070Spatrick GV->setSection(sectionName<ProtocolReferenceSection>());
1334e5dd7070Spatrick GV->setAlignment(CGM.getPointerAlign().getAsAlign());
1335e5dd7070Spatrick Ref = GV;
1336e5dd7070Spatrick }
1337e5dd7070Spatrick EmittedProtocolRef = true;
1338a9ac8606Spatrick return CGF.Builder.CreateAlignedLoad(ProtocolPtrTy, Ref,
1339a9ac8606Spatrick CGM.getPointerAlign());
1340e5dd7070Spatrick }
1341e5dd7070Spatrick
GenerateProtocolList(ArrayRef<llvm::Constant * > Protocols)1342e5dd7070Spatrick llvm::Constant *GenerateProtocolList(ArrayRef<llvm::Constant*> Protocols) {
1343e5dd7070Spatrick llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(ProtocolPtrTy,
1344e5dd7070Spatrick Protocols.size());
1345e5dd7070Spatrick llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1346e5dd7070Spatrick Protocols);
1347e5dd7070Spatrick ConstantInitBuilder builder(CGM);
1348e5dd7070Spatrick auto ProtocolBuilder = builder.beginStruct();
1349e5dd7070Spatrick ProtocolBuilder.addNullPointer(PtrTy);
1350e5dd7070Spatrick ProtocolBuilder.addInt(SizeTy, Protocols.size());
1351e5dd7070Spatrick ProtocolBuilder.add(ProtocolArray);
1352e5dd7070Spatrick return ProtocolBuilder.finishAndCreateGlobal(".objc_protocol_list",
1353e5dd7070Spatrick CGM.getPointerAlign(), false, llvm::GlobalValue::InternalLinkage);
1354e5dd7070Spatrick }
1355e5dd7070Spatrick
GenerateProtocol(const ObjCProtocolDecl * PD)1356e5dd7070Spatrick void GenerateProtocol(const ObjCProtocolDecl *PD) override {
1357e5dd7070Spatrick // Do nothing - we only emit referenced protocols.
1358e5dd7070Spatrick }
GenerateProtocolRef(const ObjCProtocolDecl * PD)1359ec727ea7Spatrick llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) override {
1360e5dd7070Spatrick std::string ProtocolName = PD->getNameAsString();
1361e5dd7070Spatrick auto *&Protocol = ExistingProtocols[ProtocolName];
1362e5dd7070Spatrick if (Protocol)
1363e5dd7070Spatrick return Protocol;
1364e5dd7070Spatrick
1365e5dd7070Spatrick EmittedProtocol = true;
1366e5dd7070Spatrick
1367e5dd7070Spatrick auto SymName = SymbolForProtocol(ProtocolName);
1368e5dd7070Spatrick auto *OldGV = TheModule.getGlobalVariable(SymName);
1369e5dd7070Spatrick
1370e5dd7070Spatrick // Use the protocol definition, if there is one.
1371e5dd7070Spatrick if (const ObjCProtocolDecl *Def = PD->getDefinition())
1372e5dd7070Spatrick PD = Def;
1373e5dd7070Spatrick else {
1374e5dd7070Spatrick // If there is no definition, then create an external linkage symbol and
1375e5dd7070Spatrick // hope that someone else fills it in for us (and fail to link if they
1376e5dd7070Spatrick // don't).
1377e5dd7070Spatrick assert(!OldGV);
1378e5dd7070Spatrick Protocol = new llvm::GlobalVariable(TheModule, ProtocolTy,
1379e5dd7070Spatrick /*isConstant*/false,
1380e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, SymName);
1381e5dd7070Spatrick return Protocol;
1382e5dd7070Spatrick }
1383e5dd7070Spatrick
1384e5dd7070Spatrick SmallVector<llvm::Constant*, 16> Protocols;
1385a9ac8606Spatrick auto RuntimeProtocols =
1386a9ac8606Spatrick GetRuntimeProtocolList(PD->protocol_begin(), PD->protocol_end());
1387a9ac8606Spatrick for (const auto *PI : RuntimeProtocols)
1388e5dd7070Spatrick Protocols.push_back(
1389e5dd7070Spatrick llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI),
1390e5dd7070Spatrick ProtocolPtrTy));
1391e5dd7070Spatrick llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1392e5dd7070Spatrick
1393e5dd7070Spatrick // Collect information about methods
1394e5dd7070Spatrick llvm::Constant *InstanceMethodList, *OptionalInstanceMethodList;
1395e5dd7070Spatrick llvm::Constant *ClassMethodList, *OptionalClassMethodList;
1396e5dd7070Spatrick EmitProtocolMethodList(PD->instance_methods(), InstanceMethodList,
1397e5dd7070Spatrick OptionalInstanceMethodList);
1398e5dd7070Spatrick EmitProtocolMethodList(PD->class_methods(), ClassMethodList,
1399e5dd7070Spatrick OptionalClassMethodList);
1400e5dd7070Spatrick
1401e5dd7070Spatrick // The isa pointer must be set to a magic number so the runtime knows it's
1402e5dd7070Spatrick // the correct layout.
1403e5dd7070Spatrick ConstantInitBuilder builder(CGM);
1404e5dd7070Spatrick auto ProtocolBuilder = builder.beginStruct();
1405e5dd7070Spatrick ProtocolBuilder.add(llvm::ConstantExpr::getIntToPtr(
1406e5dd7070Spatrick llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1407e5dd7070Spatrick ProtocolBuilder.add(MakeConstantString(ProtocolName));
1408e5dd7070Spatrick ProtocolBuilder.add(ProtocolList);
1409e5dd7070Spatrick ProtocolBuilder.add(InstanceMethodList);
1410e5dd7070Spatrick ProtocolBuilder.add(ClassMethodList);
1411e5dd7070Spatrick ProtocolBuilder.add(OptionalInstanceMethodList);
1412e5dd7070Spatrick ProtocolBuilder.add(OptionalClassMethodList);
1413e5dd7070Spatrick // Required instance properties
1414e5dd7070Spatrick ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, false));
1415e5dd7070Spatrick // Optional instance properties
1416e5dd7070Spatrick ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, true));
1417e5dd7070Spatrick // Required class properties
1418e5dd7070Spatrick ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, false));
1419e5dd7070Spatrick // Optional class properties
1420e5dd7070Spatrick ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, true));
1421e5dd7070Spatrick
1422e5dd7070Spatrick auto *GV = ProtocolBuilder.finishAndCreateGlobal(SymName,
1423e5dd7070Spatrick CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage);
1424e5dd7070Spatrick GV->setSection(sectionName<ProtocolSection>());
1425e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(SymName));
1426e5dd7070Spatrick if (OldGV) {
1427e5dd7070Spatrick OldGV->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GV,
1428e5dd7070Spatrick OldGV->getType()));
1429e5dd7070Spatrick OldGV->removeFromParent();
1430e5dd7070Spatrick GV->setName(SymName);
1431e5dd7070Spatrick }
1432e5dd7070Spatrick Protocol = GV;
1433e5dd7070Spatrick return GV;
1434e5dd7070Spatrick }
EnforceType(llvm::Constant * Val,llvm::Type * Ty)1435e5dd7070Spatrick llvm::Constant *EnforceType(llvm::Constant *Val, llvm::Type *Ty) {
1436e5dd7070Spatrick if (Val->getType() == Ty)
1437e5dd7070Spatrick return Val;
1438e5dd7070Spatrick return llvm::ConstantExpr::getBitCast(Val, Ty);
1439e5dd7070Spatrick }
GetTypedSelector(CodeGenFunction & CGF,Selector Sel,const std::string & TypeEncoding)1440e5dd7070Spatrick llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel,
1441e5dd7070Spatrick const std::string &TypeEncoding) override {
1442e5dd7070Spatrick return GetConstantSelector(Sel, TypeEncoding);
1443e5dd7070Spatrick }
GetTypeString(llvm::StringRef TypeEncoding)1444e5dd7070Spatrick llvm::Constant *GetTypeString(llvm::StringRef TypeEncoding) {
1445e5dd7070Spatrick if (TypeEncoding.empty())
1446e5dd7070Spatrick return NULLPtr;
1447ec727ea7Spatrick std::string MangledTypes = std::string(TypeEncoding);
1448e5dd7070Spatrick std::replace(MangledTypes.begin(), MangledTypes.end(),
1449e5dd7070Spatrick '@', '\1');
1450e5dd7070Spatrick std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
1451e5dd7070Spatrick auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
1452e5dd7070Spatrick if (!TypesGlobal) {
1453e5dd7070Spatrick llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
1454e5dd7070Spatrick TypeEncoding);
1455e5dd7070Spatrick auto *GV = new llvm::GlobalVariable(TheModule, Init->getType(),
1456e5dd7070Spatrick true, llvm::GlobalValue::LinkOnceODRLinkage, Init, TypesVarName);
1457e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(TypesVarName));
1458e5dd7070Spatrick GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1459e5dd7070Spatrick TypesGlobal = GV;
1460e5dd7070Spatrick }
1461e5dd7070Spatrick return llvm::ConstantExpr::getGetElementPtr(TypesGlobal->getValueType(),
1462e5dd7070Spatrick TypesGlobal, Zeros);
1463e5dd7070Spatrick }
GetConstantSelector(Selector Sel,const std::string & TypeEncoding)1464e5dd7070Spatrick llvm::Constant *GetConstantSelector(Selector Sel,
1465e5dd7070Spatrick const std::string &TypeEncoding) override {
1466e5dd7070Spatrick // @ is used as a special character in symbol names (used for symbol
1467e5dd7070Spatrick // versioning), so mangle the name to not include it. Replace it with a
1468e5dd7070Spatrick // character that is not a valid type encoding character (and, being
1469e5dd7070Spatrick // non-printable, never will be!)
1470e5dd7070Spatrick std::string MangledTypes = TypeEncoding;
1471e5dd7070Spatrick std::replace(MangledTypes.begin(), MangledTypes.end(),
1472e5dd7070Spatrick '@', '\1');
1473e5dd7070Spatrick auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
1474e5dd7070Spatrick MangledTypes).str();
1475e5dd7070Spatrick if (auto *GV = TheModule.getNamedGlobal(SelVarName))
1476e5dd7070Spatrick return EnforceType(GV, SelectorTy);
1477e5dd7070Spatrick ConstantInitBuilder builder(CGM);
1478e5dd7070Spatrick auto SelBuilder = builder.beginStruct();
1479e5dd7070Spatrick SelBuilder.add(ExportUniqueString(Sel.getAsString(), ".objc_sel_name_",
1480e5dd7070Spatrick true));
1481e5dd7070Spatrick SelBuilder.add(GetTypeString(TypeEncoding));
1482e5dd7070Spatrick auto *GV = SelBuilder.finishAndCreateGlobal(SelVarName,
1483e5dd7070Spatrick CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1484e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(SelVarName));
1485e5dd7070Spatrick GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1486e5dd7070Spatrick GV->setSection(sectionName<SelectorSection>());
1487e5dd7070Spatrick auto *SelVal = EnforceType(GV, SelectorTy);
1488e5dd7070Spatrick return SelVal;
1489e5dd7070Spatrick }
1490e5dd7070Spatrick llvm::StructType *emptyStruct = nullptr;
1491e5dd7070Spatrick
1492e5dd7070Spatrick /// Return pointers to the start and end of a section. On ELF platforms, we
1493e5dd7070Spatrick /// use the __start_ and __stop_ symbols that GNU-compatible linkers will set
1494e5dd7070Spatrick /// to the start and end of section names, as long as those section names are
1495e5dd7070Spatrick /// valid identifiers and the symbols are referenced but not defined. On
1496e5dd7070Spatrick /// Windows, we use the fact that MSVC-compatible linkers will lexically sort
1497e5dd7070Spatrick /// by subsections and place everything that we want to reference in a middle
1498e5dd7070Spatrick /// subsection and then insert zero-sized symbols in subsections a and z.
1499e5dd7070Spatrick std::pair<llvm::Constant*,llvm::Constant*>
GetSectionBounds(StringRef Section)1500e5dd7070Spatrick GetSectionBounds(StringRef Section) {
1501e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
1502e5dd7070Spatrick if (emptyStruct == nullptr) {
1503e5dd7070Spatrick emptyStruct = llvm::StructType::create(VMContext, ".objc_section_sentinel");
1504e5dd7070Spatrick emptyStruct->setBody({}, /*isPacked*/true);
1505e5dd7070Spatrick }
1506e5dd7070Spatrick auto ZeroInit = llvm::Constant::getNullValue(emptyStruct);
1507e5dd7070Spatrick auto Sym = [&](StringRef Prefix, StringRef SecSuffix) {
1508e5dd7070Spatrick auto *Sym = new llvm::GlobalVariable(TheModule, emptyStruct,
1509e5dd7070Spatrick /*isConstant*/false,
1510e5dd7070Spatrick llvm::GlobalValue::LinkOnceODRLinkage, ZeroInit, Prefix +
1511e5dd7070Spatrick Section);
1512e5dd7070Spatrick Sym->setVisibility(llvm::GlobalValue::HiddenVisibility);
1513e5dd7070Spatrick Sym->setSection((Section + SecSuffix).str());
1514e5dd7070Spatrick Sym->setComdat(TheModule.getOrInsertComdat((Prefix +
1515e5dd7070Spatrick Section).str()));
1516e5dd7070Spatrick Sym->setAlignment(CGM.getPointerAlign().getAsAlign());
1517e5dd7070Spatrick return Sym;
1518e5dd7070Spatrick };
1519e5dd7070Spatrick return { Sym("__start_", "$a"), Sym("__stop", "$z") };
1520e5dd7070Spatrick }
1521e5dd7070Spatrick auto *Start = new llvm::GlobalVariable(TheModule, PtrTy,
1522e5dd7070Spatrick /*isConstant*/false,
1523e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__start_") +
1524e5dd7070Spatrick Section);
1525e5dd7070Spatrick Start->setVisibility(llvm::GlobalValue::HiddenVisibility);
1526e5dd7070Spatrick auto *Stop = new llvm::GlobalVariable(TheModule, PtrTy,
1527e5dd7070Spatrick /*isConstant*/false,
1528e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__stop_") +
1529e5dd7070Spatrick Section);
1530e5dd7070Spatrick Stop->setVisibility(llvm::GlobalValue::HiddenVisibility);
1531e5dd7070Spatrick return { Start, Stop };
1532e5dd7070Spatrick }
getCatchAllTypeInfo()1533e5dd7070Spatrick CatchTypeInfo getCatchAllTypeInfo() override {
1534e5dd7070Spatrick return CGM.getCXXABI().getCatchAllTypeInfo();
1535e5dd7070Spatrick }
ModuleInitFunction()1536e5dd7070Spatrick llvm::Function *ModuleInitFunction() override {
1537e5dd7070Spatrick llvm::Function *LoadFunction = llvm::Function::Create(
1538e5dd7070Spatrick llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
1539e5dd7070Spatrick llvm::GlobalValue::LinkOnceODRLinkage, ".objcv2_load_function",
1540e5dd7070Spatrick &TheModule);
1541e5dd7070Spatrick LoadFunction->setVisibility(llvm::GlobalValue::HiddenVisibility);
1542e5dd7070Spatrick LoadFunction->setComdat(TheModule.getOrInsertComdat(".objcv2_load_function"));
1543e5dd7070Spatrick
1544e5dd7070Spatrick llvm::BasicBlock *EntryBB =
1545e5dd7070Spatrick llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
1546e5dd7070Spatrick CGBuilderTy B(CGM, VMContext);
1547e5dd7070Spatrick B.SetInsertPoint(EntryBB);
1548e5dd7070Spatrick ConstantInitBuilder builder(CGM);
1549e5dd7070Spatrick auto InitStructBuilder = builder.beginStruct();
1550e5dd7070Spatrick InitStructBuilder.addInt(Int64Ty, 0);
1551e5dd7070Spatrick auto §ionVec = CGM.getTriple().isOSBinFormatCOFF() ? PECOFFSectionsBaseNames : SectionsBaseNames;
1552e5dd7070Spatrick for (auto *s : sectionVec) {
1553e5dd7070Spatrick auto bounds = GetSectionBounds(s);
1554e5dd7070Spatrick InitStructBuilder.add(bounds.first);
1555e5dd7070Spatrick InitStructBuilder.add(bounds.second);
1556e5dd7070Spatrick }
1557e5dd7070Spatrick auto *InitStruct = InitStructBuilder.finishAndCreateGlobal(".objc_init",
1558e5dd7070Spatrick CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1559e5dd7070Spatrick InitStruct->setVisibility(llvm::GlobalValue::HiddenVisibility);
1560e5dd7070Spatrick InitStruct->setComdat(TheModule.getOrInsertComdat(".objc_init"));
1561e5dd7070Spatrick
1562e5dd7070Spatrick CallRuntimeFunction(B, "__objc_load", {InitStruct});;
1563e5dd7070Spatrick B.CreateRetVoid();
1564e5dd7070Spatrick // Make sure that the optimisers don't delete this function.
1565e5dd7070Spatrick CGM.addCompilerUsedGlobal(LoadFunction);
1566e5dd7070Spatrick // FIXME: Currently ELF only!
1567e5dd7070Spatrick // We have to do this by hand, rather than with @llvm.ctors, so that the
1568e5dd7070Spatrick // linker can remove the duplicate invocations.
1569e5dd7070Spatrick auto *InitVar = new llvm::GlobalVariable(TheModule, LoadFunction->getType(),
1570ec727ea7Spatrick /*isConstant*/false, llvm::GlobalValue::LinkOnceAnyLinkage,
1571e5dd7070Spatrick LoadFunction, ".objc_ctor");
1572e5dd7070Spatrick // Check that this hasn't been renamed. This shouldn't happen, because
1573e5dd7070Spatrick // this function should be called precisely once.
1574e5dd7070Spatrick assert(InitVar->getName() == ".objc_ctor");
1575e5dd7070Spatrick // In Windows, initialisers are sorted by the suffix. XCL is for library
1576e5dd7070Spatrick // initialisers, which run before user initialisers. We are running
1577e5dd7070Spatrick // Objective-C loads at the end of library load. This means +load methods
1578e5dd7070Spatrick // will run before any other static constructors, but that static
1579e5dd7070Spatrick // constructors can see a fully initialised Objective-C state.
1580e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF())
1581e5dd7070Spatrick InitVar->setSection(".CRT$XCLz");
1582e5dd7070Spatrick else
1583e5dd7070Spatrick {
1584e5dd7070Spatrick if (CGM.getCodeGenOpts().UseInitArray)
1585e5dd7070Spatrick InitVar->setSection(".init_array");
1586e5dd7070Spatrick else
1587e5dd7070Spatrick InitVar->setSection(".ctors");
1588e5dd7070Spatrick }
1589e5dd7070Spatrick InitVar->setVisibility(llvm::GlobalValue::HiddenVisibility);
1590e5dd7070Spatrick InitVar->setComdat(TheModule.getOrInsertComdat(".objc_ctor"));
1591e5dd7070Spatrick CGM.addUsedGlobal(InitVar);
1592e5dd7070Spatrick for (auto *C : Categories) {
1593e5dd7070Spatrick auto *Cat = cast<llvm::GlobalVariable>(C->stripPointerCasts());
1594e5dd7070Spatrick Cat->setSection(sectionName<CategorySection>());
1595e5dd7070Spatrick CGM.addUsedGlobal(Cat);
1596e5dd7070Spatrick }
1597e5dd7070Spatrick auto createNullGlobal = [&](StringRef Name, ArrayRef<llvm::Constant*> Init,
1598e5dd7070Spatrick StringRef Section) {
1599e5dd7070Spatrick auto nullBuilder = builder.beginStruct();
1600e5dd7070Spatrick for (auto *F : Init)
1601e5dd7070Spatrick nullBuilder.add(F);
1602e5dd7070Spatrick auto GV = nullBuilder.finishAndCreateGlobal(Name, CGM.getPointerAlign(),
1603e5dd7070Spatrick false, llvm::GlobalValue::LinkOnceODRLinkage);
1604e5dd7070Spatrick GV->setSection(Section);
1605e5dd7070Spatrick GV->setComdat(TheModule.getOrInsertComdat(Name));
1606e5dd7070Spatrick GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
1607e5dd7070Spatrick CGM.addUsedGlobal(GV);
1608e5dd7070Spatrick return GV;
1609e5dd7070Spatrick };
1610e5dd7070Spatrick for (auto clsAlias : ClassAliases)
1611e5dd7070Spatrick createNullGlobal(std::string(".objc_class_alias") +
1612e5dd7070Spatrick clsAlias.second, { MakeConstantString(clsAlias.second),
1613e5dd7070Spatrick GetClassVar(clsAlias.first) }, sectionName<ClassAliasSection>());
1614e5dd7070Spatrick // On ELF platforms, add a null value for each special section so that we
1615e5dd7070Spatrick // can always guarantee that the _start and _stop symbols will exist and be
1616e5dd7070Spatrick // meaningful. This is not required on COFF platforms, where our start and
1617e5dd7070Spatrick // stop symbols will create the section.
1618e5dd7070Spatrick if (!CGM.getTriple().isOSBinFormatCOFF()) {
1619e5dd7070Spatrick createNullGlobal(".objc_null_selector", {NULLPtr, NULLPtr},
1620e5dd7070Spatrick sectionName<SelectorSection>());
1621e5dd7070Spatrick if (Categories.empty())
1622e5dd7070Spatrick createNullGlobal(".objc_null_category", {NULLPtr, NULLPtr,
1623e5dd7070Spatrick NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr},
1624e5dd7070Spatrick sectionName<CategorySection>());
1625e5dd7070Spatrick if (!EmittedClass) {
1626e5dd7070Spatrick createNullGlobal(".objc_null_cls_init_ref", NULLPtr,
1627e5dd7070Spatrick sectionName<ClassSection>());
1628e5dd7070Spatrick createNullGlobal(".objc_null_class_ref", { NULLPtr, NULLPtr },
1629e5dd7070Spatrick sectionName<ClassReferenceSection>());
1630e5dd7070Spatrick }
1631e5dd7070Spatrick if (!EmittedProtocol)
1632e5dd7070Spatrick createNullGlobal(".objc_null_protocol", {NULLPtr, NULLPtr, NULLPtr,
1633e5dd7070Spatrick NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr,
1634e5dd7070Spatrick NULLPtr}, sectionName<ProtocolSection>());
1635e5dd7070Spatrick if (!EmittedProtocolRef)
1636e5dd7070Spatrick createNullGlobal(".objc_null_protocol_ref", {NULLPtr},
1637e5dd7070Spatrick sectionName<ProtocolReferenceSection>());
1638e5dd7070Spatrick if (ClassAliases.empty())
1639e5dd7070Spatrick createNullGlobal(".objc_null_class_alias", { NULLPtr, NULLPtr },
1640e5dd7070Spatrick sectionName<ClassAliasSection>());
1641e5dd7070Spatrick if (ConstantStrings.empty()) {
1642e5dd7070Spatrick auto i32Zero = llvm::ConstantInt::get(Int32Ty, 0);
1643e5dd7070Spatrick createNullGlobal(".objc_null_constant_string", { NULLPtr, i32Zero,
1644e5dd7070Spatrick i32Zero, i32Zero, i32Zero, NULLPtr },
1645e5dd7070Spatrick sectionName<ConstantStringSection>());
1646e5dd7070Spatrick }
1647e5dd7070Spatrick }
1648e5dd7070Spatrick ConstantStrings.clear();
1649e5dd7070Spatrick Categories.clear();
1650e5dd7070Spatrick Classes.clear();
1651e5dd7070Spatrick
1652e5dd7070Spatrick if (EarlyInitList.size() > 0) {
1653e5dd7070Spatrick auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy,
1654e5dd7070Spatrick {}), llvm::GlobalValue::InternalLinkage, ".objc_early_init",
1655e5dd7070Spatrick &CGM.getModule());
1656e5dd7070Spatrick llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry",
1657e5dd7070Spatrick Init));
1658e5dd7070Spatrick for (const auto &lateInit : EarlyInitList) {
1659e5dd7070Spatrick auto *global = TheModule.getGlobalVariable(lateInit.first);
1660e5dd7070Spatrick if (global) {
1661a9ac8606Spatrick llvm::GlobalVariable *GV = lateInit.second.first;
1662ec727ea7Spatrick b.CreateAlignedStore(
1663ec727ea7Spatrick global,
1664a9ac8606Spatrick b.CreateStructGEP(GV->getValueType(), GV, lateInit.second.second),
1665ec727ea7Spatrick CGM.getPointerAlign().getAsAlign());
1666e5dd7070Spatrick }
1667e5dd7070Spatrick }
1668e5dd7070Spatrick b.CreateRetVoid();
1669e5dd7070Spatrick // We can't use the normal LLVM global initialisation array, because we
1670e5dd7070Spatrick // need to specify that this runs early in library initialisation.
1671e5dd7070Spatrick auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
1672e5dd7070Spatrick /*isConstant*/true, llvm::GlobalValue::InternalLinkage,
1673e5dd7070Spatrick Init, ".objc_early_init_ptr");
1674e5dd7070Spatrick InitVar->setSection(".CRT$XCLb");
1675e5dd7070Spatrick CGM.addUsedGlobal(InitVar);
1676e5dd7070Spatrick }
1677e5dd7070Spatrick return nullptr;
1678e5dd7070Spatrick }
1679e5dd7070Spatrick /// In the v2 ABI, ivar offset variables use the type encoding in their name
1680e5dd7070Spatrick /// to trigger linker failures if the types don't match.
GetIVarOffsetVariableName(const ObjCInterfaceDecl * ID,const ObjCIvarDecl * Ivar)1681e5dd7070Spatrick std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID,
1682e5dd7070Spatrick const ObjCIvarDecl *Ivar) override {
1683e5dd7070Spatrick std::string TypeEncoding;
1684e5dd7070Spatrick CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
1685e5dd7070Spatrick // Prevent the @ from being interpreted as a symbol version.
1686e5dd7070Spatrick std::replace(TypeEncoding.begin(), TypeEncoding.end(),
1687e5dd7070Spatrick '@', '\1');
1688e5dd7070Spatrick const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1689e5dd7070Spatrick + '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
1690e5dd7070Spatrick return Name;
1691e5dd7070Spatrick }
EmitIvarOffset(CodeGenFunction & CGF,const ObjCInterfaceDecl * Interface,const ObjCIvarDecl * Ivar)1692e5dd7070Spatrick llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
1693e5dd7070Spatrick const ObjCInterfaceDecl *Interface,
1694e5dd7070Spatrick const ObjCIvarDecl *Ivar) override {
1695e5dd7070Spatrick const std::string Name = GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
1696e5dd7070Spatrick llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1697e5dd7070Spatrick if (!IvarOffsetPointer)
1698e5dd7070Spatrick IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
1699e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, Name);
1700e5dd7070Spatrick CharUnits Align = CGM.getIntAlign();
1701a9ac8606Spatrick llvm::Value *Offset =
1702a9ac8606Spatrick CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
1703e5dd7070Spatrick if (Offset->getType() != PtrDiffTy)
1704e5dd7070Spatrick Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
1705e5dd7070Spatrick return Offset;
1706e5dd7070Spatrick }
GenerateClass(const ObjCImplementationDecl * OID)1707e5dd7070Spatrick void GenerateClass(const ObjCImplementationDecl *OID) override {
1708e5dd7070Spatrick ASTContext &Context = CGM.getContext();
1709e5dd7070Spatrick bool IsCOFF = CGM.getTriple().isOSBinFormatCOFF();
1710e5dd7070Spatrick
1711e5dd7070Spatrick // Get the class name
1712e5dd7070Spatrick ObjCInterfaceDecl *classDecl =
1713e5dd7070Spatrick const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
1714e5dd7070Spatrick std::string className = classDecl->getNameAsString();
1715e5dd7070Spatrick auto *classNameConstant = MakeConstantString(className);
1716e5dd7070Spatrick
1717e5dd7070Spatrick ConstantInitBuilder builder(CGM);
1718e5dd7070Spatrick auto metaclassFields = builder.beginStruct();
1719e5dd7070Spatrick // struct objc_class *isa;
1720e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1721e5dd7070Spatrick // struct objc_class *super_class;
1722e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1723e5dd7070Spatrick // const char *name;
1724e5dd7070Spatrick metaclassFields.add(classNameConstant);
1725e5dd7070Spatrick // long version;
1726e5dd7070Spatrick metaclassFields.addInt(LongTy, 0);
1727e5dd7070Spatrick // unsigned long info;
1728e5dd7070Spatrick // objc_class_flag_meta
1729e5dd7070Spatrick metaclassFields.addInt(LongTy, 1);
1730e5dd7070Spatrick // long instance_size;
1731e5dd7070Spatrick // Setting this to zero is consistent with the older ABI, but it might be
1732e5dd7070Spatrick // more sensible to set this to sizeof(struct objc_class)
1733e5dd7070Spatrick metaclassFields.addInt(LongTy, 0);
1734e5dd7070Spatrick // struct objc_ivar_list *ivars;
1735e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1736e5dd7070Spatrick // struct objc_method_list *methods
1737e5dd7070Spatrick // FIXME: Almost identical code is copied and pasted below for the
1738e5dd7070Spatrick // class, but refactoring it cleanly requires C++14 generic lambdas.
1739e5dd7070Spatrick if (OID->classmeth_begin() == OID->classmeth_end())
1740e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1741e5dd7070Spatrick else {
1742e5dd7070Spatrick SmallVector<ObjCMethodDecl*, 16> ClassMethods;
1743e5dd7070Spatrick ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(),
1744e5dd7070Spatrick OID->classmeth_end());
1745e5dd7070Spatrick metaclassFields.addBitCast(
1746e5dd7070Spatrick GenerateMethodList(className, "", ClassMethods, true),
1747e5dd7070Spatrick PtrTy);
1748e5dd7070Spatrick }
1749e5dd7070Spatrick // void *dtable;
1750e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1751e5dd7070Spatrick // IMP cxx_construct;
1752e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1753e5dd7070Spatrick // IMP cxx_destruct;
1754e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1755e5dd7070Spatrick // struct objc_class *subclass_list
1756e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1757e5dd7070Spatrick // struct objc_class *sibling_class
1758e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1759e5dd7070Spatrick // struct objc_protocol_list *protocols;
1760e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1761e5dd7070Spatrick // struct reference_list *extra_data;
1762e5dd7070Spatrick metaclassFields.addNullPointer(PtrTy);
1763e5dd7070Spatrick // long abi_version;
1764e5dd7070Spatrick metaclassFields.addInt(LongTy, 0);
1765e5dd7070Spatrick // struct objc_property_list *properties
1766e5dd7070Spatrick metaclassFields.add(GeneratePropertyList(OID, classDecl, /*isClassProperty*/true));
1767e5dd7070Spatrick
1768e5dd7070Spatrick auto *metaclass = metaclassFields.finishAndCreateGlobal(
1769e5dd7070Spatrick ManglePublicSymbol("OBJC_METACLASS_") + className,
1770e5dd7070Spatrick CGM.getPointerAlign());
1771e5dd7070Spatrick
1772e5dd7070Spatrick auto classFields = builder.beginStruct();
1773e5dd7070Spatrick // struct objc_class *isa;
1774e5dd7070Spatrick classFields.add(metaclass);
1775e5dd7070Spatrick // struct objc_class *super_class;
1776e5dd7070Spatrick // Get the superclass name.
1777e5dd7070Spatrick const ObjCInterfaceDecl * SuperClassDecl =
1778e5dd7070Spatrick OID->getClassInterface()->getSuperClass();
1779e5dd7070Spatrick llvm::Constant *SuperClass = nullptr;
1780e5dd7070Spatrick if (SuperClassDecl) {
1781e5dd7070Spatrick auto SuperClassName = SymbolForClass(SuperClassDecl->getNameAsString());
1782e5dd7070Spatrick SuperClass = TheModule.getNamedGlobal(SuperClassName);
1783e5dd7070Spatrick if (!SuperClass)
1784e5dd7070Spatrick {
1785e5dd7070Spatrick SuperClass = new llvm::GlobalVariable(TheModule, PtrTy, false,
1786e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, SuperClassName);
1787e5dd7070Spatrick if (IsCOFF) {
1788e5dd7070Spatrick auto Storage = llvm::GlobalValue::DefaultStorageClass;
1789e5dd7070Spatrick if (SuperClassDecl->hasAttr<DLLImportAttr>())
1790e5dd7070Spatrick Storage = llvm::GlobalValue::DLLImportStorageClass;
1791e5dd7070Spatrick else if (SuperClassDecl->hasAttr<DLLExportAttr>())
1792e5dd7070Spatrick Storage = llvm::GlobalValue::DLLExportStorageClass;
1793e5dd7070Spatrick
1794e5dd7070Spatrick cast<llvm::GlobalValue>(SuperClass)->setDLLStorageClass(Storage);
1795e5dd7070Spatrick }
1796e5dd7070Spatrick }
1797e5dd7070Spatrick if (!IsCOFF)
1798e5dd7070Spatrick classFields.add(llvm::ConstantExpr::getBitCast(SuperClass, PtrTy));
1799e5dd7070Spatrick else
1800e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1801e5dd7070Spatrick } else
1802e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1803e5dd7070Spatrick // const char *name;
1804e5dd7070Spatrick classFields.add(classNameConstant);
1805e5dd7070Spatrick // long version;
1806e5dd7070Spatrick classFields.addInt(LongTy, 0);
1807e5dd7070Spatrick // unsigned long info;
1808e5dd7070Spatrick // !objc_class_flag_meta
1809e5dd7070Spatrick classFields.addInt(LongTy, 0);
1810e5dd7070Spatrick // long instance_size;
1811e5dd7070Spatrick int superInstanceSize = !SuperClassDecl ? 0 :
1812e5dd7070Spatrick Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
1813e5dd7070Spatrick // Instance size is negative for classes that have not yet had their ivar
1814e5dd7070Spatrick // layout calculated.
1815e5dd7070Spatrick classFields.addInt(LongTy,
1816e5dd7070Spatrick 0 - (Context.getASTObjCImplementationLayout(OID).getSize().getQuantity() -
1817e5dd7070Spatrick superInstanceSize));
1818e5dd7070Spatrick
1819e5dd7070Spatrick if (classDecl->all_declared_ivar_begin() == nullptr)
1820e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1821e5dd7070Spatrick else {
1822e5dd7070Spatrick int ivar_count = 0;
1823e5dd7070Spatrick for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD;
1824e5dd7070Spatrick IVD = IVD->getNextIvar()) ivar_count++;
1825e5dd7070Spatrick llvm::DataLayout td(&TheModule);
1826e5dd7070Spatrick // struct objc_ivar_list *ivars;
1827e5dd7070Spatrick ConstantInitBuilder b(CGM);
1828e5dd7070Spatrick auto ivarListBuilder = b.beginStruct();
1829e5dd7070Spatrick // int count;
1830e5dd7070Spatrick ivarListBuilder.addInt(IntTy, ivar_count);
1831e5dd7070Spatrick // size_t size;
1832e5dd7070Spatrick llvm::StructType *ObjCIvarTy = llvm::StructType::get(
1833e5dd7070Spatrick PtrToInt8Ty,
1834e5dd7070Spatrick PtrToInt8Ty,
1835e5dd7070Spatrick PtrToInt8Ty,
1836e5dd7070Spatrick Int32Ty,
1837e5dd7070Spatrick Int32Ty);
1838e5dd7070Spatrick ivarListBuilder.addInt(SizeTy, td.getTypeSizeInBits(ObjCIvarTy) /
1839e5dd7070Spatrick CGM.getContext().getCharWidth());
1840e5dd7070Spatrick // struct objc_ivar ivars[]
1841e5dd7070Spatrick auto ivarArrayBuilder = ivarListBuilder.beginArray();
1842e5dd7070Spatrick for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD;
1843e5dd7070Spatrick IVD = IVD->getNextIvar()) {
1844e5dd7070Spatrick auto ivarTy = IVD->getType();
1845e5dd7070Spatrick auto ivarBuilder = ivarArrayBuilder.beginStruct();
1846e5dd7070Spatrick // const char *name;
1847e5dd7070Spatrick ivarBuilder.add(MakeConstantString(IVD->getNameAsString()));
1848e5dd7070Spatrick // const char *type;
1849e5dd7070Spatrick std::string TypeStr;
1850e5dd7070Spatrick //Context.getObjCEncodingForType(ivarTy, TypeStr, IVD, true);
1851e5dd7070Spatrick Context.getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, ivarTy, TypeStr, true);
1852e5dd7070Spatrick ivarBuilder.add(MakeConstantString(TypeStr));
1853e5dd7070Spatrick // int *offset;
1854e5dd7070Spatrick uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
1855e5dd7070Spatrick uint64_t Offset = BaseOffset - superInstanceSize;
1856e5dd7070Spatrick llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
1857e5dd7070Spatrick std::string OffsetName = GetIVarOffsetVariableName(classDecl, IVD);
1858e5dd7070Spatrick llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
1859e5dd7070Spatrick if (OffsetVar)
1860e5dd7070Spatrick OffsetVar->setInitializer(OffsetValue);
1861e5dd7070Spatrick else
1862e5dd7070Spatrick OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
1863e5dd7070Spatrick false, llvm::GlobalValue::ExternalLinkage,
1864e5dd7070Spatrick OffsetValue, OffsetName);
1865e5dd7070Spatrick auto ivarVisibility =
1866e5dd7070Spatrick (IVD->getAccessControl() == ObjCIvarDecl::Private ||
1867e5dd7070Spatrick IVD->getAccessControl() == ObjCIvarDecl::Package ||
1868e5dd7070Spatrick classDecl->getVisibility() == HiddenVisibility) ?
1869e5dd7070Spatrick llvm::GlobalValue::HiddenVisibility :
1870e5dd7070Spatrick llvm::GlobalValue::DefaultVisibility;
1871e5dd7070Spatrick OffsetVar->setVisibility(ivarVisibility);
1872e5dd7070Spatrick ivarBuilder.add(OffsetVar);
1873e5dd7070Spatrick // Ivar size
1874e5dd7070Spatrick ivarBuilder.addInt(Int32Ty,
1875e5dd7070Spatrick CGM.getContext().getTypeSizeInChars(ivarTy).getQuantity());
1876e5dd7070Spatrick // Alignment will be stored as a base-2 log of the alignment.
1877e5dd7070Spatrick unsigned align =
1878e5dd7070Spatrick llvm::Log2_32(Context.getTypeAlignInChars(ivarTy).getQuantity());
1879e5dd7070Spatrick // Objects that require more than 2^64-byte alignment should be impossible!
1880e5dd7070Spatrick assert(align < 64);
1881e5dd7070Spatrick // uint32_t flags;
1882e5dd7070Spatrick // Bits 0-1 are ownership.
1883e5dd7070Spatrick // Bit 2 indicates an extended type encoding
1884e5dd7070Spatrick // Bits 3-8 contain log2(aligment)
1885e5dd7070Spatrick ivarBuilder.addInt(Int32Ty,
1886e5dd7070Spatrick (align << 3) | (1<<2) |
1887e5dd7070Spatrick FlagsForOwnership(ivarTy.getQualifiers().getObjCLifetime()));
1888e5dd7070Spatrick ivarBuilder.finishAndAddTo(ivarArrayBuilder);
1889e5dd7070Spatrick }
1890e5dd7070Spatrick ivarArrayBuilder.finishAndAddTo(ivarListBuilder);
1891e5dd7070Spatrick auto ivarList = ivarListBuilder.finishAndCreateGlobal(".objc_ivar_list",
1892e5dd7070Spatrick CGM.getPointerAlign(), /*constant*/ false,
1893e5dd7070Spatrick llvm::GlobalValue::PrivateLinkage);
1894e5dd7070Spatrick classFields.add(ivarList);
1895e5dd7070Spatrick }
1896e5dd7070Spatrick // struct objc_method_list *methods
1897e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> InstanceMethods;
1898e5dd7070Spatrick InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(),
1899e5dd7070Spatrick OID->instmeth_end());
1900e5dd7070Spatrick for (auto *propImpl : OID->property_impls())
1901e5dd7070Spatrick if (propImpl->getPropertyImplementation() ==
1902e5dd7070Spatrick ObjCPropertyImplDecl::Synthesize) {
1903e5dd7070Spatrick auto addIfExists = [&](const ObjCMethodDecl *OMD) {
1904e5dd7070Spatrick if (OMD && OMD->hasBody())
1905e5dd7070Spatrick InstanceMethods.push_back(OMD);
1906e5dd7070Spatrick };
1907e5dd7070Spatrick addIfExists(propImpl->getGetterMethodDecl());
1908e5dd7070Spatrick addIfExists(propImpl->getSetterMethodDecl());
1909e5dd7070Spatrick }
1910e5dd7070Spatrick
1911e5dd7070Spatrick if (InstanceMethods.size() == 0)
1912e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1913e5dd7070Spatrick else
1914e5dd7070Spatrick classFields.addBitCast(
1915e5dd7070Spatrick GenerateMethodList(className, "", InstanceMethods, false),
1916e5dd7070Spatrick PtrTy);
1917e5dd7070Spatrick // void *dtable;
1918e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1919e5dd7070Spatrick // IMP cxx_construct;
1920e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1921e5dd7070Spatrick // IMP cxx_destruct;
1922e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1923e5dd7070Spatrick // struct objc_class *subclass_list
1924e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1925e5dd7070Spatrick // struct objc_class *sibling_class
1926e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1927e5dd7070Spatrick // struct objc_protocol_list *protocols;
1928a9ac8606Spatrick auto RuntimeProtocols = GetRuntimeProtocolList(classDecl->protocol_begin(),
1929a9ac8606Spatrick classDecl->protocol_end());
1930e5dd7070Spatrick SmallVector<llvm::Constant *, 16> Protocols;
1931a9ac8606Spatrick for (const auto *I : RuntimeProtocols)
1932e5dd7070Spatrick Protocols.push_back(
1933e5dd7070Spatrick llvm::ConstantExpr::getBitCast(GenerateProtocolRef(I),
1934e5dd7070Spatrick ProtocolPtrTy));
1935e5dd7070Spatrick if (Protocols.empty())
1936e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1937e5dd7070Spatrick else
1938e5dd7070Spatrick classFields.add(GenerateProtocolList(Protocols));
1939e5dd7070Spatrick // struct reference_list *extra_data;
1940e5dd7070Spatrick classFields.addNullPointer(PtrTy);
1941e5dd7070Spatrick // long abi_version;
1942e5dd7070Spatrick classFields.addInt(LongTy, 0);
1943e5dd7070Spatrick // struct objc_property_list *properties
1944e5dd7070Spatrick classFields.add(GeneratePropertyList(OID, classDecl));
1945e5dd7070Spatrick
1946a9ac8606Spatrick llvm::GlobalVariable *classStruct =
1947e5dd7070Spatrick classFields.finishAndCreateGlobal(SymbolForClass(className),
1948e5dd7070Spatrick CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage);
1949e5dd7070Spatrick
1950e5dd7070Spatrick auto *classRefSymbol = GetClassVar(className);
1951e5dd7070Spatrick classRefSymbol->setSection(sectionName<ClassReferenceSection>());
1952e5dd7070Spatrick classRefSymbol->setInitializer(llvm::ConstantExpr::getBitCast(classStruct, IdTy));
1953e5dd7070Spatrick
1954e5dd7070Spatrick if (IsCOFF) {
1955e5dd7070Spatrick // we can't import a class struct.
1956e5dd7070Spatrick if (OID->getClassInterface()->hasAttr<DLLExportAttr>()) {
1957a9ac8606Spatrick classStruct->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1958e5dd7070Spatrick cast<llvm::GlobalValue>(classRefSymbol)->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1959e5dd7070Spatrick }
1960e5dd7070Spatrick
1961e5dd7070Spatrick if (SuperClass) {
1962a9ac8606Spatrick std::pair<llvm::GlobalVariable*, int> v{classStruct, 1};
1963ec727ea7Spatrick EarlyInitList.emplace_back(std::string(SuperClass->getName()),
1964ec727ea7Spatrick std::move(v));
1965e5dd7070Spatrick }
1966e5dd7070Spatrick
1967e5dd7070Spatrick }
1968e5dd7070Spatrick
1969e5dd7070Spatrick
1970e5dd7070Spatrick // Resolve the class aliases, if they exist.
1971e5dd7070Spatrick // FIXME: Class pointer aliases shouldn't exist!
1972e5dd7070Spatrick if (ClassPtrAlias) {
1973e5dd7070Spatrick ClassPtrAlias->replaceAllUsesWith(
1974e5dd7070Spatrick llvm::ConstantExpr::getBitCast(classStruct, IdTy));
1975e5dd7070Spatrick ClassPtrAlias->eraseFromParent();
1976e5dd7070Spatrick ClassPtrAlias = nullptr;
1977e5dd7070Spatrick }
1978e5dd7070Spatrick if (auto Placeholder =
1979e5dd7070Spatrick TheModule.getNamedGlobal(SymbolForClass(className)))
1980e5dd7070Spatrick if (Placeholder != classStruct) {
1981e5dd7070Spatrick Placeholder->replaceAllUsesWith(
1982e5dd7070Spatrick llvm::ConstantExpr::getBitCast(classStruct, Placeholder->getType()));
1983e5dd7070Spatrick Placeholder->eraseFromParent();
1984e5dd7070Spatrick classStruct->setName(SymbolForClass(className));
1985e5dd7070Spatrick }
1986e5dd7070Spatrick if (MetaClassPtrAlias) {
1987e5dd7070Spatrick MetaClassPtrAlias->replaceAllUsesWith(
1988e5dd7070Spatrick llvm::ConstantExpr::getBitCast(metaclass, IdTy));
1989e5dd7070Spatrick MetaClassPtrAlias->eraseFromParent();
1990e5dd7070Spatrick MetaClassPtrAlias = nullptr;
1991e5dd7070Spatrick }
1992e5dd7070Spatrick assert(classStruct->getName() == SymbolForClass(className));
1993e5dd7070Spatrick
1994e5dd7070Spatrick auto classInitRef = new llvm::GlobalVariable(TheModule,
1995e5dd7070Spatrick classStruct->getType(), false, llvm::GlobalValue::ExternalLinkage,
1996e5dd7070Spatrick classStruct, ManglePublicSymbol("OBJC_INIT_CLASS_") + className);
1997e5dd7070Spatrick classInitRef->setSection(sectionName<ClassSection>());
1998e5dd7070Spatrick CGM.addUsedGlobal(classInitRef);
1999e5dd7070Spatrick
2000e5dd7070Spatrick EmittedClass = true;
2001e5dd7070Spatrick }
2002e5dd7070Spatrick public:
CGObjCGNUstep2(CodeGenModule & Mod)2003e5dd7070Spatrick CGObjCGNUstep2(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 10, 4, 2) {
2004e5dd7070Spatrick MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
2005e5dd7070Spatrick PtrToObjCSuperTy, SelectorTy);
2006e5dd7070Spatrick // struct objc_property
2007e5dd7070Spatrick // {
2008e5dd7070Spatrick // const char *name;
2009e5dd7070Spatrick // const char *attributes;
2010e5dd7070Spatrick // const char *type;
2011e5dd7070Spatrick // SEL getter;
2012e5dd7070Spatrick // SEL setter;
2013e5dd7070Spatrick // }
2014e5dd7070Spatrick PropertyMetadataTy =
2015e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(),
2016e5dd7070Spatrick { PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty });
2017e5dd7070Spatrick }
2018e5dd7070Spatrick
2019e5dd7070Spatrick };
2020e5dd7070Spatrick
2021e5dd7070Spatrick const char *const CGObjCGNUstep2::SectionsBaseNames[8] =
2022e5dd7070Spatrick {
2023e5dd7070Spatrick "__objc_selectors",
2024e5dd7070Spatrick "__objc_classes",
2025e5dd7070Spatrick "__objc_class_refs",
2026e5dd7070Spatrick "__objc_cats",
2027e5dd7070Spatrick "__objc_protocols",
2028e5dd7070Spatrick "__objc_protocol_refs",
2029e5dd7070Spatrick "__objc_class_aliases",
2030e5dd7070Spatrick "__objc_constant_string"
2031e5dd7070Spatrick };
2032e5dd7070Spatrick
2033e5dd7070Spatrick const char *const CGObjCGNUstep2::PECOFFSectionsBaseNames[8] =
2034e5dd7070Spatrick {
2035e5dd7070Spatrick ".objcrt$SEL",
2036e5dd7070Spatrick ".objcrt$CLS",
2037e5dd7070Spatrick ".objcrt$CLR",
2038e5dd7070Spatrick ".objcrt$CAT",
2039e5dd7070Spatrick ".objcrt$PCL",
2040e5dd7070Spatrick ".objcrt$PCR",
2041e5dd7070Spatrick ".objcrt$CAL",
2042e5dd7070Spatrick ".objcrt$STR"
2043e5dd7070Spatrick };
2044e5dd7070Spatrick
2045e5dd7070Spatrick /// Support for the ObjFW runtime.
2046e5dd7070Spatrick class CGObjCObjFW: public CGObjCGNU {
2047e5dd7070Spatrick protected:
2048e5dd7070Spatrick /// The GCC ABI message lookup function. Returns an IMP pointing to the
2049e5dd7070Spatrick /// method implementation for this message.
2050e5dd7070Spatrick LazyRuntimeFunction MsgLookupFn;
2051e5dd7070Spatrick /// stret lookup function. While this does not seem to make sense at the
2052e5dd7070Spatrick /// first look, this is required to call the correct forwarding function.
2053e5dd7070Spatrick LazyRuntimeFunction MsgLookupFnSRet;
2054e5dd7070Spatrick /// The GCC ABI superclass message lookup function. Takes a pointer to a
2055e5dd7070Spatrick /// structure describing the receiver and the class, and a selector as
2056e5dd7070Spatrick /// arguments. Returns the IMP for the corresponding method.
2057e5dd7070Spatrick LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet;
2058e5dd7070Spatrick
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)2059e5dd7070Spatrick llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
2060e5dd7070Spatrick llvm::Value *cmd, llvm::MDNode *node,
2061e5dd7070Spatrick MessageSendInfo &MSI) override {
2062e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
2063e5dd7070Spatrick llvm::Value *args[] = {
2064e5dd7070Spatrick EnforceType(Builder, Receiver, IdTy),
2065e5dd7070Spatrick EnforceType(Builder, cmd, SelectorTy) };
2066e5dd7070Spatrick
2067e5dd7070Spatrick llvm::CallBase *imp;
2068e5dd7070Spatrick if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
2069e5dd7070Spatrick imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
2070e5dd7070Spatrick else
2071e5dd7070Spatrick imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
2072e5dd7070Spatrick
2073e5dd7070Spatrick imp->setMetadata(msgSendMDKind, node);
2074e5dd7070Spatrick return imp;
2075e5dd7070Spatrick }
2076e5dd7070Spatrick
LookupIMPSuper(CodeGenFunction & CGF,Address ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)2077e5dd7070Spatrick llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
2078e5dd7070Spatrick llvm::Value *cmd, MessageSendInfo &MSI) override {
2079e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
2080e5dd7070Spatrick llvm::Value *lookupArgs[] = {
2081e5dd7070Spatrick EnforceType(Builder, ObjCSuper.getPointer(), PtrToObjCSuperTy), cmd,
2082e5dd7070Spatrick };
2083e5dd7070Spatrick
2084e5dd7070Spatrick if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
2085e5dd7070Spatrick return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
2086e5dd7070Spatrick else
2087e5dd7070Spatrick return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
2088e5dd7070Spatrick }
2089e5dd7070Spatrick
GetClassNamed(CodeGenFunction & CGF,const std::string & Name,bool isWeak)2090e5dd7070Spatrick llvm::Value *GetClassNamed(CodeGenFunction &CGF, const std::string &Name,
2091e5dd7070Spatrick bool isWeak) override {
2092e5dd7070Spatrick if (isWeak)
2093e5dd7070Spatrick return CGObjCGNU::GetClassNamed(CGF, Name, isWeak);
2094e5dd7070Spatrick
2095e5dd7070Spatrick EmitClassRef(Name);
2096e5dd7070Spatrick std::string SymbolName = "_OBJC_CLASS_" + Name;
2097e5dd7070Spatrick llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
2098e5dd7070Spatrick if (!ClassSymbol)
2099e5dd7070Spatrick ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
2100e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage,
2101e5dd7070Spatrick nullptr, SymbolName);
2102e5dd7070Spatrick return ClassSymbol;
2103e5dd7070Spatrick }
2104e5dd7070Spatrick
2105e5dd7070Spatrick public:
CGObjCObjFW(CodeGenModule & Mod)2106e5dd7070Spatrick CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
2107e5dd7070Spatrick // IMP objc_msg_lookup(id, SEL);
2108e5dd7070Spatrick MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy);
2109e5dd7070Spatrick MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
2110e5dd7070Spatrick SelectorTy);
2111e5dd7070Spatrick // IMP objc_msg_lookup_super(struct objc_super*, SEL);
2112e5dd7070Spatrick MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
2113e5dd7070Spatrick PtrToObjCSuperTy, SelectorTy);
2114e5dd7070Spatrick MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
2115e5dd7070Spatrick PtrToObjCSuperTy, SelectorTy);
2116e5dd7070Spatrick }
2117e5dd7070Spatrick };
2118e5dd7070Spatrick } // end anonymous namespace
2119e5dd7070Spatrick
2120e5dd7070Spatrick /// Emits a reference to a dummy variable which is emitted with each class.
2121e5dd7070Spatrick /// This ensures that a linker error will be generated when trying to link
2122e5dd7070Spatrick /// together modules where a referenced class is not defined.
EmitClassRef(const std::string & className)2123e5dd7070Spatrick void CGObjCGNU::EmitClassRef(const std::string &className) {
2124e5dd7070Spatrick std::string symbolRef = "__objc_class_ref_" + className;
2125e5dd7070Spatrick // Don't emit two copies of the same symbol
2126e5dd7070Spatrick if (TheModule.getGlobalVariable(symbolRef))
2127e5dd7070Spatrick return;
2128e5dd7070Spatrick std::string symbolName = "__objc_class_name_" + className;
2129e5dd7070Spatrick llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
2130e5dd7070Spatrick if (!ClassSymbol) {
2131e5dd7070Spatrick ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
2132e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage,
2133e5dd7070Spatrick nullptr, symbolName);
2134e5dd7070Spatrick }
2135e5dd7070Spatrick new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
2136e5dd7070Spatrick llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
2137e5dd7070Spatrick }
2138e5dd7070Spatrick
CGObjCGNU(CodeGenModule & cgm,unsigned runtimeABIVersion,unsigned protocolClassVersion,unsigned classABI)2139e5dd7070Spatrick CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
2140e5dd7070Spatrick unsigned protocolClassVersion, unsigned classABI)
2141e5dd7070Spatrick : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
2142e5dd7070Spatrick VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr),
2143e5dd7070Spatrick MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion),
2144e5dd7070Spatrick ProtocolVersion(protocolClassVersion), ClassABIVersion(classABI) {
2145e5dd7070Spatrick
2146e5dd7070Spatrick msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
2147e5dd7070Spatrick usesSEHExceptions =
2148e5dd7070Spatrick cgm.getContext().getTargetInfo().getTriple().isWindowsMSVCEnvironment();
2149e5dd7070Spatrick
2150e5dd7070Spatrick CodeGenTypes &Types = CGM.getTypes();
2151e5dd7070Spatrick IntTy = cast<llvm::IntegerType>(
2152e5dd7070Spatrick Types.ConvertType(CGM.getContext().IntTy));
2153e5dd7070Spatrick LongTy = cast<llvm::IntegerType>(
2154e5dd7070Spatrick Types.ConvertType(CGM.getContext().LongTy));
2155e5dd7070Spatrick SizeTy = cast<llvm::IntegerType>(
2156e5dd7070Spatrick Types.ConvertType(CGM.getContext().getSizeType()));
2157e5dd7070Spatrick PtrDiffTy = cast<llvm::IntegerType>(
2158e5dd7070Spatrick Types.ConvertType(CGM.getContext().getPointerDiffType()));
2159e5dd7070Spatrick BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
2160e5dd7070Spatrick
2161e5dd7070Spatrick Int8Ty = llvm::Type::getInt8Ty(VMContext);
2162e5dd7070Spatrick // C string type. Used in lots of places.
2163e5dd7070Spatrick PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
2164e5dd7070Spatrick ProtocolPtrTy = llvm::PointerType::getUnqual(
2165e5dd7070Spatrick Types.ConvertType(CGM.getContext().getObjCProtoType()));
2166e5dd7070Spatrick
2167e5dd7070Spatrick Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
2168e5dd7070Spatrick Zeros[1] = Zeros[0];
2169e5dd7070Spatrick NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
2170e5dd7070Spatrick // Get the selector Type.
2171e5dd7070Spatrick QualType selTy = CGM.getContext().getObjCSelType();
2172e5dd7070Spatrick if (QualType() == selTy) {
2173e5dd7070Spatrick SelectorTy = PtrToInt8Ty;
2174*12c85518Srobert SelectorElemTy = Int8Ty;
2175e5dd7070Spatrick } else {
2176e5dd7070Spatrick SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
2177*12c85518Srobert SelectorElemTy = CGM.getTypes().ConvertTypeForMem(selTy->getPointeeType());
2178e5dd7070Spatrick }
2179e5dd7070Spatrick
2180e5dd7070Spatrick PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
2181e5dd7070Spatrick PtrTy = PtrToInt8Ty;
2182e5dd7070Spatrick
2183e5dd7070Spatrick Int32Ty = llvm::Type::getInt32Ty(VMContext);
2184e5dd7070Spatrick Int64Ty = llvm::Type::getInt64Ty(VMContext);
2185e5dd7070Spatrick
2186e5dd7070Spatrick IntPtrTy =
2187e5dd7070Spatrick CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty;
2188e5dd7070Spatrick
2189e5dd7070Spatrick // Object type
2190e5dd7070Spatrick QualType UnqualIdTy = CGM.getContext().getObjCIdType();
2191e5dd7070Spatrick ASTIdTy = CanQualType();
2192e5dd7070Spatrick if (UnqualIdTy != QualType()) {
2193e5dd7070Spatrick ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
2194e5dd7070Spatrick IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
2195*12c85518Srobert IdElemTy = CGM.getTypes().ConvertTypeForMem(
2196*12c85518Srobert ASTIdTy.getTypePtr()->getPointeeType());
2197e5dd7070Spatrick } else {
2198e5dd7070Spatrick IdTy = PtrToInt8Ty;
2199*12c85518Srobert IdElemTy = Int8Ty;
2200e5dd7070Spatrick }
2201e5dd7070Spatrick PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
2202e5dd7070Spatrick ProtocolTy = llvm::StructType::get(IdTy,
2203e5dd7070Spatrick PtrToInt8Ty, // name
2204e5dd7070Spatrick PtrToInt8Ty, // protocols
2205e5dd7070Spatrick PtrToInt8Ty, // instance methods
2206e5dd7070Spatrick PtrToInt8Ty, // class methods
2207e5dd7070Spatrick PtrToInt8Ty, // optional instance methods
2208e5dd7070Spatrick PtrToInt8Ty, // optional class methods
2209e5dd7070Spatrick PtrToInt8Ty, // properties
2210e5dd7070Spatrick PtrToInt8Ty);// optional properties
2211e5dd7070Spatrick
2212e5dd7070Spatrick // struct objc_property_gsv1
2213e5dd7070Spatrick // {
2214e5dd7070Spatrick // const char *name;
2215e5dd7070Spatrick // char attributes;
2216e5dd7070Spatrick // char attributes2;
2217e5dd7070Spatrick // char unused1;
2218e5dd7070Spatrick // char unused2;
2219e5dd7070Spatrick // const char *getter_name;
2220e5dd7070Spatrick // const char *getter_types;
2221e5dd7070Spatrick // const char *setter_name;
2222e5dd7070Spatrick // const char *setter_types;
2223e5dd7070Spatrick // }
2224e5dd7070Spatrick PropertyMetadataTy = llvm::StructType::get(CGM.getLLVMContext(), {
2225e5dd7070Spatrick PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty,
2226e5dd7070Spatrick PtrToInt8Ty, PtrToInt8Ty });
2227e5dd7070Spatrick
2228e5dd7070Spatrick ObjCSuperTy = llvm::StructType::get(IdTy, IdTy);
2229e5dd7070Spatrick PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
2230e5dd7070Spatrick
2231e5dd7070Spatrick llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
2232e5dd7070Spatrick
2233e5dd7070Spatrick // void objc_exception_throw(id);
2234e5dd7070Spatrick ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
2235e5dd7070Spatrick ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
2236e5dd7070Spatrick // int objc_sync_enter(id);
2237e5dd7070Spatrick SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
2238e5dd7070Spatrick // int objc_sync_exit(id);
2239e5dd7070Spatrick SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy);
2240e5dd7070Spatrick
2241e5dd7070Spatrick // void objc_enumerationMutation (id)
2242e5dd7070Spatrick EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, IdTy);
2243e5dd7070Spatrick
2244e5dd7070Spatrick // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
2245e5dd7070Spatrick GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
2246e5dd7070Spatrick PtrDiffTy, BoolTy);
2247e5dd7070Spatrick // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
2248e5dd7070Spatrick SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
2249e5dd7070Spatrick PtrDiffTy, IdTy, BoolTy, BoolTy);
2250e5dd7070Spatrick // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
2251e5dd7070Spatrick GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
2252e5dd7070Spatrick PtrDiffTy, BoolTy, BoolTy);
2253e5dd7070Spatrick // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
2254e5dd7070Spatrick SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
2255e5dd7070Spatrick PtrDiffTy, BoolTy, BoolTy);
2256e5dd7070Spatrick
2257e5dd7070Spatrick // IMP type
2258e5dd7070Spatrick llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
2259e5dd7070Spatrick IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
2260e5dd7070Spatrick true));
2261e5dd7070Spatrick
2262e5dd7070Spatrick const LangOptions &Opts = CGM.getLangOpts();
2263e5dd7070Spatrick if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
2264e5dd7070Spatrick RuntimeVersion = 10;
2265e5dd7070Spatrick
2266e5dd7070Spatrick // Don't bother initialising the GC stuff unless we're compiling in GC mode
2267e5dd7070Spatrick if (Opts.getGC() != LangOptions::NonGC) {
2268e5dd7070Spatrick // This is a bit of an hack. We should sort this out by having a proper
2269e5dd7070Spatrick // CGObjCGNUstep subclass for GC, but we may want to really support the old
2270e5dd7070Spatrick // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
2271e5dd7070Spatrick // Get selectors needed in GC mode
2272e5dd7070Spatrick RetainSel = GetNullarySelector("retain", CGM.getContext());
2273e5dd7070Spatrick ReleaseSel = GetNullarySelector("release", CGM.getContext());
2274e5dd7070Spatrick AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
2275e5dd7070Spatrick
2276e5dd7070Spatrick // Get functions needed in GC mode
2277e5dd7070Spatrick
2278e5dd7070Spatrick // id objc_assign_ivar(id, id, ptrdiff_t);
2279e5dd7070Spatrick IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy);
2280e5dd7070Spatrick // id objc_assign_strongCast (id, id*)
2281e5dd7070Spatrick StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
2282e5dd7070Spatrick PtrToIdTy);
2283e5dd7070Spatrick // id objc_assign_global(id, id*);
2284e5dd7070Spatrick GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy);
2285e5dd7070Spatrick // id objc_assign_weak(id, id*);
2286e5dd7070Spatrick WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy);
2287e5dd7070Spatrick // id objc_read_weak(id*);
2288e5dd7070Spatrick WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy);
2289e5dd7070Spatrick // void *objc_memmove_collectable(void*, void *, size_t);
2290e5dd7070Spatrick MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
2291e5dd7070Spatrick SizeTy);
2292e5dd7070Spatrick }
2293e5dd7070Spatrick }
2294e5dd7070Spatrick
GetClassNamed(CodeGenFunction & CGF,const std::string & Name,bool isWeak)2295e5dd7070Spatrick llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
2296e5dd7070Spatrick const std::string &Name, bool isWeak) {
2297e5dd7070Spatrick llvm::Constant *ClassName = MakeConstantString(Name);
2298e5dd7070Spatrick // With the incompatible ABI, this will need to be replaced with a direct
2299e5dd7070Spatrick // reference to the class symbol. For the compatible nonfragile ABI we are
2300e5dd7070Spatrick // still performing this lookup at run time but emitting the symbol for the
2301e5dd7070Spatrick // class externally so that we can make the switch later.
2302e5dd7070Spatrick //
2303e5dd7070Spatrick // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
2304e5dd7070Spatrick // with memoized versions or with static references if it's safe to do so.
2305e5dd7070Spatrick if (!isWeak)
2306e5dd7070Spatrick EmitClassRef(Name);
2307e5dd7070Spatrick
2308e5dd7070Spatrick llvm::FunctionCallee ClassLookupFn = CGM.CreateRuntimeFunction(
2309e5dd7070Spatrick llvm::FunctionType::get(IdTy, PtrToInt8Ty, true), "objc_lookup_class");
2310e5dd7070Spatrick return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName);
2311e5dd7070Spatrick }
2312e5dd7070Spatrick
2313e5dd7070Spatrick // This has to perform the lookup every time, since posing and related
2314e5dd7070Spatrick // techniques can modify the name -> class mapping.
GetClass(CodeGenFunction & CGF,const ObjCInterfaceDecl * OID)2315e5dd7070Spatrick llvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF,
2316e5dd7070Spatrick const ObjCInterfaceDecl *OID) {
2317e5dd7070Spatrick auto *Value =
2318e5dd7070Spatrick GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported());
2319e5dd7070Spatrick if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value))
2320e5dd7070Spatrick CGM.setGVProperties(ClassSymbol, OID);
2321e5dd7070Spatrick return Value;
2322e5dd7070Spatrick }
2323e5dd7070Spatrick
EmitNSAutoreleasePoolClassRef(CodeGenFunction & CGF)2324e5dd7070Spatrick llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
2325e5dd7070Spatrick auto *Value = GetClassNamed(CGF, "NSAutoreleasePool", false);
2326e5dd7070Spatrick if (CGM.getTriple().isOSBinFormatCOFF()) {
2327e5dd7070Spatrick if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) {
2328e5dd7070Spatrick IdentifierInfo &II = CGF.CGM.getContext().Idents.get("NSAutoreleasePool");
2329e5dd7070Spatrick TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
2330e5dd7070Spatrick DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
2331e5dd7070Spatrick
2332e5dd7070Spatrick const VarDecl *VD = nullptr;
2333a9ac8606Spatrick for (const auto *Result : DC->lookup(&II))
2334e5dd7070Spatrick if ((VD = dyn_cast<VarDecl>(Result)))
2335e5dd7070Spatrick break;
2336e5dd7070Spatrick
2337e5dd7070Spatrick CGM.setGVProperties(ClassSymbol, VD);
2338e5dd7070Spatrick }
2339e5dd7070Spatrick }
2340e5dd7070Spatrick return Value;
2341e5dd7070Spatrick }
2342e5dd7070Spatrick
GetTypedSelector(CodeGenFunction & CGF,Selector Sel,const std::string & TypeEncoding)2343e5dd7070Spatrick llvm::Value *CGObjCGNU::GetTypedSelector(CodeGenFunction &CGF, Selector Sel,
2344e5dd7070Spatrick const std::string &TypeEncoding) {
2345e5dd7070Spatrick SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel];
2346e5dd7070Spatrick llvm::GlobalAlias *SelValue = nullptr;
2347e5dd7070Spatrick
2348e5dd7070Spatrick for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
2349e5dd7070Spatrick e = Types.end() ; i!=e ; i++) {
2350e5dd7070Spatrick if (i->first == TypeEncoding) {
2351e5dd7070Spatrick SelValue = i->second;
2352e5dd7070Spatrick break;
2353e5dd7070Spatrick }
2354e5dd7070Spatrick }
2355e5dd7070Spatrick if (!SelValue) {
2356*12c85518Srobert SelValue = llvm::GlobalAlias::create(SelectorElemTy, 0,
2357*12c85518Srobert llvm::GlobalValue::PrivateLinkage,
2358*12c85518Srobert ".objc_selector_" + Sel.getAsString(),
2359*12c85518Srobert &TheModule);
2360e5dd7070Spatrick Types.emplace_back(TypeEncoding, SelValue);
2361e5dd7070Spatrick }
2362e5dd7070Spatrick
2363e5dd7070Spatrick return SelValue;
2364e5dd7070Spatrick }
2365e5dd7070Spatrick
GetAddrOfSelector(CodeGenFunction & CGF,Selector Sel)2366e5dd7070Spatrick Address CGObjCGNU::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) {
2367e5dd7070Spatrick llvm::Value *SelValue = GetSelector(CGF, Sel);
2368e5dd7070Spatrick
2369e5dd7070Spatrick // Store it to a temporary. Does this satisfy the semantics of
2370e5dd7070Spatrick // GetAddrOfSelector? Hopefully.
2371e5dd7070Spatrick Address tmp = CGF.CreateTempAlloca(SelValue->getType(),
2372e5dd7070Spatrick CGF.getPointerAlign());
2373e5dd7070Spatrick CGF.Builder.CreateStore(SelValue, tmp);
2374e5dd7070Spatrick return tmp;
2375e5dd7070Spatrick }
2376e5dd7070Spatrick
GetSelector(CodeGenFunction & CGF,Selector Sel)2377e5dd7070Spatrick llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) {
2378e5dd7070Spatrick return GetTypedSelector(CGF, Sel, std::string());
2379e5dd7070Spatrick }
2380e5dd7070Spatrick
GetSelector(CodeGenFunction & CGF,const ObjCMethodDecl * Method)2381e5dd7070Spatrick llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
2382e5dd7070Spatrick const ObjCMethodDecl *Method) {
2383e5dd7070Spatrick std::string SelTypes = CGM.getContext().getObjCEncodingForMethodDecl(Method);
2384e5dd7070Spatrick return GetTypedSelector(CGF, Method->getSelector(), SelTypes);
2385e5dd7070Spatrick }
2386e5dd7070Spatrick
GetEHType(QualType T)2387e5dd7070Spatrick llvm::Constant *CGObjCGNU::GetEHType(QualType T) {
2388e5dd7070Spatrick if (T->isObjCIdType() || T->isObjCQualifiedIdType()) {
2389e5dd7070Spatrick // With the old ABI, there was only one kind of catchall, which broke
2390e5dd7070Spatrick // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as
2391e5dd7070Spatrick // a pointer indicating object catchalls, and NULL to indicate real
2392e5dd7070Spatrick // catchalls
2393e5dd7070Spatrick if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2394e5dd7070Spatrick return MakeConstantString("@id");
2395e5dd7070Spatrick } else {
2396e5dd7070Spatrick return nullptr;
2397e5dd7070Spatrick }
2398e5dd7070Spatrick }
2399e5dd7070Spatrick
2400e5dd7070Spatrick // All other types should be Objective-C interface pointer types.
2401e5dd7070Spatrick const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>();
2402e5dd7070Spatrick assert(OPT && "Invalid @catch type.");
2403e5dd7070Spatrick const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface();
2404e5dd7070Spatrick assert(IDecl && "Invalid @catch type.");
2405e5dd7070Spatrick return MakeConstantString(IDecl->getIdentifier()->getName());
2406e5dd7070Spatrick }
2407e5dd7070Spatrick
GetEHType(QualType T)2408e5dd7070Spatrick llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
2409e5dd7070Spatrick if (usesSEHExceptions)
2410e5dd7070Spatrick return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
2411e5dd7070Spatrick
2412e5dd7070Spatrick if (!CGM.getLangOpts().CPlusPlus)
2413e5dd7070Spatrick return CGObjCGNU::GetEHType(T);
2414e5dd7070Spatrick
2415e5dd7070Spatrick // For Objective-C++, we want to provide the ability to catch both C++ and
2416e5dd7070Spatrick // Objective-C objects in the same function.
2417e5dd7070Spatrick
2418e5dd7070Spatrick // There's a particular fixed type info for 'id'.
2419e5dd7070Spatrick if (T->isObjCIdType() ||
2420e5dd7070Spatrick T->isObjCQualifiedIdType()) {
2421e5dd7070Spatrick llvm::Constant *IDEHType =
2422e5dd7070Spatrick CGM.getModule().getGlobalVariable("__objc_id_type_info");
2423e5dd7070Spatrick if (!IDEHType)
2424e5dd7070Spatrick IDEHType =
2425e5dd7070Spatrick new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
2426e5dd7070Spatrick false,
2427e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage,
2428e5dd7070Spatrick nullptr, "__objc_id_type_info");
2429e5dd7070Spatrick return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
2430e5dd7070Spatrick }
2431e5dd7070Spatrick
2432e5dd7070Spatrick const ObjCObjectPointerType *PT =
2433e5dd7070Spatrick T->getAs<ObjCObjectPointerType>();
2434e5dd7070Spatrick assert(PT && "Invalid @catch type.");
2435e5dd7070Spatrick const ObjCInterfaceType *IT = PT->getInterfaceType();
2436e5dd7070Spatrick assert(IT && "Invalid @catch type.");
2437ec727ea7Spatrick std::string className =
2438ec727ea7Spatrick std::string(IT->getDecl()->getIdentifier()->getName());
2439e5dd7070Spatrick
2440e5dd7070Spatrick std::string typeinfoName = "__objc_eh_typeinfo_" + className;
2441e5dd7070Spatrick
2442e5dd7070Spatrick // Return the existing typeinfo if it exists
2443e5dd7070Spatrick llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
2444e5dd7070Spatrick if (typeinfo)
2445e5dd7070Spatrick return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
2446e5dd7070Spatrick
2447e5dd7070Spatrick // Otherwise create it.
2448e5dd7070Spatrick
2449e5dd7070Spatrick // vtable for gnustep::libobjc::__objc_class_type_info
2450e5dd7070Spatrick // It's quite ugly hard-coding this. Ideally we'd generate it using the host
2451e5dd7070Spatrick // platform's name mangling.
2452e5dd7070Spatrick const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
2453e5dd7070Spatrick auto *Vtable = TheModule.getGlobalVariable(vtableName);
2454e5dd7070Spatrick if (!Vtable) {
2455e5dd7070Spatrick Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
2456e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage,
2457e5dd7070Spatrick nullptr, vtableName);
2458e5dd7070Spatrick }
2459e5dd7070Spatrick llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
2460e5dd7070Spatrick auto *BVtable = llvm::ConstantExpr::getBitCast(
2461e5dd7070Spatrick llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two),
2462e5dd7070Spatrick PtrToInt8Ty);
2463e5dd7070Spatrick
2464e5dd7070Spatrick llvm::Constant *typeName =
2465e5dd7070Spatrick ExportUniqueString(className, "__objc_eh_typename_");
2466e5dd7070Spatrick
2467e5dd7070Spatrick ConstantInitBuilder builder(CGM);
2468e5dd7070Spatrick auto fields = builder.beginStruct();
2469e5dd7070Spatrick fields.add(BVtable);
2470e5dd7070Spatrick fields.add(typeName);
2471e5dd7070Spatrick llvm::Constant *TI =
2472e5dd7070Spatrick fields.finishAndCreateGlobal("__objc_eh_typeinfo_" + className,
2473e5dd7070Spatrick CGM.getPointerAlign(),
2474e5dd7070Spatrick /*constant*/ false,
2475e5dd7070Spatrick llvm::GlobalValue::LinkOnceODRLinkage);
2476e5dd7070Spatrick return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
2477e5dd7070Spatrick }
2478e5dd7070Spatrick
2479e5dd7070Spatrick /// Generate an NSConstantString object.
GenerateConstantString(const StringLiteral * SL)2480e5dd7070Spatrick ConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
2481e5dd7070Spatrick
2482e5dd7070Spatrick std::string Str = SL->getString().str();
2483e5dd7070Spatrick CharUnits Align = CGM.getPointerAlign();
2484e5dd7070Spatrick
2485e5dd7070Spatrick // Look for an existing one
2486e5dd7070Spatrick llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
2487e5dd7070Spatrick if (old != ObjCStrings.end())
2488*12c85518Srobert return ConstantAddress(old->getValue(), Int8Ty, Align);
2489e5dd7070Spatrick
2490e5dd7070Spatrick StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
2491e5dd7070Spatrick
2492e5dd7070Spatrick if (StringClass.empty()) StringClass = "NSConstantString";
2493e5dd7070Spatrick
2494e5dd7070Spatrick std::string Sym = "_OBJC_CLASS_";
2495e5dd7070Spatrick Sym += StringClass;
2496e5dd7070Spatrick
2497e5dd7070Spatrick llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
2498e5dd7070Spatrick
2499e5dd7070Spatrick if (!isa)
2500e5dd7070Spatrick isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
2501e5dd7070Spatrick llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym);
2502e5dd7070Spatrick else if (isa->getType() != PtrToIdTy)
2503e5dd7070Spatrick isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
2504e5dd7070Spatrick
2505e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
2506e5dd7070Spatrick auto Fields = Builder.beginStruct();
2507e5dd7070Spatrick Fields.add(isa);
2508e5dd7070Spatrick Fields.add(MakeConstantString(Str));
2509e5dd7070Spatrick Fields.addInt(IntTy, Str.size());
2510e5dd7070Spatrick llvm::Constant *ObjCStr =
2511e5dd7070Spatrick Fields.finishAndCreateGlobal(".objc_str", Align);
2512e5dd7070Spatrick ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
2513e5dd7070Spatrick ObjCStrings[Str] = ObjCStr;
2514e5dd7070Spatrick ConstantStrings.push_back(ObjCStr);
2515*12c85518Srobert return ConstantAddress(ObjCStr, Int8Ty, Align);
2516e5dd7070Spatrick }
2517e5dd7070Spatrick
2518e5dd7070Spatrick ///Generates a message send where the super is the receiver. This is a message
2519e5dd7070Spatrick ///send to self with special delivery semantics indicating which class's method
2520e5dd7070Spatrick ///should be called.
2521e5dd7070Spatrick RValue
GenerateMessageSendSuper(CodeGenFunction & CGF,ReturnValueSlot Return,QualType ResultType,Selector Sel,const ObjCInterfaceDecl * Class,bool isCategoryImpl,llvm::Value * Receiver,bool IsClassMessage,const CallArgList & CallArgs,const ObjCMethodDecl * Method)2522e5dd7070Spatrick CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
2523e5dd7070Spatrick ReturnValueSlot Return,
2524e5dd7070Spatrick QualType ResultType,
2525e5dd7070Spatrick Selector Sel,
2526e5dd7070Spatrick const ObjCInterfaceDecl *Class,
2527e5dd7070Spatrick bool isCategoryImpl,
2528e5dd7070Spatrick llvm::Value *Receiver,
2529e5dd7070Spatrick bool IsClassMessage,
2530e5dd7070Spatrick const CallArgList &CallArgs,
2531e5dd7070Spatrick const ObjCMethodDecl *Method) {
2532e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
2533e5dd7070Spatrick if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
2534e5dd7070Spatrick if (Sel == RetainSel || Sel == AutoreleaseSel) {
2535e5dd7070Spatrick return RValue::get(EnforceType(Builder, Receiver,
2536e5dd7070Spatrick CGM.getTypes().ConvertType(ResultType)));
2537e5dd7070Spatrick }
2538e5dd7070Spatrick if (Sel == ReleaseSel) {
2539e5dd7070Spatrick return RValue::get(nullptr);
2540e5dd7070Spatrick }
2541e5dd7070Spatrick }
2542e5dd7070Spatrick
2543e5dd7070Spatrick llvm::Value *cmd = GetSelector(CGF, Sel);
2544e5dd7070Spatrick CallArgList ActualArgs;
2545e5dd7070Spatrick
2546e5dd7070Spatrick ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
2547e5dd7070Spatrick ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
2548e5dd7070Spatrick ActualArgs.addFrom(CallArgs);
2549e5dd7070Spatrick
2550e5dd7070Spatrick MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
2551e5dd7070Spatrick
2552e5dd7070Spatrick llvm::Value *ReceiverClass = nullptr;
2553e5dd7070Spatrick bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2);
2554e5dd7070Spatrick if (isV2ABI) {
2555e5dd7070Spatrick ReceiverClass = GetClassNamed(CGF,
2556e5dd7070Spatrick Class->getSuperClass()->getNameAsString(), /*isWeak*/false);
2557e5dd7070Spatrick if (IsClassMessage) {
2558e5dd7070Spatrick // Load the isa pointer of the superclass is this is a class method.
2559e5dd7070Spatrick ReceiverClass = Builder.CreateBitCast(ReceiverClass,
2560e5dd7070Spatrick llvm::PointerType::getUnqual(IdTy));
2561e5dd7070Spatrick ReceiverClass =
2562a9ac8606Spatrick Builder.CreateAlignedLoad(IdTy, ReceiverClass, CGF.getPointerAlign());
2563e5dd7070Spatrick }
2564e5dd7070Spatrick ReceiverClass = EnforceType(Builder, ReceiverClass, IdTy);
2565e5dd7070Spatrick } else {
2566e5dd7070Spatrick if (isCategoryImpl) {
2567e5dd7070Spatrick llvm::FunctionCallee classLookupFunction = nullptr;
2568e5dd7070Spatrick if (IsClassMessage) {
2569e5dd7070Spatrick classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
2570e5dd7070Spatrick IdTy, PtrTy, true), "objc_get_meta_class");
2571e5dd7070Spatrick } else {
2572e5dd7070Spatrick classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
2573e5dd7070Spatrick IdTy, PtrTy, true), "objc_get_class");
2574e5dd7070Spatrick }
2575e5dd7070Spatrick ReceiverClass = Builder.CreateCall(classLookupFunction,
2576e5dd7070Spatrick MakeConstantString(Class->getNameAsString()));
2577e5dd7070Spatrick } else {
2578e5dd7070Spatrick // Set up global aliases for the metaclass or class pointer if they do not
2579e5dd7070Spatrick // already exist. These will are forward-references which will be set to
2580e5dd7070Spatrick // pointers to the class and metaclass structure created for the runtime
2581e5dd7070Spatrick // load function. To send a message to super, we look up the value of the
2582e5dd7070Spatrick // super_class pointer from either the class or metaclass structure.
2583e5dd7070Spatrick if (IsClassMessage) {
2584e5dd7070Spatrick if (!MetaClassPtrAlias) {
2585e5dd7070Spatrick MetaClassPtrAlias = llvm::GlobalAlias::create(
2586*12c85518Srobert IdElemTy, 0, llvm::GlobalValue::InternalLinkage,
2587e5dd7070Spatrick ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
2588e5dd7070Spatrick }
2589e5dd7070Spatrick ReceiverClass = MetaClassPtrAlias;
2590e5dd7070Spatrick } else {
2591e5dd7070Spatrick if (!ClassPtrAlias) {
2592e5dd7070Spatrick ClassPtrAlias = llvm::GlobalAlias::create(
2593*12c85518Srobert IdElemTy, 0, llvm::GlobalValue::InternalLinkage,
2594e5dd7070Spatrick ".objc_class_ref" + Class->getNameAsString(), &TheModule);
2595e5dd7070Spatrick }
2596e5dd7070Spatrick ReceiverClass = ClassPtrAlias;
2597e5dd7070Spatrick }
2598e5dd7070Spatrick }
2599e5dd7070Spatrick // Cast the pointer to a simplified version of the class structure
2600e5dd7070Spatrick llvm::Type *CastTy = llvm::StructType::get(IdTy, IdTy);
2601e5dd7070Spatrick ReceiverClass = Builder.CreateBitCast(ReceiverClass,
2602e5dd7070Spatrick llvm::PointerType::getUnqual(CastTy));
2603e5dd7070Spatrick // Get the superclass pointer
2604e5dd7070Spatrick ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1);
2605e5dd7070Spatrick // Load the superclass pointer
2606e5dd7070Spatrick ReceiverClass =
2607a9ac8606Spatrick Builder.CreateAlignedLoad(IdTy, ReceiverClass, CGF.getPointerAlign());
2608e5dd7070Spatrick }
2609e5dd7070Spatrick // Construct the structure used to look up the IMP
2610e5dd7070Spatrick llvm::StructType *ObjCSuperTy =
2611e5dd7070Spatrick llvm::StructType::get(Receiver->getType(), IdTy);
2612e5dd7070Spatrick
2613e5dd7070Spatrick Address ObjCSuper = CGF.CreateTempAlloca(ObjCSuperTy,
2614e5dd7070Spatrick CGF.getPointerAlign());
2615e5dd7070Spatrick
2616e5dd7070Spatrick Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
2617e5dd7070Spatrick Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
2618e5dd7070Spatrick
2619e5dd7070Spatrick // Get the IMP
2620e5dd7070Spatrick llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
2621e5dd7070Spatrick imp = EnforceType(Builder, imp, MSI.MessengerType);
2622e5dd7070Spatrick
2623e5dd7070Spatrick llvm::Metadata *impMD[] = {
2624e5dd7070Spatrick llvm::MDString::get(VMContext, Sel.getAsString()),
2625e5dd7070Spatrick llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
2626e5dd7070Spatrick llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
2627e5dd7070Spatrick llvm::Type::getInt1Ty(VMContext), IsClassMessage))};
2628e5dd7070Spatrick llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
2629e5dd7070Spatrick
2630e5dd7070Spatrick CGCallee callee(CGCalleeInfo(), imp);
2631e5dd7070Spatrick
2632e5dd7070Spatrick llvm::CallBase *call;
2633e5dd7070Spatrick RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call);
2634e5dd7070Spatrick call->setMetadata(msgSendMDKind, node);
2635e5dd7070Spatrick return msgRet;
2636e5dd7070Spatrick }
2637e5dd7070Spatrick
2638e5dd7070Spatrick /// Generate code for a message send expression.
2639e5dd7070Spatrick RValue
GenerateMessageSend(CodeGenFunction & CGF,ReturnValueSlot Return,QualType ResultType,Selector Sel,llvm::Value * Receiver,const CallArgList & CallArgs,const ObjCInterfaceDecl * Class,const ObjCMethodDecl * Method)2640e5dd7070Spatrick CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
2641e5dd7070Spatrick ReturnValueSlot Return,
2642e5dd7070Spatrick QualType ResultType,
2643e5dd7070Spatrick Selector Sel,
2644e5dd7070Spatrick llvm::Value *Receiver,
2645e5dd7070Spatrick const CallArgList &CallArgs,
2646e5dd7070Spatrick const ObjCInterfaceDecl *Class,
2647e5dd7070Spatrick const ObjCMethodDecl *Method) {
2648e5dd7070Spatrick CGBuilderTy &Builder = CGF.Builder;
2649e5dd7070Spatrick
2650e5dd7070Spatrick // Strip out message sends to retain / release in GC mode
2651e5dd7070Spatrick if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
2652e5dd7070Spatrick if (Sel == RetainSel || Sel == AutoreleaseSel) {
2653e5dd7070Spatrick return RValue::get(EnforceType(Builder, Receiver,
2654e5dd7070Spatrick CGM.getTypes().ConvertType(ResultType)));
2655e5dd7070Spatrick }
2656e5dd7070Spatrick if (Sel == ReleaseSel) {
2657e5dd7070Spatrick return RValue::get(nullptr);
2658e5dd7070Spatrick }
2659e5dd7070Spatrick }
2660e5dd7070Spatrick
2661e5dd7070Spatrick IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
2662e5dd7070Spatrick llvm::Value *cmd;
2663e5dd7070Spatrick if (Method)
2664e5dd7070Spatrick cmd = GetSelector(CGF, Method);
2665e5dd7070Spatrick else
2666e5dd7070Spatrick cmd = GetSelector(CGF, Sel);
2667e5dd7070Spatrick cmd = EnforceType(Builder, cmd, SelectorTy);
2668e5dd7070Spatrick Receiver = EnforceType(Builder, Receiver, IdTy);
2669e5dd7070Spatrick
2670e5dd7070Spatrick llvm::Metadata *impMD[] = {
2671e5dd7070Spatrick llvm::MDString::get(VMContext, Sel.getAsString()),
2672e5dd7070Spatrick llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""),
2673e5dd7070Spatrick llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
2674e5dd7070Spatrick llvm::Type::getInt1Ty(VMContext), Class != nullptr))};
2675e5dd7070Spatrick llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
2676e5dd7070Spatrick
2677e5dd7070Spatrick CallArgList ActualArgs;
2678e5dd7070Spatrick ActualArgs.add(RValue::get(Receiver), ASTIdTy);
2679e5dd7070Spatrick ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
2680e5dd7070Spatrick ActualArgs.addFrom(CallArgs);
2681e5dd7070Spatrick
2682e5dd7070Spatrick MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
2683e5dd7070Spatrick
2684*12c85518Srobert // Message sends are expected to return a zero value when the
2685*12c85518Srobert // receiver is nil. At one point, this was only guaranteed for
2686*12c85518Srobert // simple integer and pointer types, but expectations have grown
2687*12c85518Srobert // over time.
2688*12c85518Srobert //
2689*12c85518Srobert // Given a nil receiver, the GNU runtime's message lookup will
2690*12c85518Srobert // return a stub function that simply sets various return-value
2691*12c85518Srobert // registers to zero and then returns. That's good enough for us
2692*12c85518Srobert // if and only if (1) the calling conventions of that stub are
2693*12c85518Srobert // compatible with the signature we're using and (2) the registers
2694*12c85518Srobert // it sets are sufficient to produce a zero value of the return type.
2695*12c85518Srobert // Rather than doing a whole target-specific analysis, we assume it
2696*12c85518Srobert // only works for void, integer, and pointer types, and in all
2697*12c85518Srobert // other cases we do an explicit nil check is emitted code. In
2698*12c85518Srobert // addition to ensuring we produe a zero value for other types, this
2699*12c85518Srobert // sidesteps the few outright CC incompatibilities we know about that
2700*12c85518Srobert // could otherwise lead to crashes, like when a method is expected to
2701*12c85518Srobert // return on the x87 floating point stack or adjust the stack pointer
2702*12c85518Srobert // because of an indirect return.
2703*12c85518Srobert bool hasParamDestroyedInCallee = false;
2704*12c85518Srobert bool requiresExplicitZeroResult = false;
2705*12c85518Srobert bool requiresNilReceiverCheck = [&] {
2706*12c85518Srobert // We never need a check if we statically know the receiver isn't nil.
2707*12c85518Srobert if (!canMessageReceiverBeNull(CGF, Method, /*IsSuper*/ false,
2708*12c85518Srobert Class, Receiver))
2709*12c85518Srobert return false;
2710*12c85518Srobert
2711*12c85518Srobert // If there's a consumed argument, we need a nil check.
2712*12c85518Srobert if (Method && Method->hasParamDestroyedInCallee()) {
2713*12c85518Srobert hasParamDestroyedInCallee = true;
2714*12c85518Srobert }
2715*12c85518Srobert
2716*12c85518Srobert // If the return value isn't flagged as unused, and the result
2717*12c85518Srobert // type isn't in our narrow set where we assume compatibility,
2718*12c85518Srobert // we need a nil check to ensure a nil value.
2719*12c85518Srobert if (!Return.isUnused()) {
2720*12c85518Srobert if (ResultType->isVoidType()) {
2721*12c85518Srobert // void results are definitely okay.
2722*12c85518Srobert } else if (ResultType->hasPointerRepresentation() &&
2723*12c85518Srobert CGM.getTypes().isZeroInitializable(ResultType)) {
2724*12c85518Srobert // Pointer types should be fine as long as they have
2725*12c85518Srobert // bitwise-zero null pointers. But do we need to worry
2726*12c85518Srobert // about unusual address spaces?
2727*12c85518Srobert } else if (ResultType->isIntegralOrEnumerationType()) {
2728*12c85518Srobert // Bitwise zero should always be zero for integral types.
2729*12c85518Srobert // FIXME: we probably need a size limit here, but we've
2730*12c85518Srobert // never imposed one before
2731*12c85518Srobert } else {
2732*12c85518Srobert // Otherwise, use an explicit check just to be sure.
2733*12c85518Srobert requiresExplicitZeroResult = true;
2734*12c85518Srobert }
2735*12c85518Srobert }
2736*12c85518Srobert
2737*12c85518Srobert return hasParamDestroyedInCallee || requiresExplicitZeroResult;
2738*12c85518Srobert }();
2739*12c85518Srobert
2740*12c85518Srobert // We will need to explicitly zero-initialize an aggregate result slot
2741*12c85518Srobert // if we generally require explicit zeroing and we have an aggregate
2742*12c85518Srobert // result.
2743*12c85518Srobert bool requiresExplicitAggZeroing =
2744*12c85518Srobert requiresExplicitZeroResult && CGF.hasAggregateEvaluationKind(ResultType);
2745*12c85518Srobert
2746*12c85518Srobert // The block we're going to end up in after any message send or nil path.
2747*12c85518Srobert llvm::BasicBlock *continueBB = nullptr;
2748*12c85518Srobert // The block that eventually branched to continueBB along the nil path.
2749*12c85518Srobert llvm::BasicBlock *nilPathBB = nullptr;
2750*12c85518Srobert // The block to do explicit work in along the nil path, if necessary.
2751*12c85518Srobert llvm::BasicBlock *nilCleanupBB = nullptr;
2752*12c85518Srobert
2753*12c85518Srobert // Emit the nil-receiver check.
2754*12c85518Srobert if (requiresNilReceiverCheck) {
2755*12c85518Srobert llvm::BasicBlock *messageBB = CGF.createBasicBlock("msgSend");
2756*12c85518Srobert continueBB = CGF.createBasicBlock("continue");
2757*12c85518Srobert
2758*12c85518Srobert // If we need to zero-initialize an aggregate result or destroy
2759*12c85518Srobert // consumed arguments, we'll need a separate cleanup block.
2760*12c85518Srobert // Otherwise we can just branch directly to the continuation block.
2761*12c85518Srobert if (requiresExplicitAggZeroing || hasParamDestroyedInCallee) {
2762*12c85518Srobert nilCleanupBB = CGF.createBasicBlock("nilReceiverCleanup");
2763*12c85518Srobert } else {
2764*12c85518Srobert nilPathBB = Builder.GetInsertBlock();
2765*12c85518Srobert }
2766*12c85518Srobert
2767*12c85518Srobert llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
2768*12c85518Srobert llvm::Constant::getNullValue(Receiver->getType()));
2769*12c85518Srobert Builder.CreateCondBr(isNil, nilCleanupBB ? nilCleanupBB : continueBB,
2770*12c85518Srobert messageBB);
2771*12c85518Srobert CGF.EmitBlock(messageBB);
2772*12c85518Srobert }
2773*12c85518Srobert
2774e5dd7070Spatrick // Get the IMP to call
2775e5dd7070Spatrick llvm::Value *imp;
2776e5dd7070Spatrick
2777e5dd7070Spatrick // If we have non-legacy dispatch specified, we try using the objc_msgSend()
2778e5dd7070Spatrick // functions. These are not supported on all platforms (or all runtimes on a
2779e5dd7070Spatrick // given platform), so we
2780e5dd7070Spatrick switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
2781e5dd7070Spatrick case CodeGenOptions::Legacy:
2782e5dd7070Spatrick imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
2783e5dd7070Spatrick break;
2784e5dd7070Spatrick case CodeGenOptions::Mixed:
2785e5dd7070Spatrick case CodeGenOptions::NonLegacy:
2786e5dd7070Spatrick if (CGM.ReturnTypeUsesFPRet(ResultType)) {
2787e5dd7070Spatrick imp =
2788e5dd7070Spatrick CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
2789e5dd7070Spatrick "objc_msgSend_fpret")
2790e5dd7070Spatrick .getCallee();
2791e5dd7070Spatrick } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
2792e5dd7070Spatrick // The actual types here don't matter - we're going to bitcast the
2793e5dd7070Spatrick // function anyway
2794e5dd7070Spatrick imp =
2795e5dd7070Spatrick CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
2796e5dd7070Spatrick "objc_msgSend_stret")
2797e5dd7070Spatrick .getCallee();
2798e5dd7070Spatrick } else {
2799e5dd7070Spatrick imp = CGM.CreateRuntimeFunction(
2800e5dd7070Spatrick llvm::FunctionType::get(IdTy, IdTy, true), "objc_msgSend")
2801e5dd7070Spatrick .getCallee();
2802e5dd7070Spatrick }
2803e5dd7070Spatrick }
2804e5dd7070Spatrick
2805e5dd7070Spatrick // Reset the receiver in case the lookup modified it
2806e5dd7070Spatrick ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy);
2807e5dd7070Spatrick
2808e5dd7070Spatrick imp = EnforceType(Builder, imp, MSI.MessengerType);
2809e5dd7070Spatrick
2810e5dd7070Spatrick llvm::CallBase *call;
2811e5dd7070Spatrick CGCallee callee(CGCalleeInfo(), imp);
2812e5dd7070Spatrick RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call);
2813e5dd7070Spatrick call->setMetadata(msgSendMDKind, node);
2814e5dd7070Spatrick
2815*12c85518Srobert if (requiresNilReceiverCheck) {
2816*12c85518Srobert llvm::BasicBlock *nonNilPathBB = CGF.Builder.GetInsertBlock();
2817e5dd7070Spatrick CGF.Builder.CreateBr(continueBB);
2818*12c85518Srobert
2819*12c85518Srobert // Emit the nil path if we decided it was necessary above.
2820*12c85518Srobert if (nilCleanupBB) {
2821*12c85518Srobert CGF.EmitBlock(nilCleanupBB);
2822*12c85518Srobert
2823*12c85518Srobert if (hasParamDestroyedInCallee) {
2824*12c85518Srobert destroyCalleeDestroyedArguments(CGF, Method, CallArgs);
2825*12c85518Srobert }
2826*12c85518Srobert
2827*12c85518Srobert if (requiresExplicitAggZeroing) {
2828*12c85518Srobert assert(msgRet.isAggregate());
2829*12c85518Srobert Address addr = msgRet.getAggregateAddress();
2830*12c85518Srobert CGF.EmitNullInitialization(addr, ResultType);
2831*12c85518Srobert }
2832*12c85518Srobert
2833*12c85518Srobert nilPathBB = CGF.Builder.GetInsertBlock();
2834*12c85518Srobert CGF.Builder.CreateBr(continueBB);
2835*12c85518Srobert }
2836*12c85518Srobert
2837*12c85518Srobert // Enter the continuation block and emit a phi if required.
2838e5dd7070Spatrick CGF.EmitBlock(continueBB);
2839e5dd7070Spatrick if (msgRet.isScalar()) {
2840*12c85518Srobert // If the return type is void, do nothing
2841*12c85518Srobert if (llvm::Value *v = msgRet.getScalarVal()) {
2842e5dd7070Spatrick llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
2843*12c85518Srobert phi->addIncoming(v, nonNilPathBB);
2844*12c85518Srobert phi->addIncoming(CGM.EmitNullConstant(ResultType), nilPathBB);
2845e5dd7070Spatrick msgRet = RValue::get(phi);
2846*12c85518Srobert }
2847e5dd7070Spatrick } else if (msgRet.isAggregate()) {
2848*12c85518Srobert // Aggregate zeroing is handled in nilCleanupBB when it's required.
2849e5dd7070Spatrick } else /* isComplex() */ {
2850e5dd7070Spatrick std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
2851e5dd7070Spatrick llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
2852*12c85518Srobert phi->addIncoming(v.first, nonNilPathBB);
2853e5dd7070Spatrick phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
2854*12c85518Srobert nilPathBB);
2855e5dd7070Spatrick llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
2856*12c85518Srobert phi2->addIncoming(v.second, nonNilPathBB);
2857e5dd7070Spatrick phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
2858*12c85518Srobert nilPathBB);
2859e5dd7070Spatrick msgRet = RValue::getComplex(phi, phi2);
2860e5dd7070Spatrick }
2861e5dd7070Spatrick }
2862e5dd7070Spatrick return msgRet;
2863e5dd7070Spatrick }
2864e5dd7070Spatrick
2865e5dd7070Spatrick /// Generates a MethodList. Used in construction of a objc_class and
2866e5dd7070Spatrick /// objc_category structures.
2867e5dd7070Spatrick llvm::Constant *CGObjCGNU::
GenerateMethodList(StringRef ClassName,StringRef CategoryName,ArrayRef<const ObjCMethodDecl * > Methods,bool isClassMethodList)2868e5dd7070Spatrick GenerateMethodList(StringRef ClassName,
2869e5dd7070Spatrick StringRef CategoryName,
2870e5dd7070Spatrick ArrayRef<const ObjCMethodDecl*> Methods,
2871e5dd7070Spatrick bool isClassMethodList) {
2872e5dd7070Spatrick if (Methods.empty())
2873e5dd7070Spatrick return NULLPtr;
2874e5dd7070Spatrick
2875e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
2876e5dd7070Spatrick
2877e5dd7070Spatrick auto MethodList = Builder.beginStruct();
2878e5dd7070Spatrick MethodList.addNullPointer(CGM.Int8PtrTy);
2879e5dd7070Spatrick MethodList.addInt(Int32Ty, Methods.size());
2880e5dd7070Spatrick
2881e5dd7070Spatrick // Get the method structure type.
2882e5dd7070Spatrick llvm::StructType *ObjCMethodTy =
2883e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(), {
2884e5dd7070Spatrick PtrToInt8Ty, // Really a selector, but the runtime creates it us.
2885e5dd7070Spatrick PtrToInt8Ty, // Method types
2886e5dd7070Spatrick IMPTy // Method pointer
2887e5dd7070Spatrick });
2888e5dd7070Spatrick bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2);
2889e5dd7070Spatrick if (isV2ABI) {
2890e5dd7070Spatrick // size_t size;
2891e5dd7070Spatrick llvm::DataLayout td(&TheModule);
2892e5dd7070Spatrick MethodList.addInt(SizeTy, td.getTypeSizeInBits(ObjCMethodTy) /
2893e5dd7070Spatrick CGM.getContext().getCharWidth());
2894e5dd7070Spatrick ObjCMethodTy =
2895e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(), {
2896e5dd7070Spatrick IMPTy, // Method pointer
2897e5dd7070Spatrick PtrToInt8Ty, // Selector
2898e5dd7070Spatrick PtrToInt8Ty // Extended type encoding
2899e5dd7070Spatrick });
2900e5dd7070Spatrick } else {
2901e5dd7070Spatrick ObjCMethodTy =
2902e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(), {
2903e5dd7070Spatrick PtrToInt8Ty, // Really a selector, but the runtime creates it us.
2904e5dd7070Spatrick PtrToInt8Ty, // Method types
2905e5dd7070Spatrick IMPTy // Method pointer
2906e5dd7070Spatrick });
2907e5dd7070Spatrick }
2908e5dd7070Spatrick auto MethodArray = MethodList.beginArray();
2909e5dd7070Spatrick ASTContext &Context = CGM.getContext();
2910e5dd7070Spatrick for (const auto *OMD : Methods) {
2911e5dd7070Spatrick llvm::Constant *FnPtr =
2912a9ac8606Spatrick TheModule.getFunction(getSymbolNameForMethod(OMD));
2913e5dd7070Spatrick assert(FnPtr && "Can't generate metadata for method that doesn't exist");
2914e5dd7070Spatrick auto Method = MethodArray.beginStruct(ObjCMethodTy);
2915e5dd7070Spatrick if (isV2ABI) {
2916e5dd7070Spatrick Method.addBitCast(FnPtr, IMPTy);
2917e5dd7070Spatrick Method.add(GetConstantSelector(OMD->getSelector(),
2918e5dd7070Spatrick Context.getObjCEncodingForMethodDecl(OMD)));
2919e5dd7070Spatrick Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD, true)));
2920e5dd7070Spatrick } else {
2921e5dd7070Spatrick Method.add(MakeConstantString(OMD->getSelector().getAsString()));
2922e5dd7070Spatrick Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD)));
2923e5dd7070Spatrick Method.addBitCast(FnPtr, IMPTy);
2924e5dd7070Spatrick }
2925e5dd7070Spatrick Method.finishAndAddTo(MethodArray);
2926e5dd7070Spatrick }
2927e5dd7070Spatrick MethodArray.finishAndAddTo(MethodList);
2928e5dd7070Spatrick
2929e5dd7070Spatrick // Create an instance of the structure
2930e5dd7070Spatrick return MethodList.finishAndCreateGlobal(".objc_method_list",
2931e5dd7070Spatrick CGM.getPointerAlign());
2932e5dd7070Spatrick }
2933e5dd7070Spatrick
2934e5dd7070Spatrick /// Generates an IvarList. Used in construction of a objc_class.
2935e5dd7070Spatrick llvm::Constant *CGObjCGNU::
GenerateIvarList(ArrayRef<llvm::Constant * > IvarNames,ArrayRef<llvm::Constant * > IvarTypes,ArrayRef<llvm::Constant * > IvarOffsets,ArrayRef<llvm::Constant * > IvarAlign,ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership)2936e5dd7070Spatrick GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
2937e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarTypes,
2938e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarOffsets,
2939e5dd7070Spatrick ArrayRef<llvm::Constant *> IvarAlign,
2940e5dd7070Spatrick ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership) {
2941e5dd7070Spatrick if (IvarNames.empty())
2942e5dd7070Spatrick return NULLPtr;
2943e5dd7070Spatrick
2944e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
2945e5dd7070Spatrick
2946e5dd7070Spatrick // Structure containing array count followed by array.
2947e5dd7070Spatrick auto IvarList = Builder.beginStruct();
2948e5dd7070Spatrick IvarList.addInt(IntTy, (int)IvarNames.size());
2949e5dd7070Spatrick
2950e5dd7070Spatrick // Get the ivar structure type.
2951e5dd7070Spatrick llvm::StructType *ObjCIvarTy =
2952e5dd7070Spatrick llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy);
2953e5dd7070Spatrick
2954e5dd7070Spatrick // Array of ivar structures.
2955e5dd7070Spatrick auto Ivars = IvarList.beginArray(ObjCIvarTy);
2956e5dd7070Spatrick for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
2957e5dd7070Spatrick auto Ivar = Ivars.beginStruct(ObjCIvarTy);
2958e5dd7070Spatrick Ivar.add(IvarNames[i]);
2959e5dd7070Spatrick Ivar.add(IvarTypes[i]);
2960e5dd7070Spatrick Ivar.add(IvarOffsets[i]);
2961e5dd7070Spatrick Ivar.finishAndAddTo(Ivars);
2962e5dd7070Spatrick }
2963e5dd7070Spatrick Ivars.finishAndAddTo(IvarList);
2964e5dd7070Spatrick
2965e5dd7070Spatrick // Create an instance of the structure
2966e5dd7070Spatrick return IvarList.finishAndCreateGlobal(".objc_ivar_list",
2967e5dd7070Spatrick CGM.getPointerAlign());
2968e5dd7070Spatrick }
2969e5dd7070Spatrick
2970e5dd7070Spatrick /// Generate a class structure
GenerateClassStructure(llvm::Constant * MetaClass,llvm::Constant * SuperClass,unsigned info,const char * Name,llvm::Constant * Version,llvm::Constant * InstanceSize,llvm::Constant * IVars,llvm::Constant * Methods,llvm::Constant * Protocols,llvm::Constant * IvarOffsets,llvm::Constant * Properties,llvm::Constant * StrongIvarBitmap,llvm::Constant * WeakIvarBitmap,bool isMeta)2971e5dd7070Spatrick llvm::Constant *CGObjCGNU::GenerateClassStructure(
2972e5dd7070Spatrick llvm::Constant *MetaClass,
2973e5dd7070Spatrick llvm::Constant *SuperClass,
2974e5dd7070Spatrick unsigned info,
2975e5dd7070Spatrick const char *Name,
2976e5dd7070Spatrick llvm::Constant *Version,
2977e5dd7070Spatrick llvm::Constant *InstanceSize,
2978e5dd7070Spatrick llvm::Constant *IVars,
2979e5dd7070Spatrick llvm::Constant *Methods,
2980e5dd7070Spatrick llvm::Constant *Protocols,
2981e5dd7070Spatrick llvm::Constant *IvarOffsets,
2982e5dd7070Spatrick llvm::Constant *Properties,
2983e5dd7070Spatrick llvm::Constant *StrongIvarBitmap,
2984e5dd7070Spatrick llvm::Constant *WeakIvarBitmap,
2985e5dd7070Spatrick bool isMeta) {
2986e5dd7070Spatrick // Set up the class structure
2987e5dd7070Spatrick // Note: Several of these are char*s when they should be ids. This is
2988e5dd7070Spatrick // because the runtime performs this translation on load.
2989e5dd7070Spatrick //
2990e5dd7070Spatrick // Fields marked New ABI are part of the GNUstep runtime. We emit them
2991e5dd7070Spatrick // anyway; the classes will still work with the GNU runtime, they will just
2992e5dd7070Spatrick // be ignored.
2993e5dd7070Spatrick llvm::StructType *ClassTy = llvm::StructType::get(
2994e5dd7070Spatrick PtrToInt8Ty, // isa
2995e5dd7070Spatrick PtrToInt8Ty, // super_class
2996e5dd7070Spatrick PtrToInt8Ty, // name
2997e5dd7070Spatrick LongTy, // version
2998e5dd7070Spatrick LongTy, // info
2999e5dd7070Spatrick LongTy, // instance_size
3000e5dd7070Spatrick IVars->getType(), // ivars
3001e5dd7070Spatrick Methods->getType(), // methods
3002e5dd7070Spatrick // These are all filled in by the runtime, so we pretend
3003e5dd7070Spatrick PtrTy, // dtable
3004e5dd7070Spatrick PtrTy, // subclass_list
3005e5dd7070Spatrick PtrTy, // sibling_class
3006e5dd7070Spatrick PtrTy, // protocols
3007e5dd7070Spatrick PtrTy, // gc_object_type
3008e5dd7070Spatrick // New ABI:
3009e5dd7070Spatrick LongTy, // abi_version
3010e5dd7070Spatrick IvarOffsets->getType(), // ivar_offsets
3011e5dd7070Spatrick Properties->getType(), // properties
3012e5dd7070Spatrick IntPtrTy, // strong_pointers
3013e5dd7070Spatrick IntPtrTy // weak_pointers
3014e5dd7070Spatrick );
3015e5dd7070Spatrick
3016e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3017e5dd7070Spatrick auto Elements = Builder.beginStruct(ClassTy);
3018e5dd7070Spatrick
3019e5dd7070Spatrick // Fill in the structure
3020e5dd7070Spatrick
3021e5dd7070Spatrick // isa
3022e5dd7070Spatrick Elements.addBitCast(MetaClass, PtrToInt8Ty);
3023e5dd7070Spatrick // super_class
3024e5dd7070Spatrick Elements.add(SuperClass);
3025e5dd7070Spatrick // name
3026e5dd7070Spatrick Elements.add(MakeConstantString(Name, ".class_name"));
3027e5dd7070Spatrick // version
3028e5dd7070Spatrick Elements.addInt(LongTy, 0);
3029e5dd7070Spatrick // info
3030e5dd7070Spatrick Elements.addInt(LongTy, info);
3031e5dd7070Spatrick // instance_size
3032e5dd7070Spatrick if (isMeta) {
3033e5dd7070Spatrick llvm::DataLayout td(&TheModule);
3034e5dd7070Spatrick Elements.addInt(LongTy,
3035e5dd7070Spatrick td.getTypeSizeInBits(ClassTy) /
3036e5dd7070Spatrick CGM.getContext().getCharWidth());
3037e5dd7070Spatrick } else
3038e5dd7070Spatrick Elements.add(InstanceSize);
3039e5dd7070Spatrick // ivars
3040e5dd7070Spatrick Elements.add(IVars);
3041e5dd7070Spatrick // methods
3042e5dd7070Spatrick Elements.add(Methods);
3043e5dd7070Spatrick // These are all filled in by the runtime, so we pretend
3044e5dd7070Spatrick // dtable
3045e5dd7070Spatrick Elements.add(NULLPtr);
3046e5dd7070Spatrick // subclass_list
3047e5dd7070Spatrick Elements.add(NULLPtr);
3048e5dd7070Spatrick // sibling_class
3049e5dd7070Spatrick Elements.add(NULLPtr);
3050e5dd7070Spatrick // protocols
3051e5dd7070Spatrick Elements.addBitCast(Protocols, PtrTy);
3052e5dd7070Spatrick // gc_object_type
3053e5dd7070Spatrick Elements.add(NULLPtr);
3054e5dd7070Spatrick // abi_version
3055e5dd7070Spatrick Elements.addInt(LongTy, ClassABIVersion);
3056e5dd7070Spatrick // ivar_offsets
3057e5dd7070Spatrick Elements.add(IvarOffsets);
3058e5dd7070Spatrick // properties
3059e5dd7070Spatrick Elements.add(Properties);
3060e5dd7070Spatrick // strong_pointers
3061e5dd7070Spatrick Elements.add(StrongIvarBitmap);
3062e5dd7070Spatrick // weak_pointers
3063e5dd7070Spatrick Elements.add(WeakIvarBitmap);
3064e5dd7070Spatrick // Create an instance of the structure
3065e5dd7070Spatrick // This is now an externally visible symbol, so that we can speed up class
3066e5dd7070Spatrick // messages in the next ABI. We may already have some weak references to
3067e5dd7070Spatrick // this, so check and fix them properly.
3068e5dd7070Spatrick std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
3069e5dd7070Spatrick std::string(Name));
3070e5dd7070Spatrick llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
3071e5dd7070Spatrick llvm::Constant *Class =
3072e5dd7070Spatrick Elements.finishAndCreateGlobal(ClassSym, CGM.getPointerAlign(), false,
3073e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage);
3074e5dd7070Spatrick if (ClassRef) {
3075e5dd7070Spatrick ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
3076e5dd7070Spatrick ClassRef->getType()));
3077e5dd7070Spatrick ClassRef->removeFromParent();
3078e5dd7070Spatrick Class->setName(ClassSym);
3079e5dd7070Spatrick }
3080e5dd7070Spatrick return Class;
3081e5dd7070Spatrick }
3082e5dd7070Spatrick
3083e5dd7070Spatrick llvm::Constant *CGObjCGNU::
GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl * > Methods)3084e5dd7070Spatrick GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl*> Methods) {
3085e5dd7070Spatrick // Get the method structure type.
3086e5dd7070Spatrick llvm::StructType *ObjCMethodDescTy =
3087e5dd7070Spatrick llvm::StructType::get(CGM.getLLVMContext(), { PtrToInt8Ty, PtrToInt8Ty });
3088e5dd7070Spatrick ASTContext &Context = CGM.getContext();
3089e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3090e5dd7070Spatrick auto MethodList = Builder.beginStruct();
3091e5dd7070Spatrick MethodList.addInt(IntTy, Methods.size());
3092e5dd7070Spatrick auto MethodArray = MethodList.beginArray(ObjCMethodDescTy);
3093e5dd7070Spatrick for (auto *M : Methods) {
3094e5dd7070Spatrick auto Method = MethodArray.beginStruct(ObjCMethodDescTy);
3095e5dd7070Spatrick Method.add(MakeConstantString(M->getSelector().getAsString()));
3096e5dd7070Spatrick Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(M)));
3097e5dd7070Spatrick Method.finishAndAddTo(MethodArray);
3098e5dd7070Spatrick }
3099e5dd7070Spatrick MethodArray.finishAndAddTo(MethodList);
3100e5dd7070Spatrick return MethodList.finishAndCreateGlobal(".objc_method_list",
3101e5dd7070Spatrick CGM.getPointerAlign());
3102e5dd7070Spatrick }
3103e5dd7070Spatrick
3104e5dd7070Spatrick // Create the protocol list structure used in classes, categories and so on
3105e5dd7070Spatrick llvm::Constant *
GenerateProtocolList(ArrayRef<std::string> Protocols)3106e5dd7070Spatrick CGObjCGNU::GenerateProtocolList(ArrayRef<std::string> Protocols) {
3107e5dd7070Spatrick
3108e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3109e5dd7070Spatrick auto ProtocolList = Builder.beginStruct();
3110e5dd7070Spatrick ProtocolList.add(NULLPtr);
3111e5dd7070Spatrick ProtocolList.addInt(LongTy, Protocols.size());
3112e5dd7070Spatrick
3113e5dd7070Spatrick auto Elements = ProtocolList.beginArray(PtrToInt8Ty);
3114e5dd7070Spatrick for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
3115e5dd7070Spatrick iter != endIter ; iter++) {
3116e5dd7070Spatrick llvm::Constant *protocol = nullptr;
3117e5dd7070Spatrick llvm::StringMap<llvm::Constant*>::iterator value =
3118e5dd7070Spatrick ExistingProtocols.find(*iter);
3119e5dd7070Spatrick if (value == ExistingProtocols.end()) {
3120e5dd7070Spatrick protocol = GenerateEmptyProtocol(*iter);
3121e5dd7070Spatrick } else {
3122e5dd7070Spatrick protocol = value->getValue();
3123e5dd7070Spatrick }
3124e5dd7070Spatrick Elements.addBitCast(protocol, PtrToInt8Ty);
3125e5dd7070Spatrick }
3126e5dd7070Spatrick Elements.finishAndAddTo(ProtocolList);
3127e5dd7070Spatrick return ProtocolList.finishAndCreateGlobal(".objc_protocol_list",
3128e5dd7070Spatrick CGM.getPointerAlign());
3129e5dd7070Spatrick }
3130e5dd7070Spatrick
GenerateProtocolRef(CodeGenFunction & CGF,const ObjCProtocolDecl * PD)3131e5dd7070Spatrick llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
3132e5dd7070Spatrick const ObjCProtocolDecl *PD) {
3133ec727ea7Spatrick auto protocol = GenerateProtocolRef(PD);
3134ec727ea7Spatrick llvm::Type *T =
3135ec727ea7Spatrick CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
3136ec727ea7Spatrick return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
3137ec727ea7Spatrick }
3138ec727ea7Spatrick
GenerateProtocolRef(const ObjCProtocolDecl * PD)3139ec727ea7Spatrick llvm::Constant *CGObjCGNU::GenerateProtocolRef(const ObjCProtocolDecl *PD) {
3140e5dd7070Spatrick llvm::Constant *&protocol = ExistingProtocols[PD->getNameAsString()];
3141e5dd7070Spatrick if (!protocol)
3142e5dd7070Spatrick GenerateProtocol(PD);
3143e5dd7070Spatrick assert(protocol && "Unknown protocol");
3144ec727ea7Spatrick return protocol;
3145e5dd7070Spatrick }
3146e5dd7070Spatrick
3147e5dd7070Spatrick llvm::Constant *
GenerateEmptyProtocol(StringRef ProtocolName)3148e5dd7070Spatrick CGObjCGNU::GenerateEmptyProtocol(StringRef ProtocolName) {
3149e5dd7070Spatrick llvm::Constant *ProtocolList = GenerateProtocolList({});
3150e5dd7070Spatrick llvm::Constant *MethodList = GenerateProtocolMethodList({});
3151e5dd7070Spatrick MethodList = llvm::ConstantExpr::getBitCast(MethodList, PtrToInt8Ty);
3152e5dd7070Spatrick // Protocols are objects containing lists of the methods implemented and
3153e5dd7070Spatrick // protocols adopted.
3154e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3155e5dd7070Spatrick auto Elements = Builder.beginStruct();
3156e5dd7070Spatrick
3157e5dd7070Spatrick // The isa pointer must be set to a magic number so the runtime knows it's
3158e5dd7070Spatrick // the correct layout.
3159e5dd7070Spatrick Elements.add(llvm::ConstantExpr::getIntToPtr(
3160e5dd7070Spatrick llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
3161e5dd7070Spatrick
3162e5dd7070Spatrick Elements.add(MakeConstantString(ProtocolName, ".objc_protocol_name"));
3163e5dd7070Spatrick Elements.add(ProtocolList); /* .protocol_list */
3164e5dd7070Spatrick Elements.add(MethodList); /* .instance_methods */
3165e5dd7070Spatrick Elements.add(MethodList); /* .class_methods */
3166e5dd7070Spatrick Elements.add(MethodList); /* .optional_instance_methods */
3167e5dd7070Spatrick Elements.add(MethodList); /* .optional_class_methods */
3168e5dd7070Spatrick Elements.add(NULLPtr); /* .properties */
3169e5dd7070Spatrick Elements.add(NULLPtr); /* .optional_properties */
3170e5dd7070Spatrick return Elements.finishAndCreateGlobal(SymbolForProtocol(ProtocolName),
3171e5dd7070Spatrick CGM.getPointerAlign());
3172e5dd7070Spatrick }
3173e5dd7070Spatrick
GenerateProtocol(const ObjCProtocolDecl * PD)3174e5dd7070Spatrick void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
3175a9ac8606Spatrick if (PD->isNonRuntimeProtocol())
3176a9ac8606Spatrick return;
3177a9ac8606Spatrick
3178e5dd7070Spatrick std::string ProtocolName = PD->getNameAsString();
3179e5dd7070Spatrick
3180e5dd7070Spatrick // Use the protocol definition, if there is one.
3181e5dd7070Spatrick if (const ObjCProtocolDecl *Def = PD->getDefinition())
3182e5dd7070Spatrick PD = Def;
3183e5dd7070Spatrick
3184e5dd7070Spatrick SmallVector<std::string, 16> Protocols;
3185e5dd7070Spatrick for (const auto *PI : PD->protocols())
3186e5dd7070Spatrick Protocols.push_back(PI->getNameAsString());
3187e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> InstanceMethods;
3188e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> OptionalInstanceMethods;
3189e5dd7070Spatrick for (const auto *I : PD->instance_methods())
3190e5dd7070Spatrick if (I->isOptional())
3191e5dd7070Spatrick OptionalInstanceMethods.push_back(I);
3192e5dd7070Spatrick else
3193e5dd7070Spatrick InstanceMethods.push_back(I);
3194e5dd7070Spatrick // Collect information about class methods:
3195e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> ClassMethods;
3196e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> OptionalClassMethods;
3197e5dd7070Spatrick for (const auto *I : PD->class_methods())
3198e5dd7070Spatrick if (I->isOptional())
3199e5dd7070Spatrick OptionalClassMethods.push_back(I);
3200e5dd7070Spatrick else
3201e5dd7070Spatrick ClassMethods.push_back(I);
3202e5dd7070Spatrick
3203e5dd7070Spatrick llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
3204e5dd7070Spatrick llvm::Constant *InstanceMethodList =
3205e5dd7070Spatrick GenerateProtocolMethodList(InstanceMethods);
3206e5dd7070Spatrick llvm::Constant *ClassMethodList =
3207e5dd7070Spatrick GenerateProtocolMethodList(ClassMethods);
3208e5dd7070Spatrick llvm::Constant *OptionalInstanceMethodList =
3209e5dd7070Spatrick GenerateProtocolMethodList(OptionalInstanceMethods);
3210e5dd7070Spatrick llvm::Constant *OptionalClassMethodList =
3211e5dd7070Spatrick GenerateProtocolMethodList(OptionalClassMethods);
3212e5dd7070Spatrick
3213e5dd7070Spatrick // Property metadata: name, attributes, isSynthesized, setter name, setter
3214e5dd7070Spatrick // types, getter name, getter types.
3215e5dd7070Spatrick // The isSynthesized value is always set to 0 in a protocol. It exists to
3216e5dd7070Spatrick // simplify the runtime library by allowing it to use the same data
3217e5dd7070Spatrick // structures for protocol metadata everywhere.
3218e5dd7070Spatrick
3219e5dd7070Spatrick llvm::Constant *PropertyList =
3220e5dd7070Spatrick GeneratePropertyList(nullptr, PD, false, false);
3221e5dd7070Spatrick llvm::Constant *OptionalPropertyList =
3222e5dd7070Spatrick GeneratePropertyList(nullptr, PD, false, true);
3223e5dd7070Spatrick
3224e5dd7070Spatrick // Protocols are objects containing lists of the methods implemented and
3225e5dd7070Spatrick // protocols adopted.
3226e5dd7070Spatrick // The isa pointer must be set to a magic number so the runtime knows it's
3227e5dd7070Spatrick // the correct layout.
3228e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3229e5dd7070Spatrick auto Elements = Builder.beginStruct();
3230e5dd7070Spatrick Elements.add(
3231e5dd7070Spatrick llvm::ConstantExpr::getIntToPtr(
3232e5dd7070Spatrick llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
3233e5dd7070Spatrick Elements.add(MakeConstantString(ProtocolName));
3234e5dd7070Spatrick Elements.add(ProtocolList);
3235e5dd7070Spatrick Elements.add(InstanceMethodList);
3236e5dd7070Spatrick Elements.add(ClassMethodList);
3237e5dd7070Spatrick Elements.add(OptionalInstanceMethodList);
3238e5dd7070Spatrick Elements.add(OptionalClassMethodList);
3239e5dd7070Spatrick Elements.add(PropertyList);
3240e5dd7070Spatrick Elements.add(OptionalPropertyList);
3241e5dd7070Spatrick ExistingProtocols[ProtocolName] =
3242e5dd7070Spatrick llvm::ConstantExpr::getBitCast(
3243e5dd7070Spatrick Elements.finishAndCreateGlobal(".objc_protocol", CGM.getPointerAlign()),
3244e5dd7070Spatrick IdTy);
3245e5dd7070Spatrick }
GenerateProtocolHolderCategory()3246e5dd7070Spatrick void CGObjCGNU::GenerateProtocolHolderCategory() {
3247e5dd7070Spatrick // Collect information about instance methods
3248e5dd7070Spatrick
3249e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3250e5dd7070Spatrick auto Elements = Builder.beginStruct();
3251e5dd7070Spatrick
3252e5dd7070Spatrick const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
3253e5dd7070Spatrick const std::string CategoryName = "AnotherHack";
3254e5dd7070Spatrick Elements.add(MakeConstantString(CategoryName));
3255e5dd7070Spatrick Elements.add(MakeConstantString(ClassName));
3256e5dd7070Spatrick // Instance method list
3257e5dd7070Spatrick Elements.addBitCast(GenerateMethodList(
3258e5dd7070Spatrick ClassName, CategoryName, {}, false), PtrTy);
3259e5dd7070Spatrick // Class method list
3260e5dd7070Spatrick Elements.addBitCast(GenerateMethodList(
3261e5dd7070Spatrick ClassName, CategoryName, {}, true), PtrTy);
3262e5dd7070Spatrick
3263e5dd7070Spatrick // Protocol list
3264e5dd7070Spatrick ConstantInitBuilder ProtocolListBuilder(CGM);
3265e5dd7070Spatrick auto ProtocolList = ProtocolListBuilder.beginStruct();
3266e5dd7070Spatrick ProtocolList.add(NULLPtr);
3267e5dd7070Spatrick ProtocolList.addInt(LongTy, ExistingProtocols.size());
3268e5dd7070Spatrick auto ProtocolElements = ProtocolList.beginArray(PtrTy);
3269e5dd7070Spatrick for (auto iter = ExistingProtocols.begin(), endIter = ExistingProtocols.end();
3270e5dd7070Spatrick iter != endIter ; iter++) {
3271e5dd7070Spatrick ProtocolElements.addBitCast(iter->getValue(), PtrTy);
3272e5dd7070Spatrick }
3273e5dd7070Spatrick ProtocolElements.finishAndAddTo(ProtocolList);
3274e5dd7070Spatrick Elements.addBitCast(
3275e5dd7070Spatrick ProtocolList.finishAndCreateGlobal(".objc_protocol_list",
3276e5dd7070Spatrick CGM.getPointerAlign()),
3277e5dd7070Spatrick PtrTy);
3278e5dd7070Spatrick Categories.push_back(llvm::ConstantExpr::getBitCast(
3279e5dd7070Spatrick Elements.finishAndCreateGlobal("", CGM.getPointerAlign()),
3280e5dd7070Spatrick PtrTy));
3281e5dd7070Spatrick }
3282e5dd7070Spatrick
3283e5dd7070Spatrick /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
3284e5dd7070Spatrick /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
3285e5dd7070Spatrick /// bits set to their values, LSB first, while larger ones are stored in a
3286e5dd7070Spatrick /// structure of this / form:
3287e5dd7070Spatrick ///
3288e5dd7070Spatrick /// struct { int32_t length; int32_t values[length]; };
3289e5dd7070Spatrick ///
3290e5dd7070Spatrick /// The values in the array are stored in host-endian format, with the least
3291e5dd7070Spatrick /// significant bit being assumed to come first in the bitfield. Therefore, a
3292e5dd7070Spatrick /// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
3293e5dd7070Spatrick /// bitfield / with the 63rd bit set will be 1<<64.
MakeBitField(ArrayRef<bool> bits)3294e5dd7070Spatrick llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
3295e5dd7070Spatrick int bitCount = bits.size();
3296e5dd7070Spatrick int ptrBits = CGM.getDataLayout().getPointerSizeInBits();
3297e5dd7070Spatrick if (bitCount < ptrBits) {
3298e5dd7070Spatrick uint64_t val = 1;
3299e5dd7070Spatrick for (int i=0 ; i<bitCount ; ++i) {
3300e5dd7070Spatrick if (bits[i]) val |= 1ULL<<(i+1);
3301e5dd7070Spatrick }
3302e5dd7070Spatrick return llvm::ConstantInt::get(IntPtrTy, val);
3303e5dd7070Spatrick }
3304e5dd7070Spatrick SmallVector<llvm::Constant *, 8> values;
3305e5dd7070Spatrick int v=0;
3306e5dd7070Spatrick while (v < bitCount) {
3307e5dd7070Spatrick int32_t word = 0;
3308e5dd7070Spatrick for (int i=0 ; (i<32) && (v<bitCount) ; ++i) {
3309e5dd7070Spatrick if (bits[v]) word |= 1<<i;
3310e5dd7070Spatrick v++;
3311e5dd7070Spatrick }
3312e5dd7070Spatrick values.push_back(llvm::ConstantInt::get(Int32Ty, word));
3313e5dd7070Spatrick }
3314e5dd7070Spatrick
3315e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3316e5dd7070Spatrick auto fields = builder.beginStruct();
3317e5dd7070Spatrick fields.addInt(Int32Ty, values.size());
3318e5dd7070Spatrick auto array = fields.beginArray();
3319*12c85518Srobert for (auto *v : values) array.add(v);
3320e5dd7070Spatrick array.finishAndAddTo(fields);
3321e5dd7070Spatrick
3322e5dd7070Spatrick llvm::Constant *GS =
3323e5dd7070Spatrick fields.finishAndCreateGlobal("", CharUnits::fromQuantity(4));
3324e5dd7070Spatrick llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
3325e5dd7070Spatrick return ptr;
3326e5dd7070Spatrick }
3327e5dd7070Spatrick
GenerateCategoryProtocolList(const ObjCCategoryDecl * OCD)3328e5dd7070Spatrick llvm::Constant *CGObjCGNU::GenerateCategoryProtocolList(const
3329e5dd7070Spatrick ObjCCategoryDecl *OCD) {
3330a9ac8606Spatrick const auto &RefPro = OCD->getReferencedProtocols();
3331a9ac8606Spatrick const auto RuntimeProtos =
3332a9ac8606Spatrick GetRuntimeProtocolList(RefPro.begin(), RefPro.end());
3333e5dd7070Spatrick SmallVector<std::string, 16> Protocols;
3334a9ac8606Spatrick for (const auto *PD : RuntimeProtos)
3335e5dd7070Spatrick Protocols.push_back(PD->getNameAsString());
3336e5dd7070Spatrick return GenerateProtocolList(Protocols);
3337e5dd7070Spatrick }
3338e5dd7070Spatrick
GenerateCategory(const ObjCCategoryImplDecl * OCD)3339e5dd7070Spatrick void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
3340e5dd7070Spatrick const ObjCInterfaceDecl *Class = OCD->getClassInterface();
3341e5dd7070Spatrick std::string ClassName = Class->getNameAsString();
3342e5dd7070Spatrick std::string CategoryName = OCD->getNameAsString();
3343e5dd7070Spatrick
3344e5dd7070Spatrick // Collect the names of referenced protocols
3345e5dd7070Spatrick const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
3346e5dd7070Spatrick
3347e5dd7070Spatrick ConstantInitBuilder Builder(CGM);
3348e5dd7070Spatrick auto Elements = Builder.beginStruct();
3349e5dd7070Spatrick Elements.add(MakeConstantString(CategoryName));
3350e5dd7070Spatrick Elements.add(MakeConstantString(ClassName));
3351e5dd7070Spatrick // Instance method list
3352e5dd7070Spatrick SmallVector<ObjCMethodDecl*, 16> InstanceMethods;
3353e5dd7070Spatrick InstanceMethods.insert(InstanceMethods.begin(), OCD->instmeth_begin(),
3354e5dd7070Spatrick OCD->instmeth_end());
3355e5dd7070Spatrick Elements.addBitCast(
3356e5dd7070Spatrick GenerateMethodList(ClassName, CategoryName, InstanceMethods, false),
3357e5dd7070Spatrick PtrTy);
3358e5dd7070Spatrick // Class method list
3359e5dd7070Spatrick
3360e5dd7070Spatrick SmallVector<ObjCMethodDecl*, 16> ClassMethods;
3361e5dd7070Spatrick ClassMethods.insert(ClassMethods.begin(), OCD->classmeth_begin(),
3362e5dd7070Spatrick OCD->classmeth_end());
3363e5dd7070Spatrick Elements.addBitCast(
3364e5dd7070Spatrick GenerateMethodList(ClassName, CategoryName, ClassMethods, true),
3365e5dd7070Spatrick PtrTy);
3366e5dd7070Spatrick // Protocol list
3367e5dd7070Spatrick Elements.addBitCast(GenerateCategoryProtocolList(CatDecl), PtrTy);
3368e5dd7070Spatrick if (isRuntime(ObjCRuntime::GNUstep, 2)) {
3369e5dd7070Spatrick const ObjCCategoryDecl *Category =
3370e5dd7070Spatrick Class->FindCategoryDeclaration(OCD->getIdentifier());
3371e5dd7070Spatrick if (Category) {
3372e5dd7070Spatrick // Instance properties
3373e5dd7070Spatrick Elements.addBitCast(GeneratePropertyList(OCD, Category, false), PtrTy);
3374e5dd7070Spatrick // Class properties
3375e5dd7070Spatrick Elements.addBitCast(GeneratePropertyList(OCD, Category, true), PtrTy);
3376e5dd7070Spatrick } else {
3377e5dd7070Spatrick Elements.addNullPointer(PtrTy);
3378e5dd7070Spatrick Elements.addNullPointer(PtrTy);
3379e5dd7070Spatrick }
3380e5dd7070Spatrick }
3381e5dd7070Spatrick
3382e5dd7070Spatrick Categories.push_back(llvm::ConstantExpr::getBitCast(
3383e5dd7070Spatrick Elements.finishAndCreateGlobal(
3384e5dd7070Spatrick std::string(".objc_category_")+ClassName+CategoryName,
3385e5dd7070Spatrick CGM.getPointerAlign()),
3386e5dd7070Spatrick PtrTy));
3387e5dd7070Spatrick }
3388e5dd7070Spatrick
GeneratePropertyList(const Decl * Container,const ObjCContainerDecl * OCD,bool isClassProperty,bool protocolOptionalProperties)3389e5dd7070Spatrick llvm::Constant *CGObjCGNU::GeneratePropertyList(const Decl *Container,
3390e5dd7070Spatrick const ObjCContainerDecl *OCD,
3391e5dd7070Spatrick bool isClassProperty,
3392e5dd7070Spatrick bool protocolOptionalProperties) {
3393e5dd7070Spatrick
3394e5dd7070Spatrick SmallVector<const ObjCPropertyDecl *, 16> Properties;
3395e5dd7070Spatrick llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
3396e5dd7070Spatrick bool isProtocol = isa<ObjCProtocolDecl>(OCD);
3397e5dd7070Spatrick ASTContext &Context = CGM.getContext();
3398e5dd7070Spatrick
3399e5dd7070Spatrick std::function<void(const ObjCProtocolDecl *Proto)> collectProtocolProperties
3400e5dd7070Spatrick = [&](const ObjCProtocolDecl *Proto) {
3401e5dd7070Spatrick for (const auto *P : Proto->protocols())
3402e5dd7070Spatrick collectProtocolProperties(P);
3403e5dd7070Spatrick for (const auto *PD : Proto->properties()) {
3404e5dd7070Spatrick if (isClassProperty != PD->isClassProperty())
3405e5dd7070Spatrick continue;
3406e5dd7070Spatrick // Skip any properties that are declared in protocols that this class
3407e5dd7070Spatrick // conforms to but are not actually implemented by this class.
3408e5dd7070Spatrick if (!isProtocol && !Context.getObjCPropertyImplDeclForPropertyDecl(PD, Container))
3409e5dd7070Spatrick continue;
3410e5dd7070Spatrick if (!PropertySet.insert(PD->getIdentifier()).second)
3411e5dd7070Spatrick continue;
3412e5dd7070Spatrick Properties.push_back(PD);
3413e5dd7070Spatrick }
3414e5dd7070Spatrick };
3415e5dd7070Spatrick
3416e5dd7070Spatrick if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
3417e5dd7070Spatrick for (const ObjCCategoryDecl *ClassExt : OID->known_extensions())
3418e5dd7070Spatrick for (auto *PD : ClassExt->properties()) {
3419e5dd7070Spatrick if (isClassProperty != PD->isClassProperty())
3420e5dd7070Spatrick continue;
3421e5dd7070Spatrick PropertySet.insert(PD->getIdentifier());
3422e5dd7070Spatrick Properties.push_back(PD);
3423e5dd7070Spatrick }
3424e5dd7070Spatrick
3425e5dd7070Spatrick for (const auto *PD : OCD->properties()) {
3426e5dd7070Spatrick if (isClassProperty != PD->isClassProperty())
3427e5dd7070Spatrick continue;
3428e5dd7070Spatrick // If we're generating a list for a protocol, skip optional / required ones
3429e5dd7070Spatrick // when generating the other list.
3430e5dd7070Spatrick if (isProtocol && (protocolOptionalProperties != PD->isOptional()))
3431e5dd7070Spatrick continue;
3432e5dd7070Spatrick // Don't emit duplicate metadata for properties that were already in a
3433e5dd7070Spatrick // class extension.
3434e5dd7070Spatrick if (!PropertySet.insert(PD->getIdentifier()).second)
3435e5dd7070Spatrick continue;
3436e5dd7070Spatrick
3437e5dd7070Spatrick Properties.push_back(PD);
3438e5dd7070Spatrick }
3439e5dd7070Spatrick
3440e5dd7070Spatrick if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
3441e5dd7070Spatrick for (const auto *P : OID->all_referenced_protocols())
3442e5dd7070Spatrick collectProtocolProperties(P);
3443e5dd7070Spatrick else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD))
3444e5dd7070Spatrick for (const auto *P : CD->protocols())
3445e5dd7070Spatrick collectProtocolProperties(P);
3446e5dd7070Spatrick
3447e5dd7070Spatrick auto numProperties = Properties.size();
3448e5dd7070Spatrick
3449e5dd7070Spatrick if (numProperties == 0)
3450e5dd7070Spatrick return NULLPtr;
3451e5dd7070Spatrick
3452e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3453e5dd7070Spatrick auto propertyList = builder.beginStruct();
3454e5dd7070Spatrick auto properties = PushPropertyListHeader(propertyList, numProperties);
3455e5dd7070Spatrick
3456e5dd7070Spatrick // Add all of the property methods need adding to the method list and to the
3457e5dd7070Spatrick // property metadata list.
3458e5dd7070Spatrick for (auto *property : Properties) {
3459e5dd7070Spatrick bool isSynthesized = false;
3460e5dd7070Spatrick bool isDynamic = false;
3461e5dd7070Spatrick if (!isProtocol) {
3462e5dd7070Spatrick auto *propertyImpl = Context.getObjCPropertyImplDeclForPropertyDecl(property, Container);
3463e5dd7070Spatrick if (propertyImpl) {
3464e5dd7070Spatrick isSynthesized = (propertyImpl->getPropertyImplementation() ==
3465e5dd7070Spatrick ObjCPropertyImplDecl::Synthesize);
3466e5dd7070Spatrick isDynamic = (propertyImpl->getPropertyImplementation() ==
3467e5dd7070Spatrick ObjCPropertyImplDecl::Dynamic);
3468e5dd7070Spatrick }
3469e5dd7070Spatrick }
3470e5dd7070Spatrick PushProperty(properties, property, Container, isSynthesized, isDynamic);
3471e5dd7070Spatrick }
3472e5dd7070Spatrick properties.finishAndAddTo(propertyList);
3473e5dd7070Spatrick
3474e5dd7070Spatrick return propertyList.finishAndCreateGlobal(".objc_property_list",
3475e5dd7070Spatrick CGM.getPointerAlign());
3476e5dd7070Spatrick }
3477e5dd7070Spatrick
RegisterAlias(const ObjCCompatibleAliasDecl * OAD)3478e5dd7070Spatrick void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
3479e5dd7070Spatrick // Get the class declaration for which the alias is specified.
3480e5dd7070Spatrick ObjCInterfaceDecl *ClassDecl =
3481e5dd7070Spatrick const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
3482e5dd7070Spatrick ClassAliases.emplace_back(ClassDecl->getNameAsString(),
3483e5dd7070Spatrick OAD->getNameAsString());
3484e5dd7070Spatrick }
3485e5dd7070Spatrick
GenerateClass(const ObjCImplementationDecl * OID)3486e5dd7070Spatrick void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
3487e5dd7070Spatrick ASTContext &Context = CGM.getContext();
3488e5dd7070Spatrick
3489e5dd7070Spatrick // Get the superclass name.
3490e5dd7070Spatrick const ObjCInterfaceDecl * SuperClassDecl =
3491e5dd7070Spatrick OID->getClassInterface()->getSuperClass();
3492e5dd7070Spatrick std::string SuperClassName;
3493e5dd7070Spatrick if (SuperClassDecl) {
3494e5dd7070Spatrick SuperClassName = SuperClassDecl->getNameAsString();
3495e5dd7070Spatrick EmitClassRef(SuperClassName);
3496e5dd7070Spatrick }
3497e5dd7070Spatrick
3498e5dd7070Spatrick // Get the class name
3499e5dd7070Spatrick ObjCInterfaceDecl *ClassDecl =
3500e5dd7070Spatrick const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
3501e5dd7070Spatrick std::string ClassName = ClassDecl->getNameAsString();
3502e5dd7070Spatrick
3503e5dd7070Spatrick // Emit the symbol that is used to generate linker errors if this class is
3504e5dd7070Spatrick // referenced in other modules but not declared.
3505e5dd7070Spatrick std::string classSymbolName = "__objc_class_name_" + ClassName;
3506e5dd7070Spatrick if (auto *symbol = TheModule.getGlobalVariable(classSymbolName)) {
3507e5dd7070Spatrick symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
3508e5dd7070Spatrick } else {
3509e5dd7070Spatrick new llvm::GlobalVariable(TheModule, LongTy, false,
3510e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage,
3511e5dd7070Spatrick llvm::ConstantInt::get(LongTy, 0),
3512e5dd7070Spatrick classSymbolName);
3513e5dd7070Spatrick }
3514e5dd7070Spatrick
3515e5dd7070Spatrick // Get the size of instances.
3516e5dd7070Spatrick int instanceSize =
3517e5dd7070Spatrick Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
3518e5dd7070Spatrick
3519e5dd7070Spatrick // Collect information about instance variables.
3520e5dd7070Spatrick SmallVector<llvm::Constant*, 16> IvarNames;
3521e5dd7070Spatrick SmallVector<llvm::Constant*, 16> IvarTypes;
3522e5dd7070Spatrick SmallVector<llvm::Constant*, 16> IvarOffsets;
3523e5dd7070Spatrick SmallVector<llvm::Constant*, 16> IvarAligns;
3524e5dd7070Spatrick SmallVector<Qualifiers::ObjCLifetime, 16> IvarOwnership;
3525e5dd7070Spatrick
3526e5dd7070Spatrick ConstantInitBuilder IvarOffsetBuilder(CGM);
3527e5dd7070Spatrick auto IvarOffsetValues = IvarOffsetBuilder.beginArray(PtrToIntTy);
3528e5dd7070Spatrick SmallVector<bool, 16> WeakIvars;
3529e5dd7070Spatrick SmallVector<bool, 16> StrongIvars;
3530e5dd7070Spatrick
3531e5dd7070Spatrick int superInstanceSize = !SuperClassDecl ? 0 :
3532e5dd7070Spatrick Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
3533e5dd7070Spatrick // For non-fragile ivars, set the instance size to 0 - {the size of just this
3534e5dd7070Spatrick // class}. The runtime will then set this to the correct value on load.
3535e5dd7070Spatrick if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
3536e5dd7070Spatrick instanceSize = 0 - (instanceSize - superInstanceSize);
3537e5dd7070Spatrick }
3538e5dd7070Spatrick
3539e5dd7070Spatrick for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
3540e5dd7070Spatrick IVD = IVD->getNextIvar()) {
3541e5dd7070Spatrick // Store the name
3542e5dd7070Spatrick IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
3543e5dd7070Spatrick // Get the type encoding for this ivar
3544e5dd7070Spatrick std::string TypeStr;
3545e5dd7070Spatrick Context.getObjCEncodingForType(IVD->getType(), TypeStr, IVD);
3546e5dd7070Spatrick IvarTypes.push_back(MakeConstantString(TypeStr));
3547e5dd7070Spatrick IvarAligns.push_back(llvm::ConstantInt::get(IntTy,
3548e5dd7070Spatrick Context.getTypeSize(IVD->getType())));
3549e5dd7070Spatrick // Get the offset
3550e5dd7070Spatrick uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
3551e5dd7070Spatrick uint64_t Offset = BaseOffset;
3552e5dd7070Spatrick if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
3553e5dd7070Spatrick Offset = BaseOffset - superInstanceSize;
3554e5dd7070Spatrick }
3555e5dd7070Spatrick llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
3556e5dd7070Spatrick // Create the direct offset value
3557e5dd7070Spatrick std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
3558e5dd7070Spatrick IVD->getNameAsString();
3559e5dd7070Spatrick
3560e5dd7070Spatrick llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
3561e5dd7070Spatrick if (OffsetVar) {
3562e5dd7070Spatrick OffsetVar->setInitializer(OffsetValue);
3563e5dd7070Spatrick // If this is the real definition, change its linkage type so that
3564e5dd7070Spatrick // different modules will use this one, rather than their private
3565e5dd7070Spatrick // copy.
3566e5dd7070Spatrick OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
3567e5dd7070Spatrick } else
3568e5dd7070Spatrick OffsetVar = new llvm::GlobalVariable(TheModule, Int32Ty,
3569e5dd7070Spatrick false, llvm::GlobalValue::ExternalLinkage,
3570e5dd7070Spatrick OffsetValue, OffsetName);
3571e5dd7070Spatrick IvarOffsets.push_back(OffsetValue);
3572e5dd7070Spatrick IvarOffsetValues.add(OffsetVar);
3573e5dd7070Spatrick Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
3574e5dd7070Spatrick IvarOwnership.push_back(lt);
3575e5dd7070Spatrick switch (lt) {
3576e5dd7070Spatrick case Qualifiers::OCL_Strong:
3577e5dd7070Spatrick StrongIvars.push_back(true);
3578e5dd7070Spatrick WeakIvars.push_back(false);
3579e5dd7070Spatrick break;
3580e5dd7070Spatrick case Qualifiers::OCL_Weak:
3581e5dd7070Spatrick StrongIvars.push_back(false);
3582e5dd7070Spatrick WeakIvars.push_back(true);
3583e5dd7070Spatrick break;
3584e5dd7070Spatrick default:
3585e5dd7070Spatrick StrongIvars.push_back(false);
3586e5dd7070Spatrick WeakIvars.push_back(false);
3587e5dd7070Spatrick }
3588e5dd7070Spatrick }
3589e5dd7070Spatrick llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
3590e5dd7070Spatrick llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
3591e5dd7070Spatrick llvm::GlobalVariable *IvarOffsetArray =
3592e5dd7070Spatrick IvarOffsetValues.finishAndCreateGlobal(".ivar.offsets",
3593e5dd7070Spatrick CGM.getPointerAlign());
3594e5dd7070Spatrick
3595e5dd7070Spatrick // Collect information about instance methods
3596e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> InstanceMethods;
3597e5dd7070Spatrick InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(),
3598e5dd7070Spatrick OID->instmeth_end());
3599e5dd7070Spatrick
3600e5dd7070Spatrick SmallVector<const ObjCMethodDecl*, 16> ClassMethods;
3601e5dd7070Spatrick ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(),
3602e5dd7070Spatrick OID->classmeth_end());
3603e5dd7070Spatrick
3604e5dd7070Spatrick llvm::Constant *Properties = GeneratePropertyList(OID, ClassDecl);
3605e5dd7070Spatrick
3606e5dd7070Spatrick // Collect the names of referenced protocols
3607a9ac8606Spatrick auto RefProtocols = ClassDecl->protocols();
3608a9ac8606Spatrick auto RuntimeProtocols =
3609a9ac8606Spatrick GetRuntimeProtocolList(RefProtocols.begin(), RefProtocols.end());
3610e5dd7070Spatrick SmallVector<std::string, 16> Protocols;
3611a9ac8606Spatrick for (const auto *I : RuntimeProtocols)
3612e5dd7070Spatrick Protocols.push_back(I->getNameAsString());
3613e5dd7070Spatrick
3614e5dd7070Spatrick // Get the superclass pointer.
3615e5dd7070Spatrick llvm::Constant *SuperClass;
3616e5dd7070Spatrick if (!SuperClassName.empty()) {
3617e5dd7070Spatrick SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
3618e5dd7070Spatrick } else {
3619e5dd7070Spatrick SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
3620e5dd7070Spatrick }
3621e5dd7070Spatrick // Empty vector used to construct empty method lists
3622e5dd7070Spatrick SmallVector<llvm::Constant*, 1> empty;
3623e5dd7070Spatrick // Generate the method and instance variable lists
3624e5dd7070Spatrick llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
3625e5dd7070Spatrick InstanceMethods, false);
3626e5dd7070Spatrick llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
3627e5dd7070Spatrick ClassMethods, true);
3628e5dd7070Spatrick llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
3629e5dd7070Spatrick IvarOffsets, IvarAligns, IvarOwnership);
3630e5dd7070Spatrick // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
3631e5dd7070Spatrick // we emit a symbol containing the offset for each ivar in the class. This
3632e5dd7070Spatrick // allows code compiled for the non-Fragile ABI to inherit from code compiled
3633e5dd7070Spatrick // for the legacy ABI, without causing problems. The converse is also
3634e5dd7070Spatrick // possible, but causes all ivar accesses to be fragile.
3635e5dd7070Spatrick
3636e5dd7070Spatrick // Offset pointer for getting at the correct field in the ivar list when
3637e5dd7070Spatrick // setting up the alias. These are: The base address for the global, the
3638e5dd7070Spatrick // ivar array (second field), the ivar in this list (set for each ivar), and
3639e5dd7070Spatrick // the offset (third field in ivar structure)
3640e5dd7070Spatrick llvm::Type *IndexTy = Int32Ty;
3641e5dd7070Spatrick llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
3642e5dd7070Spatrick llvm::ConstantInt::get(IndexTy, ClassABIVersion > 1 ? 2 : 1), nullptr,
3643e5dd7070Spatrick llvm::ConstantInt::get(IndexTy, ClassABIVersion > 1 ? 3 : 2) };
3644e5dd7070Spatrick
3645e5dd7070Spatrick unsigned ivarIndex = 0;
3646e5dd7070Spatrick for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
3647e5dd7070Spatrick IVD = IVD->getNextIvar()) {
3648e5dd7070Spatrick const std::string Name = GetIVarOffsetVariableName(ClassDecl, IVD);
3649e5dd7070Spatrick offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
3650e5dd7070Spatrick // Get the correct ivar field
3651e5dd7070Spatrick llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
3652e5dd7070Spatrick cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList,
3653e5dd7070Spatrick offsetPointerIndexes);
3654e5dd7070Spatrick // Get the existing variable, if one exists.
3655e5dd7070Spatrick llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
3656e5dd7070Spatrick if (offset) {
3657e5dd7070Spatrick offset->setInitializer(offsetValue);
3658e5dd7070Spatrick // If this is the real definition, change its linkage type so that
3659e5dd7070Spatrick // different modules will use this one, rather than their private
3660e5dd7070Spatrick // copy.
3661e5dd7070Spatrick offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
3662e5dd7070Spatrick } else
3663e5dd7070Spatrick // Add a new alias if there isn't one already.
3664e5dd7070Spatrick new llvm::GlobalVariable(TheModule, offsetValue->getType(),
3665e5dd7070Spatrick false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
3666e5dd7070Spatrick ++ivarIndex;
3667e5dd7070Spatrick }
3668e5dd7070Spatrick llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
3669e5dd7070Spatrick
3670e5dd7070Spatrick //Generate metaclass for class methods
3671e5dd7070Spatrick llvm::Constant *MetaClassStruct = GenerateClassStructure(
3672e5dd7070Spatrick NULLPtr, NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0],
3673e5dd7070Spatrick NULLPtr, ClassMethodList, NULLPtr, NULLPtr,
3674e5dd7070Spatrick GeneratePropertyList(OID, ClassDecl, true), ZeroPtr, ZeroPtr, true);
3675e5dd7070Spatrick CGM.setGVProperties(cast<llvm::GlobalValue>(MetaClassStruct),
3676e5dd7070Spatrick OID->getClassInterface());
3677e5dd7070Spatrick
3678e5dd7070Spatrick // Generate the class structure
3679e5dd7070Spatrick llvm::Constant *ClassStruct = GenerateClassStructure(
3680e5dd7070Spatrick MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr,
3681e5dd7070Spatrick llvm::ConstantInt::get(LongTy, instanceSize), IvarList, MethodList,
3682e5dd7070Spatrick GenerateProtocolList(Protocols), IvarOffsetArray, Properties,
3683e5dd7070Spatrick StrongIvarBitmap, WeakIvarBitmap);
3684e5dd7070Spatrick CGM.setGVProperties(cast<llvm::GlobalValue>(ClassStruct),
3685e5dd7070Spatrick OID->getClassInterface());
3686e5dd7070Spatrick
3687e5dd7070Spatrick // Resolve the class aliases, if they exist.
3688e5dd7070Spatrick if (ClassPtrAlias) {
3689e5dd7070Spatrick ClassPtrAlias->replaceAllUsesWith(
3690e5dd7070Spatrick llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
3691e5dd7070Spatrick ClassPtrAlias->eraseFromParent();
3692e5dd7070Spatrick ClassPtrAlias = nullptr;
3693e5dd7070Spatrick }
3694e5dd7070Spatrick if (MetaClassPtrAlias) {
3695e5dd7070Spatrick MetaClassPtrAlias->replaceAllUsesWith(
3696e5dd7070Spatrick llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
3697e5dd7070Spatrick MetaClassPtrAlias->eraseFromParent();
3698e5dd7070Spatrick MetaClassPtrAlias = nullptr;
3699e5dd7070Spatrick }
3700e5dd7070Spatrick
3701e5dd7070Spatrick // Add class structure to list to be added to the symtab later
3702e5dd7070Spatrick ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
3703e5dd7070Spatrick Classes.push_back(ClassStruct);
3704e5dd7070Spatrick }
3705e5dd7070Spatrick
ModuleInitFunction()3706e5dd7070Spatrick llvm::Function *CGObjCGNU::ModuleInitFunction() {
3707e5dd7070Spatrick // Only emit an ObjC load function if no Objective-C stuff has been called
3708e5dd7070Spatrick if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
3709e5dd7070Spatrick ExistingProtocols.empty() && SelectorTable.empty())
3710e5dd7070Spatrick return nullptr;
3711e5dd7070Spatrick
3712e5dd7070Spatrick // Add all referenced protocols to a category.
3713e5dd7070Spatrick GenerateProtocolHolderCategory();
3714e5dd7070Spatrick
3715*12c85518Srobert llvm::StructType *selStructTy = dyn_cast<llvm::StructType>(SelectorElemTy);
3716e5dd7070Spatrick llvm::Type *selStructPtrTy = SelectorTy;
3717e5dd7070Spatrick if (!selStructTy) {
3718e5dd7070Spatrick selStructTy = llvm::StructType::get(CGM.getLLVMContext(),
3719e5dd7070Spatrick { PtrToInt8Ty, PtrToInt8Ty });
3720e5dd7070Spatrick selStructPtrTy = llvm::PointerType::getUnqual(selStructTy);
3721e5dd7070Spatrick }
3722e5dd7070Spatrick
3723e5dd7070Spatrick // Generate statics list:
3724e5dd7070Spatrick llvm::Constant *statics = NULLPtr;
3725e5dd7070Spatrick if (!ConstantStrings.empty()) {
3726e5dd7070Spatrick llvm::GlobalVariable *fileStatics = [&] {
3727e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3728e5dd7070Spatrick auto staticsStruct = builder.beginStruct();
3729e5dd7070Spatrick
3730e5dd7070Spatrick StringRef stringClass = CGM.getLangOpts().ObjCConstantStringClass;
3731e5dd7070Spatrick if (stringClass.empty()) stringClass = "NXConstantString";
3732e5dd7070Spatrick staticsStruct.add(MakeConstantString(stringClass,
3733e5dd7070Spatrick ".objc_static_class_name"));
3734e5dd7070Spatrick
3735e5dd7070Spatrick auto array = staticsStruct.beginArray();
3736e5dd7070Spatrick array.addAll(ConstantStrings);
3737e5dd7070Spatrick array.add(NULLPtr);
3738e5dd7070Spatrick array.finishAndAddTo(staticsStruct);
3739e5dd7070Spatrick
3740e5dd7070Spatrick return staticsStruct.finishAndCreateGlobal(".objc_statics",
3741e5dd7070Spatrick CGM.getPointerAlign());
3742e5dd7070Spatrick }();
3743e5dd7070Spatrick
3744e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3745e5dd7070Spatrick auto allStaticsArray = builder.beginArray(fileStatics->getType());
3746e5dd7070Spatrick allStaticsArray.add(fileStatics);
3747e5dd7070Spatrick allStaticsArray.addNullPointer(fileStatics->getType());
3748e5dd7070Spatrick
3749e5dd7070Spatrick statics = allStaticsArray.finishAndCreateGlobal(".objc_statics_ptr",
3750e5dd7070Spatrick CGM.getPointerAlign());
3751e5dd7070Spatrick statics = llvm::ConstantExpr::getBitCast(statics, PtrTy);
3752e5dd7070Spatrick }
3753e5dd7070Spatrick
3754e5dd7070Spatrick // Array of classes, categories, and constant objects.
3755e5dd7070Spatrick
3756e5dd7070Spatrick SmallVector<llvm::GlobalAlias*, 16> selectorAliases;
3757e5dd7070Spatrick unsigned selectorCount;
3758e5dd7070Spatrick
3759e5dd7070Spatrick // Pointer to an array of selectors used in this module.
3760e5dd7070Spatrick llvm::GlobalVariable *selectorList = [&] {
3761e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3762e5dd7070Spatrick auto selectors = builder.beginArray(selStructTy);
3763e5dd7070Spatrick auto &table = SelectorTable; // MSVC workaround
3764e5dd7070Spatrick std::vector<Selector> allSelectors;
3765e5dd7070Spatrick for (auto &entry : table)
3766e5dd7070Spatrick allSelectors.push_back(entry.first);
3767e5dd7070Spatrick llvm::sort(allSelectors);
3768e5dd7070Spatrick
3769e5dd7070Spatrick for (auto &untypedSel : allSelectors) {
3770e5dd7070Spatrick std::string selNameStr = untypedSel.getAsString();
3771e5dd7070Spatrick llvm::Constant *selName = ExportUniqueString(selNameStr, ".objc_sel_name");
3772e5dd7070Spatrick
3773e5dd7070Spatrick for (TypedSelector &sel : table[untypedSel]) {
3774e5dd7070Spatrick llvm::Constant *selectorTypeEncoding = NULLPtr;
3775e5dd7070Spatrick if (!sel.first.empty())
3776e5dd7070Spatrick selectorTypeEncoding =
3777e5dd7070Spatrick MakeConstantString(sel.first, ".objc_sel_types");
3778e5dd7070Spatrick
3779e5dd7070Spatrick auto selStruct = selectors.beginStruct(selStructTy);
3780e5dd7070Spatrick selStruct.add(selName);
3781e5dd7070Spatrick selStruct.add(selectorTypeEncoding);
3782e5dd7070Spatrick selStruct.finishAndAddTo(selectors);
3783e5dd7070Spatrick
3784e5dd7070Spatrick // Store the selector alias for later replacement
3785e5dd7070Spatrick selectorAliases.push_back(sel.second);
3786e5dd7070Spatrick }
3787e5dd7070Spatrick }
3788e5dd7070Spatrick
3789e5dd7070Spatrick // Remember the number of entries in the selector table.
3790e5dd7070Spatrick selectorCount = selectors.size();
3791e5dd7070Spatrick
3792e5dd7070Spatrick // NULL-terminate the selector list. This should not actually be required,
3793e5dd7070Spatrick // because the selector list has a length field. Unfortunately, the GCC
3794e5dd7070Spatrick // runtime decides to ignore the length field and expects a NULL terminator,
3795e5dd7070Spatrick // and GCC cooperates with this by always setting the length to 0.
3796e5dd7070Spatrick auto selStruct = selectors.beginStruct(selStructTy);
3797e5dd7070Spatrick selStruct.add(NULLPtr);
3798e5dd7070Spatrick selStruct.add(NULLPtr);
3799e5dd7070Spatrick selStruct.finishAndAddTo(selectors);
3800e5dd7070Spatrick
3801e5dd7070Spatrick return selectors.finishAndCreateGlobal(".objc_selector_list",
3802e5dd7070Spatrick CGM.getPointerAlign());
3803e5dd7070Spatrick }();
3804e5dd7070Spatrick
3805e5dd7070Spatrick // Now that all of the static selectors exist, create pointers to them.
3806e5dd7070Spatrick for (unsigned i = 0; i < selectorCount; ++i) {
3807e5dd7070Spatrick llvm::Constant *idxs[] = {
3808e5dd7070Spatrick Zeros[0],
3809e5dd7070Spatrick llvm::ConstantInt::get(Int32Ty, i)
3810e5dd7070Spatrick };
3811e5dd7070Spatrick // FIXME: We're generating redundant loads and stores here!
3812e5dd7070Spatrick llvm::Constant *selPtr = llvm::ConstantExpr::getGetElementPtr(
3813e5dd7070Spatrick selectorList->getValueType(), selectorList, idxs);
3814e5dd7070Spatrick // If selectors are defined as an opaque type, cast the pointer to this
3815e5dd7070Spatrick // type.
3816e5dd7070Spatrick selPtr = llvm::ConstantExpr::getBitCast(selPtr, SelectorTy);
3817e5dd7070Spatrick selectorAliases[i]->replaceAllUsesWith(selPtr);
3818e5dd7070Spatrick selectorAliases[i]->eraseFromParent();
3819e5dd7070Spatrick }
3820e5dd7070Spatrick
3821e5dd7070Spatrick llvm::GlobalVariable *symtab = [&] {
3822e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3823e5dd7070Spatrick auto symtab = builder.beginStruct();
3824e5dd7070Spatrick
3825e5dd7070Spatrick // Number of static selectors
3826e5dd7070Spatrick symtab.addInt(LongTy, selectorCount);
3827e5dd7070Spatrick
3828e5dd7070Spatrick symtab.addBitCast(selectorList, selStructPtrTy);
3829e5dd7070Spatrick
3830e5dd7070Spatrick // Number of classes defined.
3831e5dd7070Spatrick symtab.addInt(CGM.Int16Ty, Classes.size());
3832e5dd7070Spatrick // Number of categories defined
3833e5dd7070Spatrick symtab.addInt(CGM.Int16Ty, Categories.size());
3834e5dd7070Spatrick
3835e5dd7070Spatrick // Create an array of classes, then categories, then static object instances
3836e5dd7070Spatrick auto classList = symtab.beginArray(PtrToInt8Ty);
3837e5dd7070Spatrick classList.addAll(Classes);
3838e5dd7070Spatrick classList.addAll(Categories);
3839e5dd7070Spatrick // NULL-terminated list of static object instances (mainly constant strings)
3840e5dd7070Spatrick classList.add(statics);
3841e5dd7070Spatrick classList.add(NULLPtr);
3842e5dd7070Spatrick classList.finishAndAddTo(symtab);
3843e5dd7070Spatrick
3844e5dd7070Spatrick // Construct the symbol table.
3845e5dd7070Spatrick return symtab.finishAndCreateGlobal("", CGM.getPointerAlign());
3846e5dd7070Spatrick }();
3847e5dd7070Spatrick
3848e5dd7070Spatrick // The symbol table is contained in a module which has some version-checking
3849e5dd7070Spatrick // constants
3850e5dd7070Spatrick llvm::Constant *module = [&] {
3851e5dd7070Spatrick llvm::Type *moduleEltTys[] = {
3852e5dd7070Spatrick LongTy, LongTy, PtrToInt8Ty, symtab->getType(), IntTy
3853e5dd7070Spatrick };
3854*12c85518Srobert llvm::StructType *moduleTy = llvm::StructType::get(
3855*12c85518Srobert CGM.getLLVMContext(),
3856*12c85518Srobert ArrayRef(moduleEltTys).drop_back(unsigned(RuntimeVersion < 10)));
3857e5dd7070Spatrick
3858e5dd7070Spatrick ConstantInitBuilder builder(CGM);
3859e5dd7070Spatrick auto module = builder.beginStruct(moduleTy);
3860e5dd7070Spatrick // Runtime version, used for ABI compatibility checking.
3861e5dd7070Spatrick module.addInt(LongTy, RuntimeVersion);
3862e5dd7070Spatrick // sizeof(ModuleTy)
3863e5dd7070Spatrick module.addInt(LongTy, CGM.getDataLayout().getTypeStoreSize(moduleTy));
3864e5dd7070Spatrick
3865e5dd7070Spatrick // The path to the source file where this module was declared
3866e5dd7070Spatrick SourceManager &SM = CGM.getContext().getSourceManager();
3867*12c85518Srobert OptionalFileEntryRef mainFile = SM.getFileEntryRefForID(SM.getMainFileID());
3868e5dd7070Spatrick std::string path =
3869*12c85518Srobert (mainFile->getDir().getName() + "/" + mainFile->getName()).str();
3870e5dd7070Spatrick module.add(MakeConstantString(path, ".objc_source_file_name"));
3871e5dd7070Spatrick module.add(symtab);
3872e5dd7070Spatrick
3873e5dd7070Spatrick if (RuntimeVersion >= 10) {
3874e5dd7070Spatrick switch (CGM.getLangOpts().getGC()) {
3875e5dd7070Spatrick case LangOptions::GCOnly:
3876e5dd7070Spatrick module.addInt(IntTy, 2);
3877e5dd7070Spatrick break;
3878e5dd7070Spatrick case LangOptions::NonGC:
3879e5dd7070Spatrick if (CGM.getLangOpts().ObjCAutoRefCount)
3880e5dd7070Spatrick module.addInt(IntTy, 1);
3881e5dd7070Spatrick else
3882e5dd7070Spatrick module.addInt(IntTy, 0);
3883e5dd7070Spatrick break;
3884e5dd7070Spatrick case LangOptions::HybridGC:
3885e5dd7070Spatrick module.addInt(IntTy, 1);
3886e5dd7070Spatrick break;
3887e5dd7070Spatrick }
3888e5dd7070Spatrick }
3889e5dd7070Spatrick
3890e5dd7070Spatrick return module.finishAndCreateGlobal("", CGM.getPointerAlign());
3891e5dd7070Spatrick }();
3892e5dd7070Spatrick
3893e5dd7070Spatrick // Create the load function calling the runtime entry point with the module
3894e5dd7070Spatrick // structure
3895e5dd7070Spatrick llvm::Function * LoadFunction = llvm::Function::Create(
3896e5dd7070Spatrick llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
3897e5dd7070Spatrick llvm::GlobalValue::InternalLinkage, ".objc_load_function",
3898e5dd7070Spatrick &TheModule);
3899e5dd7070Spatrick llvm::BasicBlock *EntryBB =
3900e5dd7070Spatrick llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
3901e5dd7070Spatrick CGBuilderTy Builder(CGM, VMContext);
3902e5dd7070Spatrick Builder.SetInsertPoint(EntryBB);
3903e5dd7070Spatrick
3904e5dd7070Spatrick llvm::FunctionType *FT =
3905e5dd7070Spatrick llvm::FunctionType::get(Builder.getVoidTy(), module->getType(), true);
3906e5dd7070Spatrick llvm::FunctionCallee Register =
3907e5dd7070Spatrick CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
3908e5dd7070Spatrick Builder.CreateCall(Register, module);
3909e5dd7070Spatrick
3910e5dd7070Spatrick if (!ClassAliases.empty()) {
3911e5dd7070Spatrick llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
3912e5dd7070Spatrick llvm::FunctionType *RegisterAliasTy =
3913e5dd7070Spatrick llvm::FunctionType::get(Builder.getVoidTy(),
3914e5dd7070Spatrick ArgTypes, false);
3915e5dd7070Spatrick llvm::Function *RegisterAlias = llvm::Function::Create(
3916e5dd7070Spatrick RegisterAliasTy,
3917e5dd7070Spatrick llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
3918e5dd7070Spatrick &TheModule);
3919e5dd7070Spatrick llvm::BasicBlock *AliasBB =
3920e5dd7070Spatrick llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
3921e5dd7070Spatrick llvm::BasicBlock *NoAliasBB =
3922e5dd7070Spatrick llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
3923e5dd7070Spatrick
3924e5dd7070Spatrick // Branch based on whether the runtime provided class_registerAlias_np()
3925e5dd7070Spatrick llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
3926e5dd7070Spatrick llvm::Constant::getNullValue(RegisterAlias->getType()));
3927e5dd7070Spatrick Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
3928e5dd7070Spatrick
3929e5dd7070Spatrick // The true branch (has alias registration function):
3930e5dd7070Spatrick Builder.SetInsertPoint(AliasBB);
3931e5dd7070Spatrick // Emit alias registration calls:
3932e5dd7070Spatrick for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
3933e5dd7070Spatrick iter != ClassAliases.end(); ++iter) {
3934e5dd7070Spatrick llvm::Constant *TheClass =
3935e5dd7070Spatrick TheModule.getGlobalVariable("_OBJC_CLASS_" + iter->first, true);
3936e5dd7070Spatrick if (TheClass) {
3937e5dd7070Spatrick TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
3938e5dd7070Spatrick Builder.CreateCall(RegisterAlias,
3939e5dd7070Spatrick {TheClass, MakeConstantString(iter->second)});
3940e5dd7070Spatrick }
3941e5dd7070Spatrick }
3942e5dd7070Spatrick // Jump to end:
3943e5dd7070Spatrick Builder.CreateBr(NoAliasBB);
3944e5dd7070Spatrick
3945e5dd7070Spatrick // Missing alias registration function, just return from the function:
3946e5dd7070Spatrick Builder.SetInsertPoint(NoAliasBB);
3947e5dd7070Spatrick }
3948e5dd7070Spatrick Builder.CreateRetVoid();
3949e5dd7070Spatrick
3950e5dd7070Spatrick return LoadFunction;
3951e5dd7070Spatrick }
3952e5dd7070Spatrick
GenerateMethod(const ObjCMethodDecl * OMD,const ObjCContainerDecl * CD)3953e5dd7070Spatrick llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
3954e5dd7070Spatrick const ObjCContainerDecl *CD) {
3955e5dd7070Spatrick CodeGenTypes &Types = CGM.getTypes();
3956e5dd7070Spatrick llvm::FunctionType *MethodTy =
3957e5dd7070Spatrick Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
3958a9ac8606Spatrick std::string FunctionName = getSymbolNameForMethod(OMD);
3959e5dd7070Spatrick
3960e5dd7070Spatrick llvm::Function *Method
3961e5dd7070Spatrick = llvm::Function::Create(MethodTy,
3962e5dd7070Spatrick llvm::GlobalValue::InternalLinkage,
3963e5dd7070Spatrick FunctionName,
3964e5dd7070Spatrick &TheModule);
3965e5dd7070Spatrick return Method;
3966e5dd7070Spatrick }
3967e5dd7070Spatrick
GenerateDirectMethodPrologue(CodeGenFunction & CGF,llvm::Function * Fn,const ObjCMethodDecl * OMD,const ObjCContainerDecl * CD)3968e5dd7070Spatrick void CGObjCGNU::GenerateDirectMethodPrologue(CodeGenFunction &CGF,
3969e5dd7070Spatrick llvm::Function *Fn,
3970e5dd7070Spatrick const ObjCMethodDecl *OMD,
3971e5dd7070Spatrick const ObjCContainerDecl *CD) {
3972e5dd7070Spatrick // GNU runtime doesn't support direct calls at this time
3973e5dd7070Spatrick }
3974e5dd7070Spatrick
GetPropertyGetFunction()3975e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetPropertyGetFunction() {
3976e5dd7070Spatrick return GetPropertyFn;
3977e5dd7070Spatrick }
3978e5dd7070Spatrick
GetPropertySetFunction()3979e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetPropertySetFunction() {
3980e5dd7070Spatrick return SetPropertyFn;
3981e5dd7070Spatrick }
3982e5dd7070Spatrick
GetOptimizedPropertySetFunction(bool atomic,bool copy)3983e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
3984e5dd7070Spatrick bool copy) {
3985e5dd7070Spatrick return nullptr;
3986e5dd7070Spatrick }
3987e5dd7070Spatrick
GetGetStructFunction()3988e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetGetStructFunction() {
3989e5dd7070Spatrick return GetStructPropertyFn;
3990e5dd7070Spatrick }
3991e5dd7070Spatrick
GetSetStructFunction()3992e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetSetStructFunction() {
3993e5dd7070Spatrick return SetStructPropertyFn;
3994e5dd7070Spatrick }
3995e5dd7070Spatrick
GetCppAtomicObjectGetFunction()3996e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetCppAtomicObjectGetFunction() {
3997e5dd7070Spatrick return nullptr;
3998e5dd7070Spatrick }
3999e5dd7070Spatrick
GetCppAtomicObjectSetFunction()4000e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::GetCppAtomicObjectSetFunction() {
4001e5dd7070Spatrick return nullptr;
4002e5dd7070Spatrick }
4003e5dd7070Spatrick
EnumerationMutationFunction()4004e5dd7070Spatrick llvm::FunctionCallee CGObjCGNU::EnumerationMutationFunction() {
4005e5dd7070Spatrick return EnumerationMutationFn;
4006e5dd7070Spatrick }
4007e5dd7070Spatrick
EmitSynchronizedStmt(CodeGenFunction & CGF,const ObjCAtSynchronizedStmt & S)4008e5dd7070Spatrick void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
4009e5dd7070Spatrick const ObjCAtSynchronizedStmt &S) {
4010e5dd7070Spatrick EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
4011e5dd7070Spatrick }
4012e5dd7070Spatrick
4013e5dd7070Spatrick
EmitTryStmt(CodeGenFunction & CGF,const ObjCAtTryStmt & S)4014e5dd7070Spatrick void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
4015e5dd7070Spatrick const ObjCAtTryStmt &S) {
4016e5dd7070Spatrick // Unlike the Apple non-fragile runtimes, which also uses
4017e5dd7070Spatrick // unwind-based zero cost exceptions, the GNU Objective C runtime's
4018e5dd7070Spatrick // EH support isn't a veneer over C++ EH. Instead, exception
4019e5dd7070Spatrick // objects are created by objc_exception_throw and destroyed by
4020e5dd7070Spatrick // the personality function; this avoids the need for bracketing
4021e5dd7070Spatrick // catch handlers with calls to __blah_begin_catch/__blah_end_catch
4022e5dd7070Spatrick // (or even _Unwind_DeleteException), but probably doesn't
4023e5dd7070Spatrick // interoperate very well with foreign exceptions.
4024e5dd7070Spatrick //
4025e5dd7070Spatrick // In Objective-C++ mode, we actually emit something equivalent to the C++
4026e5dd7070Spatrick // exception handler.
4027e5dd7070Spatrick EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
4028e5dd7070Spatrick }
4029e5dd7070Spatrick
EmitThrowStmt(CodeGenFunction & CGF,const ObjCAtThrowStmt & S,bool ClearInsertionPoint)4030e5dd7070Spatrick void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
4031e5dd7070Spatrick const ObjCAtThrowStmt &S,
4032e5dd7070Spatrick bool ClearInsertionPoint) {
4033e5dd7070Spatrick llvm::Value *ExceptionAsObject;
4034e5dd7070Spatrick bool isRethrow = false;
4035e5dd7070Spatrick
4036e5dd7070Spatrick if (const Expr *ThrowExpr = S.getThrowExpr()) {
4037e5dd7070Spatrick llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
4038e5dd7070Spatrick ExceptionAsObject = Exception;
4039e5dd7070Spatrick } else {
4040e5dd7070Spatrick assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
4041e5dd7070Spatrick "Unexpected rethrow outside @catch block.");
4042e5dd7070Spatrick ExceptionAsObject = CGF.ObjCEHValueStack.back();
4043e5dd7070Spatrick isRethrow = true;
4044e5dd7070Spatrick }
4045e5dd7070Spatrick if (isRethrow && usesSEHExceptions) {
4046e5dd7070Spatrick // For SEH, ExceptionAsObject may be undef, because the catch handler is
4047e5dd7070Spatrick // not passed it for catchalls and so it is not visible to the catch
4048e5dd7070Spatrick // funclet. The real thrown object will still be live on the stack at this
4049e5dd7070Spatrick // point and will be rethrown. If we are explicitly rethrowing the object
4050e5dd7070Spatrick // that was passed into the `@catch` block, then this code path is not
4051e5dd7070Spatrick // reached and we will instead call `objc_exception_throw` with an explicit
4052e5dd7070Spatrick // argument.
4053e5dd7070Spatrick llvm::CallBase *Throw = CGF.EmitRuntimeCallOrInvoke(ExceptionReThrowFn);
4054e5dd7070Spatrick Throw->setDoesNotReturn();
4055e5dd7070Spatrick }
4056e5dd7070Spatrick else {
4057e5dd7070Spatrick ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
4058e5dd7070Spatrick llvm::CallBase *Throw =
4059e5dd7070Spatrick CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject);
4060e5dd7070Spatrick Throw->setDoesNotReturn();
4061e5dd7070Spatrick }
4062e5dd7070Spatrick CGF.Builder.CreateUnreachable();
4063e5dd7070Spatrick if (ClearInsertionPoint)
4064e5dd7070Spatrick CGF.Builder.ClearInsertionPoint();
4065e5dd7070Spatrick }
4066e5dd7070Spatrick
EmitObjCWeakRead(CodeGenFunction & CGF,Address AddrWeakObj)4067e5dd7070Spatrick llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
4068e5dd7070Spatrick Address AddrWeakObj) {
4069e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4070*12c85518Srobert return B.CreateCall(WeakReadFn,
4071*12c85518Srobert EnforceType(B, AddrWeakObj.getPointer(), PtrToIdTy));
4072e5dd7070Spatrick }
4073e5dd7070Spatrick
EmitObjCWeakAssign(CodeGenFunction & CGF,llvm::Value * src,Address dst)4074e5dd7070Spatrick void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
4075e5dd7070Spatrick llvm::Value *src, Address dst) {
4076e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4077e5dd7070Spatrick src = EnforceType(B, src, IdTy);
4078*12c85518Srobert llvm::Value *dstVal = EnforceType(B, dst.getPointer(), PtrToIdTy);
4079*12c85518Srobert B.CreateCall(WeakAssignFn, {src, dstVal});
4080e5dd7070Spatrick }
4081e5dd7070Spatrick
EmitObjCGlobalAssign(CodeGenFunction & CGF,llvm::Value * src,Address dst,bool threadlocal)4082e5dd7070Spatrick void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
4083e5dd7070Spatrick llvm::Value *src, Address dst,
4084e5dd7070Spatrick bool threadlocal) {
4085e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4086e5dd7070Spatrick src = EnforceType(B, src, IdTy);
4087*12c85518Srobert llvm::Value *dstVal = EnforceType(B, dst.getPointer(), PtrToIdTy);
4088e5dd7070Spatrick // FIXME. Add threadloca assign API
4089e5dd7070Spatrick assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI");
4090*12c85518Srobert B.CreateCall(GlobalAssignFn, {src, dstVal});
4091e5dd7070Spatrick }
4092e5dd7070Spatrick
EmitObjCIvarAssign(CodeGenFunction & CGF,llvm::Value * src,Address dst,llvm::Value * ivarOffset)4093e5dd7070Spatrick void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
4094e5dd7070Spatrick llvm::Value *src, Address dst,
4095e5dd7070Spatrick llvm::Value *ivarOffset) {
4096e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4097e5dd7070Spatrick src = EnforceType(B, src, IdTy);
4098*12c85518Srobert llvm::Value *dstVal = EnforceType(B, dst.getPointer(), IdTy);
4099*12c85518Srobert B.CreateCall(IvarAssignFn, {src, dstVal, ivarOffset});
4100e5dd7070Spatrick }
4101e5dd7070Spatrick
EmitObjCStrongCastAssign(CodeGenFunction & CGF,llvm::Value * src,Address dst)4102e5dd7070Spatrick void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
4103e5dd7070Spatrick llvm::Value *src, Address dst) {
4104e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4105e5dd7070Spatrick src = EnforceType(B, src, IdTy);
4106*12c85518Srobert llvm::Value *dstVal = EnforceType(B, dst.getPointer(), PtrToIdTy);
4107*12c85518Srobert B.CreateCall(StrongCastAssignFn, {src, dstVal});
4108e5dd7070Spatrick }
4109e5dd7070Spatrick
EmitGCMemmoveCollectable(CodeGenFunction & CGF,Address DestPtr,Address SrcPtr,llvm::Value * Size)4110e5dd7070Spatrick void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
4111e5dd7070Spatrick Address DestPtr,
4112e5dd7070Spatrick Address SrcPtr,
4113e5dd7070Spatrick llvm::Value *Size) {
4114e5dd7070Spatrick CGBuilderTy &B = CGF.Builder;
4115*12c85518Srobert llvm::Value *DestPtrVal = EnforceType(B, DestPtr.getPointer(), PtrTy);
4116*12c85518Srobert llvm::Value *SrcPtrVal = EnforceType(B, SrcPtr.getPointer(), PtrTy);
4117e5dd7070Spatrick
4118*12c85518Srobert B.CreateCall(MemMoveFn, {DestPtrVal, SrcPtrVal, Size});
4119e5dd7070Spatrick }
4120e5dd7070Spatrick
ObjCIvarOffsetVariable(const ObjCInterfaceDecl * ID,const ObjCIvarDecl * Ivar)4121e5dd7070Spatrick llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
4122e5dd7070Spatrick const ObjCInterfaceDecl *ID,
4123e5dd7070Spatrick const ObjCIvarDecl *Ivar) {
4124e5dd7070Spatrick const std::string Name = GetIVarOffsetVariableName(ID, Ivar);
4125e5dd7070Spatrick // Emit the variable and initialize it with what we think the correct value
4126e5dd7070Spatrick // is. This allows code compiled with non-fragile ivars to work correctly
4127e5dd7070Spatrick // when linked against code which isn't (most of the time).
4128e5dd7070Spatrick llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
4129e5dd7070Spatrick if (!IvarOffsetPointer)
4130e5dd7070Spatrick IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
4131e5dd7070Spatrick llvm::Type::getInt32PtrTy(VMContext), false,
4132e5dd7070Spatrick llvm::GlobalValue::ExternalLinkage, nullptr, Name);
4133e5dd7070Spatrick return IvarOffsetPointer;
4134e5dd7070Spatrick }
4135e5dd7070Spatrick
EmitObjCValueForIvar(CodeGenFunction & CGF,QualType ObjectTy,llvm::Value * BaseValue,const ObjCIvarDecl * Ivar,unsigned CVRQualifiers)4136e5dd7070Spatrick LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
4137e5dd7070Spatrick QualType ObjectTy,
4138e5dd7070Spatrick llvm::Value *BaseValue,
4139e5dd7070Spatrick const ObjCIvarDecl *Ivar,
4140e5dd7070Spatrick unsigned CVRQualifiers) {
4141e5dd7070Spatrick const ObjCInterfaceDecl *ID =
4142e5dd7070Spatrick ObjectTy->castAs<ObjCObjectType>()->getInterface();
4143e5dd7070Spatrick return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4144e5dd7070Spatrick EmitIvarOffset(CGF, ID, Ivar));
4145e5dd7070Spatrick }
4146e5dd7070Spatrick
FindIvarInterface(ASTContext & Context,const ObjCInterfaceDecl * OID,const ObjCIvarDecl * OIVD)4147e5dd7070Spatrick static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
4148e5dd7070Spatrick const ObjCInterfaceDecl *OID,
4149e5dd7070Spatrick const ObjCIvarDecl *OIVD) {
4150e5dd7070Spatrick for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
4151e5dd7070Spatrick next = next->getNextIvar()) {
4152e5dd7070Spatrick if (OIVD == next)
4153e5dd7070Spatrick return OID;
4154e5dd7070Spatrick }
4155e5dd7070Spatrick
4156e5dd7070Spatrick // Otherwise check in the super class.
4157e5dd7070Spatrick if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
4158e5dd7070Spatrick return FindIvarInterface(Context, Super, OIVD);
4159e5dd7070Spatrick
4160e5dd7070Spatrick return nullptr;
4161e5dd7070Spatrick }
4162e5dd7070Spatrick
EmitIvarOffset(CodeGenFunction & CGF,const ObjCInterfaceDecl * Interface,const ObjCIvarDecl * Ivar)4163e5dd7070Spatrick llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
4164e5dd7070Spatrick const ObjCInterfaceDecl *Interface,
4165e5dd7070Spatrick const ObjCIvarDecl *Ivar) {
4166e5dd7070Spatrick if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
4167e5dd7070Spatrick Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
4168e5dd7070Spatrick
4169e5dd7070Spatrick // The MSVC linker cannot have a single global defined as LinkOnceAnyLinkage
4170e5dd7070Spatrick // and ExternalLinkage, so create a reference to the ivar global and rely on
4171e5dd7070Spatrick // the definition being created as part of GenerateClass.
4172e5dd7070Spatrick if (RuntimeVersion < 10 ||
4173e5dd7070Spatrick CGF.CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment())
4174e5dd7070Spatrick return CGF.Builder.CreateZExtOrBitCast(
4175e5dd7070Spatrick CGF.Builder.CreateAlignedLoad(
4176e5dd7070Spatrick Int32Ty, CGF.Builder.CreateAlignedLoad(
4177a9ac8606Spatrick llvm::Type::getInt32PtrTy(VMContext),
4178e5dd7070Spatrick ObjCIvarOffsetVariable(Interface, Ivar),
4179e5dd7070Spatrick CGF.getPointerAlign(), "ivar"),
4180e5dd7070Spatrick CharUnits::fromQuantity(4)),
4181e5dd7070Spatrick PtrDiffTy);
4182e5dd7070Spatrick std::string name = "__objc_ivar_offset_value_" +
4183e5dd7070Spatrick Interface->getNameAsString() +"." + Ivar->getNameAsString();
4184e5dd7070Spatrick CharUnits Align = CGM.getIntAlign();
4185e5dd7070Spatrick llvm::Value *Offset = TheModule.getGlobalVariable(name);
4186e5dd7070Spatrick if (!Offset) {
4187e5dd7070Spatrick auto GV = new llvm::GlobalVariable(TheModule, IntTy,
4188e5dd7070Spatrick false, llvm::GlobalValue::LinkOnceAnyLinkage,
4189e5dd7070Spatrick llvm::Constant::getNullValue(IntTy), name);
4190e5dd7070Spatrick GV->setAlignment(Align.getAsAlign());
4191e5dd7070Spatrick Offset = GV;
4192e5dd7070Spatrick }
4193a9ac8606Spatrick Offset = CGF.Builder.CreateAlignedLoad(IntTy, Offset, Align);
4194e5dd7070Spatrick if (Offset->getType() != PtrDiffTy)
4195e5dd7070Spatrick Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
4196e5dd7070Spatrick return Offset;
4197e5dd7070Spatrick }
4198e5dd7070Spatrick uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
4199e5dd7070Spatrick return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
4200e5dd7070Spatrick }
4201e5dd7070Spatrick
4202e5dd7070Spatrick CGObjCRuntime *
CreateGNUObjCRuntime(CodeGenModule & CGM)4203e5dd7070Spatrick clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
4204e5dd7070Spatrick auto Runtime = CGM.getLangOpts().ObjCRuntime;
4205e5dd7070Spatrick switch (Runtime.getKind()) {
4206e5dd7070Spatrick case ObjCRuntime::GNUstep:
4207e5dd7070Spatrick if (Runtime.getVersion() >= VersionTuple(2, 0))
4208e5dd7070Spatrick return new CGObjCGNUstep2(CGM);
4209e5dd7070Spatrick return new CGObjCGNUstep(CGM);
4210e5dd7070Spatrick
4211e5dd7070Spatrick case ObjCRuntime::GCC:
4212e5dd7070Spatrick return new CGObjCGCC(CGM);
4213e5dd7070Spatrick
4214e5dd7070Spatrick case ObjCRuntime::ObjFW:
4215e5dd7070Spatrick return new CGObjCObjFW(CGM);
4216e5dd7070Spatrick
4217e5dd7070Spatrick case ObjCRuntime::FragileMacOSX:
4218e5dd7070Spatrick case ObjCRuntime::MacOSX:
4219e5dd7070Spatrick case ObjCRuntime::iOS:
4220e5dd7070Spatrick case ObjCRuntime::WatchOS:
4221e5dd7070Spatrick llvm_unreachable("these runtimes are not GNU runtimes");
4222e5dd7070Spatrick }
4223e5dd7070Spatrick llvm_unreachable("bad runtime");
4224e5dd7070Spatrick }
4225