blob: 0f30bfaa2c4a353c10fb1851abaf3b8e6fb96fe1 [file] [log] [blame]
John McCall260611a2012-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;
David Chisnall11d3f4c2012-07-03 20:49:52 +000033 case ObjCRuntime::GNUstep: out << "gnustep"; break;
34 case ObjCRuntime::GCC: out << "gcc"; break;
John McCall260611a2012-06-20 06:18:46 +000035 }
36 if (value.getVersion() > VersionTuple(0)) {
37 out << '-' << value.getVersion();
38 }
39 return out;
40}
41
42bool ObjCRuntime::tryParse(StringRef input) {
43 // Look for the last dash.
44 std::size_t dash = input.rfind('-');
45
John McCall0b92fcb2012-06-20 21:58:02 +000046 // We permit dashes in the runtime name, and we also permit the
47 // version to be omitted, so if we see a dash not followed by a
48 // digit then we need to ignore it.
John McCall260611a2012-06-20 06:18:46 +000049 if (dash != StringRef::npos && dash + 1 != input.size() &&
50 (input[dash+1] < '0' || input[dash+1] > '9')) {
51 dash = StringRef::npos;
52 }
53
54 // Everything prior to that must be a valid string name.
55 Kind kind;
56 StringRef runtimeName = input.substr(0, dash);
57 if (runtimeName == "macosx") {
58 kind = ObjCRuntime::MacOSX;
59 } else if (runtimeName == "macosx-fragile") {
60 kind = ObjCRuntime::FragileMacOSX;
61 } else if (runtimeName == "ios") {
62 kind = ObjCRuntime::iOS;
David Chisnall11d3f4c2012-07-03 20:49:52 +000063 } else if (runtimeName == "gnustep") {
64 kind = ObjCRuntime::GNUstep;
65 } else if (runtimeName == "gcc") {
66 kind = ObjCRuntime::GCC;
John McCall260611a2012-06-20 06:18:46 +000067 } else {
68 return true;
69 }
70 TheKind = kind;
71
72 Version = VersionTuple(0);
73 if (dash != StringRef::npos) {
74 StringRef verString = input.substr(dash + 1);
75 if (Version.tryParse(verString))
76 return true;
77 }
78
79 return false;
80}