clang 23.0.0git
CGObjCGNU.cpp
Go to the documentation of this file.
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"
24#include "clang/AST/Attr.h"
25#include "clang/AST/Decl.h"
26#include "clang/AST/DeclObjC.h"
28#include "clang/AST/StmtObjC.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(RetTy, ArgTys, false);
69 }
70 else {
71 FTy = llvm::FunctionType::get(RetTy, {}, 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(FTy, 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("OBJC_PROTOCOL_") + Name).str();
188 }
189
190 std::string SymbolForProtocolRef(StringRef Name) {
191 return (ManglePublicSymbol("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(std::string(Str), 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);
213 if (!ConstStr) {
214 llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
215 auto *GV = new llvm::GlobalVariable(TheModule, value->getType(), true,
216 llvm::GlobalValue::LinkOnceODRLinkage, value, name);
217 GV->setComdat(TheModule.getOrInsertComdat(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(ObjCRuntime::GNUstep, 1, 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(NameAndAttributes);
239 }
240 return MakeConstantString(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
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(Int8Ty, 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(Int8Ty, attrs & 0xff);
267 // Two padding fields
268 Fields.addInt(Int8Ty, 0);
269 Fields.addInt(Int8Ty, 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, count);
278 // int size; (only in GNUstep v2 ABI.
279 if (isRuntime(ObjCRuntime::GNUstep, 2)) {
280 const llvm::DataLayout &DL = TheModule.getDataLayout();
281 Fields.addInt(IntTy, DL.getTypeSizeInBits(PropertyMetadataTy) /
282 CGM.getContext().getCharWidth());
283 }
284 // struct objc_property_list *next;
285 Fields.add(NULLPtr);
286 // struct objc_property properties[]
287 return Fields.beginArray(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(PropertyMetadataTy);
295 ASTContext &Context = CGM.getContext();
296 Fields.add(MakePropertyEncodingString(property, OCD));
297 PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
298 auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
299 if (accessor) {
300 std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
301 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
302 Fields.add(MakeConstantString(accessor->getSelector().getAsString()));
303 Fields.add(TypeEncoding);
304 } else {
305 Fields.add(NULLPtr);
306 Fields.add(NULLPtr);
307 }
308 };
309 addPropertyMethod(property->getGetterMethodDecl());
310 addPropertyMethod(property->getSetterMethodDecl());
311 Fields.finishAndAddTo(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, 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(I);
501 else
502 RequiredMethods.push_back(I);
503 Required = GenerateProtocolMethodList(RequiredMethods);
504 Optional = GenerateProtocolMethodList(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(M->getSelector(),
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(Builder, Receiver, IdTy),
712 EnforceType(Builder, cmd, SelectorTy) };
713 llvm::CallBase *imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
714 imp->setMetadata(msgSendMDKind, 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(Builder, ObjCSuper.emitRawPointer(CGF), PtrToObjCSuperTy),
723 cmd};
724 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
725 }
726
727public:
728 CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
729 // IMP objc_msg_lookup(id, SEL);
730 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy);
731 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
732 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
733 PtrToObjCSuperTy, 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(Receiver->getType(), CGF.getPointerAlign());
780 Builder.CreateStore(Receiver, ReceiverPtr);
781
782 llvm::Value *self;
783
785 self = CGF.LoadObjCSelf();
786 } else {
787 self = llvm::ConstantPointerNull::get(IdTy);
788 }
789
790 // The lookup function is guaranteed not to capture the receiver pointer.
791 if (auto *LookupFn2 = dyn_cast<llvm::Function>(LookupFn.getCallee()))
792 LookupFn2->addParamAttr(
793 0, llvm::Attribute::getWithCaptureInfo(CGF.getLLVMContext(),
794 llvm::CaptureInfo::none()));
795
796 llvm::Value *args[] = {
797 EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy),
798 EnforceType(Builder, cmd, SelectorTy),
799 EnforceType(Builder, self, IdTy)};
800 llvm::CallBase *slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
801 slot->setOnlyReadsMemory();
802 slot->setMetadata(msgSendMDKind, node);
803
804 // Load the imp from the slot
805 llvm::Value *imp = Builder.CreateAlignedLoad(
806 IMPTy, Builder.CreateStructGEP(SlotStructTy, slot, 4),
807 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(ReceiverPtr, 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(SlotLookupSuperFn, lookupArgs);
823 slot->setOnlyReadsMemory();
824
825 return Builder.CreateAlignedLoad(
826 IMPTy, Builder.CreateStructGEP(SlotStructTy, slot, 4),
827 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(PtrTy, PtrTy, PtrTy, IntTy, IMPTy);
838 SlotTy = PtrTy;
839 // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
840 SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
841 SelectorTy, IdTy);
842 // Slot_t objc_slot_lookup_super(struct objc_super*, SEL);
843 SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
844 PtrToObjCSuperTy, SelectorTy);
845 // If we're in ObjC++ mode, then we want to make
846 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
847 if (usesCxxExceptions) {
848 // void *__cxa_begin_catch(void *e)
849 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
850 // void __cxa_end_catch(void)
851 ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
852 // void objc_exception_rethrow(void*)
853 ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
854 } else if (usesSEHExceptions) {
855 // void objc_exception_rethrow(void)
856 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
857 } else if (CGM.getLangOpts().CPlusPlus) {
858 // void *__cxa_begin_catch(void *e)
859 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
860 // void __cxa_end_catch(void)
861 ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
862 // void _Unwind_Resume_or_Rethrow(void*)
863 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
864 PtrTy);
865 } else if (R.getVersion() >= VersionTuple(1, 7)) {
866 // id objc_begin_catch(void *e)
867 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
868 // void objc_end_catch(void)
869 ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy);
870 // void _Unwind_Resume_or_Rethrow(void*)
871 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
872 }
873 SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
874 SelectorTy, IdTy, PtrDiffTy);
875 SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
876 IdTy, SelectorTy, IdTy, PtrDiffTy);
877 SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
878 IdTy, SelectorTy, IdTy, PtrDiffTy);
879 SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
880 VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy);
881 // void objc_setCppObjectAtomic(void *dest, const void *src, void
882 // *helper);
883 CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
884 PtrTy, PtrTy);
885 // void objc_getCppObjectAtomic(void *dest, const void *src, void
886 // *helper);
887 CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
888 PtrTy, 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("OBJC_WEAK_REF_CLASS_") + Name).str();
990 else
991 return (ManglePublicSymbol("OBJC_REF_CLASS_") + Name).str();
992 }
993 /// Generate the name of a class symbol.
994 std::string SymbolForClass(StringRef Name) {
995 return (ManglePublicSymbol("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(Arg->getType());
1002 llvm::FunctionType *FT = llvm::FunctionType::get(B.getVoidTy(), Types,
1003 false);
1004 llvm::FunctionCallee Fn = CGM.CreateRuntimeFunction(FT, FunctionName);
1005 B.CreateCall(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(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(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 llvm::ConstantInt::get(Int64Ty, str), 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(StringClass);
1046
1047 llvm::Constant *isa = TheModule.getNamedGlobal(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>(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(isa);
1071 } else {
1072 Fields.addNullPointer(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(&FromPtr, FromPtr + NumU8CodeUnits,
1087 &ToPtr, ToPtr + NumU8CodeUnits, 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(Int32Ty, 2);
1093 // Number of UTF-16 codepoints
1094 Fields.addInt(Int32Ty, StringLength);
1095 // Number of bytes
1096 Fields.addInt(Int32Ty, StringLength * 2);
1097 // Hash. Not currently initialised by the compiler.
1098 Fields.addInt(Int32Ty, 0);
1099 // pointer to the data string.
1100 auto Arr = llvm::ArrayRef(&ToBuf[0], ToPtr + 1);
1101 auto *C = llvm::ConstantDataArray::get(VMContext, 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(Buffer);
1106 } else {
1107 // Flags: 0 indicates ASCII encoding
1108 Fields.addInt(Int32Ty, 0);
1109 // Number of UTF-16 codepoints, each ASCII byte is a UTF-16 codepoint
1110 Fields.addInt(Int32Ty, Str.size());
1111 // Number of bytes
1112 Fields.addInt(Int32Ty, Str.size());
1113 // Hash. Not currently initialised by the compiler.
1114 Fields.addInt(Int32Ty, 0);
1115 // Data pointer
1116 Fields.add(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 isNamed ? StringRef(StringName) : ".objc_string",
1136 Align, false, isNamed ? llvm::GlobalValue::LinkOnceODRLinkage
1137 : llvm::GlobalValue::PrivateLinkage);
1138 ObjCStrGV->setSection(sectionName<ConstantStringSection>());
1139 if (isNamed) {
1140 ObjCStrGV->setComdat(TheModule.getOrInsertComdat(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(Sym, v);
1146 }
1147 ObjCStrings[Str] = ObjCStrGV;
1148 ConstantStrings.push_back(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(PropertyMetadataTy);
1166 ASTContext &Context = CGM.getContext();
1167 Fields.add(MakeConstantString(property->getNameAsString()));
1168 std::string TypeStr =
1169 CGM.getContext().getObjCEncodingForPropertyDecl(property, OCD);
1170 Fields.add(MakeConstantString(TypeStr));
1171 std::string typeStr;
1172 Context.getObjCEncodingForType(property->getType(), typeStr);
1173 Fields.add(MakeConstantString(typeStr));
1174 auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) {
1175 if (accessor) {
1176 std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor);
1177 Fields.add(GetConstantSelector(accessor->getSelector(), TypeStr));
1178 } else {
1179 Fields.add(NULLPtr);
1180 }
1181 };
1182 addPropertyMethod(property->getGetterMethodDecl());
1183 addPropertyMethod(property->getSetterMethodDecl());
1184 Fields.finishAndAddTo(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(CGM.getLLVMContext(),
1196 { 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, Methods.size());
1208 // int size; // sizeof(struct objc_method_description)
1209 const llvm::DataLayout &DL = TheModule.getDataLayout();
1210 MethodList.addInt(IntTy, DL.getTypeSizeInBits(ObjCMethodDescTy) /
1211 CGM.getContext().getCharWidth());
1212 // struct objc_method_description[]
1213 auto MethodArray = MethodList.beginArray(ObjCMethodDescTy);
1214 for (auto *M : Methods) {
1215 auto Method = MethodArray.beginStruct(ObjCMethodDescTy);
1216 Method.add(CGObjCGNU::GetConstantSelector(M));
1217 Method.add(GetTypeString(Context.getObjCEncodingForMethodDecl(M, true)));
1218 Method.finishAndAddTo(MethodArray);
1219 }
1220 MethodArray.finishAndAddTo(MethodList);
1221 return MethodList.finishAndCreateGlobal(".objc_protocol_method_list",
1222 CGM.getPointerAlign());
1223 }
1224 llvm::Constant *GenerateCategoryProtocolList(const ObjCCategoryDecl *OCD)
1225 override {
1226 const auto &ReferencedProtocols = OCD->getReferencedProtocols();
1227 auto RuntimeProtocols = GetRuntimeProtocolList(ReferencedProtocols.begin(),
1228 ReferencedProtocols.end());
1229 SmallVector<llvm::Constant *, 16> Protocols;
1230 for (const auto *PI : RuntimeProtocols)
1231 Protocols.push_back(GenerateProtocolRef(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(Builder, ObjCSuper.emitRawPointer(CGF),
1241 PtrToObjCSuperTy),
1242 cmd};
1243 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
1244 }
1245
1246 llvm::GlobalVariable *GetClassVar(StringRef Name, bool isWeak=false) {
1247 std::string SymbolName = SymbolForClassRef(Name, isWeak);
1248 auto *ClassSymbol = TheModule.getNamedGlobal(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(TUDecl);
1267
1268 const ObjCInterfaceDecl *OID = nullptr;
1269 for (const auto *Result : DC->lookup(&II))
1270 if ((OID = dyn_cast<ObjCInterfaceDecl>(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>(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 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) {
1309 Flag = 1;
1310 break;
1312 Flag = 2;
1313 break;
1315 Flag = 3;
1316 break;
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(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(RefName));
1362 GV->setSection(sectionName<ProtocolReferenceSection>());
1363 GV->setAlignment(CGM.getPointerAlign().getAsAlign());
1364 Ref = GV;
1365 }
1366 EmittedProtocolRef = true;
1367 return CGF.Builder.CreateAlignedLoad(ProtocolPtrTy, Ref,
1368 CGM.getPointerAlign());
1369 }
1370
1371 llvm::Constant *GenerateProtocolList(ArrayRef<llvm::Constant*> Protocols) {
1372 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(ProtocolPtrTy,
1373 Protocols.size());
1374 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1375 Protocols);
1376 ConstantInitBuilder builder(CGM);
1377 auto ProtocolBuilder = builder.beginStruct();
1378 ProtocolBuilder.addNullPointer(PtrTy);
1379 ProtocolBuilder.addInt(SizeTy, Protocols.size());
1380 ProtocolBuilder.add(ProtocolArray);
1381 return ProtocolBuilder.finishAndCreateGlobal(".objc_protocol_list",
1382 CGM.getPointerAlign(), false, 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(ProtocolName);
1397 auto *OldGV = TheModule.getGlobalVariable(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(PD->protocol_begin(), PD->protocol_end());
1416 for (const auto *PI : RuntimeProtocols)
1417 Protocols.push_back(GenerateProtocolRef(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(PD->instance_methods(), InstanceMethodList,
1424 OptionalInstanceMethodList);
1425 EmitProtocolMethodList(PD->class_methods(), ClassMethodList,
1426 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(llvm::ConstantExpr::getIntToPtr(
1433 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1434 ProtocolBuilder.add(MakeConstantString(ProtocolName));
1435 ProtocolBuilder.add(ProtocolList);
1436 ProtocolBuilder.add(InstanceMethodList);
1437 ProtocolBuilder.add(ClassMethodList);
1438 ProtocolBuilder.add(OptionalInstanceMethodList);
1439 ProtocolBuilder.add(OptionalClassMethodList);
1440 // Required instance properties
1441 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, false));
1442 // Optional instance properties
1443 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, true));
1444 // Required class properties
1445 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, false));
1446 // Optional class properties
1447 ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, true));
1448
1449 auto *GV = ProtocolBuilder.finishAndCreateGlobal(SymName,
1450 CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage);
1451 GV->setSection(sectionName<ProtocolSection>());
1452 GV->setComdat(TheModule.getOrInsertComdat(SymName));
1453 if (OldGV) {
1454 OldGV->replaceAllUsesWith(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(MangledTypes, '@', '\1');
1473 // = in dll exported names causes lld to fail when linking on Windows.
1474 if (CGM.getTriple().isOSWindows())
1475 llvm::replace(MangledTypes, '=', '\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(std::string(TypeEncoding));
1483 std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
1484 auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
1485 if (!TypesGlobal) {
1486 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
1487 TypeEncoding);
1488 auto *GV = new llvm::GlobalVariable(TheModule, Init->getType(),
1489 true, llvm::GlobalValue::LinkOnceODRLinkage, Init, TypesVarName);
1490 GV->setComdat(TheModule.getOrInsertComdat(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(SelVarName))
1502 return GV;
1503 ConstantInitBuilder builder(CGM);
1504 auto SelBuilder = builder.beginStruct();
1505 SelBuilder.add(ExportUniqueString(Sel.getAsString(), ".objc_sel_name_",
1506 true));
1507 SelBuilder.add(GetTypeString(TypeEncoding));
1508 auto *GV = SelBuilder.finishAndCreateGlobal(SelVarName,
1509 CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1510 GV->setComdat(TheModule.getOrInsertComdat(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 VMContext, {}, ".objc_section_sentinel", /*isPacked=*/true);
1530 }
1531 auto ZeroInit = llvm::Constant::getNullValue(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((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 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
1564 llvm::GlobalValue::LinkOnceODRLinkage, ".objcv2_load_function",
1565 &TheModule);
1566 LoadFunction->setVisibility(llvm::GlobalValue::HiddenVisibility);
1567 LoadFunction->setComdat(TheModule.getOrInsertComdat(".objcv2_load_function"));
1568
1569 llvm::BasicBlock *EntryBB =
1570 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
1571 CGBuilderTy B(CGM, VMContext);
1572 B.SetInsertPoint(EntryBB);
1573 ConstantInitBuilder builder(CGM);
1574 auto InitStructBuilder = builder.beginStruct();
1575 InitStructBuilder.addInt(Int64Ty, 0);
1576 auto &sectionVec = CGM.getTriple().isOSBinFormatCOFF() ? PECOFFSectionsBaseNames : SectionsBaseNames;
1577 for (auto *s : sectionVec) {
1578 auto bounds = GetSectionBounds(s);
1579 InitStructBuilder.add(bounds.first);
1580 InitStructBuilder.add(bounds.second);
1581 }
1582 auto *InitStruct = InitStructBuilder.finishAndCreateGlobal(".objc_init",
1583 CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage);
1584 InitStruct->setVisibility(llvm::GlobalValue::HiddenVisibility);
1585 InitStruct->setComdat(TheModule.getOrInsertComdat(".objc_init"));
1586
1587 CallRuntimeFunction(B, "__objc_load", {InitStruct});;
1588 B.CreateRetVoid();
1589 // Make sure that the optimisers don't delete this function.
1590 CGM.addCompilerUsedGlobal(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(".objc_ctor"));
1616 CGM.addUsedGlobal(InitVar);
1617 for (auto *C : Categories) {
1618 auto *Cat = cast<llvm::GlobalVariable>(C->stripPointerCasts());
1619 Cat->setSection(sectionName<CategorySection>());
1620 CGM.addUsedGlobal(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(F);
1627 auto GV = nullBuilder.finishAndCreateGlobal(Name, CGM.getPointerAlign(),
1628 false, 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(clsAlias.second),
1638 GetClassVar(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(Int32Ty, 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(llvm::FunctionType::get(CGM.VoidTy,
1679 {}), llvm::GlobalValue::InternalLinkage, ".objc_early_init",
1680 &CGM.getModule());
1681 llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry",
1682 Init));
1683 for (const auto &lateInit : EarlyInitList) {
1684 auto *global = TheModule.getGlobalVariable(lateInit.first);
1685 if (global) {
1686 llvm::GlobalVariable *GV = lateInit.second.first;
1687 b.CreateAlignedStore(
1688 global,
1689 b.CreateStructGEP(GV->getValueType(), GV, lateInit.second.second),
1690 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(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(Ivar->getType(), 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(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 &&
1728 CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
1729 }
1730 CharUnits Align = CGM.getIntAlign();
1731 llvm::Value *Offset =
1732 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
1733 if (Offset->getType() != PtrDiffTy)
1734 Offset = CGF.Builder.CreateZExtOrBitCast(Offset, 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(className);
1746
1747 ConstantInitBuilder builder(CGM);
1748 auto metaclassFields = builder.beginStruct();
1749 // struct objc_class *isa;
1750 metaclassFields.addNullPointer(PtrTy);
1751 // struct objc_class *super_class;
1752 metaclassFields.addNullPointer(PtrTy);
1753 // const char *name;
1754 metaclassFields.add(classNameConstant);
1755 // long version;
1756 metaclassFields.addInt(LongTy, 0);
1757 // unsigned long info;
1758 // objc_class_flag_meta
1759 metaclassFields.addInt(LongTy, 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(LongTy, 0);
1764 // struct objc_ivar_list *ivars;
1765 metaclassFields.addNullPointer(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);
1771 else {
1772 SmallVector<ObjCMethodDecl*, 16> ClassMethods;
1773 ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(),
1774 OID->classmeth_end());
1775 metaclassFields.add(
1776 GenerateMethodList(className, "", ClassMethods, true));
1777 }
1778 // void *dtable;
1779 metaclassFields.addNullPointer(PtrTy);
1780 // IMP cxx_construct;
1781 metaclassFields.addNullPointer(PtrTy);
1782 // IMP cxx_destruct;
1783 metaclassFields.addNullPointer(PtrTy);
1784 // struct objc_class *subclass_list
1785 metaclassFields.addNullPointer(PtrTy);
1786 // struct objc_class *sibling_class
1787 metaclassFields.addNullPointer(PtrTy);
1788 // struct objc_protocol_list *protocols;
1789 metaclassFields.addNullPointer(PtrTy);
1790 // struct reference_list *extra_data;
1791 metaclassFields.addNullPointer(PtrTy);
1792 // long abi_version;
1793 metaclassFields.addInt(LongTy, 0);
1794 // struct objc_property_list *properties
1795 metaclassFields.add(GeneratePropertyList(OID, classDecl, /*isClassProperty*/true));
1796
1797 auto *metaclass = metaclassFields.finishAndCreateGlobal(
1798 ManglePublicSymbol("OBJC_METACLASS_") + className,
1799 CGM.getPointerAlign());
1800
1801 auto classFields = builder.beginStruct();
1802 // struct objc_class *isa;
1803 classFields.add(metaclass);
1804 // struct objc_class *super_class;
1805 // Get the superclass name.
1806 const ObjCInterfaceDecl * SuperClassDecl =
1808 llvm::Constant *SuperClass = nullptr;
1809 if (SuperClassDecl) {
1810 auto SuperClassName = SymbolForClass(SuperClassDecl->getNameAsString());
1811 SuperClass = TheModule.getNamedGlobal(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>(SuperClass)->setDLLStorageClass(Storage);
1824 }
1825 }
1826 if (!IsCOFF)
1827 classFields.add(SuperClass);
1828 else
1829 classFields.addNullPointer(PtrTy);
1830 } else
1831 classFields.addNullPointer(PtrTy);
1832 // const char *name;
1833 classFields.add(classNameConstant);
1834 // long version;
1835 classFields.addInt(LongTy, 0);
1836 // unsigned long info;
1837 // !objc_class_flag_meta
1838 classFields.addInt(LongTy, 0);
1839 // long instance_size;
1840 int superInstanceSize = !SuperClassDecl ? 0 :
1841 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
1842 // Instance size is negative for classes that have not yet had their ivar
1843 // layout calculated.
1844 classFields.addInt(
1845 LongTy,
1846 0 - (Context.getASTObjCInterfaceLayout(OID->getClassInterface())
1847 .getSize()
1848 .getQuantity() -
1849 superInstanceSize),
1850 /*isSigned=*/true);
1851
1852 if (classDecl->all_declared_ivar_begin() == nullptr)
1853 classFields.addNullPointer(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, ivar_count);
1864 // size_t size;
1865 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
1866 PtrToInt8Ty,
1867 PtrToInt8Ty,
1868 PtrToInt8Ty,
1869 Int32Ty,
1870 Int32Ty);
1871 ivarListBuilder.addInt(SizeTy, DL.getTypeSizeInBits(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(MakeConstantString(IVD->getNameAsString()));
1881 // const char *type;
1882 std::string TypeStr;
1883 //Context.getObjCEncodingForType(ivarTy, TypeStr, IVD, true);
1884 Context.getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, ivarTy, TypeStr, true);
1885 ivarBuilder.add(MakeConstantString(TypeStr));
1886 // int *offset;
1887 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
1888 uint64_t Offset = BaseOffset - superInstanceSize;
1889 llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
1890 std::string OffsetName = GetIVarOffsetVariableName(classDecl, IVD);
1891 llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(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(OffsetVar, OID->getClassInterface());
1907 ivarBuilder.add(OffsetVar);
1908 // Ivar size
1909 ivarBuilder.addInt(Int32Ty,
1910 CGM.getContext().getTypeSizeInChars(ivarTy).getQuantity());
1911 // Alignment will be stored as a base-2 log of the alignment.
1912 unsigned align =
1913 llvm::Log2_32(Context.getTypeAlignInChars(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(Int32Ty,
1921 (align << 3) | (1<<2) |
1922 FlagsForOwnership(ivarTy.getQualifiers().getObjCLifetime()));
1923 ivarBuilder.finishAndAddTo(ivarArrayBuilder);
1924 }
1925 ivarArrayBuilder.finishAndAddTo(ivarListBuilder);
1926 auto ivarList = ivarListBuilder.finishAndCreateGlobal(".objc_ivar_list",
1927 CGM.getPointerAlign(), /*constant*/ false,
1928 llvm::GlobalValue::PrivateLinkage);
1929 classFields.add(ivarList);
1930 }
1931 // struct objc_method_list *methods
1932 SmallVector<const ObjCMethodDecl*, 16> InstanceMethods;
1933 InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(),
1934 OID->instmeth_end());
1935 for (auto *propImpl : OID->property_impls())
1936 if (propImpl->getPropertyImplementation() ==
1938 auto addIfExists = [&](const ObjCMethodDecl *OMD) {
1939 if (OMD && OMD->hasBody())
1940 InstanceMethods.push_back(OMD);
1941 };
1942 addIfExists(propImpl->getGetterMethodDecl());
1943 addIfExists(propImpl->getSetterMethodDecl());
1944 }
1945
1946 if (InstanceMethods.size() == 0)
1947 classFields.addNullPointer(PtrTy);
1948 else
1949 classFields.add(
1950 GenerateMethodList(className, "", InstanceMethods, false));
1951
1952 // void *dtable;
1953 classFields.addNullPointer(PtrTy);
1954 // IMP cxx_construct;
1955 classFields.addNullPointer(PtrTy);
1956 // IMP cxx_destruct;
1957 classFields.addNullPointer(PtrTy);
1958 // struct objc_class *subclass_list
1959 classFields.addNullPointer(PtrTy);
1960 // struct objc_class *sibling_class
1961 classFields.addNullPointer(PtrTy);
1962 // struct objc_protocol_list *protocols;
1963 auto RuntimeProtocols =
1964 GetRuntimeProtocolList(classDecl->all_referenced_protocol_begin(),
1965 classDecl->all_referenced_protocol_end());
1966 SmallVector<llvm::Constant *, 16> Protocols;
1967 for (const auto *I : RuntimeProtocols)
1968 Protocols.push_back(GenerateProtocolRef(I));
1969
1970 if (Protocols.empty())
1971 classFields.addNullPointer(PtrTy);
1972 else
1973 classFields.add(GenerateProtocolList(Protocols));
1974 // struct reference_list *extra_data;
1975 classFields.addNullPointer(PtrTy);
1976 // long abi_version;
1977 classFields.addInt(LongTy, 0);
1978 // struct objc_property_list *properties
1979 classFields.add(GeneratePropertyList(OID, classDecl));
1980
1981 llvm::GlobalVariable *classStruct =
1982 classFields.finishAndCreateGlobal(SymbolForClass(className),
1983 CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage);
1984
1985 auto *classRefSymbol = GetClassVar(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>(classRefSymbol)->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1994 }
1995
1996 if (SuperClass) {
1997 std::pair<llvm::GlobalVariable*, int> v{classStruct, 1};
1998 EarlyInitList.emplace_back(std::string(SuperClass->getName()),
1999 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(classStruct);
2009 ClassPtrAlias->eraseFromParent();
2010 ClassPtrAlias = nullptr;
2011 }
2012 if (auto Placeholder =
2013 TheModule.getNamedGlobal(SymbolForClass(className)))
2014 if (Placeholder != classStruct) {
2015 Placeholder->replaceAllUsesWith(classStruct);
2016 Placeholder->eraseFromParent();
2017 classStruct->setName(SymbolForClass(className));
2018 }
2019 if (MetaClassPtrAlias) {
2020 MetaClassPtrAlias->replaceAllUsesWith(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("OBJC_INIT_CLASS_") + className);
2029 classInitRef->setSection(sectionName<ClassSection>());
2030 CGM.addUsedGlobal(classInitRef);
2031
2032 EmittedClass = true;
2033 }
2034 public:
2035 CGObjCGNUstep2(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 10, 4, 2) {
2036 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
2037 PtrToObjCSuperTy, SelectorTy);
2038 SentInitializeFn.init(&CGM, "objc_send_initialize",
2039 llvm::Type::getVoidTy(VMContext), 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(CGM.getLLVMContext(),
2050 { 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(OMD->getSelfDecl());
2059 auto selfValue = Builder.CreateLoad(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>(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(OID);
2085 }
2086
2087 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
2088 if (ReceiverCanBeNull) {
2089 llvm::BasicBlock *SelfIsNilBlock =
2090 CGF.createBasicBlock("objc_direct_method.self_is_nil");
2091 llvm::BasicBlock *ContBlock =
2092 CGF.createBasicBlock("objc_direct_method.cont");
2093
2094 // if (self == nil) {
2095 auto selfTy = cast<llvm::PointerType>(selfValue->getType());
2096 auto Zero = llvm::ConstantPointerNull::get(selfTy);
2097
2098 Builder.CreateCondBr(Builder.CreateICmpEQ(selfValue, Zero),
2099 SelfIsNilBlock, ContBlock,
2100 MDHelper.createUnlikelyBranchWeights());
2101
2102 CGF.EmitBlock(SelfIsNilBlock);
2103
2104 // return (ReturnType){ };
2105 auto retTy = OMD->getReturnType();
2106 Builder.SetInsertPoint(SelfIsNilBlock);
2107 if (!retTy->isVoidType()) {
2108 CGF.EmitNullInitialization(CGF.ReturnValue, retTy);
2109 }
2111 // }
2112
2113 // rest of the body
2114 CGF.EmitBlock(ContBlock);
2115 Builder.SetInsertPoint(ContBlock);
2116 }
2117
2118 if (OMD->isClassMethod()) {
2119 // Prefix of the class type.
2120 auto *classStart =
2121 llvm::StructType::get(PtrTy, PtrTy, PtrTy, LongTy, 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(classStart, selfValue, 4);
2129 auto Align = CharUnits::fromQuantity(
2130 astContext.getTypeAlign(astContext.UnsignedLongTy));
2131 auto flags = Builder.CreateLoad(Address{Val, LongTy, Align});
2132 auto isInitialized =
2133 Builder.CreateAnd(flags, ClassFlags::ClassFlagInitialized);
2134 llvm::BasicBlock *notInitializedBlock =
2135 CGF.createBasicBlock("objc_direct_method.class_uninitialized");
2136 llvm::BasicBlock *initializedBlock =
2137 CGF.createBasicBlock("objc_direct_method.class_initialized");
2138 Builder.CreateCondBr(Builder.CreateICmpEQ(isInitialized, Zeros[0]),
2139 notInitializedBlock, initializedBlock,
2140 MDHelper.createUnlikelyBranchWeights());
2141 CGF.EmitBlock(notInitializedBlock);
2142 Builder.SetInsertPoint(notInitializedBlock);
2143 CGF.EmitRuntimeCall(SentInitializeFn, selfValue);
2144 Builder.CreateBr(initializedBlock);
2145 CGF.EmitBlock(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(*OMD->getCmdDecl());
2154 Builder.CreateStore(GetSelector(CGF, OMD),
2155 CGF.GetAddrOfLocalVar(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(Builder, Receiver, IdTy),
2204 EnforceType(Builder, cmd, SelectorTy) };
2205
2206 llvm::CallBase *imp;
2207 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
2208 imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
2209 else
2210 imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
2211
2212 imp->setMetadata(msgSendMDKind, 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(Builder, ObjCSuper.emitRawPointer(CGF), PtrToObjCSuperTy),
2221 cmd,
2222 };
2223
2224 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
2225 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
2226 else
2227 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, 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(Name);
2236 std::string SymbolName = "_OBJC_CLASS_" + Name;
2237 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(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(OMD->getSelfDecl());
2251 auto selfValue = Builder.CreateLoad(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>(CD);
2268 assert(
2269 OID &&
2270 "GenerateDirectMethod() should be called with the Class Interface");
2271 Selector SelfSel = GetNullarySelector("self", 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, ReturnValueSlot(), ResultType, SelfSel, selfValue, Args, OID,
2285 nullptr, true);
2286 Builder.CreateStore(result.getScalarVal(), 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(OID);
2292 }
2293
2294 if (ReceiverCanBeNull) {
2295 llvm::BasicBlock *SelfIsNilBlock =
2296 CGF.createBasicBlock("objc_direct_method.self_is_nil");
2297 llvm::BasicBlock *ContBlock =
2298 CGF.createBasicBlock("objc_direct_method.cont");
2299
2300 // if (self == nil) {
2301 auto selfTy = cast<llvm::PointerType>(selfValue->getType());
2302 auto Zero = llvm::ConstantPointerNull::get(selfTy);
2303
2304 llvm::MDBuilder MDHelper(CGM.getLLVMContext());
2305 Builder.CreateCondBr(Builder.CreateICmpEQ(selfValue, Zero),
2306 SelfIsNilBlock, ContBlock,
2307 MDHelper.createUnlikelyBranchWeights());
2308
2309 CGF.EmitBlock(SelfIsNilBlock);
2310
2311 // return (ReturnType){ };
2312 auto retTy = OMD->getReturnType();
2313 Builder.SetInsertPoint(SelfIsNilBlock);
2314 if (!retTy->isVoidType()) {
2315 CGF.EmitNullInitialization(CGF.ReturnValue, retTy);
2316 }
2318 // }
2319
2320 // rest of the body
2321 CGF.EmitBlock(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(*OMD->getCmdDecl());
2330 Builder.CreateStore(GetSelector(CGF, OMD),
2331 CGF.GetAddrOfLocalVar(OMD->getCmdDecl()));
2332 }
2333 }
2334
2335public:
2336 CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
2337 // IMP objc_msg_lookup(id, SEL);
2338 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy);
2339 MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
2340 SelectorTy);
2341 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
2342 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
2343 PtrToObjCSuperTy, SelectorTy);
2344 MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
2345 PtrToObjCSuperTy, 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(symbolRef))
2357 return;
2358 std::string symbolName = "__objc_class_name_" + className;
2359 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(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("GNUObjCMessageSend");
2377 usesSEHExceptions =
2378 cgm.getContext().getTargetInfo().getTriple().isWindowsMSVCEnvironment();
2379 usesCxxExceptions =
2380 cgm.getContext().getTargetInfo().getTriple().isOSCygMing() &&
2381 isRuntime(ObjCRuntime::GNUstep, 2);
2382
2383 CodeGenTypes &Types = CGM.getTypes();
2385 Types.ConvertType(CGM.getContext().IntTy));
2386 LongTy = cast<llvm::IntegerType>(
2387 Types.ConvertType(CGM.getContext().LongTy));
2388 SizeTy = cast<llvm::IntegerType>(
2389 Types.ConvertType(CGM.getContext().getSizeType()));
2390 PtrDiffTy = cast<llvm::IntegerType>(
2392 BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
2393
2394 Int8Ty = llvm::Type::getInt8Ty(VMContext);
2395
2396 PtrTy = llvm::PointerType::getUnqual(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(LongTy, 0);
2403 Zeros[1] = Zeros[0];
2404 NULLPtr = llvm::ConstantPointerNull::get(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>(CGM.getTypes().ConvertType(selTy));
2412 SelectorElemTy = CGM.getTypes().ConvertTypeForMem(selTy->getPointeeType());
2413 }
2414
2415 Int32Ty = llvm::Type::getInt32Ty(VMContext);
2416 Int64Ty = llvm::Type::getInt64Ty(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(UnqualIdTy);
2426 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
2427 IdElemTy = CGM.getTypes().ConvertTypeForMem(
2428 ASTIdTy.getTypePtr()->getPointeeType());
2429 } else {
2430 IdTy = PtrToInt8Ty;
2431 IdElemTy = Int8Ty;
2432 }
2433 PtrToIdTy = PtrTy;
2434 ProtocolTy = llvm::StructType::get(IdTy,
2435 PtrToInt8Ty, // name
2436 PtrToInt8Ty, // protocols
2437 PtrToInt8Ty, // instance methods
2438 PtrToInt8Ty, // class methods
2439 PtrToInt8Ty, // optional instance methods
2440 PtrToInt8Ty, // optional class methods
2441 PtrToInt8Ty, // properties
2442 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(CGM.getLLVMContext(), {
2457 PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty,
2458 PtrToInt8Ty, PtrToInt8Ty });
2459
2460 ObjCSuperTy = llvm::StructType::get(IdTy, IdTy);
2461 PtrToObjCSuperTy = PtrTy;
2462
2463 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
2464
2465 // void objc_exception_throw(id);
2466 ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
2467 ExceptionReThrowFn.init(&CGM,
2468 usesCxxExceptions ? "objc_exception_rethrow"
2469 : "objc_exception_throw",
2470 VoidTy, IdTy);
2471 // int objc_sync_enter(id);
2472 SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
2473 // int objc_sync_exit(id);
2474 SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy);
2475
2476 // void objc_enumerationMutation (id)
2477 EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, IdTy);
2478
2479 // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
2480 GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
2481 PtrDiffTy, BoolTy);
2482 // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
2483 SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
2484 PtrDiffTy, IdTy, BoolTy, BoolTy);
2485 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
2486 GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
2487 PtrDiffTy, BoolTy, BoolTy);
2488 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
2489 SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
2490 PtrDiffTy, BoolTy, 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("retain", CGM.getContext());
2506 ReleaseSel = GetNullarySelector("release", CGM.getContext());
2507 AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
2508
2509 // Get functions needed in GC mode
2510
2511 // id objc_assign_ivar(id, id, ptrdiff_t);
2512 IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy);
2513 // id objc_assign_strongCast (id, id*)
2514 StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
2515 PtrToIdTy);
2516 // id objc_assign_global(id, id*);
2517 GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy);
2518 // id objc_assign_weak(id, id*);
2519 WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy);
2520 // id objc_read_weak(id*);
2521 WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy);
2522 // void *objc_memmove_collectable(void*, void *, size_t);
2523 MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
2524 SizeTy);
2525 }
2526}
2527
2528llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
2529 const std::string &Name, bool isWeak) {
2530 llvm::Constant *ClassName = MakeConstantString(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(Name);
2540
2541 llvm::FunctionCallee ClassLookupFn = CGM.CreateRuntimeFunction(
2542 llvm::FunctionType::get(IdTy, PtrToInt8Ty, true), "objc_lookup_class");
2543 return CGF.EmitNounwindRuntimeCall(ClassLookupFn, 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, OID->getNameAsString(), OID->isWeakImported());
2552 if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value))
2553 CGM.setGVProperties(ClassSymbol, OID);
2554 return Value;
2555}
2556
2557llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
2558 auto *Value = GetClassNamed(CGF, "NSAutoreleasePool", false);
2559 if (CGM.getTriple().isOSBinFormatCOFF()) {
2560 if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) {
2561 IdentifierInfo &II = CGF.CGM.getContext().Idents.get("NSAutoreleasePool");
2562 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
2563 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
2564
2565 const VarDecl *VD = nullptr;
2566 for (const auto *Result : DC->lookup(&II))
2567 if ((VD = dyn_cast<VarDecl>(Result)))
2568 break;
2569
2570 CGM.setGVProperties(ClassSymbol, 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(SelectorElemTy, 0,
2589 llvm::GlobalValue::PrivateLinkage,
2590 ".objc_selector_" + Sel.getAsString(),
2591 &TheModule);
2592 Types.emplace_back(TypeEncoding, 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(SelValue->getType(),
2604 CGF.getPointerAlign());
2605 CGF.Builder.CreateStore(SelValue, tmp);
2606 return tmp;
2607}
2608
2609llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) {
2610 return GetTypedSelector(CGF, Sel, std::string());
2611}
2612
2613llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
2614 const ObjCMethodDecl *Method) {
2615 std::string SelTypes = CGM.getContext().getObjCEncodingForMethodDecl(Method);
2616 return GetTypedSelector(CGF, Method->getSelector(), 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("@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(IDecl->getIdentifier()->getName());
2638}
2639
2640llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
2641 if (usesSEHExceptions)
2642 return CGM.getCXXABI().getAddrOfRTTIDescriptor(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("__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(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(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(IntTy, 2);
2691 auto *BVtable =
2692 llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two);
2693
2694 llvm::Constant *typeName =
2695 ExportUniqueString(className, "__objc_eh_typename_");
2696
2697 ConstantInitBuilder builder(CGM);
2698 auto fields = builder.beginStruct();
2699 fields.add(BVtable);
2700 fields.add(typeName);
2701 llvm::Constant *TI =
2702 fields.finishAndCreateGlobal("__objc_eh_typeinfo_" + className,
2703 CGM.getPointerAlign(),
2704 /*constant*/ false,
2705 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(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(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(isa);
2737 Fields.add(MakeConstantString(Str));
2738 Fields.addInt(IntTy, Str.size());
2739 llvm::Constant *ObjCStr = Fields.finishAndCreateGlobal(".objc_str", Align);
2740 ObjCStrings[Str] = ObjCStr;
2741 ConstantStrings.push_back(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(EnforceType(Builder, Receiver,
2794 CGM.getTypes().ConvertType(ResultType)));
2795 }
2796 if (Sel == ReleaseSel) {
2797 return RValue::get(nullptr);
2798 }
2799 }
2800
2801 llvm::Value *cmd = GetSelector(CGF, Sel);
2802 CallArgList ActualArgs;
2803
2804 ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
2805 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
2806 ActualArgs.addFrom(CallArgs);
2807
2808 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
2809
2810 llvm::Value *ReceiverClass = nullptr;
2811 bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2);
2812 if (isV2ABI) {
2813 ReceiverClass = GetClassNamed(CGF,
2814 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(IdTy, ReceiverClass, CGF.getPointerAlign());
2819 }
2820 ReceiverClass = EnforceType(Builder, ReceiverClass, IdTy);
2821 } else {
2822 if (isCategoryImpl) {
2823 llvm::FunctionCallee classLookupFunction = nullptr;
2824 if (IsClassMessage) {
2825 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
2826 IdTy, PtrTy, true), "objc_get_meta_class");
2827 } else {
2828 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
2829 IdTy, PtrTy, true), "objc_get_class");
2830 }
2831 ReceiverClass = Builder.CreateCall(classLookupFunction,
2832 MakeConstantString(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 IdElemTy, 0, llvm::GlobalValue::InternalLinkage,
2843 ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
2844 }
2845 ReceiverClass = MetaClassPtrAlias;
2846 } else {
2847 if (!ClassPtrAlias) {
2848 ClassPtrAlias = llvm::GlobalAlias::create(
2849 IdElemTy, 0, llvm::GlobalValue::InternalLinkage,
2850 ".objc_class_ref" + Class->getNameAsString(), &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(IdTy, IdTy);
2857 // Get the superclass pointer
2858 ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1);
2859 // Load the superclass pointer
2860 ReceiverClass =
2861 Builder.CreateAlignedLoad(IdTy, ReceiverClass, CGF.getPointerAlign());
2862 }
2863 // Construct the structure used to look up the IMP
2864 llvm::StructType *ObjCSuperTy =
2865 llvm::StructType::get(Receiver->getType(), IdTy);
2866
2867 Address ObjCSuper = CGF.CreateTempAlloca(ObjCSuperTy,
2868 CGF.getPointerAlign());
2869
2870 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
2871 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
2872
2873 // Get the IMP
2874 llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
2875 imp = EnforceType(Builder, imp, MSI.MessengerType);
2876
2877 llvm::Metadata *impMD[] = {
2878 llvm::MDString::get(VMContext, Sel.getAsString()),
2879 llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
2880 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
2881 llvm::Type::getInt1Ty(VMContext), IsClassMessage))};
2882 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
2883
2884 CGCallee callee(CGCalleeInfo(), imp);
2885
2886 llvm::CallBase *call;
2887 RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call);
2888 call->setMetadata(msgSendMDKind, 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(EnforceType(Builder, Receiver,
2908 CGM.getTypes().ConvertType(ResultType)));
2909 }
2910 if (Sel == ReleaseSel) {
2911 return RValue::get(nullptr);
2912 }
2913 }
2914
2915 bool isDirect = Method && Method->isDirectMethod();
2916
2917 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(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(Builder, cmd, SelectorTy);
2925 }
2926
2927 Receiver = EnforceType(Builder, Receiver, IdTy);
2928
2929 llvm::Metadata *impMD[] = {
2930 llvm::MDString::get(VMContext, Sel.getAsString()),
2931 llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""),
2932 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
2933 llvm::Type::getInt1Ty(VMContext), Class != nullptr))};
2934 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
2935
2936 CallArgList ActualArgs;
2937 ActualArgs.add(RValue::get(Receiver), ASTIdTy);
2938 if (!isDirect)
2939 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
2940 ActualArgs.addFrom(CallArgs);
2941
2942 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, 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, /*IsSuper*/ false,
2968 Class, 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(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(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("msgSend");
3017 continueBB = CGF.createBasicBlock("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("nilReceiverCleanup");
3024 } else {
3025 nilPathBB = Builder.GetInsertBlock();
3026 }
3027
3028 llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
3029 llvm::Constant::getNullValue(Receiver->getType()));
3030 Builder.CreateCondBr(isNil, nilCleanupBB ? nilCleanupBB : continueBB,
3031 messageBB);
3032 CGF.EmitBlock(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(Method, 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()) {
3047 imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
3048 break;
3051 StringRef name = "objc_msgSend";
3052 if (CGM.ReturnTypeUsesFPRet(ResultType)) {
3053 name = "objc_msgSend_fpret";
3054 } else if (CGM.ReturnTypeUsesSRet(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(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(llvm::FunctionType::get(IdTy, IdTy, true),
3072 name)
3073 .getCallee();
3074 }
3075
3076 // Reset the receiver in case the lookup modified it
3077 ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy);
3078
3079 imp = EnforceType(Builder, imp, MSI.MessengerType);
3080
3081 llvm::CallBase *call;
3082 CGCallee callee(CGCalleeInfo(), imp);
3083 RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call);
3084 if (!isDirect)
3085 call->setMetadata(msgSendMDKind, node);
3086
3087 if (requiresNilReceiverCheck) {
3088 llvm::BasicBlock *nonNilPathBB = CGF.Builder.GetInsertBlock();
3089 CGF.Builder.CreateBr(continueBB);
3090
3091 // Emit the nil path if we decided it was necessary above.
3092 if (nilCleanupBB) {
3093 CGF.EmitBlock(nilCleanupBB);
3094
3095 if (hasParamDestroyedInCallee) {
3096 destroyCalleeDestroyedArguments(CGF, Method, CallArgs);
3097 }
3098
3099 if (requiresExplicitAggZeroing) {
3100 assert(msgRet.isAggregate());
3101 Address addr = msgRet.getAggregateAddress();
3102 CGF.EmitNullInitialization(addr, ResultType);
3103 }
3104
3105 nilPathBB = CGF.Builder.GetInsertBlock();
3106 CGF.Builder.CreateBr(continueBB);
3107 }
3108
3109 // Enter the continuation block and emit a phi if required.
3110 CGF.EmitBlock(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(v->getType(), 2);
3115 phi->addIncoming(v, nonNilPathBB);
3116 phi->addIncoming(CGM.EmitNullConstant(ResultType), nilPathBB);
3117 msgRet = RValue::get(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(v.first->getType(), 2);
3124 phi->addIncoming(v.first, nonNilPathBB);
3125 phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
3126 nilPathBB);
3127 llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
3128 phi2->addIncoming(v.second, nonNilPathBB);
3129 phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
3130 nilPathBB);
3131 msgRet = RValue::getComplex(phi, 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(CGM.Int8PtrTy);
3151 MethodList.addInt(Int32Ty, Methods.size());
3152
3153 // Get the method structure type.
3154 llvm::StructType *ObjCMethodTy =
3155 llvm::StructType::get(CGM.getLLVMContext(), {
3156 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
3157 PtrToInt8Ty, // Method types
3158 IMPTy // Method pointer
3159 });
3160 bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2);
3161 if (isV2ABI) {
3162 // size_t size;
3163 const llvm::DataLayout &DL = TheModule.getDataLayout();
3164 MethodList.addInt(SizeTy, DL.getTypeSizeInBits(ObjCMethodTy) /
3165 CGM.getContext().getCharWidth());
3166 ObjCMethodTy =
3167 llvm::StructType::get(CGM.getLLVMContext(), {
3168 IMPTy, // Method pointer
3169 PtrToInt8Ty, // Selector
3170 PtrToInt8Ty // Extended type encoding
3171 });
3172 } else {
3173 ObjCMethodTy =
3174 llvm::StructType::get(CGM.getLLVMContext(), {
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(getSymbolNameForMethod(OMD));
3185 assert(FnPtr && "Can't generate metadata for method that doesn't exist");
3186 auto Method = MethodArray.beginStruct(ObjCMethodTy);
3187 if (isV2ABI) {
3188 Method.add(FnPtr);
3189 Method.add(GetConstantSelector(OMD->getSelector(),
3190 Context.getObjCEncodingForMethodDecl(OMD)));
3191 Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD, true)));
3192 } else {
3193 Method.add(MakeConstantString(OMD->getSelector().getAsString()));
3194 Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD)));
3195 Method.add(FnPtr);
3196 }
3197 Method.finishAndAddTo(MethodArray);
3198 }
3199 MethodArray.finishAndAddTo(MethodList);
3200
3201 // Create an instance of the structure
3202 return MethodList.finishAndCreateGlobal(".objc_method_list",
3203 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, (int)IvarNames.size());
3221
3222 // Get the ivar structure type.
3223 llvm::StructType *ObjCIvarTy =
3224 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy);
3225
3226 // Array of ivar structures.
3227 auto Ivars = IvarList.beginArray(ObjCIvarTy);
3228 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
3229 auto Ivar = Ivars.beginStruct(ObjCIvarTy);
3230 Ivar.add(IvarNames[i]);
3231 Ivar.add(IvarTypes[i]);
3232 Ivar.add(IvarOffsets[i]);
3233 Ivar.finishAndAddTo(Ivars);
3234 }
3235 Ivars.finishAndAddTo(IvarList);
3236
3237 // Create an instance of the structure
3238 return IvarList.finishAndCreateGlobal(".objc_ivar_list",
3239 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 PtrToInt8Ty, // isa
3267 PtrToInt8Ty, // super_class
3268 PtrToInt8Ty, // name
3269 LongTy, // version
3270 LongTy, // info
3271 LongTy, // instance_size
3272 IVars->getType(), // ivars
3273 Methods->getType(), // methods
3274 // These are all filled in by the runtime, so we pretend
3275 PtrTy, // dtable
3276 PtrTy, // subclass_list
3277 PtrTy, // sibling_class
3278 PtrTy, // protocols
3279 PtrTy, // gc_object_type
3280 // New ABI:
3281 LongTy, // abi_version
3282 IvarOffsets->getType(), // ivar_offsets
3283 Properties->getType(), // properties
3284 IntPtrTy, // strong_pointers
3285 IntPtrTy // weak_pointers
3286 );
3287
3288 ConstantInitBuilder Builder(CGM);
3289 auto Elements = Builder.beginStruct(ClassTy);
3290
3291 // Fill in the structure
3292
3293 // isa
3294 Elements.add(MetaClass);
3295 // super_class
3296 Elements.add(SuperClass);
3297 // name
3298 Elements.add(MakeConstantString(Name, ".class_name"));
3299 // version
3300 Elements.addInt(LongTy, 0);
3301 // info
3302 Elements.addInt(LongTy, info);
3303 // instance_size
3304 if (isMeta) {
3305 const llvm::DataLayout &DL = TheModule.getDataLayout();
3306 Elements.addInt(LongTy, DL.getTypeSizeInBits(ClassTy) /
3307 CGM.getContext().getCharWidth());
3308 } else
3309 Elements.add(InstanceSize);
3310 // ivars
3311 Elements.add(IVars);
3312 // methods
3313 Elements.add(Methods);
3314 // These are all filled in by the runtime, so we pretend
3315 // dtable
3316 Elements.add(NULLPtr);
3317 // subclass_list
3318 Elements.add(NULLPtr);
3319 // sibling_class
3320 Elements.add(NULLPtr);
3321 // protocols
3322 Elements.add(Protocols);
3323 // gc_object_type
3324 Elements.add(NULLPtr);
3325 // abi_version
3326 Elements.addInt(LongTy, ClassABIVersion);
3327 // ivar_offsets
3328 Elements.add(IvarOffsets);
3329 // properties
3330 Elements.add(Properties);
3331 // strong_pointers
3332 Elements.add(StrongIvarBitmap);
3333 // weak_pointers
3334 Elements.add(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(ClassSym);
3342 llvm::Constant *Class =
3343 Elements.finishAndCreateGlobal(ClassSym, CGM.getPointerAlign(), false,
3344 llvm::GlobalValue::ExternalLinkage);
3345 if (ClassRef) {
3346 ClassRef->replaceAllUsesWith(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(CGM.getLLVMContext(), { PtrToInt8Ty, PtrToInt8Ty });
3358 ASTContext &Context = CGM.getContext();
3359 ConstantInitBuilder Builder(CGM);
3360 auto MethodList = Builder.beginStruct();
3361 MethodList.addInt(IntTy, Methods.size());
3362 auto MethodArray = MethodList.beginArray(ObjCMethodDescTy);
3363 for (auto *M : Methods) {
3364 auto Method = MethodArray.beginStruct(ObjCMethodDescTy);
3365 Method.add(MakeConstantString(M->getSelector().getAsString()));
3366 Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(M)));
3367 Method.finishAndAddTo(MethodArray);
3368 }
3369 MethodArray.finishAndAddTo(MethodList);
3370 return MethodList.finishAndCreateGlobal(".objc_method_list",
3371 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(NULLPtr);
3381 ProtocolList.addInt(LongTy, Protocols.size());
3382
3383 auto Elements = ProtocolList.beginArray(PtrToInt8Ty);
3384 for (const std::string &Protocol : Protocols) {
3385 llvm::Constant *protocol = nullptr;
3386 llvm::StringMap<llvm::Constant *>::iterator value =
3387 ExistingProtocols.find(Protocol);
3388 if (value == ExistingProtocols.end()) {
3389 protocol = GenerateEmptyProtocol(Protocol);
3390 } else {
3391 protocol = value->getValue();
3392 }
3393 Elements.add(protocol);
3394 }
3395 Elements.finishAndAddTo(ProtocolList);
3396 return ProtocolList.finishAndCreateGlobal(".objc_protocol_list",
3397 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({});
3416 llvm::Constant *MethodList = GenerateProtocolMethodList({});
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(llvm::ConstantExpr::getIntToPtr(
3425 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
3426
3427 Elements.add(MakeConstantString(ProtocolName, ".objc_protocol_name"));
3428 Elements.add(ProtocolList); /* .protocol_list */
3429 Elements.add(MethodList); /* .instance_methods */
3430 Elements.add(MethodList); /* .class_methods */
3431 Elements.add(MethodList); /* .optional_instance_methods */
3432 Elements.add(MethodList); /* .optional_class_methods */
3433 Elements.add(NULLPtr); /* .properties */
3434 Elements.add(NULLPtr); /* .optional_properties */
3435 return Elements.finishAndCreateGlobal(SymbolForProtocol(ProtocolName),
3436 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(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(I);
3457 else
3458 InstanceMethods.push_back(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(I);
3465 else
3466 ClassMethods.push_back(I);
3467
3468 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
3469 llvm::Constant *InstanceMethodList =
3470 GenerateProtocolMethodList(InstanceMethods);
3471 llvm::Constant *ClassMethodList =
3472 GenerateProtocolMethodList(ClassMethods);
3473 llvm::Constant *OptionalInstanceMethodList =
3474 GenerateProtocolMethodList(OptionalInstanceMethods);
3475 llvm::Constant *OptionalClassMethodList =
3476 GenerateProtocolMethodList(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(nullptr, PD, false, false);
3486 llvm::Constant *OptionalPropertyList =
3487 GeneratePropertyList(nullptr, PD, false, 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 llvm::ConstantExpr::getIntToPtr(
3497 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
3498 Elements.add(MakeConstantString(ProtocolName));
3499 Elements.add(ProtocolList);
3500 Elements.add(InstanceMethodList);
3501 Elements.add(ClassMethodList);
3502 Elements.add(OptionalInstanceMethodList);
3503 Elements.add(OptionalClassMethodList);
3504 Elements.add(PropertyList);
3505 Elements.add(OptionalPropertyList);
3506 ExistingProtocols[ProtocolName] =
3507 Elements.finishAndCreateGlobal(".objc_protocol", 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(MakeConstantString(CategoryName));
3518 Elements.add(MakeConstantString(ClassName));
3519 // Instance method list
3520 Elements.add(GenerateMethodList(ClassName, CategoryName, {}, false));
3521 // Class method list
3522 Elements.add(GenerateMethodList(ClassName, CategoryName, {}, true));
3523
3524 // Protocol list
3525 ConstantInitBuilder ProtocolListBuilder(CGM);
3526 auto ProtocolList = ProtocolListBuilder.beginStruct();
3527 ProtocolList.add(NULLPtr);
3528 ProtocolList.addInt(LongTy, ExistingProtocols.size());
3529 auto ProtocolElements = ProtocolList.beginArray(PtrTy);
3530 for (auto iter = ExistingProtocols.begin(), endIter = ExistingProtocols.end();
3531 iter != endIter ; iter++) {
3532 ProtocolElements.add(iter->getValue());
3533 }
3534 ProtocolElements.finishAndAddTo(ProtocolList);
3535 Elements.add(ProtocolList.finishAndCreateGlobal(".objc_protocol_list",
3536 CGM.getPointerAlign()));
3537 Categories.push_back(
3538 Elements.finishAndCreateGlobal("", 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(IntPtrTy, 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(llvm::ConstantInt::get(Int32Ty, word));
3571 }
3572
3573 ConstantInitBuilder builder(CGM);
3574 auto fields = builder.beginStruct();
3575 fields.addInt(Int32Ty, values.size());
3576 auto array = fields.beginArray();
3577 for (auto *v : values) array.add(v);
3578 array.finishAndAddTo(fields);
3579
3580 llvm::Constant *GS =
3581 fields.finishAndCreateGlobal("", CharUnits::fromQuantity(4));
3582 llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, 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(RefPro.begin(), RefPro.end());
3591 SmallVector<std::string, 16> Protocols;
3592 for (const auto *PD : RuntimeProtos)
3593 Protocols.push_back(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(MakeConstantString(CategoryName));
3608 Elements.add(MakeConstantString(ClassName));
3609 // Instance method list
3610 SmallVector<ObjCMethodDecl*, 16> InstanceMethods;
3611 InstanceMethods.insert(InstanceMethods.begin(), OCD->instmeth_begin(),
3612 OCD->instmeth_end());
3613 Elements.add(
3614 GenerateMethodList(ClassName, CategoryName, InstanceMethods, false));
3615
3616 // Class method list
3617
3618 SmallVector<ObjCMethodDecl*, 16> ClassMethods;
3619 ClassMethods.insert(ClassMethods.begin(), OCD->classmeth_begin(),
3620 OCD->classmeth_end());
3621 Elements.add(GenerateMethodList(ClassName, CategoryName, ClassMethods, true));
3622
3623 // Protocol list
3624 Elements.add(GenerateCategoryProtocolList(CatDecl));
3625 if (isRuntime(ObjCRuntime::GNUstep, 2)) {
3626 const ObjCCategoryDecl *Category =
3627 Class->FindCategoryDeclaration(OCD->getIdentifier());
3628 if (Category) {
3629 // Instance properties
3630 Elements.add(GeneratePropertyList(OCD, Category, false));
3631 // Class properties
3632 Elements.add(GeneratePropertyList(OCD, Category, true));
3633 } else {
3634 Elements.addNullPointer(PtrTy);
3635 Elements.addNullPointer(PtrTy);
3636 }
3637 }
3638
3639 Categories.push_back(Elements.finishAndCreateGlobal(
3640 std::string(".objc_category_") + ClassName + CategoryName,
3641 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>(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(PD->getIdentifier()).second)
3666 continue;
3667 Properties.push_back(PD);
3668 }
3669 };
3670
3671 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(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(PD->getIdentifier());
3677 Properties.push_back(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(PD->getIdentifier()).second)
3690 continue;
3691
3692 Properties.push_back(PD);
3693 }
3694
3695 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
3696 for (const auto *P : OID->all_referenced_protocols())
3697 collectProtocolProperties(P);
3698 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(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(propertyList, 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(property, Container);
3718 if (propertyImpl) {
3719 isSynthesized = (propertyImpl->getPropertyImplementation() ==
3721 isDynamic = (propertyImpl->getPropertyImplementation() ==
3723 }
3724 }
3725 PushProperty(properties, property, Container, isSynthesized, isDynamic);
3726 }
3727 properties.finishAndAddTo(propertyList);
3728
3729 return propertyList.finishAndCreateGlobal(".objc_property_list",
3730 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(ClassDecl->getNameAsString(),
3738 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 =
3747 std::string SuperClassName;
3748 if (SuperClassDecl) {
3749 SuperClassName = SuperClassDecl->getNameAsString();
3750 EmitClassRef(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(classSymbolName)) {
3762 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
3763 } else {
3764 new llvm::GlobalVariable(TheModule, LongTy, false,
3765 llvm::GlobalValue::ExternalLinkage,
3766 llvm::ConstantInt::get(LongTy, 0),
3767 classSymbolName);
3768 }
3769
3770 // Get the size of instances.
3771 int instanceSize = Context.getASTObjCInterfaceLayout(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(PtrToIntTy);
3784 SmallVector<bool, 16> WeakIvars;
3785 SmallVector<bool, 16> StrongIvars;
3786
3787 int superInstanceSize = !SuperClassDecl ? 0 :
3788 Context.getASTObjCInterfaceLayout(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(MakeConstantString(IVD->getNameAsString()));
3799 // Get the type encoding for this ivar
3800 std::string TypeStr;
3801 Context.getObjCEncodingForType(IVD->getType(), TypeStr, IVD);
3802 IvarTypes.push_back(MakeConstantString(TypeStr));
3803 IvarAligns.push_back(llvm::ConstantInt::get(IntTy,
3804 Context.getTypeSize(IVD->getType())));
3805 // Get the offset
3806 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
3807 uint64_t Offset = BaseOffset;
3808 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
3809 Offset = BaseOffset - superInstanceSize;
3810 }
3811 llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, 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(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(OffsetValue);
3828 IvarOffsetValues.add(OffsetVar);
3829 Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
3830 IvarOwnership.push_back(lt);
3831 switch (lt) {
3833 StrongIvars.push_back(true);
3834 WeakIvars.push_back(false);
3835 break;
3837 StrongIvars.push_back(false);
3838 WeakIvars.push_back(true);
3839 break;
3840 default:
3841 StrongIvars.push_back(false);
3842 WeakIvars.push_back(false);
3843 }
3844 }
3845 llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
3846 llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
3847 llvm::GlobalVariable *IvarOffsetArray =
3848 IvarOffsetValues.finishAndCreateGlobal(".ivar.offsets",
3849 CGM.getPointerAlign());
3850
3851 // Collect information about instance methods
3852 SmallVector<const ObjCMethodDecl*, 16> InstanceMethods;
3853 InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(),
3854 OID->instmeth_end());
3855
3856 SmallVector<const ObjCMethodDecl*, 16> ClassMethods;
3857 ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(),
3858 OID->classmeth_end());
3859
3860 llvm::Constant *Properties = GeneratePropertyList(OID, ClassDecl);
3861
3862 // Collect the names of referenced protocols
3863 auto RefProtocols = ClassDecl->protocols();
3864 auto RuntimeProtocols =
3865 GetRuntimeProtocolList(RefProtocols.begin(), RefProtocols.end());
3866 SmallVector<std::string, 16> Protocols;
3867 for (const auto *I : RuntimeProtocols)
3868 Protocols.push_back(I->getNameAsString());
3869
3870 // Get the superclass pointer.
3871 llvm::Constant *SuperClass;
3872 if (!SuperClassName.empty()) {
3873 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
3874 } else {
3875 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
3876 }
3877 // Generate the method and instance variable lists
3878 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
3879 InstanceMethods, false);
3880 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
3881 ClassMethods, true);
3882 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
3883 IvarOffsets, 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(IndexTy, ClassABIVersion > 1 ? 2 : 1), nullptr,
3897 llvm::ConstantInt::get(IndexTy, 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(ClassDecl, IVD);
3903 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
3904 // Get the correct ivar field
3905 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
3906 cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList,
3907 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(IntPtrTy, 0);
3923
3924 //Generate metaclass for class methods
3925 llvm::Constant *MetaClassStruct = GenerateClassStructure(
3926 NULLPtr, NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0],
3927 NULLPtr, ClassMethodList, NULLPtr, NULLPtr,
3928 GeneratePropertyList(OID, ClassDecl, true), ZeroPtr, ZeroPtr, true);
3929 CGM.setGVProperties(cast<llvm::GlobalValue>(MetaClassStruct),
3930 OID->getClassInterface());
3931
3932 // Generate the class structure
3933 llvm::Constant *ClassStruct = GenerateClassStructure(
3934 MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr,
3935 llvm::ConstantInt::getSigned(LongTy, instanceSize), IvarList, MethodList,
3936 GenerateProtocolList(Protocols), IvarOffsetArray, Properties,
3937 StrongIvarBitmap, WeakIvarBitmap);
3939 OID->getClassInterface());
3940
3941 // Resolve the class aliases, if they exist.
3942 if (ClassPtrAlias) {
3943 ClassPtrAlias->replaceAllUsesWith(ClassStruct);
3944 ClassPtrAlias->eraseFromParent();
3945 ClassPtrAlias = nullptr;
3946 }
3947 if (MetaClassPtrAlias) {
3948 MetaClassPtrAlias->replaceAllUsesWith(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(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>(SelectorElemTy);
3967 if (!selStructTy) {
3968 selStructTy = llvm::StructType::get(CGM.getLLVMContext(),
3969 { 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(MakeConstantString(stringClass,
3982 ".objc_static_class_name"));
3983
3984 auto array = staticsStruct.beginArray();
3985 array.addAll(ConstantStrings);
3986 array.add(NULLPtr);
3987 array.finishAndAddTo(staticsStruct);
3988
3989 return staticsStruct.finishAndCreateGlobal(".objc_statics",
3990 CGM.getPointerAlign());
3991 }();
3992
3993 ConstantInitBuilder builder(CGM);
3994 auto allStaticsArray = builder.beginArray(fileStatics->getType());
3995 allStaticsArray.add(fileStatics);
3996 allStaticsArray.addNullPointer(fileStatics->getType());
3997
3998 statics = allStaticsArray.finishAndCreateGlobal(".objc_statics_ptr",
3999 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(selStructTy);
4011 auto &table = SelectorTable; // MSVC workaround
4012 std::vector<Selector> allSelectors;
4013 for (auto &entry : table)
4014 allSelectors.push_back(entry.first);
4015 llvm::sort(allSelectors);
4016
4017 for (auto &untypedSel : allSelectors) {
4018 std::string selNameStr = untypedSel.getAsString();
4019 llvm::Constant *selName = ExportUniqueString(selNameStr, ".objc_sel_name");
4020
4021 for (TypedSelector &sel : table[untypedSel]) {
4022 llvm::Constant *selectorTypeEncoding = NULLPtr;
4023 if (!sel.first.empty())
4024 selectorTypeEncoding =
4025 MakeConstantString(sel.first, ".objc_sel_types");
4026
4027 auto selStruct = selectors.beginStruct(selStructTy);
4028 selStruct.add(selName);
4029 selStruct.add(selectorTypeEncoding);
4030 selStruct.finishAndAddTo(selectors);
4031
4032 // Store the selector alias for later replacement
4033 selectorAliases.push_back(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(selStructTy);
4045 selStruct.add(NULLPtr);
4046 selStruct.add(NULLPtr);
4047 selStruct.finishAndAddTo(selectors);
4048
4049 return selectors.finishAndCreateGlobal(".objc_selector_list",
4050 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(Int32Ty, i)
4058 };
4059 // FIXME: We're generating redundant loads and stores here!
4060 llvm::Constant *selPtr = llvm::ConstantExpr::getGetElementPtr(
4061 selectorList->getValueType(), selectorList, idxs);
4062 selectorAliases[i]->replaceAllUsesWith(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(LongTy, selectorCount);
4072
4073 symtab.add(selectorList);
4074
4075 // Number of classes defined.
4076 symtab.addInt(CGM.Int16Ty, Classes.size());
4077 // Number of categories defined
4078 symtab.addInt(CGM.Int16Ty, Categories.size());
4079
4080 // Create an array of classes, then categories, then static object instances
4081 auto classList = symtab.beginArray(PtrToInt8Ty);
4082 classList.addAll(Classes);
4083 classList.addAll(Categories);
4084 // NULL-terminated list of static object instances (mainly constant strings)
4085 classList.add(statics);
4086 classList.add(NULLPtr);
4087 classList.finishAndAddTo(symtab);
4088
4089 // Construct the symbol table.
4090 return symtab.finishAndCreateGlobal("", 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 CGM.getLLVMContext(),
4101 ArrayRef(moduleEltTys).drop_back(unsigned(RuntimeVersion < 10)));
4102
4103 ConstantInitBuilder builder(CGM);
4104 auto module = builder.beginStruct(moduleTy);
4105 // Runtime version, used for ABI compatibility checking.
4106 module.addInt(LongTy, RuntimeVersion);
4107 // sizeof(ModuleTy)
4108 module.addInt(LongTy, CGM.getDataLayout().getTypeStoreSize(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(SM.getMainFileID());
4113 std::string path =
4114 (mainFile->getDir().getName() + "/" + mainFile->getName()).str();
4115 module.add(MakeConstantString(path, ".objc_source_file_name"));
4116 module.add(symtab);
4117
4118 if (RuntimeVersion >= 10) {
4119 switch (CGM.getLangOpts().getGC()) {
4120 case LangOptions::GCOnly:
4121 module.addInt(IntTy, 2);
4122 break;
4123 case LangOptions::NonGC:
4124 if (CGM.getLangOpts().ObjCAutoRefCount)
4125 module.addInt(IntTy, 1);
4126 else
4127 module.addInt(IntTy, 0);
4128 break;
4129 case LangOptions::HybridGC:
4130 module.addInt(IntTy, 1);
4131 break;
4132 }
4133 }
4134
4135 return module.finishAndCreateGlobal("", 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 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
4142 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
4143 &TheModule);
4144 llvm::BasicBlock *EntryBB =
4145 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
4146 CGBuilderTy Builder(CGM, VMContext);
4147 Builder.SetInsertPoint(EntryBB);
4148
4149 llvm::FunctionType *FT =
4150 llvm::FunctionType::get(Builder.getVoidTy(), module->getType(), true);
4151 llvm::FunctionCallee Register =
4152 CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
4153 Builder.CreateCall(Register, module);
4154
4155 if (!ClassAliases.empty()) {
4156 llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
4157 llvm::FunctionType *RegisterAliasTy =
4158 llvm::FunctionType::get(Builder.getVoidTy(),
4159 ArgTypes, false);
4160 llvm::Function *RegisterAlias = llvm::Function::Create(
4161 RegisterAliasTy,
4162 llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
4163 &TheModule);
4164 llvm::BasicBlock *AliasBB =
4165 llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
4166 llvm::BasicBlock *NoAliasBB =
4167 llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
4168
4169 // Branch based on whether the runtime provided class_registerAlias_np()
4170 llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
4171 llvm::Constant::getNullValue(RegisterAlias->getType()));
4172 Builder.CreateCondBr(HasRegisterAlias, AliasBB, 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("_OBJC_CLASS_" + iter->first, true);
4181 if (TheClass) {
4182 Builder.CreateCall(RegisterAlias,
4183 {TheClass, MakeConstantString(iter->second)});
4184 }
4185 }
4186 // Jump to end:
4187 Builder.CreateBr(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 =
4202
4203 bool isDirect = OMD->isDirectMethod();
4204 std::string FunctionName =
4205 getSymbolNameForMethod(OMD, /*include category*/ !isDirect);
4206
4207 if (!isDirect)
4208 return llvm::Function::Create(MethodTy,
4209 llvm::GlobalVariable::InternalLinkage,
4210 FunctionName, &TheModule);
4211
4212 auto *COMD = OMD->getCanonicalDecl();
4213 auto I = DirectMethodDefinitions.find(COMD);
4214 llvm::Function *OldFn = nullptr, *Fn = nullptr;
4215
4216 if (I == DirectMethodDefinitions.end()) {
4217 auto *F =
4218 llvm::Function::Create(MethodTy, llvm::GlobalVariable::ExternalLinkage,
4219 FunctionName, &TheModule);
4220 DirectMethodDefinitions.insert(std::make_pair(COMD, 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(MethodTy, llvm::GlobalValue::ExternalLinkage, "",
4238 &CGM.getModule());
4239 Fn->takeName(OldFn);
4240 OldFn->replaceAllUsesWith(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, 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, EnterCatchFn, ExitCatchFn, 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(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(ExceptionReThrowFn);
4342 Throw->setDoesNotReturn();
4343 } else {
4344 ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
4345 llvm::CallBase *Throw =
4346 CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, 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 WeakReadFn, EnforceType(B, AddrWeakObj.emitRawPointer(CGF), PtrToIdTy));
4359}
4360
4361void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
4362 llvm::Value *src, Address dst) {
4363 CGBuilderTy &B = CGF.Builder;
4364 src = EnforceType(B, src, IdTy);
4365 llvm::Value *dstVal = EnforceType(B, dst.emitRawPointer(CGF), PtrToIdTy);
4366 B.CreateCall(WeakAssignFn, {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, src, IdTy);
4374 llvm::Value *dstVal = EnforceType(B, dst.emitRawPointer(CGF), PtrToIdTy);
4375 // FIXME. Add threadloca assign API
4376 assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI");
4377 B.CreateCall(GlobalAssignFn, {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, src, IdTy);
4385 llvm::Value *dstVal = EnforceType(B, dst.emitRawPointer(CGF), IdTy);
4386 B.CreateCall(IvarAssignFn, {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, src, IdTy);
4393 llvm::Value *dstVal = EnforceType(B, dst.emitRawPointer(CGF), PtrToIdTy);
4394 B.CreateCall(StrongCastAssignFn, {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, DestPtr.emitRawPointer(CGF), PtrTy);
4403 llvm::Value *SrcPtrVal = EnforceType(B, SrcPtr.emitRawPointer(CGF), PtrTy);
4404
4405 B.CreateCall(MemMoveFn, {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(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, ID, BaseValue, Ivar, CVRQualifiers,
4431 EmitIvarOffset(CGF, ID, Ivar));
4432}
4433
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, 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()) {
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(
4463 Int32Ty,
4465 llvm::PointerType::getUnqual(VMContext),
4466 ObjCIvarOffsetVariable(Interface, Ivar),
4467 CGF.getPointerAlign(), "ivar"),
4469 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);
4474 if (!Offset) {
4475 auto GV = new llvm::GlobalVariable(TheModule, IntTy,
4476 false, llvm::GlobalValue::LinkOnceAnyLinkage,
4477 llvm::Constant::getNullValue(IntTy), name);
4478 GV->setAlignment(Align.getAsAlign());
4479 Offset = GV;
4480 }
4481 Offset = CGF.Builder.CreateAlignedLoad(IntTy, Offset, Align);
4482 if (Offset->getType() != PtrDiffTy)
4483 Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
4484 return Offset;
4485 }
4486 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
4487 return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
4488}
4489
4490CGObjCRuntime *
4492 auto Runtime = CGM.getLangOpts().ObjCRuntime;
4493 switch (Runtime.getKind()) {
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
4507 case ObjCRuntime::iOS:
4509 llvm_unreachable("these runtimes are not GNU runtimes");
4510 }
4511 llvm_unreachable("bad runtime");
4512}
Defines the clang::ASTContext interface.
#define V(N, I)
static const ObjCInterfaceDecl * FindIvarInterface(ASTContext &Context, const ObjCInterfaceDecl *OID, const ObjCIvarDecl *OIVD)
static bool isNamed(const NamedDecl *ND, const char(&Str)[Len])
Definition Decl.cpp:3308
TokenType getType() const
Returns the token's type, e.g.
static StringRef getTriple(const Command &Job)
#define SM(sm)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
llvm::json::Array Array
Defines the SourceManager interface.
Defines the Objective-C statement AST node classes.
__device__ __2f16 b
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
SourceManager & getSourceManager()
Definition ASTContext.h:859
TranslationUnitDecl * getTranslationUnitDecl() const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
CanQualType LongTy
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
std::string getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, bool Extended=false) const
Emit the encoded type for the method declaration Decl into S.
std::string getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, const Decl *Container) const
getObjCEncodingForPropertyDecl - Return the encoded type for this method declaration.
IdentifierTable & Idents
Definition ASTContext.h:798
const ASTRecordLayout & getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const
Get or compute information about the layout of the specified Objective-C interface.
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
ObjCPropertyImplDecl * getObjCPropertyImplDeclForPropertyDecl(const ObjCPropertyDecl *PD, const Decl *Container) const
CanQualType BoolTy
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C 'SEL' type.
CanQualType IntTy
QualType getObjCIdType() const
Represents the Objective-CC id type.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
void getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, QualType T, std::string &S, bool Extended) const
getObjCEncodingForMethodParameter - Return the encoded type for a single method parameter or return t...
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:917
uint64_t getCharWidth() const
Return the size of the character type, in bits.
CharUnits getSize() const
getSize - Get the record size in characters.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition CGBuilder.h:146
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition CGBuilder.h:138
virtual llvm::Constant * getAddrOfRTTIDescriptor(QualType Ty)=0
virtual CatchTypeInfo getCatchAllTypeInfo()
Definition CGCXXABI.cpp:346
Implements runtime-specific code generation functions.
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition CGCall.h:311
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitNullInitialization(Address DestPtr, QualType Ty)
EmitNullInitialization - Generate code to set a value of the given type to null, If the type contains...
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition CGCall.cpp:5203
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Value * EmitObjCThrowOperand(const Expr *expr)
Definition CGObjC.cpp:3569
void EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
JumpDest ReturnBlock
ReturnBlock - Unified return block.
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Definition CGExpr.cpp:153
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5359
llvm::Value * LoadObjCSelf()
LoadObjCSelf - Load the value of self.
Definition CGObjC.cpp:1815
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
SmallVector< llvm::Value *, 8 > ObjCEHValueStack
ObjCEHValueStack - Stack of Objective-C exception values, used for rethrows.
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition CGDecl.cpp:203
static bool hasAggregateEvaluationKind(QualType T)
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::LLVMContext & getLLVMContext()
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
This class organizes the cross-function state that is used while generating LLVM code.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
void addCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
bool ReturnTypeUsesFPRet(QualType ResultType)
Return true iff the given type uses 'fpret' when used as a return type.
Definition CGCall.cpp:1772
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
const llvm::DataLayout & getDataLayout() const
const llvm::Triple & getTriple() const
bool ReturnTypeHasInReg(const CGFunctionInfo &FI)
Return true iff the given type has inreg set.
Definition CGCall.cpp:1762
ASTContext & getContext() const
bool ReturnTypeUsesSRet(const CGFunctionInfo &FI)
Return true iff the given type uses 'sret' when used as a return type.
Definition CGCall.cpp:1757
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
ConstantAddress GetAddrOfConstantCString(const std::string &Str, StringRef GlobalName=".str")
Returns a pointer to a character array containing the literal and a terminating '\0' character.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:1805
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition CGCall.cpp:558
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
llvm::Constant * getPointer() const
Definition Address.h:308
void add(llvm::Constant *value)
Add a new value to this initializer.
void addInt(llvm::IntegerType *intTy, uint64_t value, bool isSigned=false)
Add an integer value of a specific type.
void addNullPointer(llvm::PointerType *ptrTy)
Add a null pointer of a specific type.
ArrayBuilder beginArray(llvm::Type *eltTy=nullptr)
llvm::GlobalVariable * finishAndCreateGlobal(As &&...args)
Given that this builder was created by beginning an array or struct directly on a ConstantInitBuilder...
StructBuilder beginStruct(llvm::StructType *ty=nullptr)
void finishAndAddTo(AggregateBuilderBase &parent)
Given that this builder was created by beginning an array or struct component on the given parent bui...
bool isScalar() const
Definition CGValue.h:64
static RValue get(llvm::Value *V)
Definition CGValue.h:99
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition CGValue.h:109
bool isAggregate() const
Definition CGValue.h:66
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition CGValue.h:84
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition CGValue.h:79
llvm::Value * getPointer() const
Definition Address.h:66
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
static void add(Kind k)
Definition DeclBase.cpp:248
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
Definition DeclBase.cpp:871
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition DeclBase.cpp:576
bool hasAttr() const
Definition DeclBase.h:585
StringRef getName() const
The name of this FileEntry.
Definition FileEntry.h:61
DirectoryEntryRef getDir() const
Definition FileEntry.h:78
StringRef getName() const
Return the actual identifier string.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
clang::ObjCRuntime ObjCRuntime
std::string ObjCConstantStringClass
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
Visibility getVisibility() const
Determines the visibility of this entity.
Definition Decl.h:444
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:317
const Expr * getThrowExpr() const
Definition StmtObjC.h:370
const ObjCProtocolList & getReferencedProtocols() const
Definition DeclObjC.h:2396
ObjCCategoryDecl * getCategoryDecl() const
const ObjCInterfaceDecl * getClassInterface() const
Definition DeclObjC.h:2793
classmeth_iterator classmeth_end() const
Definition DeclObjC.h:1058
classmeth_iterator classmeth_begin() const
Definition DeclObjC.h:1054
instmeth_range instance_methods() const
Definition DeclObjC.h:1033
instmeth_iterator instmeth_end() const
Definition DeclObjC.h:1041
instmeth_iterator instmeth_begin() const
Definition DeclObjC.h:1037
prop_range properties() const
Definition DeclObjC.h:967
classmeth_range class_methods() const
Definition DeclObjC.h:1050
propimpl_range property_impls() const
Definition DeclObjC.h:2513
const ObjCInterfaceDecl * getClassInterface() const
Definition DeclObjC.h:2486
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
all_protocol_iterator all_referenced_protocol_end() const
Definition DeclObjC.h:1435
all_protocol_range all_referenced_protocols() const
Definition DeclObjC.h:1417
ObjCIvarDecl * all_declared_ivar_begin()
all_declared_ivar_begin - return first ivar declared in this class, its extensions and its implementa...
protocol_range protocols() const
Definition DeclObjC.h:1359
all_protocol_iterator all_referenced_protocol_begin() const
Definition DeclObjC.h:1422
ObjCInterfaceDecl * getSuperClass() const
Definition DeclObjC.cpp:349
ObjCInterfaceDecl * getDefinition()
Retrieve the definition of this class, or NULL if this class has been forward-declared (with @class) ...
Definition DeclObjC.h:1542
known_extensions_range known_extensions() const
Definition DeclObjC.h:1762
ObjCInterfaceDecl * getDecl() const
Get the declaration of this interface.
Definition Type.cpp:953
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
AccessControl getAccessControl() const
Definition DeclObjC.h:2000
ObjCInterfaceDecl * getContainingInterface()
Return the class interface that this ivar is logically contained in; this is either the interface whe...
ObjCIvarDecl * getNextIvar()
Definition DeclObjC.h:1987
ImplicitParamDecl * getSelfDecl() const
Definition DeclObjC.h:418
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
Definition DeclObjC.cpp:906
ObjCMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
bool isDirectMethod() const
True if the method is tagged as objc_direct.
Definition DeclObjC.cpp:868
Selector getSelector() const
Definition DeclObjC.h:327
ImplicitParamDecl * getCmdDecl() const
Definition DeclObjC.h:420
QualType getReturnType() const
Definition DeclObjC.h:329
bool isClassMethod() const
Definition DeclObjC.h:434
const ObjCObjectType * getObjectType() const
Gets the type pointed to by this ObjC pointer.
Definition TypeBase.h:8086
const ObjCInterfaceType * getInterfaceType() const
If this pointer points to an Objective C @interface type, gets the type for that interface.
Definition Type.cpp:1854
ObjCMethodDecl * getGetterMethodDecl() const
Definition DeclObjC.h:901
ObjCMethodDecl * getSetterMethodDecl() const
Definition DeclObjC.h:904
QualType getType() const
Definition DeclObjC.h:804
ObjCProtocolDecl * getDefinition()
Retrieve the definition of this protocol, if any.
Definition DeclObjC.h:2250
bool isNonRuntimeProtocol() const
This is true iff the protocol is tagged with the objc_non_runtime_protocol attribute.
protocol_iterator protocol_begin() const
Definition DeclObjC.h:2165
protocol_range protocols() const
Definition DeclObjC.h:2161
protocol_iterator protocol_end() const
Definition DeclObjC.h:2172
const VersionTuple & getVersion() const
Definition ObjCRuntime.h:78
bool isNonFragile() const
Does this runtime follow the set of implied behaviors for a "non-fragile" ABI?
Definition ObjCRuntime.h:82
Kind
The basic Objective-C runtimes that we know about.
Definition ObjCRuntime.h:31
@ MacOSX
'macosx' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition ObjCRuntime.h:35
@ FragileMacOSX
'macosx-fragile' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition ObjCRuntime.h:40
@ GNUstep
'gnustep' is the modern non-fragile GNUstep runtime.
Definition ObjCRuntime.h:56
@ ObjFW
'objfw' is the Objective-C runtime included in ObjFW
Definition ObjCRuntime.h:59
@ iOS
'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition ObjCRuntime.h:45
@ GCC
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI
Definition ObjCRuntime.h:53
@ WatchOS
'watchos' is a variant of iOS for Apple's watchOS.
Definition ObjCRuntime.h:49
A (possibly-)qualified type.
Definition TypeBase.h:937
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition TypeBase.h:361
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition TypeBase.h:354
@ OCL_None
There is no lifetime qualification on this type.
Definition TypeBase.h:350
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition TypeBase.h:364
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition TypeBase.h:367
std::string getAsString() const
Derive the full selector name (e.g.
bool containsNonAscii() const
Definition Expr.h:1927
unsigned getLength() const
Definition Expr.h:1912
uint32_t getCodeUnit(size_t i) const
Definition Expr.h:1885
StringRef getString() const
Definition Expr.h:1870
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:490
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
Definition Decl.h:151
bool isVoidType() const
Definition TypeBase.h:9034
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
bool isObjCQualifiedIdType() const
Definition TypeBase.h:8868
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:754
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9156
bool isObjCIdType() const
Definition TypeBase.h:8880
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
bool hasPointerRepresentation() const
Whether this type is represented natively as a pointer.
Definition TypeBase.h:9205
QualType getType() const
Definition Decl.h:723
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
CGObjCRuntime * CreateGNUObjCRuntime(CodeGenModule &CGM)
Creates an instance of an Objective-C runtime class.
StringRef getName(const HeaderType T)
Definition HeaderFile.h:38
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
RangeSelector node(std::string ID)
Selects a node, including trailing semicolon, if any (for declarations and non-expression statements)...
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
@ Private
'private' clause, allowed on 'parallel', 'serial', 'loop', 'parallel loop', and 'serial loop' constru...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Result
The result type of a method or function.
Definition TypeBase.h:905
Selector GetNullarySelector(StringRef name, ASTContext &Ctx)
Utility function for constructing a nullary selector.
@ Type
The name was classified as a type.
Definition Sema.h:564
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Interface
The "__interface" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5961
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5967
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition Visibility.h:37
unsigned long uint64_t
unsigned int uint32_t
int const char * function
Definition c++config.h:31