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