ArgList.h revision 58a2cbef4aac9ee7d530dfb690c78d6fc11a2371
1//===--- ArgList.h - Argument List Management -------------------*- 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_SUPPORT_ARGLIST_H_ 11#define LLVM_SUPPORT_ARGLIST_H_ 12 13#include "llvm/ADT/SmallVector.h" 14#include "llvm/ADT/StringRef.h" 15#include "llvm/Option/OptSpecifier.h" 16#include "llvm/Option/Option.h" 17#include <list> 18#include <string> 19#include <vector> 20 21namespace llvm { 22namespace opt { 23class Arg; 24class ArgList; 25class Option; 26 27/// arg_iterator - Iterates through arguments stored inside an ArgList. 28class arg_iterator { 29 /// The current argument. 30 SmallVectorImpl<Arg*>::const_iterator Current; 31 32 /// The argument list we are iterating over. 33 const ArgList &Args; 34 35 /// Optional filters on the arguments which will be match. Most clients 36 /// should never want to iterate over arguments without filters, so we won't 37 /// bother to factor this into two separate iterator implementations. 38 // 39 // FIXME: Make efficient; the idea is to provide efficient iteration over 40 // all arguments which match a particular id and then just provide an 41 // iterator combinator which takes multiple iterators which can be 42 // efficiently compared and returns them in order. 43 OptSpecifier Id0, Id1, Id2; 44 45 void SkipToNextArg(); 46 47public: 48 typedef Arg * const * value_type; 49 typedef Arg * const & reference; 50 typedef Arg * const * pointer; 51 typedef std::forward_iterator_tag iterator_category; 52 typedef std::ptrdiff_t difference_type; 53 54 arg_iterator(SmallVectorImpl<Arg*>::const_iterator it, 55 const ArgList &_Args, OptSpecifier _Id0 = 0U, 56 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U) 57 : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) { 58 SkipToNextArg(); 59 } 60 61 operator const Arg*() { return *Current; } 62 reference operator*() const { return *Current; } 63 pointer operator->() const { return Current; } 64 65 arg_iterator &operator++() { 66 ++Current; 67 SkipToNextArg(); 68 return *this; 69 } 70 71 arg_iterator operator++(int) { 72 arg_iterator tmp(*this); 73 ++(*this); 74 return tmp; 75 } 76 77 friend bool operator==(arg_iterator LHS, arg_iterator RHS) { 78 return LHS.Current == RHS.Current; 79 } 80 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) { 81 return !(LHS == RHS); 82 } 83}; 84 85/// ArgList - Ordered collection of driver arguments. 86/// 87/// The ArgList class manages a list of Arg instances as well as 88/// auxiliary data and convenience methods to allow Tools to quickly 89/// check for the presence of Arg instances for a particular Option 90/// and to iterate over groups of arguments. 91class ArgList { 92private: 93 ArgList(const ArgList &) LLVM_DELETED_FUNCTION; 94 void operator=(const ArgList &) LLVM_DELETED_FUNCTION; 95 96public: 97 typedef SmallVector<Arg*, 16> arglist_type; 98 typedef arglist_type::iterator iterator; 99 typedef arglist_type::const_iterator const_iterator; 100 typedef arglist_type::reverse_iterator reverse_iterator; 101 typedef arglist_type::const_reverse_iterator const_reverse_iterator; 102 103private: 104 /// The internal list of arguments. 105 arglist_type Args; 106 107protected: 108 ArgList(); 109 110public: 111 virtual ~ArgList(); 112 113 /// @name Arg Access 114 /// @{ 115 116 /// append - Append \p A to the arg list. 117 void append(Arg *A); 118 119 arglist_type &getArgs() { return Args; } 120 const arglist_type &getArgs() const { return Args; } 121 122 unsigned size() const { return Args.size(); } 123 124 /// @} 125 /// @name Arg Iteration 126 /// @{ 127 128 iterator begin() { return Args.begin(); } 129 iterator end() { return Args.end(); } 130 131 reverse_iterator rbegin() { return Args.rbegin(); } 132 reverse_iterator rend() { return Args.rend(); } 133 134 const_iterator begin() const { return Args.begin(); } 135 const_iterator end() const { return Args.end(); } 136 137 const_reverse_iterator rbegin() const { return Args.rbegin(); } 138 const_reverse_iterator rend() const { return Args.rend(); } 139 140 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U, 141 OptSpecifier Id2 = 0U) const { 142 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2); 143 } 144 arg_iterator filtered_end() const { 145 return arg_iterator(Args.end(), *this); 146 } 147 148 /// @} 149 /// @name Arg Removal 150 /// @{ 151 152 /// eraseArg - Remove any option matching \p Id. 153 void eraseArg(OptSpecifier Id); 154 155 /// @} 156 /// @name Arg Access 157 /// @{ 158 159 /// hasArg - Does the arg list contain any option matching \p Id. 160 /// 161 /// \p Claim Whether the argument should be claimed, if it exists. 162 bool hasArgNoClaim(OptSpecifier Id) const { 163 return getLastArgNoClaim(Id) != 0; 164 } 165 bool hasArg(OptSpecifier Id) const { 166 return getLastArg(Id) != 0; 167 } 168 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const { 169 return getLastArg(Id0, Id1) != 0; 170 } 171 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const { 172 return getLastArg(Id0, Id1, Id2) != 0; 173 } 174 175 /// getLastArg - Return the last argument matching \p Id, or null. 176 /// 177 /// \p Claim Whether the argument should be claimed, if it exists. 178 Arg *getLastArgNoClaim(OptSpecifier Id) const; 179 Arg *getLastArg(OptSpecifier Id) const; 180 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const; 181 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const; 182 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 183 OptSpecifier Id3) const; 184 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 185 OptSpecifier Id3, OptSpecifier Id4) const; 186 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 187 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const; 188 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 189 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5, 190 OptSpecifier Id6) const; 191 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 192 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5, 193 OptSpecifier Id6, OptSpecifier Id7) const; 194 195 /// getArgString - Return the input argument string at \p Index. 196 virtual const char *getArgString(unsigned Index) const = 0; 197 198 /// getNumInputArgStrings - Return the number of original argument strings, 199 /// which are guaranteed to be the first strings in the argument string 200 /// list. 201 virtual unsigned getNumInputArgStrings() const = 0; 202 203 /// @} 204 /// @name Argument Lookup Utilities 205 /// @{ 206 207 /// getLastArgValue - Return the value of the last argument, or a default. 208 StringRef getLastArgValue(OptSpecifier Id, 209 StringRef Default = "") const; 210 211 /// getAllArgValues - Get the values of all instances of the given argument 212 /// as strings. 213 std::vector<std::string> getAllArgValues(OptSpecifier Id) const; 214 215 /// @} 216 /// @name Translation Utilities 217 /// @{ 218 219 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return 220 /// true if the option is present, false if the negation is present, and 221 /// \p Default if neither option is given. If both the option and its 222 /// negation are present, the last one wins. 223 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const; 224 225 /// AddLastArg - Render only the last argument match \p Id0, if present. 226 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const; 227 228 /// AddAllArgs - Render all arguments matching the given ids. 229 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 230 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const; 231 232 /// AddAllArgValues - Render the argument values of all arguments 233 /// matching the given ids. 234 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 235 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const; 236 237 /// AddAllArgsTranslated - Render all the arguments matching the 238 /// given ids, but forced to separate args and using the provided 239 /// name instead of the first option value. 240 /// 241 /// \param Joined - If true, render the argument as joined with 242 /// the option specifier. 243 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 244 const char *Translation, 245 bool Joined = false) const; 246 247 /// ClaimAllArgs - Claim all arguments which match the given 248 /// option id. 249 void ClaimAllArgs(OptSpecifier Id0) const; 250 251 /// ClaimAllArgs - Claim all arguments. 252 /// 253 void ClaimAllArgs() const; 254 255 /// @} 256 /// @name Arg Synthesis 257 /// @{ 258 259 /// MakeArgString - Construct a constant string pointer whose 260 /// lifetime will match that of the ArgList. 261 virtual const char *MakeArgString(StringRef Str) const = 0; 262 const char *MakeArgString(const char *Str) const { 263 return MakeArgString(StringRef(Str)); 264 } 265 const char *MakeArgString(std::string Str) const { 266 return MakeArgString(StringRef(Str)); 267 } 268 const char *MakeArgString(const Twine &Str) const; 269 270 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the 271 /// string at \p Index if possible. 272 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS, 273 StringRef RHS) const; 274 275 /// @} 276}; 277 278class InputArgList : public ArgList { 279private: 280 /// List of argument strings used by the contained Args. 281 /// 282 /// This is mutable since we treat the ArgList as being the list 283 /// of Args, and allow routines to add new strings (to have a 284 /// convenient place to store the memory) via MakeIndex. 285 mutable ArgStringList ArgStrings; 286 287 /// Strings for synthesized arguments. 288 /// 289 /// This is mutable since we treat the ArgList as being the list 290 /// of Args, and allow routines to add new strings (to have a 291 /// convenient place to store the memory) via MakeIndex. 292 mutable std::list<std::string> SynthesizedStrings; 293 294 /// The number of original input argument strings. 295 unsigned NumInputArgStrings; 296 297public: 298 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd); 299 ~InputArgList(); 300 301 virtual const char *getArgString(unsigned Index) const { 302 return ArgStrings[Index]; 303 } 304 305 virtual unsigned getNumInputArgStrings() const { 306 return NumInputArgStrings; 307 } 308 309 /// @name Arg Synthesis 310 /// @{ 311 312public: 313 /// MakeIndex - Get an index for the given string(s). 314 unsigned MakeIndex(StringRef String0) const; 315 unsigned MakeIndex(StringRef String0, StringRef String1) const; 316 317 virtual const char *MakeArgString(StringRef Str) const; 318 319 /// @} 320}; 321 322/// DerivedArgList - An ordered collection of driver arguments, 323/// whose storage may be in another argument list. 324class DerivedArgList : public ArgList { 325 const InputArgList &BaseArgs; 326 327 /// The list of arguments we synthesized. 328 mutable arglist_type SynthesizedArgs; 329 330public: 331 /// Construct a new derived arg list from \p BaseArgs. 332 DerivedArgList(const InputArgList &BaseArgs); 333 ~DerivedArgList(); 334 335 virtual const char *getArgString(unsigned Index) const { 336 return BaseArgs.getArgString(Index); 337 } 338 339 virtual unsigned getNumInputArgStrings() const { 340 return BaseArgs.getNumInputArgStrings(); 341 } 342 343 const InputArgList &getBaseArgs() const { 344 return BaseArgs; 345 } 346 347 /// @name Arg Synthesis 348 /// @{ 349 350 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments 351 /// (to be freed). 352 void AddSynthesizedArg(Arg *A) { 353 SynthesizedArgs.push_back(A); 354 } 355 356 virtual const char *MakeArgString(StringRef Str) const; 357 358 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and 359 /// append it to the argument list. 360 void AddFlagArg(const Arg *BaseArg, const Option Opt) { 361 append(MakeFlagArg(BaseArg, Opt)); 362 } 363 364 /// AddPositionalArg - Construct a new Positional arg for the given option 365 /// \p Id, with the provided \p Value and append it to the argument 366 /// list. 367 void AddPositionalArg(const Arg *BaseArg, const Option Opt, 368 StringRef Value) { 369 append(MakePositionalArg(BaseArg, Opt, Value)); 370 } 371 372 373 /// AddSeparateArg - Construct a new Positional arg for the given option 374 /// \p Id, with the provided \p Value and append it to the argument 375 /// list. 376 void AddSeparateArg(const Arg *BaseArg, const Option Opt, 377 StringRef Value) { 378 append(MakeSeparateArg(BaseArg, Opt, Value)); 379 } 380 381 382 /// AddJoinedArg - Construct a new Positional arg for the given option 383 /// \p Id, with the provided \p Value and append it to the argument list. 384 void AddJoinedArg(const Arg *BaseArg, const Option Opt, 385 StringRef Value) { 386 append(MakeJoinedArg(BaseArg, Opt, Value)); 387 } 388 389 390 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id. 391 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const; 392 393 /// MakePositionalArg - Construct a new Positional arg for the 394 /// given option \p Id, with the provided \p Value. 395 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt, 396 StringRef Value) const; 397 398 /// MakeSeparateArg - Construct a new Positional arg for the 399 /// given option \p Id, with the provided \p Value. 400 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt, 401 StringRef Value) const; 402 403 /// MakeJoinedArg - Construct a new Positional arg for the 404 /// given option \p Id, with the provided \p Value. 405 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt, 406 StringRef Value) const; 407 408 /// @} 409}; 410 411} // end namespace opt 412} // end namespace llvm 413 414#endif 415