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