Eugene Zelenko | 5a52011 | 2018-03-28 22:09:09 +0000 | [diff] [blame] | 1 | //===- ObjCRuntime.cpp - Objective-C Runtime Handling ---------------------===// |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 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 | // This file implements the ObjCRuntime class, which represents the |
| 11 | // target Objective-C runtime. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 5a52011 | 2018-03-28 22:09:09 +0000 | [diff] [blame] | 14 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 15 | #include "clang/Basic/ObjCRuntime.h" |
Eugene Zelenko | 5a52011 | 2018-03-28 22:09:09 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Pavel Labath | d8c6290 | 2018-06-11 10:28:04 +0000 | [diff] [blame] | 17 | #include "llvm/Support/VersionTuple.h" |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5a52011 | 2018-03-28 22:09:09 +0000 | [diff] [blame] | 19 | #include <cstddef> |
| 20 | #include <string> |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | std::string ObjCRuntime::getAsString() const { |
| 25 | std::string Result; |
| 26 | { |
| 27 | llvm::raw_string_ostream Out(Result); |
| 28 | Out << *this; |
| 29 | } |
| 30 | return Result; |
| 31 | } |
| 32 | |
| 33 | raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) { |
| 34 | switch (value.getKind()) { |
| 35 | case ObjCRuntime::MacOSX: out << "macosx"; break; |
| 36 | case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break; |
| 37 | case ObjCRuntime::iOS: out << "ios"; break; |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 38 | case ObjCRuntime::WatchOS: out << "watchos"; break; |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 39 | case ObjCRuntime::GNUstep: out << "gnustep"; break; |
| 40 | case ObjCRuntime::GCC: out << "gcc"; break; |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 41 | case ObjCRuntime::ObjFW: out << "objfw"; break; |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 42 | } |
| 43 | if (value.getVersion() > VersionTuple(0)) { |
| 44 | out << '-' << value.getVersion(); |
| 45 | } |
| 46 | return out; |
| 47 | } |
| 48 | |
| 49 | bool ObjCRuntime::tryParse(StringRef input) { |
| 50 | // Look for the last dash. |
| 51 | std::size_t dash = input.rfind('-'); |
| 52 | |
John McCall | 18ac163 | 2012-06-20 21:58:02 +0000 | [diff] [blame] | 53 | // We permit dashes in the runtime name, and we also permit the |
| 54 | // version to be omitted, so if we see a dash not followed by a |
| 55 | // digit then we need to ignore it. |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 56 | if (dash != StringRef::npos && dash + 1 != input.size() && |
| 57 | (input[dash+1] < '0' || input[dash+1] > '9')) { |
| 58 | dash = StringRef::npos; |
| 59 | } |
| 60 | |
| 61 | // Everything prior to that must be a valid string name. |
| 62 | Kind kind; |
| 63 | StringRef runtimeName = input.substr(0, dash); |
David Chisnall | 314896c | 2012-07-04 10:37:03 +0000 | [diff] [blame] | 64 | Version = VersionTuple(0); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 65 | if (runtimeName == "macosx") { |
| 66 | kind = ObjCRuntime::MacOSX; |
| 67 | } else if (runtimeName == "macosx-fragile") { |
| 68 | kind = ObjCRuntime::FragileMacOSX; |
| 69 | } else if (runtimeName == "ios") { |
| 70 | kind = ObjCRuntime::iOS; |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 71 | } else if (runtimeName == "watchos") { |
| 72 | kind = ObjCRuntime::WatchOS; |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 73 | } else if (runtimeName == "gnustep") { |
David Chisnall | 314896c | 2012-07-04 10:37:03 +0000 | [diff] [blame] | 74 | // If no version is specified then default to the most recent one that we |
| 75 | // know about. |
| 76 | Version = VersionTuple(1, 6); |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 77 | kind = ObjCRuntime::GNUstep; |
| 78 | } else if (runtimeName == "gcc") { |
| 79 | kind = ObjCRuntime::GCC; |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 80 | } else if (runtimeName == "objfw") { |
| 81 | kind = ObjCRuntime::ObjFW; |
Benjamin Kramer | 4d6efbb | 2013-09-16 16:31:49 +0000 | [diff] [blame] | 82 | Version = VersionTuple(0, 8); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 83 | } else { |
| 84 | return true; |
| 85 | } |
| 86 | TheKind = kind; |
Benjamin Kramer | 4d6efbb | 2013-09-16 16:31:49 +0000 | [diff] [blame] | 87 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 88 | if (dash != StringRef::npos) { |
| 89 | StringRef verString = input.substr(dash + 1); |
Benjamin Kramer | 4d6efbb | 2013-09-16 16:31:49 +0000 | [diff] [blame] | 90 | if (Version.tryParse(verString)) |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
Benjamin Kramer | 4d6efbb | 2013-09-16 16:31:49 +0000 | [diff] [blame] | 94 | if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8)) |
| 95 | Version = VersionTuple(0, 8); |
| 96 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |