clang  8.0.0svn
ObjCRuntime.h
Go to the documentation of this file.
1 //===- ObjCRuntime.h - Objective-C Runtime Configuration --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// Defines types useful for describing an Objective-C runtime.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_BASIC_OBJCRUNTIME_H
16 #define LLVM_CLANG_BASIC_OBJCRUNTIME_H
17 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/VersionTuple.h"
23 #include <string>
24 
25 namespace clang {
26 
27 /// The basic abstraction for the target Objective-C runtime.
28 class ObjCRuntime {
29 public:
30  /// The basic Objective-C runtimes that we know about.
31  enum Kind {
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.
36 
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.
41 
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
44  /// version of iOS.
45  iOS,
46 
47  /// 'watchos' is a variant of iOS for Apple's watchOS. The version
48  /// is a release version of watchOS.
50 
51  /// 'gcc' is the Objective-C runtime shipped with GCC, implementing a
52  /// fragile Objective-C ABI
53  GCC,
54 
55  /// 'gnustep' is the modern non-fragile GNUstep runtime.
57 
58  /// 'objfw' is the Objective-C runtime included in ObjFW
60  };
61 
62 private:
63  Kind TheKind = MacOSX;
64  VersionTuple Version;
65 
66 public:
67  /// A bogus initialization of the runtime.
68  ObjCRuntime() = default;
69  ObjCRuntime(Kind kind, const VersionTuple &version)
70  : TheKind(kind), Version(version) {}
71 
72  void set(Kind kind, VersionTuple version) {
73  TheKind = kind;
74  Version = version;
75  }
76 
77  Kind getKind() const { return TheKind; }
78  const VersionTuple &getVersion() const { return Version; }
79 
80  /// Does this runtime follow the set of implied behaviors for a
81  /// "non-fragile" ABI?
82  bool isNonFragile() const {
83  switch (getKind()) {
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;
91  }
92  llvm_unreachable("bad kind");
93  }
94 
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(); }
98 
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)
107  return false;
108  }
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.
115  return true;
116  }
117 
118  /// Is this runtime basically of the GNU family of runtimes?
119  bool isGNUFamily() const {
120  switch (getKind()) {
121  case FragileMacOSX:
122  case MacOSX:
123  case iOS:
124  case WatchOS:
125  return false;
126  case GCC:
127  case GNUstep:
128  case ObjFW:
129  return true;
130  }
131  llvm_unreachable("bad kind");
132  }
133 
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();
139  }
140 
141  /// Does this runtime allow ARC at all?
142  bool allowsARC() const {
143  switch (getKind()) {
144  case FragileMacOSX:
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;
153  }
154  llvm_unreachable("bad kind");
155  }
156 
157  /// Does this runtime natively provide the ARC entrypoints?
158  ///
159  /// ARC cannot be directly supported on a platform that does not provide
160  /// these entrypoints, although it may be supportable via a stub
161  /// library.
162  bool hasNativeARC() const {
163  switch (getKind()) {
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;
168 
169  case GCC: return false;
170  case GNUstep: return getVersion() >= VersionTuple(1, 6);
171  case ObjFW: return true;
172  }
173  llvm_unreachable("bad kind");
174  }
175 
176  /// Does this runtime provide entrypoints that are likely to be faster
177  /// than an ordinary message send of the "alloc" selector?
178  ///
179  /// The "alloc" entrypoint is 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  /// alloc behavior; if that's dynamically a large proportion of all
185  /// objects, using the entrypoint will also be faster than using a message
186  /// send.
187  ///
188  /// When this method returns true, Clang will turn non-super message sends of
189  /// certain selectors into calls to the corresponding entrypoint:
190  /// alloc => objc_alloc
191  /// allocWithZone:nil => objc_allocWithZone
193  switch (getKind()) {
194  case FragileMacOSX:
195  return false;
196  case MacOSX:
197  return getVersion() >= VersionTuple(10, 10);
198  case iOS:
199  return getVersion() >= VersionTuple(8);
200  case WatchOS:
201  return true;
202 
203  case GCC:
204  return false;
205  case GNUstep:
206  return false;
207  case ObjFW:
208  return false;
209  }
210  llvm_unreachable("bad kind");
211  }
212 
213  /// Does this runtime supports optimized setter entrypoints?
214  bool hasOptimizedSetter() const {
215  switch (getKind()) {
216  case MacOSX:
217  return getVersion() >= VersionTuple(10, 8);
218  case iOS:
219  return (getVersion() >= VersionTuple(6));
220  case WatchOS:
221  return true;
222  case GNUstep:
223  return getVersion() >= VersionTuple(1, 7);
224  default:
225  return false;
226  }
227  }
228 
229  /// Does this runtime allow the use of __weak?
230  bool allowsWeak() const {
231  return hasNativeWeak();
232  }
233 
234  /// Does this runtime natively provide ARC-compliant 'weak'
235  /// entrypoints?
236  bool hasNativeWeak() const {
237  // Right now, this is always equivalent to whether the runtime
238  // natively supports ARC decision.
239  return hasNativeARC();
240  }
241 
242  /// Does this runtime directly support the subscripting methods?
243  ///
244  /// This is really a property of the library, not the runtime.
245  bool hasSubscripting() const {
246  switch (getKind()) {
247  case FragileMacOSX: return false;
248  case MacOSX: return getVersion() >= VersionTuple(10, 11);
249  case iOS: return getVersion() >= VersionTuple(9);
250  case WatchOS: return true;
251 
252  // This is really a lie, because some implementations and versions
253  // of the runtime do not support ARC. Probably -fgnu-runtime
254  // should imply a "maximal" runtime or something?
255  case GCC: return true;
256  case GNUstep: return true;
257  case ObjFW: return true;
258  }
259  llvm_unreachable("bad kind");
260  }
261 
262  /// Does this runtime allow sizeof or alignof on object types?
263  bool allowsSizeofAlignof() const {
264  return isFragile();
265  }
266 
267  /// Does this runtime allow pointer arithmetic on objects?
268  ///
269  /// This covers +, -, ++, --, and (if isSubscriptPointerArithmetic()
270  /// yields true) [].
271  bool allowsPointerArithmetic() const {
272  switch (getKind()) {
273  case FragileMacOSX:
274  case GCC:
275  return true;
276  case MacOSX:
277  case iOS:
278  case WatchOS:
279  case GNUstep:
280  case ObjFW:
281  return false;
282  }
283  llvm_unreachable("bad kind");
284  }
285 
286  /// Is subscripting pointer arithmetic?
288  return allowsPointerArithmetic();
289  }
290 
291  /// Does this runtime provide an objc_terminate function?
292  ///
293  /// This is used in handlers for exceptions during the unwind process;
294  /// without it, abort() must be used in pure ObjC files.
295  bool hasTerminate() const {
296  switch (getKind()) {
297  case FragileMacOSX: return getVersion() >= VersionTuple(10, 8);
298  case MacOSX: return getVersion() >= VersionTuple(10, 8);
299  case iOS: return getVersion() >= VersionTuple(5);
300  case WatchOS: return true;
301  case GCC: return false;
302  case GNUstep: return false;
303  case ObjFW: return false;
304  }
305  llvm_unreachable("bad kind");
306  }
307 
308  /// Does this runtime support weakly importing classes?
309  bool hasWeakClassImport() const {
310  switch (getKind()) {
311  case MacOSX: return true;
312  case iOS: return true;
313  case WatchOS: return true;
314  case FragileMacOSX: return false;
315  case GCC: return true;
316  case GNUstep: return true;
317  case ObjFW: return true;
318  }
319  llvm_unreachable("bad kind");
320  }
321 
322  /// Does this runtime use zero-cost exceptions?
323  bool hasUnwindExceptions() const {
324  switch (getKind()) {
325  case MacOSX: return true;
326  case iOS: return true;
327  case WatchOS: return true;
328  case FragileMacOSX: return false;
329  case GCC: return true;
330  case GNUstep: return true;
331  case ObjFW: return true;
332  }
333  llvm_unreachable("bad kind");
334  }
335 
336  bool hasAtomicCopyHelper() const {
337  switch (getKind()) {
338  case FragileMacOSX:
339  case MacOSX:
340  case iOS:
341  case WatchOS:
342  return true;
343  case GNUstep:
344  return getVersion() >= VersionTuple(1, 7);
345  default: return false;
346  }
347  }
348 
349  /// Is objc_unsafeClaimAutoreleasedReturnValue available?
351  switch (getKind()) {
352  case MacOSX:
353  case FragileMacOSX:
354  return getVersion() >= VersionTuple(10, 11);
355  case iOS:
356  return getVersion() >= VersionTuple(9);
357  case WatchOS:
358  return getVersion() >= VersionTuple(2);
359  case GNUstep:
360  return false;
361  default:
362  return false;
363  }
364  }
365 
366  /// Are the empty collection symbols available?
367  bool hasEmptyCollections() const {
368  switch (getKind()) {
369  default:
370  return false;
371  case MacOSX:
372  return getVersion() >= VersionTuple(10, 11);
373  case iOS:
374  return getVersion() >= VersionTuple(9);
375  case WatchOS:
376  return getVersion() >= VersionTuple(2);
377  }
378  }
379 
380  /// Try to parse an Objective-C runtime specification from the given
381  /// string.
382  ///
383  /// \return true on error.
384  bool tryParse(StringRef input);
385 
386  std::string getAsString() const;
387 
388  friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right) {
389  return left.getKind() == right.getKind() &&
390  left.getVersion() == right.getVersion();
391  }
392 
393  friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right) {
394  return !(left == right);
395  }
396 };
397 
398 raw_ostream &operator<<(raw_ostream &out, const ObjCRuntime &value);
399 
400 } // namespace clang
401 
402 #endif // LLVM_CLANG_BASIC_OBJCRUNTIME_H
bool shouldUseRuntimeFunctionsForAlloc() const
Does this runtime provide entrypoints that are likely to be faster than an ordinary message send of t...
Definition: ObjCRuntime.h:192
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:335
bool allowsPointerArithmetic() const
Does this runtime allow pointer arithmetic on objects?
Definition: ObjCRuntime.h:271
&#39;gcc&#39; is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI ...
Definition: ObjCRuntime.h:53
bool hasWeakClassImport() const
Does this runtime support weakly importing classes?
Definition: ObjCRuntime.h:309
&#39;macosx-fragile&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition: ObjCRuntime.h:40
bool hasOptimizedSetter() const
Does this runtime supports optimized setter entrypoints?
Definition: ObjCRuntime.h:214
bool isNonFragile() const
Does this runtime follow the set of implied behaviors for a "non-fragile" ABI?
Definition: ObjCRuntime.h:82
bool isNeXTFamily() const
Is this runtime basically of the NeXT family of runtimes?
Definition: ObjCRuntime.h:135
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
&#39;watchos&#39; is a variant of iOS for Apple&#39;s watchOS.
Definition: ObjCRuntime.h:49
bool isFragile() const
The inverse of isNonFragile(): does this runtime follow the set of implied behaviors for a "fragile" ...
Definition: ObjCRuntime.h:97
bool isSubscriptPointerArithmetic() const
Is subscripting pointer arithmetic?
Definition: ObjCRuntime.h:287
bool isGNUFamily() const
Is this runtime basically of the GNU family of runtimes?
Definition: ObjCRuntime.h:119
bool hasAtomicCopyHelper() const
Definition: ObjCRuntime.h:336
&#39;macosx&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition: ObjCRuntime.h:35
std::string getAsString() const
Definition: ObjCRuntime.cpp:24
bool allowsSizeofAlignof() const
Does this runtime allow sizeof or alignof on object types?
Definition: ObjCRuntime.h:263
bool tryParse(StringRef input)
Try to parse an Objective-C runtime specification from the given string.
Definition: ObjCRuntime.cpp:49
bool hasTerminate() const
Does this runtime provide an objc_terminate function?
Definition: ObjCRuntime.h:295
ObjCRuntime()=default
A bogus initialization of the runtime.
ObjCRuntime(Kind kind, const VersionTuple &version)
Definition: ObjCRuntime.h:69
&#39;gnustep&#39; is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:56
bool hasEmptyCollections() const
Are the empty collection symbols available?
Definition: ObjCRuntime.h:367
bool hasNativeWeak() const
Does this runtime natively provide ARC-compliant &#39;weak&#39; entrypoints?
Definition: ObjCRuntime.h:236
&#39;objfw&#39; is the Objective-C runtime included in ObjFW
Definition: ObjCRuntime.h:59
Kind getKind() const
Definition: ObjCRuntime.h:77
bool hasUnwindExceptions() const
Does this runtime use zero-cost exceptions?
Definition: ObjCRuntime.h:323
bool isLegacyDispatchDefaultForArch(llvm::Triple::ArchType Arch)
The default dispatch mechanism to use for the specified architecture.
Definition: ObjCRuntime.h:100
const VersionTuple & getVersion() const
Definition: ObjCRuntime.h:78
bool allowsARC() const
Does this runtime allow ARC at all?
Definition: ObjCRuntime.h:142
Dataflow Directional Tag Classes.
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:28
friend bool operator!=(const ObjCRuntime &left, const ObjCRuntime &right)
Definition: ObjCRuntime.h:393
&#39;ios&#39; is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition: ObjCRuntime.h:45
friend bool operator==(const ObjCRuntime &left, const ObjCRuntime &right)
Definition: ObjCRuntime.h:388
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
bool hasARCUnsafeClaimAutoreleasedReturnValue() const
Is objc_unsafeClaimAutoreleasedReturnValue available?
Definition: ObjCRuntime.h:350
bool hasNativeARC() const
Does this runtime natively provide the ARC entrypoints?
Definition: ObjCRuntime.h:162
bool hasSubscripting() const
Does this runtime directly support the subscripting methods?
Definition: ObjCRuntime.h:245
Kind
The basic Objective-C runtimes that we know about.
Definition: ObjCRuntime.h:31
bool allowsWeak() const
Does this runtime allow the use of __weak?
Definition: ObjCRuntime.h:230