LLVM API Documentation

CommandLine.h

Go to the documentation of this file.
00001 //===- llvm/Support/CommandLine.h - Command line handler --------*- 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 // This class implements a command line argument processor that is useful when
00011 // creating a tool.  It provides a simple, minimalistic interface that is easily
00012 // extensible and supports nonlocal (library) command line options.
00013 //
00014 // Note that rather than trying to figure out what this code does, you should
00015 // read the library documentation located in docs/CommandLine.html or looks at
00016 // the many example usages in tools/*/*.cpp
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_SUPPORT_COMMANDLINE_H
00021 #define LLVM_SUPPORT_COMMANDLINE_H
00022 
00023 #include "llvm/Support/type_traits.h"
00024 #include "llvm/Support/DataTypes.h"
00025 #include "llvm/Support/Compiler.h"
00026 #include "llvm/ADT/SmallVector.h"
00027 #include <cassert>
00028 #include <cstdarg>
00029 #include <string>
00030 #include <utility>
00031 #include <vector>
00032 
00033 namespace llvm {
00034 
00035 /// cl Namespace - This namespace contains all of the command line option
00036 /// processing machinery.  It is intentionally a short name to make qualified
00037 /// usage concise.
00038 namespace cl {
00039 
00040 //===----------------------------------------------------------------------===//
00041 // ParseCommandLineOptions - Command line option processing entry point.
00042 //
00043 void ParseCommandLineOptions(int argc, char **argv,
00044                              const char *Overview = 0,
00045                              bool ReadResponseFiles = false);
00046 
00047 //===----------------------------------------------------------------------===//
00048 // ParseEnvironmentOptions - Environment variable option processing alternate
00049 //                           entry point.
00050 //
00051 void ParseEnvironmentOptions(const char *progName, const char *envvar,
00052                              const char *Overview = 0,
00053                              bool ReadResponseFiles = false);
00054 
00055 ///===---------------------------------------------------------------------===//
00056 /// SetVersionPrinter - Override the default (LLVM specific) version printer
00057 ///                     used to print out the version when --version is given
00058 ///                     on the command line. This allows other systems using the
00059 ///                     CommandLine utilities to print their own version string.
00060 void SetVersionPrinter(void (*func)());
00061 
00062 
00063 // MarkOptionsChanged - Internal helper function.
00064 void MarkOptionsChanged();
00065 
00066 //===----------------------------------------------------------------------===//
00067 // Flags permitted to be passed to command line arguments
00068 //
00069 
00070 enum NumOccurrences {          // Flags for the number of occurrences allowed
00071   Optional        = 0x01,      // Zero or One occurrence
00072   ZeroOrMore      = 0x02,      // Zero or more occurrences allowed
00073   Required        = 0x03,      // One occurrence required
00074   OneOrMore       = 0x04,      // One or more occurrences required
00075 
00076   // ConsumeAfter - Indicates that this option is fed anything that follows the
00077   // last positional argument required by the application (it is an error if
00078   // there are zero positional arguments, and a ConsumeAfter option is used).
00079   // Thus, for example, all arguments to LLI are processed until a filename is
00080   // found.  Once a filename is found, all of the succeeding arguments are
00081   // passed, unprocessed, to the ConsumeAfter option.
00082   //
00083   ConsumeAfter    = 0x05,
00084 
00085   OccurrencesMask  = 0x07
00086 };
00087 
00088 enum ValueExpected {           // Is a value required for the option?
00089   ValueOptional   = 0x08,      // The value can appear... or not
00090   ValueRequired   = 0x10,      // The value is required to appear!
00091   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
00092   ValueMask       = 0x18
00093 };
00094 
00095 enum OptionHidden {            // Control whether -help shows this option
00096   NotHidden       = 0x20,      // Option included in --help & --help-hidden
00097   Hidden          = 0x40,      // -help doesn't, but --help-hidden does
00098   ReallyHidden    = 0x60,      // Neither --help nor --help-hidden show this arg
00099   HiddenMask      = 0x60
00100 };
00101 
00102 // Formatting flags - This controls special features that the option might have
00103 // that cause it to be parsed differently...
00104 //
00105 // Prefix - This option allows arguments that are otherwise unrecognized to be
00106 // matched by options that are a prefix of the actual value.  This is useful for
00107 // cases like a linker, where options are typically of the form '-lfoo' or
00108 // '-L../../include' where -l or -L are the actual flags.  When prefix is
00109 // enabled, and used, the value for the flag comes from the suffix of the
00110 // argument.
00111 //
00112 // Grouping - With this option enabled, multiple letter options are allowed to
00113 // bunch together with only a single hyphen for the whole group.  This allows
00114 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
00115 //
00116 
00117 enum FormattingFlags {
00118   NormalFormatting = 0x000,     // Nothing special
00119   Positional       = 0x080,     // Is a positional argument, no '-' required
00120   Prefix           = 0x100,     // Can this option directly prefix its value?
00121   Grouping         = 0x180,     // Can this option group with other options?
00122   FormattingMask   = 0x180      // Union of the above flags.
00123 };
00124 
00125 enum MiscFlags {               // Miscellaneous flags to adjust argument
00126   CommaSeparated     = 0x200,  // Should this cl::list split between commas?
00127   PositionalEatsArgs = 0x400,  // Should this positional cl::list eat -args?
00128   Sink               = 0x800,  // Should this cl::list eat all unknown options?
00129   AllowInverse       = 0x1000, // Can this option take a -Xno- form?
00130   MiscMask           = 0x1E00  // Union of the above flags.
00131 };
00132 
00133 
00134 
00135 //===----------------------------------------------------------------------===//
00136 // Option Base class
00137 //
00138 class alias;
00139 class Option {
00140   friend class alias;
00141 
00142   // handleOccurrences - Overriden by subclasses to handle the value passed into
00143   // an argument.  Should return true if there was an error processing the
00144   // argument and the program should exit.
00145   //
00146   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
00147                                 const std::string &Arg) = 0;
00148 
00149   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00150     return ValueOptional;
00151   }
00152 
00153   // Out of line virtual function to provide home for the class.
00154   virtual void anchor();
00155 
00156   int NumOccurrences;     // The number of times specified
00157   int Flags;              // Flags for the argument
00158   unsigned Position;      // Position of last occurrence of the option
00159   unsigned AdditionalVals;// Greater than 0 for multi-valued option.
00160   Option *NextRegistered; // Singly linked list of registered options.
00161 public:
00162   const char *ArgStr;     // The argument string itself (ex: "help", "o")
00163   const char *HelpStr;    // The descriptive text message for --help
00164   const char *ValueStr;   // String describing what the value of this option is
00165 
00166   inline enum NumOccurrences getNumOccurrencesFlag() const {
00167     return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
00168   }
00169   inline enum ValueExpected getValueExpectedFlag() const {
00170     int VE = Flags & ValueMask;
00171     return VE ? static_cast<enum ValueExpected>(VE)
00172               : getValueExpectedFlagDefault();
00173   }
00174   inline enum OptionHidden getOptionHiddenFlag() const {
00175     return static_cast<enum OptionHidden>(Flags & HiddenMask);
00176   }
00177   inline enum FormattingFlags getFormattingFlag() const {
00178     return static_cast<enum FormattingFlags>(Flags & FormattingMask);
00179   }
00180   inline unsigned getMiscFlags() const {
00181     return Flags & MiscMask;
00182   }
00183   inline unsigned getPosition() const { return Position; }
00184   inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
00185 
00186   // hasArgStr - Return true if the argstr != ""
00187   bool hasArgStr() const { return ArgStr[0] != 0; }
00188 
00189   //-------------------------------------------------------------------------===
00190   // Accessor functions set by OptionModifiers
00191   //
00192   void setArgStr(const char *S) { ArgStr = S; }
00193   void setDescription(const char *S) { HelpStr = S; }
00194   void setValueStr(const char *S) { ValueStr = S; }
00195 
00196   void setFlag(unsigned Flag, unsigned FlagMask) {
00197     Flags &= ~FlagMask;
00198     Flags |= Flag;
00199   }
00200 
00201   void setNumOccurrencesFlag(enum NumOccurrences Val) {
00202     setFlag(Val, OccurrencesMask);
00203   }
00204   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
00205   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
00206   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
00207   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
00208   void setPosition(unsigned pos) { Position = pos; }
00209 protected:
00210   explicit Option(unsigned DefaultFlags)
00211     : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
00212       AdditionalVals(0), NextRegistered(0),
00213       ArgStr(""), HelpStr(""), ValueStr("") {
00214     assert(getNumOccurrencesFlag() != 0 &&
00215            getOptionHiddenFlag() != 0 && "Not all default flags specified!");
00216   }
00217 
00218   inline void setNumAdditionalVals(unsigned n)
00219   { AdditionalVals = n; }
00220 public:
00221   // addArgument - Register this argument with the commandline system.
00222   //
00223   void addArgument();
00224 
00225   Option *getNextRegisteredOption() const { return NextRegistered; }
00226 
00227   // Return the width of the option tag for printing...
00228   virtual size_t getOptionWidth() const = 0;
00229 
00230   // printOptionInfo - Print out information about this option.  The
00231   // to-be-maintained width is specified.
00232   //
00233   virtual void printOptionInfo(size_t GlobalWidth) const = 0;
00234 
00235   virtual void getExtraOptionNames(std::vector<const char*> &) {}
00236 
00237   // addOccurrence - Wrapper around handleOccurrence that enforces Flags
00238   //
00239   bool addOccurrence(unsigned pos, const char *ArgName,
00240                      const std::string &Value, bool MultiArg = false);
00241 
00242   // Prints option name followed by message.  Always returns true.
00243   bool error(std::string Message, const char *ArgName = 0);
00244 
00245 public:
00246   inline int getNumOccurrences() const { return NumOccurrences; }
00247   virtual ~Option() {}
00248 };
00249 
00250 
00251 //===----------------------------------------------------------------------===//
00252 // Command line option modifiers that can be used to modify the behavior of
00253 // command line option parsers...
00254 //
00255 
00256 // desc - Modifier to set the description shown in the --help output...
00257 struct desc {
00258   const char *Desc;
00259   desc(const char *Str) : Desc(Str) {}
00260   void apply(Option &O) const { O.setDescription(Desc); }
00261 };
00262 
00263 // value_desc - Modifier to set the value description shown in the --help
00264 // output...
00265 struct value_desc {
00266   const char *Desc;
00267   value_desc(const char *Str) : Desc(Str) {}
00268   void apply(Option &O) const { O.setValueStr(Desc); }
00269 };
00270 
00271 // init - Specify a default (initial) value for the command line argument, if
00272 // the default constructor for the argument type does not give you what you
00273 // want.  This is only valid on "opt" arguments, not on "list" arguments.
00274 //
00275 template<class Ty>
00276 struct initializer {
00277   const Ty &Init;
00278   initializer(const Ty &Val) : Init(Val) {}
00279 
00280   template<class Opt>
00281   void apply(Opt &O) const { O.setInitialValue(Init); }
00282 };
00283 
00284 template<class Ty>
00285 initializer<Ty> init(const Ty &Val) {
00286   return initializer<Ty>(Val);
00287 }
00288 
00289 
00290 // location - Allow the user to specify which external variable they want to
00291 // store the results of the command line argument processing into, if they don't
00292 // want to store it in the option itself.
00293 //
00294 template<class Ty>
00295 struct LocationClass {
00296   Ty &Loc;
00297   LocationClass(Ty &L) : Loc(L) {}
00298 
00299   template<class Opt>
00300   void apply(Opt &O) const { O.setLocation(O, Loc); }
00301 };
00302 
00303 template<class Ty>
00304 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
00305 
00306 
00307 //===----------------------------------------------------------------------===//
00308 // Enum valued command line option
00309 //
00310 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
00311 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
00312 #define clEnumValEnd (reinterpret_cast<void*>(0))
00313 
00314 // values - For custom data types, allow specifying a group of values together
00315 // as the values that go into the mapping that the option handler uses.  Note
00316 // that the values list must always have a 0 at the end of the list to indicate
00317 // that the list has ended.
00318 //
00319 template<class DataType>
00320 class ValuesClass {
00321   // Use a vector instead of a map, because the lists should be short,
00322   // the overhead is less, and most importantly, it keeps them in the order
00323   // inserted so we can print our option out nicely.
00324   SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
00325   void processValues(va_list Vals);
00326 public:
00327   ValuesClass(const char *EnumName, DataType Val, const char *Desc,
00328               va_list ValueArgs) {
00329     // Insert the first value, which is required.
00330     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
00331 
00332     // Process the varargs portion of the values...
00333     while (const char *enumName = va_arg(ValueArgs, const char *)) {
00334       DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
00335       const char *EnumDesc = va_arg(ValueArgs, const char *);
00336       Values.push_back(std::make_pair(enumName,      // Add value to value map
00337                                       std::make_pair(EnumVal, EnumDesc)));
00338     }
00339   }
00340 
00341   template<class Opt>
00342   void apply(Opt &O) const {
00343     for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
00344          i != e; ++i)
00345       O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
00346                                      Values[i].second.second);
00347   }
00348 };
00349 
00350 template<class DataType>
00351 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
00352                                            const char *Desc, ...) {
00353     va_list ValueArgs;
00354     va_start(ValueArgs, Desc);
00355     ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
00356     va_end(ValueArgs);
00357     return Vals;
00358 }
00359 
00360 
00361 //===----------------------------------------------------------------------===//
00362 // parser class - Parameterizable parser for different data types.  By default,
00363 // known data types (string, int, bool) have specialized parsers, that do what
00364 // you would expect.  The default parser, used for data types that are not
00365 // built-in, uses a mapping table to map specific options to values, which is
00366 // used, among other things, to handle enum types.
00367 
00368 //--------------------------------------------------
00369 // generic_parser_base - This class holds all the non-generic code that we do
00370 // not need replicated for every instance of the generic parser.  This also
00371 // allows us to put stuff into CommandLine.cpp
00372 //
00373 struct generic_parser_base {
00374   virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
00375 
00376   // getNumOptions - Virtual function implemented by generic subclass to
00377   // indicate how many entries are in Values.
00378   //
00379   virtual unsigned getNumOptions() const = 0;
00380 
00381   // getOption - Return option name N.
00382   virtual const char *getOption(unsigned N) const = 0;
00383 
00384   // getDescription - Return description N
00385   virtual const char *getDescription(unsigned N) const = 0;
00386 
00387   // Return the width of the option tag for printing...
00388   virtual size_t getOptionWidth(const Option &O) const;
00389 
00390   // printOptionInfo - Print out information about this option.  The
00391   // to-be-maintained width is specified.
00392   //
00393   virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
00394 
00395   void initialize(Option &O) {
00396     // All of the modifiers for the option have been processed by now, so the
00397     // argstr field should be stable, copy it down now.
00398     //
00399     hasArgStr = O.hasArgStr();
00400   }
00401 
00402   void getExtraOptionNames(std::vector<const char*> &OptionNames) {
00403     // If there has been no argstr specified, that means that we need to add an
00404     // argument for every possible option.  This ensures that our options are
00405     // vectored to us.
00406     if (!hasArgStr)
00407       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
00408         OptionNames.push_back(getOption(i));
00409   }
00410 
00411 
00412   enum ValueExpected getValueExpectedFlagDefault() const {
00413     // If there is an ArgStr specified, then we are of the form:
00414     //
00415     //    -opt=O2   or   -opt O2  or  -optO2
00416     //
00417     // In which case, the value is required.  Otherwise if an arg str has not
00418     // been specified, we are of the form:
00419     //
00420     //    -O2 or O2 or -la (where -l and -a are separate options)
00421     //
00422     // If this is the case, we cannot allow a value.
00423     //
00424     if (hasArgStr)
00425       return ValueRequired;
00426     else
00427       return ValueDisallowed;
00428   }
00429 
00430   // findOption - Return the option number corresponding to the specified
00431   // argument string.  If the option is not found, getNumOptions() is returned.
00432   //
00433   unsigned findOption(const char *Name);
00434 
00435 protected:
00436   bool hasArgStr;
00437 };
00438 
00439 // Default parser implementation - This implementation depends on having a
00440 // mapping of recognized options to values of some sort.  In addition to this,
00441 // each entry in the mapping also tracks a help message that is printed with the
00442 // command line option for --help.  Because this is a simple mapping parser, the
00443 // data type can be any unsupported type.
00444 //
00445 template <class DataType>
00446 class parser : public generic_parser_base {
00447 protected:
00448   SmallVector<std::pair<const char *,
00449                         std::pair<DataType, const char *> >, 8> Values;
00450 public:
00451   typedef DataType parser_data_type;
00452 
00453   // Implement virtual functions needed by generic_parser_base
00454   unsigned getNumOptions() const { return unsigned(Values.size()); }
00455   const char *getOption(unsigned N) const { return Values[N].first; }
00456   const char *getDescription(unsigned N) const {
00457     return Values[N].second.second;
00458   }
00459 
00460   // parse - Return true on error.
00461   bool parse(Option &O, const char *ArgName, const std::string &Arg,
00462              DataType &V) {
00463     std::string ArgVal;
00464     if (hasArgStr)
00465       ArgVal = Arg;
00466     else
00467       ArgVal = ArgName;
00468 
00469     for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
00470          i != e; ++i)
00471       if (ArgVal == Values[i].first) {
00472         V = Values[i].second.first;
00473         return false;
00474       }
00475 
00476     return O.error(": Cannot find option named '" + ArgVal + "'!");
00477   }
00478 
00479   /// addLiteralOption - Add an entry to the mapping table.
00480   ///
00481   template <class DT>
00482   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
00483     assert(findOption(Name) == Values.size() && "Option already exists!");
00484     Values.push_back(std::make_pair(Name,
00485                              std::make_pair(static_cast<DataType>(V),HelpStr)));
00486     MarkOptionsChanged();
00487   }
00488 
00489   /// removeLiteralOption - Remove the specified option.
00490   ///
00491   void removeLiteralOption(const char *Name) {
00492     unsigned N = findOption(Name);
00493     assert(N != Values.size() && "Option not found!");
00494     Values.erase(Values.begin()+N);
00495   }
00496 };
00497 
00498 //--------------------------------------------------
00499 // basic_parser - Super class of parsers to provide boilerplate code
00500 //
00501 struct basic_parser_impl {  // non-template implementation of basic_parser<t>
00502   virtual ~basic_parser_impl() {}
00503 
00504   enum ValueExpected getValueExpectedFlagDefault() const {
00505     return ValueRequired;
00506   }
00507 
00508   void getExtraOptionNames(std::vector<const char*> &) {}
00509 
00510   void initialize(Option &) {}
00511 
00512   // Return the width of the option tag for printing...
00513   size_t getOptionWidth(const Option &O) const;
00514 
00515   // printOptionInfo - Print out information about this option.  The
00516   // to-be-maintained width is specified.
00517   //
00518   void printOptionInfo(const Option &O, size_t GlobalWidth) const;
00519 
00520   // getValueName - Overload in subclass to provide a better default value.
00521   virtual const char *getValueName() const { return "value"; }
00522 
00523   // An out-of-line virtual method to provide a 'home' for this class.
00524   virtual void anchor();
00525 };
00526 
00527 // basic_parser - The real basic parser is just a template wrapper that provides
00528 // a typedef for the provided data type.
00529 //
00530 template<class DataType>
00531 struct basic_parser : public basic_parser_impl {
00532   typedef DataType parser_data_type;
00533 };
00534 
00535 //--------------------------------------------------
00536 // parser<bool>
00537 //
00538 template<>
00539 class parser<bool> : public basic_parser<bool> {
00540   bool IsInvertable;  // Should we synthezise a -xno- style option?
00541   const char *ArgStr;
00542 public:
00543   void getExtraOptionNames(std::vector<const char*> &OptionNames) {
00544     if (IsInvertable) {
00545       char *s = new char [strlen(ArgStr) + 3 + 1];
00546       s[0] = ArgStr[0];
00547       s[1] = 'n';
00548       s[2] = 'o';
00549       s[3] = '-';
00550       strcpy(&s[4], ArgStr+1);
00551       OptionNames.push_back(s);
00552     }
00553   }
00554 
00555   // parse - Return true on error.
00556   bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
00557 
00558   template <class Opt>
00559   void initialize(Opt &O) {
00560     if (O.getMiscFlags() & llvm::cl::AllowInverse)
00561       IsInvertable = true;
00562     else
00563       IsInvertable = false;
00564     ArgStr = O.ArgStr;
00565   }
00566 
00567   enum ValueExpected getValueExpectedFlagDefault() const {
00568     return ValueOptional;
00569   }
00570 
00571   // getValueName - Do not print =<value> at all.
00572   virtual const char *getValueName() const { return 0; }
00573 
00574   // An out-of-line virtual method to provide a 'home' for this class.
00575   virtual void anchor();
00576 };
00577 
00578 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
00579 
00580 //--------------------------------------------------
00581 // parser<boolOrDefault>
00582 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
00583 template<>
00584 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
00585 public:
00586   // parse - Return true on error.
00587   bool parse(Option &O, const char *ArgName, const std::string &Arg,
00588              boolOrDefault &Val);
00589 
00590   enum ValueExpected getValueExpectedFlagDefault() const {
00591     return ValueOptional;
00592   }
00593 
00594   // getValueName - Do not print =<value> at all.
00595   virtual const char *getValueName() const { return 0; }
00596 
00597   // An out-of-line virtual method to provide a 'home' for this class.
00598   virtual void anchor();
00599 };
00600 
00601 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
00602 
00603 //--------------------------------------------------
00604 // parser<int>
00605 //
00606 template<>
00607 class parser<int> : public basic_parser<int> {
00608 public:
00609   // parse - Return true on error.
00610   bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
00611 
00612   // getValueName - Overload in subclass to provide a better default value.
00613   virtual const char *getValueName() const { return "int"; }
00614 
00615   // An out-of-line virtual method to provide a 'home' for this class.
00616   virtual void anchor();
00617 };
00618 
00619 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
00620 
00621 
00622 //--------------------------------------------------
00623 // parser<unsigned>
00624 //
00625 template<>
00626 class parser<unsigned> : public basic_parser<unsigned> {
00627 public:
00628   // parse - Return true on error.
00629   bool parse(Option &O, const char *AN, const std::string &Arg, unsigned &Val);
00630 
00631   // getValueName - Overload in subclass to provide a better default value.
00632   virtual const char *getValueName() const { return "uint"; }
00633 
00634   // An out-of-line virtual method to provide a 'home' for this class.
00635   virtual void anchor();
00636 };
00637 
00638 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
00639 
00640 //--------------------------------------------------
00641 // parser<double>
00642 //
00643 template<>
00644 class parser<double> : public basic_parser<double> {
00645 public:
00646   // parse - Return true on error.
00647   bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
00648 
00649   // getValueName - Overload in subclass to provide a better default value.
00650   virtual const char *getValueName() const { return "number"; }
00651 
00652   // An out-of-line virtual method to provide a 'home' for this class.
00653   virtual void anchor();
00654 };
00655 
00656 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
00657 
00658 //--------------------------------------------------
00659 // parser<float>
00660 //
00661 template<>
00662 class parser<float> : public basic_parser<float> {
00663 public:
00664   // parse - Return true on error.
00665   bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
00666 
00667   // getValueName - Overload in subclass to provide a better default value.
00668   virtual const char *getValueName() const { return "number"; }
00669 
00670   // An out-of-line virtual method to provide a 'home' for this class.
00671   virtual void anchor();
00672 };
00673 
00674 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
00675 
00676 //--------------------------------------------------
00677 // parser<std::string>
00678 //
00679 template<>
00680 class parser<std::string> : public basic_parser<std::string> {
00681 public:
00682   // parse - Return true on error.
00683   bool parse(Option &, const char *, const std::string &Arg,
00684              std::string &Value) {
00685     Value = Arg;
00686     return false;
00687   }
00688 
00689   // getValueName - Overload in subclass to provide a better default value.
00690   virtual const char *getValueName() const { return "string"; }
00691 
00692   // An out-of-line virtual method to provide a 'home' for this class.
00693   virtual void anchor();
00694 };
00695 
00696 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
00697 
00698 //===----------------------------------------------------------------------===//
00699 // applicator class - This class is used because we must use partial
00700 // specialization to handle literal string arguments specially (const char* does
00701 // not correctly respond to the apply method).  Because the syntax to use this
00702 // is a pain, we have the 'apply' method below to handle the nastiness...
00703 //
00704 template<class Mod> struct applicator {
00705   template<class Opt>
00706   static void opt(const Mod &M, Opt &O) { M.apply(O); }
00707 };
00708 
00709 // Handle const char* as a special case...
00710 template<unsigned n> struct applicator<char[n]> {
00711   template<class Opt>
00712   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00713 };
00714 template<unsigned n> struct applicator<const char[n]> {
00715   template<class Opt>
00716   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00717 };
00718 template<> struct applicator<const char*> {
00719   template<class Opt>
00720   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
00721 };
00722 
00723 template<> struct applicator<NumOccurrences> {
00724   static void opt(NumOccurrences NO, Option &O) { O.setNumOccurrencesFlag(NO); }
00725 };
00726 template<> struct applicator<ValueExpected> {
00727   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
00728 };
00729 template<> struct applicator<OptionHidden> {
00730   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
00731 };
00732 template<> struct applicator<FormattingFlags> {
00733   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
00734 };
00735 template<> struct applicator<MiscFlags> {
00736   static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
00737 };
00738 
00739 // apply method - Apply a modifier to an option in a type safe way.
00740 template<class Mod, class Opt>
00741 void apply(const Mod &M, Opt *O) {
00742   applicator<Mod>::opt(M, *O);
00743 }
00744 
00745 
00746 //===----------------------------------------------------------------------===//
00747 // opt_storage class
00748 
00749 // Default storage class definition: external storage.  This implementation
00750 // assumes the user will specify a variable to store the data into with the
00751 // cl::location(x) modifier.
00752 //
00753 template<class DataType, bool ExternalStorage, bool isClass>
00754 class opt_storage {
00755   DataType *Location;   // Where to store the object...
00756 
00757   void check() {
00758     assert(Location != 0 && "cl::location(...) not specified for a command "
00759            "line option with external storage, "
00760            "or cl::init specified before cl::location()!!");
00761   }
00762 public:
00763   opt_storage() : Location(0) {}
00764 
00765   bool setLocation(Option &O, DataType &L) {
00766     if (Location)
00767       return O.error(": cl::location(x) specified more than once!");
00768     Location = &L;
00769     return false;
00770   }
00771 
00772   template<class T>
00773   void setValue(const T &V) {
00774     check();
00775     *Location = V;
00776   }
00777 
00778   DataType &getValue() { check(); return *Location; }
00779   const DataType &getValue() const { check(); return *Location; }
00780 };
00781 
00782 
00783 // Define how to hold a class type object, such as a string.  Since we can
00784 // inherit from a class, we do so.  This makes us exactly compatible with the
00785 // object in all cases that it is used.
00786 //
00787 template<class DataType>
00788 class opt_storage<DataType,false,true> : public DataType {
00789 public:
00790   template<class T>
00791   void setValue(const T &V) { DataType::operator=(V); }
00792 
00793   DataType &getValue() { return *this; }
00794   const DataType &getValue() const { return *this; }
00795 };
00796 
00797 // Define a partial specialization to handle things we cannot inherit from.  In
00798 // this case, we store an instance through containment, and overload operators
00799 // to get at the value.
00800 //
00801 template<class DataType>
00802 class opt_storage<DataType, false, false> {
00803 public:
00804   DataType Value;
00805 
00806   // Make sure we initialize the value with the default constructor for the
00807   // type.
00808   opt_storage() : Value(DataType()) {}
00809 
00810   template<class T>
00811   void setValue(const T &V) { Value = V; }
00812   DataType &getValue() { return Value; }
00813   DataType getValue() const { return Value; }
00814 
00815   // If the datatype is a pointer, support -> on it.
00816   DataType operator->() const { return Value; }
00817 };
00818 
00819 
00820 //===----------------------------------------------------------------------===//
00821 // opt - A scalar command line option.
00822 //
00823 template <class DataType, bool ExternalStorage = false,
00824           class ParserClass = parser<DataType> >
00825 class opt : public Option,
00826             public opt_storage<DataType, ExternalStorage,
00827                                is_class<DataType>::value> {
00828   ParserClass Parser;
00829 
00830   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
00831                                 const std::string &Arg) {
00832     typename ParserClass::parser_data_type Val =
00833        typename ParserClass::parser_data_type();
00834     if (Parser.parse(*this, ArgName, Arg, Val))
00835       return true;                            // Parse error!
00836     setValue(Val);
00837     setPosition(pos);
00838     return false;
00839   }
00840 
00841   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00842     return Parser.getValueExpectedFlagDefault();
00843   }
00844   virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
00845     return Parser.getExtraOptionNames(OptionNames);
00846   }
00847 
00848   // Forward printing stuff to the parser...
00849   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
00850   virtual void printOptionInfo(size_t GlobalWidth) const {
00851     Parser.printOptionInfo(*this, GlobalWidth);
00852   }
00853 
00854   void done() {
00855     addArgument();
00856     Parser.initialize(*this);
00857   }
00858 public:
00859   // setInitialValue - Used by the cl::init modifier...
00860   void setInitialValue(const DataType &V) { this->setValue(V); }
00861 
00862   ParserClass &getParser() { return Parser; }
00863 
00864   operator DataType() const { return this->getValue(); }
00865 
00866   template<class T>
00867   DataType &operator=(const T &Val) {
00868     this->setValue(Val);
00869     return this->getValue();
00870   }
00871 
00872   // One option...
00873   template<class M0t>
00874   explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
00875     apply(M0, this);
00876     done();
00877   }
00878 
00879   // Two options...
00880   template<class M0t, class M1t>
00881   opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
00882     apply(M0, this); apply(M1, this);
00883     done();
00884   }
00885 
00886   // Three options...
00887   template<class M0t, class M1t, class M2t>
00888   opt(const M0t &M0, const M1t &M1,
00889       const M2t &M2) : Option(Optional | NotHidden) {
00890     apply(M0, this); apply(M1, this); apply(M2, this);
00891     done();
00892   }
00893   // Four options...
00894   template<class M0t, class M1t, class M2t, class M3t>
00895   opt(const M0t &M0, const M1t &M1, const M2t &M2,
00896       const M3t &M3) : Option(Optional | NotHidden) {
00897     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00898     done();
00899   }
00900   // Five options...
00901   template<class M0t, class M1t, class M2t, class M3t, class M4t>
00902   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00903       const M4t &M4) : Option(Optional | NotHidden) {
00904     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00905     apply(M4, this);
00906     done();
00907   }
00908   // Six options...
00909   template<class M0t, class M1t, class M2t, class M3t,
00910            class M4t, class M5t>
00911   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00912       const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
00913     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00914     apply(M4, this); apply(M5, this);
00915     done();
00916   }
00917   // Seven options...
00918   template<class M0t, class M1t, class M2t, class M3t,
00919            class M4t, class M5t, class M6t>
00920   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00921       const M4t &M4, const M5t &M5,
00922       const M6t &M6) : Option(Optional | NotHidden) {
00923     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00924     apply(M4, this); apply(M5, this); apply(M6, this);
00925     done();
00926   }
00927   // Eight options...
00928   template<class M0t, class M1t, class M2t, class M3t,
00929            class M4t, class M5t, class M6t, class M7t>
00930   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
00931       const M4t &M4, const M5t &M5, const M6t &M6,
00932       const M7t &M7) : Option(Optional | NotHidden) {
00933     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
00934     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
00935     done();
00936   }
00937 };
00938 
00939 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
00940 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
00941 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
00942 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
00943 
00944 //===----------------------------------------------------------------------===//
00945 // list_storage class
00946 
00947 // Default storage class definition: external storage.  This implementation
00948 // assumes the user will specify a variable to store the data into with the
00949 // cl::location(x) modifier.
00950 //
00951 template<class DataType, class StorageClass>
00952 class list_storage {
00953   StorageClass *Location;   // Where to store the object...
00954 
00955 public:
00956   list_storage() : Location(0) {}
00957 
00958   bool setLocation(Option &O, StorageClass &L) {
00959     if (Location)
00960       return O.error(": cl::location(x) specified more than once!");
00961     Location = &L;
00962     return false;
00963   }
00964 
00965   template<class T>
00966   void addValue(const T &V) {
00967     assert(Location != 0 && "cl::location(...) not specified for a command "
00968            "line option with external storage!");
00969     Location->push_back(V);
00970   }
00971 };
00972 
00973 
00974 // Define how to hold a class type object, such as a string.  Since we can
00975 // inherit from a class, we do so.  This makes us exactly compatible with the
00976 // object in all cases that it is used.
00977 //
00978 template<class DataType>
00979 class list_storage<DataType, bool> : public std::vector<DataType> {
00980 public:
00981   template<class T>
00982   void addValue(const T &V) { push_back(V); }
00983 };
00984 
00985 
00986 //===----------------------------------------------------------------------===//
00987 // list - A list of command line options.
00988 //
00989 template <class DataType, class Storage = bool,
00990           class ParserClass = parser<DataType> >
00991 class list : public Option, public list_storage<DataType, Storage> {
00992   std::vector<unsigned> Positions;
00993   ParserClass Parser;
00994 
00995   virtual enum ValueExpected getValueExpectedFlagDefault() const {
00996     return Parser.getValueExpectedFlagDefault();
00997   }
00998   virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
00999     return Parser.getExtraOptionNames(OptionNames);
01000   }
01001 
01002   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
01003                                 const std::string &Arg) {
01004     typename ParserClass::parser_data_type Val =
01005       typename ParserClass::parser_data_type();
01006     if (Parser.parse(*this, ArgName, Arg, Val))
01007       return true;  // Parse Error!
01008     addValue(Val);
01009     setPosition(pos);
01010     Positions.push_back(pos);
01011     return false;
01012   }
01013 
01014   // Forward printing stuff to the parser...
01015   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
01016   virtual void printOptionInfo(size_t GlobalWidth) const {
01017     Parser.printOptionInfo(*this, GlobalWidth);
01018   }
01019 
01020   void done() {
01021     addArgument();
01022     Parser.initialize(*this);
01023   }
01024 public:
01025   ParserClass &getParser() { return Parser; }
01026 
01027   unsigned getPosition(unsigned optnum) const {
01028     assert(optnum < this->size() && "Invalid option index");
01029     return Positions[optnum];
01030   }
01031 
01032   void setNumAdditionalVals(unsigned n) {
01033     Option::setNumAdditionalVals(n);
01034   }
01035 
01036   // One option...
01037   template<class M0t>
01038   explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
01039     apply(M0, this);
01040     done();
01041   }
01042   // Two options...
01043   template<class M0t, class M1t>
01044   list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
01045     apply(M0, this); apply(M1, this);
01046     done();
01047   }
01048   // Three options...
01049   template<class M0t, class M1t, class M2t>
01050   list(const M0t &M0, const M1t &M1, const M2t &M2)
01051     : Option(ZeroOrMore | NotHidden) {
01052     apply(M0, this); apply(M1, this); apply(M2, this);
01053     done();
01054   }
01055   // Four options...
01056   template<class M0t, class M1t, class M2t, class M3t>
01057   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
01058     : Option(ZeroOrMore | NotHidden) {
01059     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01060     done();
01061   }
01062   // Five options...
01063   template<class M0t, class M1t, class M2t, class M3t, class M4t>
01064   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01065        const M4t &M4) : Option(ZeroOrMore | NotHidden) {
01066     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01067     apply(M4, this);
01068     done();
01069   }
01070   // Six options...
01071   template<class M0t, class M1t, class M2t, class M3t,
01072            class M4t, class M5t>
01073   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01074        const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
01075     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01076     apply(M4, this); apply(M5, this);
01077     done();
01078   }
01079   // Seven options...
01080   template<class M0t, class M1t, class M2t, class M3t,
01081            class M4t, class M5t, class M6t>
01082   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01083        const M4t &M4, const M5t &M5, const M6t &M6)
01084     : Option(ZeroOrMore | NotHidden) {
01085     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01086     apply(M4, this); apply(M5, this); apply(M6, this);
01087     done();
01088   }
01089   // Eight options...
01090   template<class M0t, class M1t, class M2t, class M3t,
01091            class M4t, class M5t, class M6t, class M7t>
01092   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01093        const M4t &M4, const M5t &M5, const M6t &M6,
01094        const M7t &M7) : Option(ZeroOrMore | NotHidden) {
01095     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01096     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
01097     done();
01098   }
01099 };
01100 
01101 // multi_arg - Modifier to set the number of additional values.
01102 struct multi_val {
01103   unsigned AdditionalVals;
01104   explicit multi_val(unsigned N) : AdditionalVals(N) {}
01105 
01106   template <typename D, typename S, typename P>
01107   void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
01108 };
01109 
01110 
01111 //===----------------------------------------------------------------------===//
01112 // bits_storage class
01113 
01114 // Default storage class definition: external storage.  This implementation
01115 // assumes the user will specify a variable to store the data into with the
01116 // cl::location(x) modifier.
01117 //
01118 template<class DataType, class StorageClass>
01119 class bits_storage {
01120   unsigned *Location;   // Where to store the bits...
01121 
01122   template<class T>
01123   static unsigned Bit(const T &V) {
01124     unsigned BitPos = reinterpret_cast<unsigned>(V);
01125     assert(BitPos < sizeof(unsigned) * 8 &&
01126           "enum exceeds width of bit vector!");
01127     return 1 << BitPos;
01128   }
01129 
01130 public:
01131   bits_storage() : Location(0) {}
01132 
01133   bool setLocation(Option &O, unsigned &L) {
01134     if (Location)
01135       return O.error(": cl::location(x) specified more than once!");
01136     Location = &L;
01137     return false;
01138   }
01139 
01140   template<class T>
01141   void addValue(const T &V) {
01142     assert(Location != 0 && "cl::location(...) not specified for a command "
01143            "line option with external storage!");
01144     *Location |= Bit(V);
01145   }
01146 
01147   unsigned getBits() { return *Location; }
01148 
01149   template<class T>
01150   bool isSet(const T &V) {
01151     return (*Location & Bit(V)) != 0;
01152   }
01153 };
01154 
01155 
01156 // Define how to hold bits.  Since we can inherit from a class, we do so.
01157 // This makes us exactly compatible with the bits in all cases that it is used.
01158 //
01159 template<class DataType>
01160 class bits_storage<DataType, bool> {
01161   unsigned Bits;   // Where to store the bits...
01162 
01163   template<class T>
01164   static unsigned Bit(const T &V) {
01165     unsigned BitPos = reinterpret_cast<unsigned>(V);
01166     assert(BitPos < sizeof(unsigned) * 8 &&
01167           "enum exceeds width of bit vector!");
01168     return 1 << BitPos;
01169   }
01170 
01171 public:
01172   template<class T>
01173   void addValue(const T &V) {
01174     Bits |=  Bit(V);
01175   }
01176 
01177   unsigned getBits() { return Bits; }
01178 
01179   template<class T>
01180   bool isSet(const T &V) {
01181     return (Bits & Bit(V)) != 0;
01182   }
01183 };
01184 
01185 
01186 //===----------------------------------------------------------------------===//
01187 // bits - A bit vector of command options.
01188 //
01189 template <class DataType, class Storage = bool,
01190           class ParserClass = parser<DataType> >
01191 class bits : public Option, public bits_storage<DataType, Storage> {
01192   std::vector<unsigned> Positions;
01193   ParserClass Parser;
01194 
01195   virtual enum ValueExpected getValueExpectedFlagDefault() const {
01196     return Parser.getValueExpectedFlagDefault();
01197   }
01198   virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
01199     return Parser.getExtraOptionNames(OptionNames);
01200   }
01201 
01202   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
01203                                 const std::string &Arg) {
01204     typename ParserClass::parser_data_type Val =
01205       typename ParserClass::parser_data_type();
01206     if (Parser.parse(*this, ArgName, Arg, Val))
01207       return true;  // Parse Error!
01208     addValue(Val);
01209     setPosition(pos);
01210     Positions.push_back(pos);
01211     return false;
01212   }
01213 
01214   // Forward printing stuff to the parser...
01215   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
01216   virtual void printOptionInfo(size_t GlobalWidth) const {
01217     Parser.printOptionInfo(*this, GlobalWidth);
01218   }
01219 
01220   void done() {
01221     addArgument();
01222     Parser.initialize(*this);
01223   }
01224 public:
01225   ParserClass &getParser() { return Parser; }
01226 
01227   unsigned getPosition(unsigned optnum) const {
01228     assert(optnum < this->size() && "Invalid option index");
01229     return Positions[optnum];
01230   }
01231 
01232   // One option...
01233   template<class M0t>
01234   explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
01235     apply(M0, this);
01236     done();
01237   }
01238   // Two options...
01239   template<class M0t, class M1t>
01240   bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
01241     apply(M0, this); apply(M1, this);
01242     done();
01243   }
01244   // Three options...
01245   template<class M0t, class M1t, class M2t>
01246   bits(const M0t &M0, const M1t &M1, const M2t &M2)
01247     : Option(ZeroOrMore | NotHidden) {
01248     apply(M0, this); apply(M1, this); apply(M2, this);
01249     done();
01250   }
01251   // Four options...
01252   template<class M0t, class M1t, class M2t, class M3t>
01253   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
01254     : Option(ZeroOrMore | NotHidden) {
01255     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01256     done();
01257   }
01258   // Five options...
01259   template<class M0t, class M1t, class M2t, class M3t, class M4t>
01260   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01261        const M4t &M4) : Option(ZeroOrMore | NotHidden) {
01262     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01263     apply(M4, this);
01264     done();
01265   }
01266   // Six options...
01267   template<class M0t, class M1t, class M2t, class M3t,
01268            class M4t, class M5t>
01269   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01270        const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
01271     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01272     apply(M4, this); apply(M5, this);
01273     done();
01274   }
01275   // Seven options...
01276   template<class M0t, class M1t, class M2t, class M3t,
01277            class M4t, class M5t, class M6t>
01278   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01279        const M4t &M4, const M5t &M5, const M6t &M6)
01280     : Option(ZeroOrMore | NotHidden) {
01281     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01282     apply(M4, this); apply(M5, this); apply(M6, this);
01283     done();
01284   }
01285   // Eight options...
01286   template<class M0t, class M1t, class M2t, class M3t,
01287            class M4t, class M5t, class M6t, class M7t>
01288   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
01289        const M4t &M4, const M5t &M5, const M6t &M6,
01290        const M7t &M7) : Option(ZeroOrMore | NotHidden) {
01291     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01292     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
01293     done();
01294   }
01295 };
01296 
01297 //===----------------------------------------------------------------------===//
01298 // Aliased command line option (alias this name to a preexisting name)
01299 //
01300 
01301 class alias : public Option {
01302   Option *AliasFor;
01303   virtual bool handleOccurrence(unsigned pos, const char * /*ArgName*/,
01304                                 const std::string &Arg) {
01305     return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
01306   }
01307   // Handle printing stuff...
01308   virtual size_t getOptionWidth() const;
01309   virtual void printOptionInfo(size_t GlobalWidth) const;
01310 
01311   void done() {
01312     if (!hasArgStr())
01313       error(": cl::alias must have argument name specified!");
01314     if (AliasFor == 0)
01315       error(": cl::alias must have an cl::aliasopt(option) specified!");
01316       addArgument();
01317   }
01318 public:
01319   void setAliasFor(Option &O) {
01320     if (AliasFor)
01321       error(": cl::alias must only have one cl::aliasopt(...) specified!");
01322     AliasFor = &O;
01323   }
01324 
01325   // One option...
01326   template<class M0t>
01327   explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
01328     apply(M0, this);
01329     done();
01330   }
01331   // Two options...
01332   template<class M0t, class M1t>
01333   alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
01334     apply(M0, this); apply(M1, this);
01335     done();
01336   }
01337   // Three options...
01338   template<class M0t, class M1t, class M2t>
01339   alias(const M0t &M0, const M1t &M1, const M2t &M2)
01340     : Option(Optional | Hidden), AliasFor(0) {
01341     apply(M0, this); apply(M1, this); apply(M2, this);
01342     done();
01343   }
01344   // Four options...
01345   template<class M0t, class M1t, class M2t, class M3t>
01346   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
01347     : Option(Optional | Hidden), AliasFor(0) {
01348     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
01349     done();
01350   }
01351 };
01352 
01353 // aliasfor - Modifier to set the option an alias aliases.
01354 struct aliasopt {
01355   Option &Opt;
01356   explicit aliasopt(Option &O) : Opt(O) {}
01357   void apply(alias &A) const { A.setAliasFor(Opt); }
01358 };
01359 
01360 // extrahelp - provide additional help at the end of the normal help
01361 // output. All occurrences of cl::extrahelp will be accumulated and
01362 // printed to std::cerr at the end of the regular help, just before
01363 // exit is called.
01364 struct extrahelp {
01365   const char * morehelp;
01366   explicit extrahelp(const char* help);
01367 };
01368 
01369 void PrintVersionMessage();
01370 // This function just prints the help message, exactly the same way as if the
01371 // --help option had been given on the command line.
01372 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
01373 void PrintHelpMessage();
01374 
01375 } // End namespace cl
01376 
01377 } // End namespace llvm
01378 
01379 #endif