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