1 //===- ObjCRuntime.h - Objective-C Runtime Configuration --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines types useful for describing an Objective-C runtime.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_OBJCRUNTIME_H
15 #define LLVM_CLANG_BASIC_OBJCRUNTIME_H
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/HashBuilder.h"
22 #include "llvm/Support/VersionTuple.h"
27 /// The basic abstraction for the target Objective-C runtime.
30 /// The basic Objective-C runtimes that we know about.
32 /// 'macosx' is the Apple-provided NeXT-derived runtime on Mac OS
33 /// X platforms that use the non-fragile ABI; the version is a
34 /// release of that OS.
37 /// 'macosx-fragile' is the Apple-provided NeXT-derived runtime on
38 /// Mac OS X platforms that use the fragile ABI; the version is a
39 /// release of that OS.
42 /// 'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS
43 /// simulator; it is always non-fragile. The version is a release
47 /// 'watchos' is a variant of iOS for Apple's watchOS. The version
48 /// is a release version of watchOS.
51 /// 'gcc' is the Objective-C runtime shipped with GCC, implementing a
52 /// fragile Objective-C ABI
55 /// 'gnustep' is the modern non-fragile GNUstep runtime.
58 /// 'objfw' is the Objective-C runtime included in ObjFW
63 Kind TheKind = MacOSX;
67 /// A bogus initialization of the runtime.
68 ObjCRuntime() = default;
69 ObjCRuntime(Kind kind, const VersionTuple &version)
70 : TheKind(kind), Version(version) {}
72 void set(Kind kind, VersionTuple version) {
77 Kind getKind() const { return TheKind; }
78 const VersionTuple &getVersion() const { return Version; }
80 /// Does this runtime follow the set of implied behaviors for a
81 /// "non-fragile" ABI?
82 bool isNonFragile() const {
84 case FragileMacOSX: return false;
85 case GCC: return false;
86 case MacOSX: return true;
87 case GNUstep: return true;
88 case ObjFW: return true;
89 case iOS: return true;
90 case WatchOS: return true;
92 llvm_unreachable("bad kind");
95 /// The inverse of isNonFragile(): does this runtime follow the set of
96 /// implied behaviors for a "fragile" ABI?
97 bool isFragile() const { return !isNonFragile(); }
99 /// The default dispatch mechanism to use for the specified architecture
100 bool isLegacyDispatchDefaultForArch(llvm::Triple::ArchType Arch) {
101 // The GNUstep runtime uses a newer dispatch method by default from
102 // version 1.6 onwards
103 if (getKind() == GNUstep && getVersion() >= VersionTuple(1, 6)) {
104 if (Arch == llvm::Triple::arm ||
105 Arch == llvm::Triple::x86 ||
106 Arch == llvm::Triple::x86_64)
109 else if ((getKind() == MacOSX) && isNonFragile() &&
110 (getVersion() >= VersionTuple(10, 0)) &&
111 (getVersion() < VersionTuple(10, 6)))
112 return Arch != llvm::Triple::x86_64;
113 // Except for deployment target of 10.5 or less,
114 // Mac runtimes use legacy dispatch everywhere now.
118 /// Is this runtime basically of the GNU family of runtimes?
119 bool isGNUFamily() const {
131 llvm_unreachable("bad kind");
134 /// Is this runtime basically of the NeXT family of runtimes?
135 bool isNeXTFamily() const {
136 // For now, this is just the inverse of isGNUFamily(), but that's
137 // not inherently true.
138 return !isGNUFamily();
141 /// Does this runtime allow ARC at all?
142 bool allowsARC() const {
145 // No stub library for the fragile runtime.
146 return getVersion() >= VersionTuple(10, 7);
147 case MacOSX: return true;
148 case iOS: return true;
149 case WatchOS: return true;
150 case GCC: return false;
151 case GNUstep: return true;
152 case ObjFW: return true;
154 llvm_unreachable("bad kind");
157 /// Does this runtime natively provide the ARC entrypoints?
159 /// ARC cannot be directly supported on a platform that does not provide
160 /// these entrypoints, although it may be supportable via a stub
162 bool hasNativeARC() const {
164 case FragileMacOSX: return getVersion() >= VersionTuple(10, 7);
165 case MacOSX: return getVersion() >= VersionTuple(10, 7);
166 case iOS: return getVersion() >= VersionTuple(5);
167 case WatchOS: return true;
169 case GCC: return false;
170 case GNUstep: return getVersion() >= VersionTuple(1, 6);
171 case ObjFW: return true;
173 llvm_unreachable("bad kind");
176 /// Does this runtime provide ARC entrypoints that are likely to be faster
177 /// than an ordinary message send of the appropriate selector?
179 /// The ARC entrypoints are guaranteed to be equivalent to just sending the
180 /// corresponding message. If the entrypoint is implemented naively as just a
181 /// message send, using it is a trade-off: it sacrifices a few cycles of
182 /// overhead to save a small amount of code. However, it's possible for
183 /// runtimes to detect and special-case classes that use "standard"
184 /// retain/release behavior; if that's dynamically a large proportion of all
185 /// retained objects, using the entrypoint will also be faster than using a
188 /// When this method returns true, Clang will turn non-super message sends of
189 /// certain selectors into calls to the correspond entrypoint:
190 /// retain => objc_retain
191 /// release => objc_release
192 /// autorelease => objc_autorelease
193 bool shouldUseARCFunctionsForRetainRelease() const {
198 return getVersion() >= VersionTuple(10, 10);
200 return getVersion() >= VersionTuple(8);
210 llvm_unreachable("bad kind");
213 /// Does this runtime provide entrypoints that are likely to be faster
214 /// than an ordinary message send of the "alloc" selector?
216 /// The "alloc" entrypoint is guaranteed to be equivalent to just sending the
217 /// corresponding message. If the entrypoint is implemented naively as just a
218 /// message send, using it is a trade-off: it sacrifices a few cycles of
219 /// overhead to save a small amount of code. However, it's possible for
220 /// runtimes to detect and special-case classes that use "standard"
221 /// alloc behavior; if that's dynamically a large proportion of all
222 /// objects, using the entrypoint will also be faster than using a message
225 /// When this method returns true, Clang will turn non-super message sends of
226 /// certain selectors into calls to the corresponding entrypoint:
227 /// alloc => objc_alloc
228 /// allocWithZone:nil => objc_allocWithZone
229 bool shouldUseRuntimeFunctionsForAlloc() const {
234 return getVersion() >= VersionTuple(10, 10);
236 return getVersion() >= VersionTuple(8);
247 llvm_unreachable("bad kind");
250 /// Does this runtime provide the objc_alloc_init entrypoint? This can apply
251 /// the same optimization as objc_alloc, but also sends an -init message,
252 /// reducing code size on the caller.
253 bool shouldUseRuntimeFunctionForCombinedAllocInit() const {
256 return getVersion() >= VersionTuple(10, 14, 4);
258 return getVersion() >= VersionTuple(12, 2);
260 return getVersion() >= VersionTuple(5, 2);
266 /// Does this runtime supports optimized setter entrypoints?
267 bool hasOptimizedSetter() const {
270 return getVersion() >= VersionTuple(10, 8);
272 return (getVersion() >= VersionTuple(6));
276 return getVersion() >= VersionTuple(1, 7);
282 /// Does this runtime allow the use of __weak?
283 bool allowsWeak() const {
284 return hasNativeWeak();
287 /// Does this runtime natively provide ARC-compliant 'weak'
289 bool hasNativeWeak() const {
290 // Right now, this is always equivalent to whether the runtime
291 // natively supports ARC decision.
292 return hasNativeARC();
295 /// Does this runtime directly support the subscripting methods?
297 /// This is really a property of the library, not the runtime.
298 bool hasSubscripting() const {
300 case FragileMacOSX: return false;
301 case MacOSX: return getVersion() >= VersionTuple(10, 11);
302 case iOS: return getVersion() >= VersionTuple(9);
303 case WatchOS: return true;
305 // This is really a lie, because some implementations and versions
306 // of the runtime do not support ARC. Probably -fgnu-runtime
307 // should imply a "maximal" runtime or something?
308 case GCC: return true;
309 case GNUstep: return true;
310 case ObjFW: return true;
312 llvm_unreachable("bad kind");
315 /// Does this runtime allow sizeof or alignof on object types?
316 bool allowsSizeofAlignof() const {
320 /// Does this runtime allow pointer arithmetic on objects?
322 /// This covers +, -, ++, --, and (if isSubscriptPointerArithmetic()
324 bool allowsPointerArithmetic() const {
336 llvm_unreachable("bad kind");
339 /// Is subscripting pointer arithmetic?
340 bool isSubscriptPointerArithmetic() const {
341 return allowsPointerArithmetic();
344 /// Does this runtime provide an objc_terminate function?
346 /// This is used in handlers for exceptions during the unwind process;
347 /// without it, abort() must be used in pure ObjC files.
348 bool hasTerminate() const {
350 case FragileMacOSX: return getVersion() >= VersionTuple(10, 8);
351 case MacOSX: return getVersion() >= VersionTuple(10, 8);
352 case iOS: return getVersion() >= VersionTuple(5);
353 case WatchOS: return true;
354 case GCC: return false;
355 case GNUstep: return false;
356 case ObjFW: return false;
358 llvm_unreachable("bad kind");
361 /// Does this runtime support weakly importing classes?
362 bool hasWeakClassImport() const {
364 case MacOSX: return true;
365 case iOS: return true;
366 case WatchOS: return true;
367 case FragileMacOSX: return false;
368 case GCC: return true;
369 case GNUstep: return true;
370 case ObjFW: return true;
372 llvm_unreachable("bad kind");
375 /// Does this runtime use zero-cost exceptions?
376 bool hasUnwindExceptions() const {
378 case MacOSX: return true;
379 case iOS: return true;
380 case WatchOS: return true;
381 case FragileMacOSX: return false;
382 case GCC: return true;
383 case GNUstep: return true;
384 case ObjFW: return true;
386 llvm_unreachable("bad kind");
389 bool hasAtomicCopyHelper() const {
397 return getVersion() >= VersionTuple(1, 7);
398 default: return false;
402 /// Is objc_unsafeClaimAutoreleasedReturnValue available?
403 bool hasARCUnsafeClaimAutoreleasedReturnValue() const {
407 return getVersion() >= VersionTuple(10, 11);
409 return getVersion() >= VersionTuple(9);
411 return getVersion() >= VersionTuple(2);
419 /// Are the empty collection symbols available?
420 bool hasEmptyCollections() const {
425 return getVersion() >= VersionTuple(10, 11);
427 return getVersion() >= VersionTuple(9);
429 return getVersion() >= VersionTuple(2);
433 /// Returns true if this Objective-C runtime supports Objective-C class
435 bool allowsClassStubs() const {
447 llvm_unreachable("bad kind");
450 /// Does this runtime supports direct dispatch
451 bool allowsDirectDispatch() const {
453 case FragileMacOSX: return false;
454 case MacOSX: return true;
455 case iOS: return true;
456 case WatchOS: return true;
457 case GCC: return false;
458 case GNUstep: return false;
459 case ObjFW: return false;
461 llvm_unreachable("bad kind");
464 /// Try to parse an Objective-C runtime specification from the given
467 /// \return true on error.
468 bool tryParse(StringRef input);
470 std::string getAsString() const;
472 friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right) {
473 return left.getKind() == right.getKind() &&
474 left.getVersion() == right.getVersion();
477 friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right) {
478 return !(left == right);
481 friend llvm::hash_code hash_value(const ObjCRuntime &OCR) {
482 return llvm::hash_combine(OCR.getKind(), OCR.getVersion());
485 template <typename HasherT, llvm::support::endianness Endianness>
486 friend void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
487 const ObjCRuntime &OCR) {
488 HBuilder.add(OCR.getKind(), OCR.getVersion());
492 raw_ostream &operator<<(raw_ostream &out, const ObjCRuntime &value);
496 #endif // LLVM_CLANG_BASIC_OBJCRUNTIME_H