Triple.h revision 193323
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    Darwin,
53    DragonFly,
54    FreeBSD,
55    Linux
56  };
57
58private:
59  std::string Data;
60
61  /// The parsed arch type (or InvalidArch if uninitialized).
62  mutable ArchType Arch;
63
64  /// The parsed vendor type.
65  mutable VendorType Vendor;
66
67  /// The parsed OS type.
68  mutable OSType OS;
69
70  bool isInitialized() const { return Arch != InvalidArch; }
71  void Parse() const;
72
73public:
74  /// @name Constructors
75  /// @{
76
77  Triple() : Data(""), Arch(InvalidArch) {}
78  explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
79  explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
80    : Data(ArchStr), Arch(InvalidArch) {
81    Data += '-';
82    Data += VendorStr;
83    Data += '-';
84    Data += OSStr;
85  }
86
87  /// @}
88  /// @name Typed Component Access
89  /// @{
90
91  /// getArch - Get the parsed architecture type of this triple.
92  ArchType getArch() const {
93    if (!isInitialized()) Parse();
94    return Arch;
95  }
96
97  /// getVendor - Get the parsed vendor type of this triple.
98  VendorType getVendor() const {
99    if (!isInitialized()) Parse();
100    return Vendor;
101  }
102
103  /// getOS - Get the parsed operating system type of this triple.
104  OSType getOS() const {
105    if (!isInitialized()) Parse();
106    return OS;
107  }
108
109  /// hasEnvironment - Does this triple have the optional environment
110  /// (fourth) component?
111  bool hasEnvironment() const {
112    return getEnvironmentName() != "";
113  }
114
115  /// @}
116  /// @name Direct Component Access
117  /// @{
118
119  const std::string &getTriple() const { return Data; }
120
121  // FIXME: Invent a lightweight string representation for these to
122  // use.
123
124  /// getArchName - Get the architecture (first) component of the
125  /// triple.
126  std::string getArchName() const;
127
128  /// getVendorName - Get the vendor (second) component of the triple.
129  std::string getVendorName() const;
130
131  /// getOSName - Get the operating system (third) component of the
132  /// triple.
133  std::string getOSName() const;
134
135  /// getEnvironmentName - Get the optional environment (fourth)
136  /// component of the triple, or "" if empty.
137  std::string getEnvironmentName() const;
138
139  /// getOSAndEnvironmentName - Get the operating system and optional
140  /// environment components as a single string (separated by a '-'
141  /// if the environment component is present).
142  std::string getOSAndEnvironmentName() const;
143
144  /// @}
145  /// @name Mutators
146  /// @{
147
148  /// setArch - Set the architecture (first) component of the triple
149  /// to a known type.
150  void setArch(ArchType Kind);
151
152  /// setVendor - Set the vendor (second) component of the triple to a
153  /// known type.
154  void setVendor(VendorType Kind);
155
156  /// setOS - Set the operating system (third) component of the triple
157  /// to a known type.
158  void setOS(OSType Kind);
159
160  /// setTriple - Set all components to the new triple \arg Str.
161  void setTriple(const std::string &Str);
162
163  /// setArchName - Set the architecture (first) component of the
164  /// triple by name.
165  void setArchName(const std::string &Str);
166
167  /// setVendorName - Set the vendor (second) component of the triple
168  /// by name.
169  void setVendorName(const std::string &Str);
170
171  /// setOSName - Set the operating system (third) component of the
172  /// triple by name.
173  void setOSName(const std::string &Str);
174
175  /// setEnvironmentName - Set the optional environment (fourth)
176  /// component of the triple by name.
177  void setEnvironmentName(const std::string &Str);
178
179  /// setOSAndEnvironmentName - Set the operating system and optional
180  /// environment components with a single string.
181  void setOSAndEnvironmentName(const std::string &Str);
182
183  /// @}
184  /// @name Static helpers for IDs.
185  /// @{
186
187  /// getArchTypeName - Get the canonical name for the \arg Kind
188  /// architecture.
189  static const char *getArchTypeName(ArchType Kind);
190
191  /// getVendorTypeName - Get the canonical name for the \arg Kind
192  /// vendor.
193  static const char *getVendorTypeName(VendorType Kind);
194
195  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
196  static const char *getOSTypeName(OSType Kind);
197
198  /// @}
199};
200
201} // End llvm namespace
202
203
204#endif
205