LLVM API Documentation

ArgList.h
Go to the documentation of this file.
00001 //===--- ArgList.h - Argument List Management -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_OPTION_ARGLIST_H
00011 #define LLVM_OPTION_ARGLIST_H
00012 
00013 #include "llvm/ADT/SmallVector.h"
00014 #include "llvm/ADT/StringRef.h"
00015 #include "llvm/Option/OptSpecifier.h"
00016 #include "llvm/Option/Option.h"
00017 #include <list>
00018 #include <memory>
00019 #include <string>
00020 #include <vector>
00021 
00022 namespace llvm {
00023 namespace opt {
00024 class Arg;
00025 class ArgList;
00026 class Option;
00027 
00028 /// arg_iterator - Iterates through arguments stored inside an ArgList.
00029 class arg_iterator {
00030   /// The current argument.
00031   SmallVectorImpl<Arg*>::const_iterator Current;
00032 
00033   /// The argument list we are iterating over.
00034   const ArgList &Args;
00035 
00036   /// Optional filters on the arguments which will be match. Most clients
00037   /// should never want to iterate over arguments without filters, so we won't
00038   /// bother to factor this into two separate iterator implementations.
00039   //
00040   // FIXME: Make efficient; the idea is to provide efficient iteration over
00041   // all arguments which match a particular id and then just provide an
00042   // iterator combinator which takes multiple iterators which can be
00043   // efficiently compared and returns them in order.
00044   OptSpecifier Id0, Id1, Id2;
00045 
00046   void SkipToNextArg();
00047 
00048 public:
00049   typedef Arg * const *                 value_type;
00050   typedef Arg * const &                 reference;
00051   typedef Arg * const *                 pointer;
00052   typedef std::forward_iterator_tag   iterator_category;
00053   typedef std::ptrdiff_t              difference_type;
00054 
00055   arg_iterator(SmallVectorImpl<Arg*>::const_iterator it,
00056                 const ArgList &_Args, OptSpecifier _Id0 = 0U,
00057                 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
00058     : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
00059     SkipToNextArg();
00060   }
00061 
00062   operator const Arg*() { return *Current; }
00063   reference operator*() const { return *Current; }
00064   pointer operator->() const { return Current; }
00065 
00066   arg_iterator &operator++() {
00067     ++Current;
00068     SkipToNextArg();
00069     return *this;
00070   }
00071 
00072   arg_iterator operator++(int) {
00073     arg_iterator tmp(*this);
00074     ++(*this);
00075     return tmp;
00076   }
00077 
00078   friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
00079     return LHS.Current == RHS.Current;
00080   }
00081   friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
00082     return !(LHS == RHS);
00083   }
00084 };
00085 
00086 /// ArgList - Ordered collection of driver arguments.
00087 ///
00088 /// The ArgList class manages a list of Arg instances as well as
00089 /// auxiliary data and convenience methods to allow Tools to quickly
00090 /// check for the presence of Arg instances for a particular Option
00091 /// and to iterate over groups of arguments.
00092 class ArgList {
00093 private:
00094   ArgList(const ArgList &) LLVM_DELETED_FUNCTION;
00095   void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
00096 
00097 public:
00098   typedef SmallVector<Arg*, 16> arglist_type;
00099   typedef arglist_type::iterator iterator;
00100   typedef arglist_type::const_iterator const_iterator;
00101   typedef arglist_type::reverse_iterator reverse_iterator;
00102   typedef arglist_type::const_reverse_iterator const_reverse_iterator;
00103 
00104 private:
00105   /// The internal list of arguments.
00106   arglist_type Args;
00107 
00108 protected:
00109   // Default ctor provided explicitly as it is not provided implicitly due to
00110   // the presence of the (deleted) copy ctor above.
00111   ArgList() { }
00112   // Virtual to provide a vtable anchor and because -Wnon-virtua-dtor warns, not
00113   // because this type is ever actually destroyed polymorphically.
00114   virtual ~ArgList();
00115 
00116 public:
00117 
00118   /// @name Arg Access
00119   /// @{
00120 
00121   /// append - Append \p A to the arg list.
00122   void append(Arg *A);
00123 
00124   arglist_type &getArgs() { return Args; }
00125   const arglist_type &getArgs() const { return Args; }
00126 
00127   unsigned size() const { return Args.size(); }
00128 
00129   /// @}
00130   /// @name Arg Iteration
00131   /// @{
00132 
00133   iterator begin() { return Args.begin(); }
00134   iterator end() { return Args.end(); }
00135 
00136   reverse_iterator rbegin() { return Args.rbegin(); }
00137   reverse_iterator rend() { return Args.rend(); }
00138 
00139   const_iterator begin() const { return Args.begin(); }
00140   const_iterator end() const { return Args.end(); }
00141 
00142   const_reverse_iterator rbegin() const { return Args.rbegin(); }
00143   const_reverse_iterator rend() const { return Args.rend(); }
00144 
00145   arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
00146                               OptSpecifier Id2 = 0U) const {
00147     return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
00148   }
00149   arg_iterator filtered_end() const {
00150     return arg_iterator(Args.end(), *this);
00151   }
00152 
00153   iterator_range<arg_iterator> filtered(OptSpecifier Id0 = 0U,
00154                                         OptSpecifier Id1 = 0U,
00155                                         OptSpecifier Id2 = 0U) const {
00156     return make_range(filtered_begin(Id0, Id1, Id2), filtered_end());
00157   }
00158 
00159   /// @}
00160   /// @name Arg Removal
00161   /// @{
00162 
00163   /// eraseArg - Remove any option matching \p Id.
00164   void eraseArg(OptSpecifier Id);
00165 
00166   /// @}
00167   /// @name Arg Access
00168   /// @{
00169 
00170   /// hasArg - Does the arg list contain any option matching \p Id.
00171   ///
00172   /// \p Claim Whether the argument should be claimed, if it exists.
00173   bool hasArgNoClaim(OptSpecifier Id) const {
00174     return getLastArgNoClaim(Id) != nullptr;
00175   }
00176   bool hasArg(OptSpecifier Id) const {
00177     return getLastArg(Id) != nullptr;
00178   }
00179   bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
00180     return getLastArg(Id0, Id1) != nullptr;
00181   }
00182   bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
00183     return getLastArg(Id0, Id1, Id2) != nullptr;
00184   }
00185 
00186   /// getLastArg - Return the last argument matching \p Id, or null.
00187   ///
00188   /// \p Claim Whether the argument should be claimed, if it exists.
00189   Arg *getLastArgNoClaim(OptSpecifier Id) const;
00190   Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const;
00191   Arg *getLastArg(OptSpecifier Id) const;
00192   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
00193   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
00194   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
00195                   OptSpecifier Id3) const;
00196   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
00197                   OptSpecifier Id3, OptSpecifier Id4) const;
00198   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
00199                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
00200   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
00201                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
00202                   OptSpecifier Id6) const;
00203   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
00204                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
00205                   OptSpecifier Id6, OptSpecifier Id7) const;
00206 
00207   /// getArgString - Return the input argument string at \p Index.
00208   virtual const char *getArgString(unsigned Index) const = 0;
00209 
00210   /// getNumInputArgStrings - Return the number of original argument strings,
00211   /// which are guaranteed to be the first strings in the argument string
00212   /// list.
00213   virtual unsigned getNumInputArgStrings() const = 0;
00214 
00215   /// @}
00216   /// @name Argument Lookup Utilities
00217   /// @{
00218 
00219   /// getLastArgValue - Return the value of the last argument, or a default.
00220   StringRef getLastArgValue(OptSpecifier Id,
00221                                   StringRef Default = "") const;
00222 
00223   /// getAllArgValues - Get the values of all instances of the given argument
00224   /// as strings.
00225   std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
00226 
00227   /// @}
00228   /// @name Translation Utilities
00229   /// @{
00230 
00231   /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
00232   /// true if the option is present, false if the negation is present, and
00233   /// \p Default if neither option is given. If both the option and its
00234   /// negation are present, the last one wins.
00235   bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
00236 
00237   /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
00238   /// form \p Neg, return true if the option or its alias is present, false if
00239   /// the negation is present, and \p Default if none of the options are
00240   /// given. If multiple options are present, the last one wins.
00241   bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
00242                bool Default = true) const;
00243 
00244   /// AddLastArg - Render only the last argument match \p Id0, if present.
00245   void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
00246   void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
00247                   OptSpecifier Id1) const;
00248 
00249   /// AddAllArgs - Render all arguments matching the given ids.
00250   void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
00251                   OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
00252 
00253   /// AddAllArgValues - Render the argument values of all arguments
00254   /// matching the given ids.
00255   void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
00256                         OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
00257 
00258   /// AddAllArgsTranslated - Render all the arguments matching the
00259   /// given ids, but forced to separate args and using the provided
00260   /// name instead of the first option value.
00261   ///
00262   /// \param Joined - If true, render the argument as joined with
00263   /// the option specifier.
00264   void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
00265                             const char *Translation,
00266                             bool Joined = false) const;
00267 
00268   /// ClaimAllArgs - Claim all arguments which match the given
00269   /// option id.
00270   void ClaimAllArgs(OptSpecifier Id0) const;
00271 
00272   /// ClaimAllArgs - Claim all arguments.
00273   ///
00274   void ClaimAllArgs() const;
00275 
00276   /// @}
00277   /// @name Arg Synthesis
00278   /// @{
00279 
00280   /// MakeArgString - Construct a constant string pointer whose
00281   /// lifetime will match that of the ArgList.
00282   virtual const char *MakeArgString(StringRef Str) const = 0;
00283   const char *MakeArgString(const char *Str) const {
00284     return MakeArgString(StringRef(Str));
00285   }
00286   const char *MakeArgString(std::string Str) const {
00287     return MakeArgString(StringRef(Str));
00288   }
00289   const char *MakeArgString(const Twine &Str) const;
00290 
00291   /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
00292   /// string at \p Index if possible.
00293   const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
00294                                         StringRef RHS) const;
00295 
00296   /// @}
00297 };
00298 
00299 class InputArgList : public ArgList  {
00300 private:
00301   /// List of argument strings used by the contained Args.
00302   ///
00303   /// This is mutable since we treat the ArgList as being the list
00304   /// of Args, and allow routines to add new strings (to have a
00305   /// convenient place to store the memory) via MakeIndex.
00306   mutable ArgStringList ArgStrings;
00307 
00308   /// Strings for synthesized arguments.
00309   ///
00310   /// This is mutable since we treat the ArgList as being the list
00311   /// of Args, and allow routines to add new strings (to have a
00312   /// convenient place to store the memory) via MakeIndex.
00313   mutable std::list<std::string> SynthesizedStrings;
00314 
00315   /// The number of original input argument strings.
00316   unsigned NumInputArgStrings;
00317 
00318 public:
00319   InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
00320   ~InputArgList();
00321 
00322   const char *getArgString(unsigned Index) const override {
00323     return ArgStrings[Index];
00324   }
00325 
00326   unsigned getNumInputArgStrings() const override {
00327     return NumInputArgStrings;
00328   }
00329 
00330   /// @name Arg Synthesis
00331   /// @{
00332 
00333 public:
00334   /// MakeIndex - Get an index for the given string(s).
00335   unsigned MakeIndex(StringRef String0) const;
00336   unsigned MakeIndex(StringRef String0, StringRef String1) const;
00337 
00338   using ArgList::MakeArgString;
00339   const char *MakeArgString(StringRef Str) const override;
00340 
00341   /// @}
00342 };
00343 
00344 /// DerivedArgList - An ordered collection of driver arguments,
00345 /// whose storage may be in another argument list.
00346 class DerivedArgList : public ArgList {
00347   const InputArgList &BaseArgs;
00348 
00349   /// The list of arguments we synthesized.
00350   mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
00351 
00352 public:
00353   /// Construct a new derived arg list from \p BaseArgs.
00354   DerivedArgList(const InputArgList &BaseArgs);
00355   ~DerivedArgList();
00356 
00357   const char *getArgString(unsigned Index) const override {
00358     return BaseArgs.getArgString(Index);
00359   }
00360 
00361   unsigned getNumInputArgStrings() const override {
00362     return BaseArgs.getNumInputArgStrings();
00363   }
00364 
00365   const InputArgList &getBaseArgs() const {
00366     return BaseArgs;
00367   }
00368 
00369   /// @name Arg Synthesis
00370   /// @{
00371 
00372   /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
00373   /// (to be freed).
00374   void AddSynthesizedArg(Arg *A);
00375 
00376   using ArgList::MakeArgString;
00377   const char *MakeArgString(StringRef Str) const override;
00378 
00379   /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
00380   /// append it to the argument list.
00381   void AddFlagArg(const Arg *BaseArg, const Option Opt) {
00382     append(MakeFlagArg(BaseArg, Opt));
00383   }
00384 
00385   /// AddPositionalArg - Construct a new Positional arg for the given option
00386   /// \p Id, with the provided \p Value and append it to the argument
00387   /// list.
00388   void AddPositionalArg(const Arg *BaseArg, const Option Opt,
00389                         StringRef Value) {
00390     append(MakePositionalArg(BaseArg, Opt, Value));
00391   }
00392 
00393 
00394   /// AddSeparateArg - Construct a new Positional arg for the given option
00395   /// \p Id, with the provided \p Value and append it to the argument
00396   /// list.
00397   void AddSeparateArg(const Arg *BaseArg, const Option Opt,
00398                       StringRef Value) {
00399     append(MakeSeparateArg(BaseArg, Opt, Value));
00400   }
00401 
00402 
00403   /// AddJoinedArg - Construct a new Positional arg for the given option
00404   /// \p Id, with the provided \p Value and append it to the argument list.
00405   void AddJoinedArg(const Arg *BaseArg, const Option Opt,
00406                     StringRef Value) {
00407     append(MakeJoinedArg(BaseArg, Opt, Value));
00408   }
00409 
00410 
00411   /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
00412   Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
00413 
00414   /// MakePositionalArg - Construct a new Positional arg for the
00415   /// given option \p Id, with the provided \p Value.
00416   Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
00417                           StringRef Value) const;
00418 
00419   /// MakeSeparateArg - Construct a new Positional arg for the
00420   /// given option \p Id, with the provided \p Value.
00421   Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
00422                         StringRef Value) const;
00423 
00424   /// MakeJoinedArg - Construct a new Positional arg for the
00425   /// given option \p Id, with the provided \p Value.
00426   Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
00427                       StringRef Value) const;
00428 
00429   /// @}
00430 };
00431 
00432 } // end namespace opt
00433 } // end namespace llvm
00434 
00435 #endif