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