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