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