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_OBJCRUNTIME_H
16 #define LLVM_CLANG_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 /// 'gcc' is the Objective-C runtime shipped with GCC, implementing a
45 /// fragile Objective-C ABI
48 /// 'gnustep' is the modern non-fragile GNUstep runtime.
51 /// 'objfw' is the Objective-C runtime included in ObjFW
60 /// A bogus initialization of the runtime.
61 ObjCRuntime() : TheKind(MacOSX) {}
63 ObjCRuntime(Kind kind, const VersionTuple &version)
64 : TheKind(kind), Version(version) {}
66 void set(Kind kind, VersionTuple version) {
71 Kind getKind() const { return TheKind; }
72 const VersionTuple &getVersion() const { return Version; }
74 /// \brief Does this runtime follow the set of implied behaviors for a
75 /// "non-fragile" ABI?
76 bool isNonFragile() const {
78 case FragileMacOSX: return false;
79 case GCC: return false;
80 case MacOSX: return true;
81 case GNUstep: return true;
82 case ObjFW: return true;
83 case iOS: return true;
85 llvm_unreachable("bad kind");
88 /// The inverse of isNonFragile(): does this runtime follow the set of
89 /// implied behaviors for a "fragile" ABI?
90 bool isFragile() const { return !isNonFragile(); }
92 /// The default dispatch mechanism to use for the specified architecture
93 bool isLegacyDispatchDefaultForArch(llvm::Triple::ArchType Arch) {
94 // The GNUstep runtime uses a newer dispatch method by default from
95 // version 1.6 onwards
96 if (getKind() == GNUstep && getVersion() >= VersionTuple(1, 6)) {
97 if (Arch == llvm::Triple::arm ||
98 Arch == llvm::Triple::x86 ||
99 Arch == llvm::Triple::x86_64)
102 else if ((getKind() == MacOSX) && isNonFragile() &&
103 (getVersion() >= VersionTuple(10, 0)) &&
104 (getVersion() < VersionTuple(10, 6)))
105 return Arch != llvm::Triple::x86_64;
106 // Except for deployment target of 10.5 or less,
107 // Mac runtimes use legacy dispatch everywhere now.
111 /// \brief Is this runtime basically of the GNU family of runtimes?
112 bool isGNUFamily() const {
123 llvm_unreachable("bad kind");
126 /// \brief Is this runtime basically of the NeXT family of runtimes?
127 bool isNeXTFamily() const {
128 // For now, this is just the inverse of isGNUFamily(), but that's
129 // not inherently true.
130 return !isGNUFamily();
133 /// \brief Does this runtime allow ARC at all?
134 bool allowsARC() const {
136 case FragileMacOSX: return false;
137 case MacOSX: return true;
138 case iOS: return true;
139 case GCC: return false;
140 case GNUstep: return true;
141 case ObjFW: return true;
143 llvm_unreachable("bad kind");
146 /// \brief Does this runtime natively provide the ARC entrypoints?
148 /// ARC cannot be directly supported on a platform that does not provide
149 /// these entrypoints, although it may be supportable via a stub
151 bool hasNativeARC() const {
153 case FragileMacOSX: return false;
154 case MacOSX: return getVersion() >= VersionTuple(10, 7);
155 case iOS: return getVersion() >= VersionTuple(5);
157 case GCC: return false;
158 case GNUstep: return getVersion() >= VersionTuple(1, 6);
159 case ObjFW: return true;
161 llvm_unreachable("bad kind");
164 /// \brief Does this runtime supports optimized setter entrypoints?
165 bool hasOptimizedSetter() const {
168 return getVersion() >= VersionTuple(10, 8);
170 return (getVersion() >= VersionTuple(6));
172 return getVersion() >= VersionTuple(1, 7);
179 /// Does this runtime allow the use of __weak?
180 bool allowsWeak() const {
181 return hasNativeWeak();
184 /// \brief Does this runtime natively provide ARC-compliant 'weak'
186 bool hasNativeWeak() const {
187 // Right now, this is always equivalent to whether the runtime
188 // natively supports ARC decision.
189 return hasNativeARC();
192 /// \brief Does this runtime directly support the subscripting methods?
194 /// This is really a property of the library, not the runtime.
195 bool hasSubscripting() const {
197 case FragileMacOSX: return false;
198 case MacOSX: return getVersion() >= VersionTuple(10, 8);
199 case iOS: return getVersion() >= VersionTuple(6);
201 // This is really a lie, because some implementations and versions
202 // of the runtime do not support ARC. Probably -fgnu-runtime
203 // should imply a "maximal" runtime or something?
204 case GCC: return true;
205 case GNUstep: return true;
206 case ObjFW: return true;
208 llvm_unreachable("bad kind");
211 /// \brief Does this runtime allow sizeof or alignof on object types?
212 bool allowsSizeofAlignof() const {
216 /// \brief Does this runtime allow pointer arithmetic on objects?
218 /// This covers +, -, ++, --, and (if isSubscriptPointerArithmetic()
220 bool allowsPointerArithmetic() const {
231 llvm_unreachable("bad kind");
234 /// \brief Is subscripting pointer arithmetic?
235 bool isSubscriptPointerArithmetic() const {
236 return allowsPointerArithmetic();
239 /// \brief Does this runtime provide an objc_terminate function?
241 /// This is used in handlers for exceptions during the unwind process;
242 /// without it, abort() must be used in pure ObjC files.
243 bool hasTerminate() const {
245 case FragileMacOSX: return getVersion() >= VersionTuple(10, 8);
246 case MacOSX: return getVersion() >= VersionTuple(10, 8);
247 case iOS: return getVersion() >= VersionTuple(5);
248 case GCC: return false;
249 case GNUstep: return false;
250 case ObjFW: return false;
252 llvm_unreachable("bad kind");
255 /// \brief Does this runtime support weakly importing classes?
256 bool hasWeakClassImport() const {
258 case MacOSX: return true;
259 case iOS: return true;
260 case FragileMacOSX: return false;
261 case GCC: return true;
262 case GNUstep: return true;
263 case ObjFW: return true;
265 llvm_unreachable("bad kind");
268 /// \brief Does this runtime use zero-cost exceptions?
269 bool hasUnwindExceptions() const {
271 case MacOSX: return true;
272 case iOS: return true;
273 case FragileMacOSX: return false;
274 case GCC: return true;
275 case GNUstep: return true;
276 case ObjFW: return true;
278 llvm_unreachable("bad kind");
281 bool hasAtomicCopyHelper() const {
288 return getVersion() >= VersionTuple(1, 7);
289 default: return false;
293 /// \brief Try to parse an Objective-C runtime specification from the given
296 /// \return true on error.
297 bool tryParse(StringRef input);
299 std::string getAsString() const;
301 friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right) {
302 return left.getKind() == right.getKind() &&
303 left.getVersion() == right.getVersion();
306 friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right) {
307 return !(left == right);
311 raw_ostream &operator<<(raw_ostream &out, const ObjCRuntime &value);
313 } // end namespace clang