Update Triple to use StringRef/Twine based APIs.
[oota-llvm.git] / lib / Support / Triple.cpp
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
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 #include "llvm/ADT/Triple.h"
11
12 #include "llvm/ADT/Twine.h"
13 #include <cassert>
14 #include <cstring>
15 using namespace llvm;
16
17 //
18
19 const char *Triple::getArchTypeName(ArchType Kind) {
20   switch (Kind) {
21   case InvalidArch: return "<invalid>";
22   case UnknownArch: return "unknown";
23
24   case x86: return "i386";
25   case x86_64: return "x86_64";
26   case ppc: return "powerpc";
27   case ppc64: return "powerpc64";
28   }
29
30   return "<invalid>";
31 }
32
33 const char *Triple::getVendorTypeName(VendorType Kind) {
34   switch (Kind) {
35   case UnknownVendor: return "unknown";
36
37   case Apple: return "apple";
38   case PC: return "PC";
39   }
40
41   return "<invalid>";
42 }
43
44 const char *Triple::getOSTypeName(OSType Kind) {
45   switch (Kind) {
46   case UnknownOS: return "unknown";
47
48   case AuroraUX: return "auroraux";
49   case Darwin: return "darwin";
50   case DragonFly: return "dragonfly";
51   case FreeBSD: return "freebsd";
52   case Linux: return "linux";
53   case NetBSD: return "netbsd";
54   case OpenBSD: return "openbsd";
55   }
56
57   return "<invalid>";
58 }
59
60 //
61
62 void Triple::Parse() const {
63   assert(!isInitialized() && "Invalid parse call.");
64
65   StringRef ArchName = getArchName();
66   if (ArchName.size() == 4 && ArchName[0] == 'i' && 
67       ArchName[2] == '8' && ArchName[3] == '6')
68     Arch = x86;
69   else if (ArchName == "amd64" || ArchName == "x86_64")
70     Arch = x86_64;
71   else if (ArchName == "powerpc")
72     Arch = ppc;
73   else if (ArchName == "powerpc64")
74     Arch = ppc64;
75   else
76     Arch = UnknownArch;
77
78   StringRef VendorName = getVendorName();
79   if (VendorName == "apple")
80     Vendor = Apple;
81   else if (VendorName == "pc")
82     Vendor = PC;
83   else
84     Vendor = UnknownVendor;
85
86   StringRef OSName = getOSName();
87   if (OSName.startswith("auroraux"))
88     OS = AuroraUX;
89   else if (OSName.startswith("darwin"))
90     OS = Darwin;
91   else if (OSName.startswith("dragonfly"))
92     OS = DragonFly;
93   else if (OSName.startswith("freebsd"))
94     OS = FreeBSD;
95   else if (OSName.startswith("linux"))
96     OS = Linux;
97   else if (OSName.startswith("netbsd"))
98     OS = NetBSD;
99   else if (OSName.startswith("openbsd"))
100     OS = OpenBSD;
101   else
102     OS = UnknownOS;
103
104   assert(isInitialized() && "Failed to initialize!");
105 }
106
107 StringRef Triple::getArchName() const {
108   return StringRef(Data).split('-').first;           // Isolate first component
109 }
110
111 StringRef Triple::getVendorName() const {
112   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
113   return Tmp.split('-').first;                       // Isolate second component
114 }
115
116 StringRef Triple::getOSName() const {
117   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
118   Tmp = Tmp.split('-').second;                       // Strip second component
119   return Tmp.split('-').first;                       // Isolate third component
120 }
121
122 StringRef Triple::getEnvironmentName() const {
123   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
124   Tmp = Tmp.split('-').second;                       // Strip second component
125   return Tmp.split('-').second;                      // Strip third component
126 }
127
128 StringRef Triple::getOSAndEnvironmentName() const {
129   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
130   return Tmp.split('-').second;                      // Strip second component
131 }
132
133 void Triple::setTriple(const Twine &Str) {
134   Data = Str.str();
135   Arch = InvalidArch;
136 }
137
138 void Triple::setArch(ArchType Kind) {
139   setArchName(getArchTypeName(Kind));
140 }
141
142 void Triple::setVendor(VendorType Kind) {
143   setVendorName(getVendorTypeName(Kind));
144 }
145
146 void Triple::setOS(OSType Kind) {
147   setOSName(getOSTypeName(Kind));
148 }
149
150 void Triple::setArchName(const StringRef &Str) {
151   setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
152 }
153
154 void Triple::setVendorName(const StringRef &Str) {
155   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
156 }
157
158 void Triple::setOSName(const StringRef &Str) {
159   if (hasEnvironment())
160     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
161               "-" + getEnvironmentName());
162   else
163     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
164 }
165
166 void Triple::setEnvironmentName(const StringRef &Str) {
167   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 
168             "-" + Str);
169 }
170
171 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
172   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
173 }