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