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