Add facility to dump pass manager structure
[oota-llvm.git] / include / llvm / Support / CommandLine.h
index 8440c97602d7fe9b7aae31c8f1b7fa61db5182ea..23b7cf380e7c33f6c07e14a3bd652bf25b79499d 100644 (file)
@@ -23,6 +23,7 @@
 #include "llvm/Support/type_traits.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/Compiler.h"
+#include "llvm/ADT/SmallVector.h"
 #include <string>
 #include <vector>
 #include <utility>
@@ -56,6 +57,10 @@ void ParseEnvironmentOptions(const char *progName, const char *envvar,
 ///                     CommandLine utilities to print their own version string.
 void SetVersionPrinter(void (*func)());
 
+
+// MarkOptionsChanged - Internal helper function.
+void MarkOptionsChanged();
+
 //===----------------------------------------------------------------------===//
 // Flags permitted to be passed to command line arguments
 //
@@ -138,34 +143,24 @@ class Option {
   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
                                 const std::string &Arg) = 0;
 
-  virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
-    return Optional;
-  }
   virtual enum ValueExpected getValueExpectedFlagDefault() const {
     return ValueOptional;
   }
-  virtual enum OptionHidden getOptionHiddenFlagDefault() const {
-    return NotHidden;
-  }
-  virtual enum FormattingFlags getFormattingFlagDefault() const {
-    return NormalFormatting;
-  }
-
+  
   // Out of line virtual function to provide home for the class.
   virtual void anchor();
   
-  int NumOccurrences;   // The number of times specified
-  int Flags;            // Flags for the argument
-  unsigned Position;    // Position of last occurrence of the option
+  int NumOccurrences;     // The number of times specified
+  int Flags;              // Flags for the argument
+  unsigned Position;      // Position of last occurrence of the option
+  Option *NextRegistered; // Singly linked list of registered options.
 public:
-  const char *ArgStr;   // The argument string itself (ex: "help", "o")
-  const char *HelpStr;  // The descriptive text message for --help
-  const char *ValueStr; // String describing what the value of this option is
+  const char *ArgStr;     // The argument string itself (ex: "help", "o")
+  const char *HelpStr;    // The descriptive text message for --help
+  const char *ValueStr;   // String describing what the value of this option is
 
   inline enum NumOccurrences getNumOccurrencesFlag() const {
-    int NO = Flags & OccurrencesMask;
-    return NO ? static_cast<enum NumOccurrences>(NO)
-              : getNumOccurrencesFlagDefault();
+    return static_cast<enum NumOccurrences>(Flags & OccurrencesMask);
   }
   inline enum ValueExpected getValueExpectedFlag() const {
     int VE = Flags & ValueMask;
@@ -173,14 +168,10 @@ public:
               : getValueExpectedFlagDefault();
   }
   inline enum OptionHidden getOptionHiddenFlag() const {
-    int OH = Flags & HiddenMask;
-    return OH ? static_cast<enum OptionHidden>(OH)
-              : getOptionHiddenFlagDefault();
+    return static_cast<enum OptionHidden>(Flags & HiddenMask);
   }
   inline enum FormattingFlags getFormattingFlag() const {
-    int OH = Flags & FormattingMask;
-    return OH ? static_cast<enum FormattingFlags>(OH)
-              : getFormattingFlagDefault();
+    return static_cast<enum FormattingFlags>(Flags & FormattingMask);
   }
   inline unsigned getMiscFlags() const {
     return Flags & MiscMask;
@@ -211,14 +202,19 @@ public:
   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
   void setPosition(unsigned pos) { Position = pos; }
 protected:
-  Option() : NumOccurrences(0), Flags(0), Position(0),
-             ArgStr(""), HelpStr(""), ValueStr("") {}
+  explicit Option(unsigned DefaultFlags)
+    : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
+      NextRegistered(0), ArgStr(""), HelpStr(""), ValueStr("") {
+    assert(getNumOccurrencesFlag() != 0 &&
+           getOptionHiddenFlag() != 0 && "Not all default flags specified!");
+  }
 
 public:
-  // addArgument - Tell the system that this Option subclass will handle all
-  // occurrences of -ArgStr on the command line.
+  // addArgument - Register this argument with the commandline system.
   //
-  void addArgument(const char *ArgStr);
+  void addArgument();
+  
+  Option *getNextRegisteredOption() const { return NextRegistered; }
 
   // Return the width of the option tag for printing...
   virtual unsigned getOptionWidth() const = 0;
@@ -228,6 +224,8 @@ public:
   //
   virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
 
+  virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
+  
   // addOccurrence - Wrapper around handleOccurrence that enforces Flags
   //
   bool addOccurrence(unsigned pos, const char *ArgName,
@@ -315,7 +313,7 @@ class ValuesClass {
   // Use a vector instead of a map, because the lists should be short,
   // the overhead is less, and most importantly, it keeps them in the order
   // inserted so we can print our option out nicely.
-  std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
+  SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
   void processValues(va_list Vals);
 public:
   ValuesClass(const char *EnumName, DataType Val, const char *Desc,
@@ -390,16 +388,18 @@ struct generic_parser_base {
     // argstr field should be stable, copy it down now.
     //
     hasArgStr = O.hasArgStr();
-
+  }
+  
+  void getExtraOptionNames(std::vector<const char*> &OptionNames) {
     // If there has been no argstr specified, that means that we need to add an
     // argument for every possible option.  This ensures that our options are
     // vectored to us.
-    //
     if (!hasArgStr)
       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
-        O.addArgument(getOption(i));
+        OptionNames.push_back(getOption(i));
   }
 
+
   enum ValueExpected getValueExpectedFlagDefault() const {
     // If there is an ArgStr specified, then we are of the form:
     //
@@ -436,8 +436,8 @@ protected:
 template <class DataType>
 class parser : public generic_parser_base {
 protected:
-  std::vector<std::pair<const char *,
-                        std::pair<DataType, const char *> > > Values;
+  SmallVector<std::pair<const char *,
+                        std::pair<DataType, const char *> >, 8> Values;
 public:
   typedef DataType parser_data_type;
 
@@ -466,16 +466,18 @@ public:
     return O.error(": Cannot find option named '" + ArgVal + "'!");
   }
 
-  // addLiteralOption - Add an entry to the mapping table...
+  /// addLiteralOption - Add an entry to the mapping table.
+  ///
   template <class DT>
   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
     assert(findOption(Name) == Values.size() && "Option already exists!");
     Values.push_back(std::make_pair(Name,
                              std::make_pair(static_cast<DataType>(V),HelpStr)));
+    MarkOptionsChanged();
   }
 
-  // removeLiteralOption - Remove the specified option.
-  //
+  /// removeLiteralOption - Remove the specified option.
+  ///
   void removeLiteralOption(const char *Name) {
     unsigned N = findOption(Name);
     assert(N != Values.size() && "Option not found!");
@@ -493,6 +495,8 @@ struct basic_parser_impl {  // non-template implementation of basic_parser<t>
     return ValueRequired;
   }
 
+  void getExtraOptionNames(std::vector<const char*> &OptionNames) {}
+
   void initialize(Option &O) {}
 
   // Return the width of the option tag for printing...
@@ -540,6 +544,28 @@ public:
 
 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
 
+//--------------------------------------------------
+// parser<boolOrDefault>
+enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
+template<>
+class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
+public:
+  // parse - Return true on error.
+  bool parse(Option &O, const char *ArgName, const std::string &Arg, 
+             boolOrDefault &Val);
+
+  enum ValueExpected getValueExpectedFlagDefault() const {
+    return ValueOptional;
+  }
+
+  // getValueName - Do not print =<value> at all.
+  virtual const char *getValueName() const { return 0; }
+  
+  // An out-of-line virtual method to provide a 'home' for this class.
+  virtual void anchor();
+};
+
+EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
 
 //--------------------------------------------------
 // parser<int>
@@ -782,6 +808,9 @@ class opt : public Option,
   virtual enum ValueExpected getValueExpectedFlagDefault() const {
     return Parser.getValueExpectedFlagDefault();
   }
+  virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+    return Parser.getExtraOptionNames(OptionNames);
+  }
 
   // Forward printing stuff to the parser...
   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
@@ -790,7 +819,7 @@ class opt : public Option,
   }
 
   void done() {
-    addArgument(ArgStr);
+    addArgument();
     Parser.initialize(*this);
   }
 public:
@@ -809,34 +838,36 @@ public:
 
   // One option...
   template<class M0t>
-  opt(const M0t &M0) {
+  opt(const M0t &M0) : Option(Optional | NotHidden) {
     apply(M0, this);
     done();
   }
 
   // Two options...
   template<class M0t, class M1t>
-  opt(const M0t &M0, const M1t &M1) {
+  opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this);
     done();
   }
 
   // Three options...
   template<class M0t, class M1t, class M2t>
-  opt(const M0t &M0, const M1t &M1, const M2t &M2) {
+  opt(const M0t &M0, const M1t &M1,
+      const M2t &M2) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this);
     done();
   }
   // Four options...
   template<class M0t, class M1t, class M2t, class M3t>
-  opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+  opt(const M0t &M0, const M1t &M1, const M2t &M2,
+      const M3t &M3) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     done();
   }
   // Five options...
   template<class M0t, class M1t, class M2t, class M3t, class M4t>
   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4) {
+      const M4t &M4) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this);
     done();
@@ -845,7 +876,7 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t>
   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5) {
+      const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this);
     done();
@@ -854,7 +885,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t>
   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6) {
+      const M4t &M4, const M5t &M5,
+      const M6t &M6) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this);
     done();
@@ -863,7 +895,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t, class M7t>
   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+      const M4t &M4, const M5t &M5, const M6t &M6,
+      const M7t &M7) : Option(Optional | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
     done();
@@ -926,13 +959,13 @@ class list : public Option, public list_storage<DataType, Storage> {
   std::vector<unsigned> Positions;
   ParserClass Parser;
 
-  virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
-    return ZeroOrMore;
-  }
   virtual enum ValueExpected getValueExpectedFlagDefault() const {
     return Parser.getValueExpectedFlagDefault();
   }
-
+  virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+    return Parser.getExtraOptionNames(OptionNames);
+  }
+  
   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
                                 const std::string &Arg) {
     typename ParserClass::parser_data_type Val =
@@ -952,7 +985,7 @@ class list : public Option, public list_storage<DataType, Storage> {
   }
 
   void done() {
-    addArgument(ArgStr);
+    addArgument();
     Parser.initialize(*this);
   }
 public:
@@ -965,32 +998,34 @@ public:
 
   // One option...
   template<class M0t>
-  list(const M0t &M0) {
+  list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this);
     done();
   }
   // Two options...
   template<class M0t, class M1t>
-  list(const M0t &M0, const M1t &M1) {
+  list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this);
     done();
   }
   // Three options...
   template<class M0t, class M1t, class M2t>
-  list(const M0t &M0, const M1t &M1, const M2t &M2) {
+  list(const M0t &M0, const M1t &M1, const M2t &M2)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this);
     done();
   }
   // Four options...
   template<class M0t, class M1t, class M2t, class M3t>
-  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+  list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     done();
   }
   // Five options...
   template<class M0t, class M1t, class M2t, class M3t, class M4t>
   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-       const M4t &M4) {
+       const M4t &M4) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this);
     done();
@@ -999,7 +1034,7 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t>
   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-       const M4t &M4, const M5t &M5) {
+       const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this);
     done();
@@ -1008,7 +1043,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t>
   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6) {
+       const M4t &M4, const M5t &M5, const M6t &M6)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this);
     done();
@@ -1017,7 +1053,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t, class M7t>
   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+       const M4t &M4, const M5t &M5, const M6t &M6,
+       const M7t &M7) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
     done();
@@ -1108,13 +1145,13 @@ class bits : public Option, public bits_storage<DataType, Storage> {
   std::vector<unsigned> Positions;
   ParserClass Parser;
 
-  virtual enum NumOccurrences getNumOccurrencesFlagDefault() const {
-    return ZeroOrMore;
-  }
   virtual enum ValueExpected getValueExpectedFlagDefault() const {
     return Parser.getValueExpectedFlagDefault();
   }
-
+  virtual void getExtraOptionNames(std::vector<const char*> &OptionNames) {
+    return Parser.getExtraOptionNames(OptionNames);
+  }
+  
   virtual bool handleOccurrence(unsigned pos, const char *ArgName,
                                 const std::string &Arg) {
     typename ParserClass::parser_data_type Val =
@@ -1134,7 +1171,7 @@ class bits : public Option, public bits_storage<DataType, Storage> {
   }
 
   void done() {
-    addArgument(ArgStr);
+    addArgument();
     Parser.initialize(*this);
   }
 public:
@@ -1147,32 +1184,34 @@ public:
 
   // One option...
   template<class M0t>
-  bits(const M0t &M0) {
+  bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this);
     done();
   }
   // Two options...
   template<class M0t, class M1t>
-  bits(const M0t &M0, const M1t &M1) {
+  bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this);
     done();
   }
   // Three options...
   template<class M0t, class M1t, class M2t>
-  bits(const M0t &M0, const M1t &M1, const M2t &M2) {
+  bits(const M0t &M0, const M1t &M1, const M2t &M2)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this);
     done();
   }
   // Four options...
   template<class M0t, class M1t, class M2t, class M3t>
-  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
+  bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     done();
   }
   // Five options...
   template<class M0t, class M1t, class M2t, class M3t, class M4t>
   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-       const M4t &M4) {
+       const M4t &M4) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this);
     done();
@@ -1181,7 +1220,7 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t>
   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-       const M4t &M4, const M5t &M5) {
+       const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this);
     done();
@@ -1190,7 +1229,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t>
   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6) {
+       const M4t &M4, const M5t &M5, const M6t &M6)
+    : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this);
     done();
@@ -1199,7 +1239,8 @@ public:
   template<class M0t, class M1t, class M2t, class M3t,
            class M4t, class M5t, class M6t, class M7t>
   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
-      const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
+       const M4t &M4, const M5t &M5, const M6t &M6,
+       const M7t &M7) : Option(ZeroOrMore | NotHidden) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
     done();
@@ -1216,9 +1257,6 @@ class alias : public Option {
                                 const std::string &Arg) {
     return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
   }
-  // Aliases default to be hidden...
-  virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
-
   // Handle printing stuff...
   virtual unsigned getOptionWidth() const;
   virtual void printOptionInfo(unsigned GlobalWidth) const;
@@ -1228,7 +1266,7 @@ class alias : public Option {
       error(": cl::alias must have argument name specified!");
     if (AliasFor == 0)
       error(": cl::alias must have an cl::aliasopt(option) specified!");
-    addArgument(ArgStr);
+      addArgument();
   }
 public:
   void setAliasFor(Option &O) {
@@ -1239,26 +1277,27 @@ public:
 
   // One option...
   template<class M0t>
-  alias(const M0t &M0) : AliasFor(0) {
+  alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
     apply(M0, this);
     done();
   }
   // Two options...
   template<class M0t, class M1t>
-  alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
+  alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
     apply(M0, this); apply(M1, this);
     done();
   }
   // Three options...
   template<class M0t, class M1t, class M2t>
-  alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
+  alias(const M0t &M0, const M1t &M1, const M2t &M2)
+    : Option(Optional | Hidden), AliasFor(0) {
     apply(M0, this); apply(M1, this); apply(M2, this);
     done();
   }
   // Four options...
   template<class M0t, class M1t, class M2t, class M3t>
   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
-    : AliasFor(0) {
+    : Option(Optional | Hidden), AliasFor(0) {
     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
     done();
   }