First cut at the Code Generator using the TableGen methodology.
[oota-llvm.git] / include / Support / CommandLine.h
1 //===- Support/CommandLine.h - Flexible Command line parser ------*- C++ -*--=//
2 //
3 // This class implements a command line argument processor that is useful when
4 // creating a tool.  It provides a simple, minimalistic interface that is easily
5 // extensible and supports nonlocal (library) command line options.
6 //
7 // Note that rather than trying to figure out what this code does, you should
8 // read the library documentation located in docs/CommandLine.html or looks at
9 // the many example usages in tools/*/*.cpp
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_COMMANDLINE_H
14 #define LLVM_SUPPORT_COMMANDLINE_H
15
16 #include <string>
17 #include <vector>
18 #include <utility>
19 #include <cstdarg>
20 #include "boost/type_traits/object_traits.hpp"
21
22 namespace cl {   // Short namespace to make usage concise
23
24 //===----------------------------------------------------------------------===//
25 // ParseCommandLineOptions - Command line option processing entry point.
26 //
27 void cl::ParseCommandLineOptions(int &argc, char **argv,
28                                  const char *Overview = 0);
29
30 //===----------------------------------------------------------------------===//
31 // Flags permitted to be passed to command line arguments
32 //
33
34 enum NumOccurances {           // Flags for the number of occurances allowed...
35   Optional        = 0x01,      // Zero or One occurance
36   ZeroOrMore      = 0x02,      // Zero or more occurances allowed
37   Required        = 0x03,      // One occurance required
38   OneOrMore       = 0x04,      // One or more occurances required
39
40   // ConsumeAfter - Indicates that this option is fed anything that follows the
41   // last positional argument required by the application (it is an error if
42   // there are zero positional arguments, and a ConsumeAfter option is used).
43   // Thus, for example, all arguments to LLI are processed until a filename is
44   // found.  Once a filename is found, all of the succeeding arguments are
45   // passed, unprocessed, to the ConsumeAfter option.
46   //
47   ConsumeAfter    = 0x05,
48
49   OccurancesMask  = 0x07,
50 };
51
52 enum ValueExpected {           // Is a value required for the option?
53   ValueOptional   = 0x08,      // The value can appear... or not
54   ValueRequired   = 0x10,      // The value is required to appear!
55   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
56   ValueMask       = 0x18,
57 };
58
59 enum OptionHidden {            // Control whether -help shows this option
60   NotHidden       = 0x20,      // Option included in --help & --help-hidden
61   Hidden          = 0x40,      // -help doesn't, but --help-hidden does
62   ReallyHidden    = 0x60,      // Neither --help nor --help-hidden show this arg
63   HiddenMask      = 0x60,
64 };
65
66 // Formatting flags - This controls special features that the option might have
67 // that cause it to be parsed differently...
68 //
69 // Prefix - This option allows arguments that are otherwise unrecognized to be
70 // matched by options that are a prefix of the actual value.  This is useful for
71 // cases like a linker, where options are typically of the form '-lfoo' or
72 // '-L../../include' where -l or -L are the actual flags.  When prefix is
73 // enabled, and used, the value for the flag comes from the suffix of the
74 // argument.
75 //
76 // Grouping - With this option enabled, multiple letter options are allowed to
77 // bunch together with only a single hyphen for the whole group.  This allows
78 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
79 //
80
81 enum FormattingFlags {
82   NormalFormatting = 0x000,     // Nothing special
83   Positional       = 0x080,     // Is a positional argument, no '-' required
84   Prefix           = 0x100,     // Can this option directly prefix its value?
85   Grouping         = 0x180,     // Can this option group with other options?
86   FormattingMask   = 0x180,
87 };
88
89 enum MiscFlags {                // Miscellaneous flags to adjust argument
90   CommaSeparated   = 0x200,     // Should this cl::list split between commas?
91   MiscMask         = 0x200,
92 };
93
94
95
96 //===----------------------------------------------------------------------===//
97 // Option Base class
98 //
99 class alias;
100 class Option {
101   friend void cl::ParseCommandLineOptions(int &, char **, const char *, int);
102   friend class alias;
103
104   // handleOccurances - Overriden by subclasses to handle the value passed into
105   // an argument.  Should return true if there was an error processing the
106   // argument and the program should exit.
107   //
108   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) = 0;
109
110   virtual enum NumOccurances getNumOccurancesFlagDefault() const { 
111     return Optional;
112   }
113   virtual enum ValueExpected getValueExpectedFlagDefault() const {
114     return ValueOptional; 
115   }
116   virtual enum OptionHidden getOptionHiddenFlagDefault() const {
117     return NotHidden;
118   }
119   virtual enum FormattingFlags getFormattingFlagDefault() const {
120     return NormalFormatting;
121   }
122
123   int NumOccurances;    // The number of times specified
124   int Flags;            // Flags for the argument
125 public:
126   const char *ArgStr;   // The argument string itself (ex: "help", "o")
127   const char *HelpStr;  // The descriptive text message for --help
128   const char *ValueStr; // String describing what the value of this option is
129
130   inline enum NumOccurances getNumOccurancesFlag() const {
131     int NO = Flags & OccurancesMask;
132     return NO ? (enum NumOccurances)NO : getNumOccurancesFlagDefault();
133   }
134   inline enum ValueExpected getValueExpectedFlag() const {
135     int VE = Flags & ValueMask;
136     return VE ? (enum ValueExpected)VE : getValueExpectedFlagDefault();
137   }
138   inline enum OptionHidden getOptionHiddenFlag() const {
139     int OH = Flags & HiddenMask;
140     return OH ? (enum OptionHidden)OH : getOptionHiddenFlagDefault();
141   }
142   inline enum FormattingFlags getFormattingFlag() const {
143     int OH = Flags & FormattingMask;
144     return OH ? (enum FormattingFlags)OH : getFormattingFlagDefault();
145   }
146   inline unsigned getMiscFlags() const {
147     return Flags & MiscMask;
148   }
149
150   // hasArgStr - Return true if the argstr != ""
151   bool hasArgStr() const { return ArgStr[0] != 0; }
152
153   //-------------------------------------------------------------------------===
154   // Accessor functions set by OptionModifiers
155   //
156   void setArgStr(const char *S) { ArgStr = S; }
157   void setDescription(const char *S) { HelpStr = S; }
158   void setValueStr(const char *S) { ValueStr = S; }
159
160   void setFlag(unsigned Flag, unsigned FlagMask) {
161     if (Flags & FlagMask) {
162       error(": Specified two settings for the same option!");
163       exit(1);
164     }
165
166     Flags |= Flag;
167   }
168
169   void setNumOccurancesFlag(enum NumOccurances Val) {
170     setFlag(Val, OccurancesMask);
171   }
172   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
173   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
174   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
175   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
176 protected:
177   Option() : NumOccurances(0), Flags(0),
178              ArgStr(""), HelpStr(""), ValueStr("") {}
179
180 public:
181   // addArgument - Tell the system that this Option subclass will handle all
182   // occurances of -ArgStr on the command line.
183   //
184   void addArgument(const char *ArgStr);
185   void removeArgument(const char *ArgStr);
186
187   // Return the width of the option tag for printing...
188   virtual unsigned getOptionWidth() const = 0;
189
190   // printOptionInfo - Print out information about this option.  The 
191   // to-be-maintained width is specified.
192   //
193   virtual void printOptionInfo(unsigned GlobalWidth) const = 0;
194
195   // addOccurance - Wrapper around handleOccurance that enforces Flags
196   //
197   bool addOccurance(const char *ArgName, const std::string &Value);
198
199   // Prints option name followed by message.  Always returns true.
200   bool error(std::string Message, const char *ArgName = 0);
201
202 public:
203   inline int getNumOccurances() const { return NumOccurances; }
204   virtual ~Option() {}
205 };
206
207
208 //===----------------------------------------------------------------------===//
209 // Command line option modifiers that can be used to modify the behavior of
210 // command line option parsers...
211 //
212
213 // desc - Modifier to set the description shown in the --help output...
214 struct desc {
215   const char *Desc;
216   desc(const char *Str) : Desc(Str) {}
217   void apply(Option &O) const { O.setDescription(Desc); }
218 };
219
220 // value_desc - Modifier to set the value description shown in the --help
221 // output...
222 struct value_desc {
223   const char *Desc;
224   value_desc(const char *Str) : Desc(Str) {}
225   void apply(Option &O) const { O.setValueStr(Desc); }
226 };
227
228
229 // init - Specify a default (initial) value for the command line argument, if
230 // the default constructor for the argument type does not give you what you
231 // want.  This is only valid on "opt" arguments, not on "list" arguments.
232 //
233 template<class Ty>
234 struct initializer {
235   const Ty &Init;
236   initializer(const Ty &Val) : Init(Val) {}
237
238   template<class Opt>
239   void apply(Opt &O) const { O.setInitialValue(Init); }
240 };
241
242 template<class Ty>
243 initializer<Ty> init(const Ty &Val) {
244   return initializer<Ty>(Val);
245 }
246
247
248 // location - Allow the user to specify which external variable they want to
249 // store the results of the command line argument processing into, if they don't
250 // want to store it in the option itself.
251 //
252 template<class Ty>
253 struct LocationClass {
254   Ty &Loc;
255   LocationClass(Ty &L) : Loc(L) {}
256
257   template<class Opt>
258   void apply(Opt &O) const { O.setLocation(O, Loc); }
259 };
260
261 template<class Ty>
262 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
263
264
265 //===----------------------------------------------------------------------===//
266 // Enum valued command line option
267 //
268 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, (int)ENUMVAL, DESC
269 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, (int)ENUMVAL, DESC
270
271 // values - For custom data types, allow specifying a group of values together
272 // as the values that go into the mapping that the option handler uses.  Note
273 // that the values list must always have a 0 at the end of the list to indicate
274 // that the list has ended.
275 //
276 template<class DataType>
277 class ValuesClass {
278   // Use a vector instead of a map, because the lists should be short,
279   // the overhead is less, and most importantly, it keeps them in the order
280   // inserted so we can print our option out nicely.
281   std::vector<std::pair<const char *, std::pair<int, const char *> > > Values;
282   void processValues(va_list Vals);
283 public:
284   ValuesClass(const char *EnumName, DataType Val, const char *Desc, 
285               va_list ValueArgs) {
286     // Insert the first value, which is required.
287     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
288
289     // Process the varargs portion of the values...
290     while (const char *EnumName = va_arg(ValueArgs, const char *)) {
291       DataType EnumVal = (DataType)va_arg(ValueArgs, int);
292       const char *EnumDesc = va_arg(ValueArgs, const char *);
293       Values.push_back(std::make_pair(EnumName,      // Add value to value map
294                                       std::make_pair(EnumVal, EnumDesc)));
295     }
296   }
297
298   template<class Opt>
299   void apply(Opt &O) const {
300     for (unsigned i = 0, e = Values.size(); i != e; ++i)
301       O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
302                                      Values[i].second.second);
303   }
304 };
305
306 template<class DataType>
307 ValuesClass<DataType> values(const char *Arg, DataType Val, const char *Desc,
308                              ...) {
309     va_list ValueArgs;
310     va_start(ValueArgs, Desc);
311     ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
312     va_end(ValueArgs);
313     return Vals;
314 }
315
316
317 //===----------------------------------------------------------------------===//
318 // parser class - Parameterizable parser for different data types.  By default,
319 // known data types (string, int, bool) have specialized parsers, that do what
320 // you would expect.  The default parser, used for data types that are not
321 // built-in, uses a mapping table to map specific options to values, which is
322 // used, among other things, to handle enum types.
323
324 //--------------------------------------------------
325 // generic_parser_base - This class holds all the non-generic code that we do
326 // not need replicated for every instance of the generic parser.  This also
327 // allows us to put stuff into CommandLine.cpp
328 //
329 struct generic_parser_base {
330   virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
331
332   // getNumOptions - Virtual function implemented by generic subclass to
333   // indicate how many entries are in Values.
334   //
335   virtual unsigned getNumOptions() const = 0;
336
337   // getOption - Return option name N.
338   virtual const char *getOption(unsigned N) const = 0;
339   
340   // getDescription - Return description N
341   virtual const char *getDescription(unsigned N) const = 0;
342
343   // Return the width of the option tag for printing...
344   virtual unsigned getOptionWidth(const Option &O) const;
345
346   // printOptionInfo - Print out information about this option.  The 
347   // to-be-maintained width is specified.
348   //
349   virtual void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
350
351   void initialize(Option &O) {
352     // All of the modifiers for the option have been processed by now, so the
353     // argstr field should be stable, copy it down now.
354     //
355     hasArgStr = O.hasArgStr();
356
357     // If there has been no argstr specified, that means that we need to add an
358     // argument for every possible option.  This ensures that our options are
359     // vectored to us.
360     //
361     if (!hasArgStr)
362       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
363         O.addArgument(getOption(i));
364   }
365
366   enum ValueExpected getValueExpectedFlagDefault() const {
367     // If there is an ArgStr specified, then we are of the form:
368     //
369     //    -opt=O2   or   -opt O2  or  -optO2
370     //
371     // In which case, the value is required.  Otherwise if an arg str has not
372     // been specified, we are of the form:
373     //
374     //    -O2 or O2 or -la (where -l and -a are seperate options)
375     //
376     // If this is the case, we cannot allow a value.
377     //
378     if (hasArgStr)
379       return ValueRequired;
380     else
381       return ValueDisallowed;
382   }
383
384   // findOption - Return the option number corresponding to the specified
385   // argument string.  If the option is not found, getNumOptions() is returned.
386   //
387   unsigned findOption(const char *Name);
388
389 protected:
390   bool hasArgStr;
391 };
392
393 // Default parser implementation - This implementation depends on having a
394 // mapping of recognized options to values of some sort.  In addition to this,
395 // each entry in the mapping also tracks a help message that is printed with the
396 // command line option for --help.  Because this is a simple mapping parser, the
397 // data type can be any unsupported type.
398 //
399 template <class DataType>
400 class parser : public generic_parser_base {
401 protected:
402   std::vector<std::pair<const char *,
403                         std::pair<DataType, const char *> > > Values;
404 public:
405   typedef DataType parser_data_type;
406
407   // Implement virtual functions needed by generic_parser_base
408   unsigned getNumOptions() const { return Values.size(); }
409   const char *getOption(unsigned N) const { return Values[N].first; }
410   const char *getDescription(unsigned N) const {
411     return Values[N].second.second;
412   }
413
414   // parse - Return true on error.
415   bool parse(Option &O, const char *ArgName, const std::string &Arg,
416              DataType &V) {
417     std::string ArgVal;
418     if (hasArgStr)
419       ArgVal = Arg;
420     else
421       ArgVal = ArgName;
422
423     for (unsigned i = 0, e = Values.size(); i != e; ++i)
424       if (ArgVal == Values[i].first) {
425         V = Values[i].second.first;
426         return false;
427       }
428
429     return O.error(": Cannot find option named '" + ArgVal + "'!");
430   }
431
432   // addLiteralOption - Add an entry to the mapping table...
433   template <class DT>
434   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
435     assert(findOption(Name) == Values.size() && "Option already exists!");
436     Values.push_back(std::make_pair(Name, std::make_pair((DataType)V,HelpStr)));
437   }
438
439   // removeLiteralOption - Remove the specified option.
440   //
441   void removeLiteralOption(const char *Name) {
442     unsigned N = findOption(Name);
443     assert(N != Values.size() && "Option not found!");
444     Values.erase(Values.begin()+N);
445   }
446 };
447
448 //--------------------------------------------------
449 // basic_parser - Super class of parsers to provide boilerplate code
450 //
451 struct basic_parser_impl {  // non-template implementation of basic_parser<t>
452   virtual ~basic_parser_impl() {}
453
454   enum ValueExpected getValueExpectedFlagDefault() const {
455     return ValueRequired;
456   }
457   
458   void initialize(Option &O) {}
459   
460   // Return the width of the option tag for printing...
461   unsigned getOptionWidth(const Option &O) const;
462   
463   // printOptionInfo - Print out information about this option.  The
464   // to-be-maintained width is specified.
465   //
466   void printOptionInfo(const Option &O, unsigned GlobalWidth) const;
467
468
469   // getValueName - Overload in subclass to provide a better default value.
470   virtual const char *getValueName() const { return "value"; }
471 };
472
473 // basic_parser - The real basic parser is just a template wrapper that provides
474 // a typedef for the provided data type.
475 //
476 template<class DataType>
477 struct basic_parser : public basic_parser_impl {
478   typedef DataType parser_data_type;
479 };
480
481
482 //--------------------------------------------------
483 // parser<bool>
484 //
485 template<>
486 struct parser<bool> : public basic_parser<bool> {
487
488   // parse - Return true on error.
489   bool parse(Option &O, const char *ArgName, const std::string &Arg, bool &Val);
490
491   enum ValueExpected getValueExpectedFlagDefault() const {
492     return ValueOptional; 
493   }
494
495   // getValueName - Do not print =<value> at all
496   virtual const char *getValueName() const { return 0; }
497 };
498
499
500 //--------------------------------------------------
501 // parser<int>
502 //
503 template<>
504 struct parser<int> : public basic_parser<int> {
505   
506   // parse - Return true on error.
507   bool parse(Option &O, const char *ArgName, const std::string &Arg, int &Val);
508
509   // getValueName - Overload in subclass to provide a better default value.
510   virtual const char *getValueName() const { return "int"; }
511 };
512
513
514 //--------------------------------------------------
515 // parser<double>
516 //
517 template<>
518 struct parser<double> : public basic_parser<double> {
519   // parse - Return true on error.
520   bool parse(Option &O, const char *AN, const std::string &Arg, double &Val);
521
522   // getValueName - Overload in subclass to provide a better default value.
523   virtual const char *getValueName() const { return "number"; }
524 };
525
526
527 //--------------------------------------------------
528 // parser<float>
529 //
530 template<>
531 struct parser<float> : public basic_parser<float> {
532   // parse - Return true on error.
533   bool parse(Option &O, const char *AN, const std::string &Arg, float &Val);
534
535   // getValueName - Overload in subclass to provide a better default value.
536   virtual const char *getValueName() const { return "number"; }
537 };
538
539
540 //--------------------------------------------------
541 // parser<std::string>
542 //
543 template<>
544 struct parser<std::string> : public basic_parser<std::string> {
545   // parse - Return true on error.
546   bool parse(Option &O, const char *ArgName, const std::string &Arg,
547              std::string &Value) {
548     Value = Arg;
549     return false;
550   }
551
552   // getValueName - Overload in subclass to provide a better default value.
553   virtual const char *getValueName() const { return "string"; }
554 };
555
556
557
558 //===----------------------------------------------------------------------===//
559 // applicator class - This class is used because we must use partial
560 // specialization to handle literal string arguments specially (const char* does
561 // not correctly respond to the apply method).  Because the syntax to use this
562 // is a pain, we have the 'apply' method below to handle the nastiness...
563 //
564 template<class Mod> struct applicator {
565   template<class Opt>
566   static void opt(const Mod &M, Opt &O) { M.apply(O); }
567 };
568
569 // Handle const char* as a special case...
570 template<unsigned n> struct applicator<char[n]> {
571   template<class Opt>
572   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
573 };
574 template<unsigned n> struct applicator<const char[n]> {
575   template<class Opt>
576   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
577 };
578 template<> struct applicator<const char*> {
579   template<class Opt>
580   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
581 };
582
583 template<> struct applicator<NumOccurances> {
584   static void opt(NumOccurances NO, Option &O) { O.setNumOccurancesFlag(NO); }
585 };
586 template<> struct applicator<ValueExpected> {
587   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
588 };
589 template<> struct applicator<OptionHidden> {
590   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
591 };
592 template<> struct applicator<FormattingFlags> {
593   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
594 };
595 template<> struct applicator<MiscFlags> {
596   static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
597 };
598
599 // apply method - Apply a modifier to an option in a type safe way.
600 template<class Mod, class Opt>
601 void apply(const Mod &M, Opt *O) {
602   applicator<Mod>::opt(M, *O);
603 }
604
605
606 //===----------------------------------------------------------------------===//
607 // opt_storage class
608
609 // Default storage class definition: external storage.  This implementation
610 // assumes the user will specify a variable to store the data into with the
611 // cl::location(x) modifier.
612 //
613 template<class DataType, bool ExternalStorage, bool isClass>
614 class opt_storage {
615   DataType *Location;   // Where to store the object...
616
617   void check() {
618     assert(Location != 0 && "cl::location(...) not specified for a command "
619            "line option with external storage!");
620   }
621 public:
622   opt_storage() : Location(0) {}
623
624   bool setLocation(Option &O, DataType &L) {
625     if (Location)
626       return O.error(": cl::location(x) specified more than once!");
627     Location = &L;
628     return false;
629   }
630
631   template<class T>
632   void setValue(const T &V) {
633     check();
634     *Location = V;
635   }
636
637   DataType &getValue() { check(); return *Location; }
638   const DataType &getValue() const { check(); return *Location; }
639 };
640
641
642 // Define how to hold a class type object, such as a string.  Since we can
643 // inherit from a class, we do so.  This makes us exactly compatible with the
644 // object in all cases that it is used.
645 //
646 template<class DataType>
647 struct opt_storage<DataType,false,true> : public DataType {
648
649   template<class T>
650   void setValue(const T &V) { DataType::operator=(V); }
651
652   DataType &getValue() { return *this; }
653   const DataType &getValue() const { return *this; }
654 };
655
656 // Define a partial specialization to handle things we cannot inherit from.  In
657 // this case, we store an instance through containment, and overload operators
658 // to get at the value.
659 //
660 template<class DataType>
661 struct opt_storage<DataType, false, false> {
662   DataType Value;
663
664   // Make sure we initialize the value with the default constructor for the
665   // type.
666   opt_storage() : Value(DataType()) {}
667
668   template<class T>
669   void setValue(const T &V) { Value = V; }
670   DataType &getValue() { return Value; }
671   DataType getValue() const { return Value; }
672 };
673
674
675 //===----------------------------------------------------------------------===//
676 // opt - A scalar command line option.
677 //
678 template <class DataType, bool ExternalStorage = false,
679           class ParserClass = parser<DataType> >
680 class opt : public Option, 
681             public opt_storage<DataType, ExternalStorage,
682                                ::boost::is_class<DataType>::value> {
683   ParserClass Parser;
684
685   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
686     typename ParserClass::parser_data_type Val;
687     if (Parser.parse(*this, ArgName, Arg, Val))
688       return true;                            // Parse error!
689     setValue(Val);
690     return false;
691   }
692
693   virtual enum ValueExpected getValueExpectedFlagDefault() const {
694     return Parser.getValueExpectedFlagDefault();
695   }
696
697   // Forward printing stuff to the parser...
698   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
699   virtual void printOptionInfo(unsigned GlobalWidth) const {
700     Parser.printOptionInfo(*this, GlobalWidth);
701   }
702
703   void done() {
704     addArgument(ArgStr);
705     Parser.initialize(*this);
706   }
707 public:
708   // setInitialValue - Used by the cl::init modifier...
709   void setInitialValue(const DataType &V) { setValue(V); }
710
711   ParserClass &getParser() { return Parser; }
712
713   operator DataType() const { return getValue(); }
714
715   template<class T>
716   DataType &operator=(const T &Val) { setValue(Val); return getValue(); }
717
718   // One option...
719   template<class M0t>
720   opt(const M0t &M0) {
721     apply(M0, this);
722     done();
723   }
724
725   // Two options...
726   template<class M0t, class M1t>
727   opt(const M0t &M0, const M1t &M1) {
728     apply(M0, this); apply(M1, this);
729     done();
730   }
731
732   // Three options...
733   template<class M0t, class M1t, class M2t>
734   opt(const M0t &M0, const M1t &M1, const M2t &M2) {
735     apply(M0, this); apply(M1, this); apply(M2, this);
736     done();
737   }
738   // Four options...
739   template<class M0t, class M1t, class M2t, class M3t>
740   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
741     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
742     done();
743   }
744   // Five options...
745   template<class M0t, class M1t, class M2t, class M3t, class M4t>
746   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
747       const M4t &M4) {
748     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
749     apply(M4, this);
750     done();
751   }
752   // Six options...
753   template<class M0t, class M1t, class M2t, class M3t,
754            class M4t, class M5t>
755   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
756       const M4t &M4, const M5t &M5) {
757     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
758     apply(M4, this); apply(M5, this);
759     done();
760   }
761   // Seven options...
762   template<class M0t, class M1t, class M2t, class M3t,
763            class M4t, class M5t, class M6t>
764   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
765       const M4t &M4, const M5t &M5, const M6t &M6) {
766     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
767     apply(M4, this); apply(M5, this); apply(M6, this);
768     done();
769   }
770   // Eight options...
771   template<class M0t, class M1t, class M2t, class M3t,
772            class M4t, class M5t, class M6t, class M7t>
773   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
774       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
775     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
776     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
777     done();
778   }
779 };
780
781 //===----------------------------------------------------------------------===//
782 // list_storage class
783
784 // Default storage class definition: external storage.  This implementation
785 // assumes the user will specify a variable to store the data into with the
786 // cl::location(x) modifier.
787 //
788 template<class DataType, class StorageClass>
789 class list_storage {
790   StorageClass *Location;   // Where to store the object...
791
792 public:
793   list_storage() : Location(0) {}
794
795   bool setLocation(Option &O, StorageClass &L) {
796     if (Location)
797       return O.error(": cl::location(x) specified more than once!");
798     Location = &L;
799     return false;
800   }
801
802   template<class T>
803   void addValue(const T &V) {
804     assert(Location != 0 && "cl::location(...) not specified for a command "
805            "line option with external storage!");
806     Location->push_back(V);
807   }
808 };
809
810
811 // Define how to hold a class type object, such as a string.  Since we can
812 // inherit from a class, we do so.  This makes us exactly compatible with the
813 // object in all cases that it is used.
814 //
815 template<class DataType>
816 struct list_storage<DataType, bool> : public std::vector<DataType> {
817
818   template<class T>
819   void addValue(const T &V) { push_back(V); }
820 };
821
822
823 //===----------------------------------------------------------------------===//
824 // list - A list of command line options.
825 //
826 template <class DataType, class Storage = bool,
827           class ParserClass = parser<DataType> >
828 class list : public Option, public list_storage<DataType, Storage> {
829   ParserClass Parser;
830
831   virtual enum NumOccurances getNumOccurancesFlagDefault() const { 
832     return ZeroOrMore;
833   }
834   virtual enum ValueExpected getValueExpectedFlagDefault() const {
835     return Parser.getValueExpectedFlagDefault();
836   }
837
838   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
839     typename ParserClass::parser_data_type Val;
840     if (Parser.parse(*this, ArgName, Arg, Val))
841       return true;  // Parse Error!
842     addValue(Val);
843     return false;
844   }
845
846   // Forward printing stuff to the parser...
847   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
848   virtual void printOptionInfo(unsigned GlobalWidth) const {
849     Parser.printOptionInfo(*this, GlobalWidth);
850   }
851
852   void done() {
853     addArgument(ArgStr);
854     Parser.initialize(*this);
855   }
856 public:
857   ParserClass &getParser() { return Parser; }
858
859   // One option...
860   template<class M0t>
861   list(const M0t &M0) {
862     apply(M0, this);
863     done();
864   }
865   // Two options...
866   template<class M0t, class M1t>
867   list(const M0t &M0, const M1t &M1) {
868     apply(M0, this); apply(M1, this);
869     done();
870   }
871   // Three options...
872   template<class M0t, class M1t, class M2t>
873   list(const M0t &M0, const M1t &M1, const M2t &M2) {
874     apply(M0, this); apply(M1, this); apply(M2, this);
875     done();
876   }
877   // Four options...
878   template<class M0t, class M1t, class M2t, class M3t>
879   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
880     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
881     done();
882   }
883   // Five options...
884   template<class M0t, class M1t, class M2t, class M3t, class M4t>
885   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
886        const M4t &M4) {
887     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
888     apply(M4, this);
889     done();
890   }
891   // Six options...
892   template<class M0t, class M1t, class M2t, class M3t,
893            class M4t, class M5t>
894   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
895        const M4t &M4, const M5t &M5) {
896     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
897     apply(M4, this); apply(M5, this);
898     done();
899   }
900   // Seven options...
901   template<class M0t, class M1t, class M2t, class M3t,
902            class M4t, class M5t, class M6t>
903   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
904       const M4t &M4, const M5t &M5, const M6t &M6) {
905     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
906     apply(M4, this); apply(M5, this); apply(M6, this);
907     done();
908   }
909   // Eight options...
910   template<class M0t, class M1t, class M2t, class M3t,
911            class M4t, class M5t, class M6t, class M7t>
912   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
913       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
914     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
915     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
916     done();
917   }
918 };
919
920
921
922 //===----------------------------------------------------------------------===//
923 // Aliased command line option (alias this name to a preexisting name)
924 //
925
926 class alias : public Option {
927   Option *AliasFor;
928   virtual bool handleOccurance(const char *ArgName, const std::string &Arg) {
929     return AliasFor->handleOccurance(AliasFor->ArgStr, Arg);
930   }
931   // Aliases default to be hidden...
932   virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
933
934   // Handle printing stuff...
935   virtual unsigned getOptionWidth() const;
936   virtual void printOptionInfo(unsigned GlobalWidth) const;
937
938   void done() {
939     if (!hasArgStr())
940       error(": cl::alias must have argument name specified!");
941     if (AliasFor == 0)
942       error(": cl::alias must have an cl::aliasopt(option) specified!");
943     addArgument(ArgStr);
944   }
945 public:
946   void setAliasFor(Option &O) {
947     if (AliasFor)
948       error(": cl::alias must only have one cl::aliasopt(...) specified!");
949     AliasFor = &O;
950   }
951
952   // One option...
953   template<class M0t>
954   alias(const M0t &M0) : AliasFor(0) {
955     apply(M0, this);
956     done();
957   }
958   // Two options...
959   template<class M0t, class M1t>
960   alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
961     apply(M0, this); apply(M1, this);
962     done();
963   }
964   // Three options...
965   template<class M0t, class M1t, class M2t>
966   alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
967     apply(M0, this); apply(M1, this); apply(M2, this);
968     done();
969   }
970   // Four options...
971   template<class M0t, class M1t, class M2t, class M3t>
972   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
973     : AliasFor(0) {
974     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
975     done();
976   }
977 };
978
979 // aliasfor - Modifier to set the option an alias aliases.
980 struct aliasopt {
981   Option &Opt;
982   aliasopt(Option &O) : Opt(O) {}
983   void apply(alias &A) const { A.setAliasFor(Opt); }
984 };
985
986 } // End namespace cl
987
988 #endif