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 false;
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)
101 // Mac runtimes use legacy dispatch everywhere except x86-64
102 } else if (isNeXTFamily() && isNonFragile())
103 return Arch != llvm::Triple::x86_64;
107 /// \brief Is this runtime basically of the GNU family of runtimes?
108 bool isGNUFamily() const {
119 llvm_unreachable("bad kind");
122 /// \brief Is this runtime basically of the NeXT family of runtimes?
123 bool isNeXTFamily() const {
124 // For now, this is just the inverse of isGNUFamily(), but that's
125 // not inherently true.
126 return !isGNUFamily();
129 /// \brief Does this runtime allow ARC at all?
130 bool allowsARC() const {
132 case FragileMacOSX: return false;
133 case MacOSX: return true;
134 case iOS: return true;
135 case GCC: return false;
136 case GNUstep: return true;
137 case ObjFW: return true;
139 llvm_unreachable("bad kind");
142 /// \brief Does this runtime natively provide the ARC entrypoints?
144 /// ARC cannot be directly supported on a platform that does not provide
145 /// these entrypoints, although it may be supportable via a stub
147 bool hasNativeARC() const {
149 case FragileMacOSX: return false;
150 case MacOSX: return getVersion() >= VersionTuple(10, 7);
151 case iOS: return getVersion() >= VersionTuple(5);
153 case GCC: return false;
154 case GNUstep: return getVersion() >= VersionTuple(1, 6);
155 case ObjFW: return true;
157 llvm_unreachable("bad kind");
160 /// \brief Does this runtime supports optimized setter entrypoints?
161 bool hasOptimizedSetter() const {
164 return getVersion() >= VersionTuple(10, 8);
166 return (getVersion() >= VersionTuple(6));
168 return getVersion() >= VersionTuple(1, 7);
175 /// Does this runtime allow the use of __weak?
176 bool allowsWeak() const {
177 return hasNativeWeak();
180 /// \brief Does this runtime natively provide ARC-compliant 'weak'
182 bool hasNativeWeak() const {
183 // Right now, this is always equivalent to whether the runtime
184 // natively supports ARC decision.
185 return hasNativeARC();
188 /// \brief Does this runtime directly support the subscripting methods?
190 /// This is really a property of the library, not the runtime.
191 bool hasSubscripting() const {
193 case FragileMacOSX: return false;
194 case MacOSX: return getVersion() >= VersionTuple(10, 8);
195 case iOS: return getVersion() >= VersionTuple(6);
197 // This is really a lie, because some implementations and versions
198 // of the runtime do not support ARC. Probably -fgnu-runtime
199 // should imply a "maximal" runtime or something?
200 case GCC: return true;
201 case GNUstep: return true;
202 case ObjFW: return true;
204 llvm_unreachable("bad kind");
207 /// \brief Does this runtime allow sizeof or alignof on object types?
208 bool allowsSizeofAlignof() const {
212 /// \brief Does this runtime allow pointer arithmetic on objects?
214 /// This covers +, -, ++, --, and (if isSubscriptPointerArithmetic()
216 bool allowsPointerArithmetic() const {
227 llvm_unreachable("bad kind");
230 /// \brief Is subscripting pointer arithmetic?
231 bool isSubscriptPointerArithmetic() const {
232 return allowsPointerArithmetic();
235 /// \brief Does this runtime provide an objc_terminate function?
237 /// This is used in handlers for exceptions during the unwind process;
238 /// without it, abort() must be used in pure ObjC files.
239 bool hasTerminate() const {
241 case FragileMacOSX: return getVersion() >= VersionTuple(10, 8);
242 case MacOSX: return getVersion() >= VersionTuple(10, 8);
243 case iOS: return getVersion() >= VersionTuple(5);
244 case GCC: return false;
245 case GNUstep: return false;
246 case ObjFW: return false;
248 llvm_unreachable("bad kind");
251 /// \brief Does this runtime support weakly importing classes?
252 bool hasWeakClassImport() const {
254 case MacOSX: return true;
255 case iOS: return true;
256 case FragileMacOSX: return false;
257 case GCC: return true;
258 case GNUstep: return true;
259 case ObjFW: return true;
261 llvm_unreachable("bad kind");
264 /// \brief Does this runtime use zero-cost exceptions?
265 bool hasUnwindExceptions() const {
267 case MacOSX: return true;
268 case iOS: return true;
269 case FragileMacOSX: return false;
270 case GCC: return true;
271 case GNUstep: return true;
272 case ObjFW: return true;
274 llvm_unreachable("bad kind");
277 bool hasAtomicCopyHelper() const {
284 return getVersion() >= VersionTuple(1, 7);
285 default: return false;
289 /// \brief Try to parse an Objective-C runtime specification from the given
292 /// \return true on error.
293 bool tryParse(StringRef input);
295 std::string getAsString() const;
297 friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right) {
298 return left.getKind() == right.getKind() &&
299 left.getVersion() == right.getVersion();
302 friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right) {
303 return !(left == right);
307 raw_ostream &operator<<(raw_ostream &out, const ObjCRuntime &value);
309 } // end namespace clang