blob: 133c66945dde163514338bfc678300183ec09695 [file] [log] [blame]
John McCall5fb5df92012-06-20 06:18:46 +00001//===- ObjCRuntime.cpp - Objective-C Runtime Handling -----------*- 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// This file implements the ObjCRuntime class, which represents the
11// target Objective-C runtime.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/Basic/ObjCRuntime.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace clang;
18
19std::string ObjCRuntime::getAsString() const {
20 std::string Result;
21 {
22 llvm::raw_string_ostream Out(Result);
23 Out << *this;
24 }
25 return Result;
26}
27
28raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) {
29 switch (value.getKind()) {
30 case ObjCRuntime::MacOSX: out << "macosx"; break;
31 case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break;
32 case ObjCRuntime::iOS: out << "ios"; break;
Tim Northover756447a2015-10-30 16:30:36 +000033 case ObjCRuntime::WatchOS: out << "watchos"; break;
David Chisnallb601c962012-07-03 20:49:52 +000034 case ObjCRuntime::GNUstep: out << "gnustep"; break;
35 case ObjCRuntime::GCC: out << "gcc"; break;
John McCall775086e2012-07-12 02:07:58 +000036 case ObjCRuntime::ObjFW: out << "objfw"; break;
John McCall5fb5df92012-06-20 06:18:46 +000037 }
38 if (value.getVersion() > VersionTuple(0)) {
39 out << '-' << value.getVersion();
40 }
41 return out;
42}
43
44bool ObjCRuntime::tryParse(StringRef input) {
45 // Look for the last dash.
46 std::size_t dash = input.rfind('-');
47
John McCall18ac1632012-06-20 21:58:02 +000048 // We permit dashes in the runtime name, and we also permit the
49 // version to be omitted, so if we see a dash not followed by a
50 // digit then we need to ignore it.
John McCall5fb5df92012-06-20 06:18:46 +000051 if (dash != StringRef::npos && dash + 1 != input.size() &&
52 (input[dash+1] < '0' || input[dash+1] > '9')) {
53 dash = StringRef::npos;
54 }
55
56 // Everything prior to that must be a valid string name.
57 Kind kind;
58 StringRef runtimeName = input.substr(0, dash);
David Chisnall314896c2012-07-04 10:37:03 +000059 Version = VersionTuple(0);
John McCall5fb5df92012-06-20 06:18:46 +000060 if (runtimeName == "macosx") {
61 kind = ObjCRuntime::MacOSX;
62 } else if (runtimeName == "macosx-fragile") {
63 kind = ObjCRuntime::FragileMacOSX;
64 } else if (runtimeName == "ios") {
65 kind = ObjCRuntime::iOS;
Tim Northover756447a2015-10-30 16:30:36 +000066 } else if (runtimeName == "watchos") {
67 kind = ObjCRuntime::WatchOS;
David Chisnallb601c962012-07-03 20:49:52 +000068 } else if (runtimeName == "gnustep") {
David Chisnall314896c2012-07-04 10:37:03 +000069 // If no version is specified then default to the most recent one that we
70 // know about.
71 Version = VersionTuple(1, 6);
David Chisnallb601c962012-07-03 20:49:52 +000072 kind = ObjCRuntime::GNUstep;
73 } else if (runtimeName == "gcc") {
74 kind = ObjCRuntime::GCC;
John McCall775086e2012-07-12 02:07:58 +000075 } else if (runtimeName == "objfw") {
76 kind = ObjCRuntime::ObjFW;
Benjamin Kramer4d6efbb2013-09-16 16:31:49 +000077 Version = VersionTuple(0, 8);
John McCall5fb5df92012-06-20 06:18:46 +000078 } else {
79 return true;
80 }
81 TheKind = kind;
Benjamin Kramer4d6efbb2013-09-16 16:31:49 +000082
John McCall5fb5df92012-06-20 06:18:46 +000083 if (dash != StringRef::npos) {
84 StringRef verString = input.substr(dash + 1);
Benjamin Kramer4d6efbb2013-09-16 16:31:49 +000085 if (Version.tryParse(verString))
John McCall5fb5df92012-06-20 06:18:46 +000086 return true;
87 }
88
Benjamin Kramer4d6efbb2013-09-16 16:31:49 +000089 if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8))
90 Version = VersionTuple(0, 8);
91
John McCall5fb5df92012-06-20 06:18:46 +000092 return false;
93}