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