1 //===--- ObjCRuntime.h - Objective-C Runtime Configuration ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines types useful for describing an Objective-C runtime.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_OBJCRUNTIME_H
16 #define LLVM_CLANG_BASIC_OBJCRUNTIME_H
18 #include "clang/Basic/VersionTuple.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Support/ErrorHandling.h"
24 /// \brief The basic abstraction for the target Objective-C runtime.
27 /// \brief The basic Objective-C runtimes that we know about.
29 /// 'macosx' is the Apple-provided NeXT-derived runtime on Mac OS
30 /// X platforms that use the non-fragile ABI; the version is a
31 /// release of that OS.
34 /// 'macosx-fragile' is the Apple-provided NeXT-derived runtime on
35 /// Mac OS X platforms that use the fragile ABI; the version is a
36 /// release of that OS.
39 /// 'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS
40 /// simulator; it is always non-fragile. The version is a release
44 /// 'watchos' is a variant of iOS for Apple's watchOS. The version
45 /// is a release version of watchOS.
48 /// 'gcc' is the Objective-C runtime shipped with GCC, implementing a
49 /// fragile Objective-C ABI
52 /// 'gnustep' is the modern non-fragile GNUstep runtime.
55 /// 'objfw' is the Objective-C runtime included in ObjFW
64 /// A bogus initialization of the runtime.
65 ObjCRuntime() : TheKind(MacOSX) {}
67 ObjCRuntime(Kind kind, const VersionTuple &version)
68 : TheKind(kind), Version(version) {}
70 void set(Kind kind, VersionTuple version) {
75 Kind getKind() const { return TheKind; }
76 const VersionTuple &getVersion() const { return Version; }
78 /// \brief Does this runtime follow the set of implied behaviors for a
79 /// "non-fragile" ABI?
80 bool isNonFragile() const {
82 case FragileMacOSX: return false;
83 case GCC: return false;
84 case MacOSX: return true;
85 case GNUstep: return true;
86 case ObjFW: return true;
87 case iOS: return true;
88 case WatchOS: return true;
90 llvm_unreachable("bad kind");
93 /// The inverse of isNonFragile(): does this runtime follow the set of
94 /// implied behaviors for a "fragile" ABI?
95 bool isFragile() const { return !isNonFragile(); }
97 /// The default dispatch mechanism to use for the specified architecture
98 bool isLegacyDispatchDefaultForArch(llvm::Triple::ArchType Arch) {
99 // The GNUstep runtime uses a newer dispatch method by default from
100 // version 1.6 onwards
101 if (getKind() == GNUstep && getVersion() >= VersionTuple(1, 6)) {
102 if (Arch == llvm::Triple::arm ||
103 Arch == llvm::Triple::x86 ||
104 Arch == llvm::Triple::x86_64)
107 else if ((getKind() == MacOSX) && isNonFragile() &&
108 (getVersion() >= VersionTuple(10, 0)) &&
109 (getVersion() < VersionTuple(10, 6)))
110 return Arch != llvm::Triple::x86_64;
111 // Except for deployment target of 10.5 or less,
112 // Mac runtimes use legacy dispatch everywhere now.
116 /// \brief Is this runtime basically of the GNU family of runtimes?
117 bool isGNUFamily() const {
129 llvm_unreachable("bad kind");
132 /// \brief Is this runtime basically of the NeXT family of runtimes?
133 bool isNeXTFamily() const {
134 // For now, this is just the inverse of isGNUFamily(), but that's
135 // not inherently true.
136 return !isGNUFamily();
139 /// \brief Does this runtime allow ARC at all?
140 bool allowsARC() const {
143 // No stub library for the fragile runtime.
144 return getVersion() >= VersionTuple(10, 7);
145 case MacOSX: return true;
146 case iOS: return true;
147 case WatchOS: return true;
148 case GCC: return false;
149 case GNUstep: return true;
150 case ObjFW: return true;
152 llvm_unreachable("bad kind");
155 /// \brief Does this runtime natively provide the ARC entrypoints?
157 /// ARC cannot be directly supported on a platform that does not provide
158 /// these entrypoints, although it may be supportable via a stub
160 bool hasNativeARC() const {
162 case FragileMacOSX: return getVersion() >= VersionTuple(10, 7);
163 case MacOSX: return getVersion() >= VersionTuple(10, 7);
164 case iOS: return getVersion() >= VersionTuple(5);
165 case WatchOS: return true;
167 case GCC: return false;
168 case GNUstep: return getVersion() >= VersionTuple(1, 6);
169 case ObjFW: return true;
171 llvm_unreachable("bad kind");
174 /// \brief Does this runtime supports optimized setter entrypoints?
175 bool hasOptimizedSetter() const {
178 return getVersion() >= VersionTuple(10, 8);
180 return (getVersion() >= VersionTuple(6));
184 return getVersion() >= VersionTuple(1, 7);
191 /// Does this runtime allow the use of __weak?
192 bool allowsWeak() const {
193 return hasNativeWeak();
196 /// \brief Does this runtime natively provide ARC-compliant 'weak'
198 bool hasNativeWeak() const {
199 // Right now, this is always equivalent to whether the runtime
200 // natively supports ARC decision.
201 return hasNativeARC();
204 /// \brief Does this runtime directly support the subscripting methods?
206 /// This is really a property of the library, not the runtime.
207 bool hasSubscripting() const {
209 case FragileMacOSX: return false;
210 case MacOSX: return getVersion() >= VersionTuple(10, 8);
211 case iOS: return getVersion() >= VersionTuple(6);
212 case WatchOS: return true;
214 // This is really a lie, because some implementations and versions
215 // of the runtime do not support ARC. Probably -fgnu-runtime
216 // should imply a "maximal" runtime or something?
217 case GCC: return true;
218 case GNUstep: return true;
219 case ObjFW: return true;
221 llvm_unreachable("bad kind");
224 /// \brief Does this runtime allow sizeof or alignof on object types?
225 bool allowsSizeofAlignof() const {
229 /// \brief Does this runtime allow pointer arithmetic on objects?
231 /// This covers +, -, ++, --, and (if isSubscriptPointerArithmetic()
233 bool allowsPointerArithmetic() const {
245 llvm_unreachable("bad kind");
248 /// \brief Is subscripting pointer arithmetic?
249 bool isSubscriptPointerArithmetic() const {
250 return allowsPointerArithmetic();
253 /// \brief Does this runtime provide an objc_terminate function?
255 /// This is used in handlers for exceptions during the unwind process;
256 /// without it, abort() must be used in pure ObjC files.
257 bool hasTerminate() const {
259 case FragileMacOSX: return getVersion() >= VersionTuple(10, 8);
260 case MacOSX: return getVersion() >= VersionTuple(10, 8);
261 case iOS: return getVersion() >= VersionTuple(5);
262 case WatchOS: return true;
263 case GCC: return false;
264 case GNUstep: return false;
265 case ObjFW: return false;
267 llvm_unreachable("bad kind");
270 /// \brief Does this runtime support weakly importing classes?
271 bool hasWeakClassImport() const {
273 case MacOSX: return true;
274 case iOS: return true;
275 case WatchOS: return true;
276 case FragileMacOSX: return false;
277 case GCC: return true;
278 case GNUstep: return true;
279 case ObjFW: return true;
281 llvm_unreachable("bad kind");
284 /// \brief Does this runtime use zero-cost exceptions?
285 bool hasUnwindExceptions() const {
287 case MacOSX: return true;
288 case iOS: return true;
289 case WatchOS: return true;
290 case FragileMacOSX: return false;
291 case GCC: return true;
292 case GNUstep: return true;
293 case ObjFW: return true;
295 llvm_unreachable("bad kind");
298 bool hasAtomicCopyHelper() const {
306 return getVersion() >= VersionTuple(1, 7);
307 default: return false;
311 /// Is objc_unsafeClaimAutoreleasedReturnValue available?
312 bool hasARCUnsafeClaimAutoreleasedReturnValue() const {
316 return getVersion() >= VersionTuple(10, 11);
318 return getVersion() >= VersionTuple(9);
320 return getVersion() >= VersionTuple(2);
329 /// \brief Try to parse an Objective-C runtime specification from the given
332 /// \return true on error.
333 bool tryParse(StringRef input);
335 std::string getAsString() const;
337 friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right) {
338 return left.getKind() == right.getKind() &&
339 left.getVersion() == right.getVersion();
342 friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right) {
343 return !(left == right);
347 raw_ostream &operator<<(raw_ostream &out, const ObjCRuntime &value);
349 } // end namespace clang