Triple.h revision b8ac841c9a275cc8d4e1a92dd06cc99323e35fa2
1//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- 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#ifndef LLVM_ADT_TRIPLE_H
11#define LLVM_ADT_TRIPLE_H
12
13#include <string>
14
15namespace llvm {
16
17/// Triple - Helper class for working with target triples.
18///
19/// Target triples are strings in the format of:
20///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
21/// or
22///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
23///
24/// This class is used for clients which want to support arbitrary
25/// target triples, but also want to implement certain special
26/// behavior for particular targets. This class isolates the mapping
27/// from the components of the target triple to well known IDs.
28///
29/// See autoconf/config.guess for a glimpse into what they look like
30/// in practice.
31class Triple {
32public:
33  enum ArchType {
34    UnknownArch,
35
36    x86,    // i?86
37    ppc,    // powerpc
38    ppc64,  // powerpc64
39    x86_64, // amd64, x86_64
40
41    InvalidArch
42  };
43  enum VendorType {
44    UnknownVendor,
45
46    Apple,
47    PC
48  };
49  enum OSType {
50    UnknownOS,
51
52    AuroraUX,
53    Darwin,
54    DragonFly,
55    FreeBSD,
56    Linux,
57    NetBSD,
58    OpenBSD
59  };
60
61private:
62  std::string Data;
63
64  /// The parsed arch type (or InvalidArch if uninitialized).
65  mutable ArchType Arch;
66
67  /// The parsed vendor type.
68  mutable VendorType Vendor;
69
70  /// The parsed OS type.
71  mutable OSType OS;
72
73  bool isInitialized() const { return Arch != InvalidArch; }
74  void Parse() const;
75
76public:
77  /// @name Constructors
78  /// @{
79
80  Triple() : Data(""), Arch(InvalidArch) {}
81  explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
82  explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
83    : Data(ArchStr), Arch(InvalidArch) {
84    Data += '-';
85    Data += VendorStr;
86    Data += '-';
87    Data += OSStr;
88  }
89
90  /// @}
91  /// @name Typed Component Access
92  /// @{
93
94  /// getArch - Get the parsed architecture type of this triple.
95  ArchType getArch() const {
96    if (!isInitialized()) Parse();
97    return Arch;
98  }
99
100  /// getVendor - Get the parsed vendor type of this triple.
101  VendorType getVendor() const {
102    if (!isInitialized()) Parse();
103    return Vendor;
104  }
105
106  /// getOS - Get the parsed operating system type of this triple.
107  OSType getOS() const {
108    if (!isInitialized()) Parse();
109    return OS;
110  }
111
112  /// hasEnvironment - Does this triple have the optional environment
113  /// (fourth) component?
114  bool hasEnvironment() const {
115    return getEnvironmentName() != "";
116  }
117
118  /// @}
119  /// @name Direct Component Access
120  /// @{
121
122  const std::string &getTriple() const { return Data; }
123
124  // FIXME: Invent a lightweight string representation for these to
125  // use.
126
127  /// getArchName - Get the architecture (first) component of the
128  /// triple.
129  std::string getArchName() const;
130
131  /// getVendorName - Get the vendor (second) component of the triple.
132  std::string getVendorName() const;
133
134  /// getOSName - Get the operating system (third) component of the
135  /// triple.
136  std::string getOSName() const;
137
138  /// getEnvironmentName - Get the optional environment (fourth)
139  /// component of the triple, or "" if empty.
140  std::string getEnvironmentName() const;
141
142  /// getOSAndEnvironmentName - Get the operating system and optional
143  /// environment components as a single string (separated by a '-'
144  /// if the environment component is present).
145  std::string getOSAndEnvironmentName() const;
146
147  /// @}
148  /// @name Mutators
149  /// @{
150
151  /// setArch - Set the architecture (first) component of the triple
152  /// to a known type.
153  void setArch(ArchType Kind);
154
155  /// setVendor - Set the vendor (second) component of the triple to a
156  /// known type.
157  void setVendor(VendorType Kind);
158
159  /// setOS - Set the operating system (third) component of the triple
160  /// to a known type.
161  void setOS(OSType Kind);
162
163  /// setTriple - Set all components to the new triple \arg Str.
164  void setTriple(const std::string &Str);
165
166  /// setArchName - Set the architecture (first) component of the
167  /// triple by name.
168  void setArchName(const std::string &Str);
169
170  /// setVendorName - Set the vendor (second) component of the triple
171  /// by name.
172  void setVendorName(const std::string &Str);
173
174  /// setOSName - Set the operating system (third) component of the
175  /// triple by name.
176  void setOSName(const std::string &Str);
177
178  /// setEnvironmentName - Set the optional environment (fourth)
179  /// component of the triple by name.
180  void setEnvironmentName(const std::string &Str);
181
182  /// setOSAndEnvironmentName - Set the operating system and optional
183  /// environment components with a single string.
184  void setOSAndEnvironmentName(const std::string &Str);
185
186  /// @}
187  /// @name Static helpers for IDs.
188  /// @{
189
190  /// getArchTypeName - Get the canonical name for the \arg Kind
191  /// architecture.
192  static const char *getArchTypeName(ArchType Kind);
193
194  /// getVendorTypeName - Get the canonical name for the \arg Kind
195  /// vendor.
196  static const char *getVendorTypeName(VendorType Kind);
197
198  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
199  static const char *getOSTypeName(OSType Kind);
200
201  /// @}
202};
203
204} // End llvm namespace
205
206
207#endif
208