b7bd62a38074886cf1c695ea359ffe96fe4275e1
[oota-llvm.git] / include / llvm / 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 *);
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   // If the datatype is a pointer, support -> on it.
716   DataType operator->() const { return Value; }
717 };
718
719
720 //===----------------------------------------------------------------------===//
721 // opt - A scalar command line option.
722 //
723 template <class DataType, bool ExternalStorage = false,
724           class ParserClass = parser<DataType> >
725 class opt : public Option, 
726             public opt_storage<DataType, ExternalStorage,
727                                is_class<DataType>::value> {
728   ParserClass Parser;
729
730   virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
731     typename ParserClass::parser_data_type Val;
732     if (Parser.parse(*this, ArgName, Arg, Val))
733       return true;                            // Parse error!
734     setValue(Val);
735     return false;
736   }
737
738   virtual enum ValueExpected getValueExpectedFlagDefault() const {
739     return Parser.getValueExpectedFlagDefault();
740   }
741
742   // Forward printing stuff to the parser...
743   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
744   virtual void printOptionInfo(unsigned GlobalWidth) const {
745     Parser.printOptionInfo(*this, GlobalWidth);
746   }
747
748   void done() {
749     addArgument(ArgStr);
750     Parser.initialize(*this);
751   }
752 public:
753   // setInitialValue - Used by the cl::init modifier...
754   void setInitialValue(const DataType &V) { this->setValue(V); }
755
756   ParserClass &getParser() { return Parser; }
757
758   operator DataType() const { return this->getValue(); }
759
760   template<class T>
761   DataType &operator=(const T &Val) {
762     this->setValue(Val);
763     return this->getValue();
764   }
765
766   // One option...
767   template<class M0t>
768   opt(const M0t &M0) {
769     apply(M0, this);
770     done();
771   }
772
773   // Two options...
774   template<class M0t, class M1t>
775   opt(const M0t &M0, const M1t &M1) {
776     apply(M0, this); apply(M1, this);
777     done();
778   }
779
780   // Three options...
781   template<class M0t, class M1t, class M2t>
782   opt(const M0t &M0, const M1t &M1, const M2t &M2) {
783     apply(M0, this); apply(M1, this); apply(M2, this);
784     done();
785   }
786   // Four options...
787   template<class M0t, class M1t, class M2t, class M3t>
788   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
789     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
790     done();
791   }
792   // Five options...
793   template<class M0t, class M1t, class M2t, class M3t, class M4t>
794   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
795       const M4t &M4) {
796     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
797     apply(M4, this);
798     done();
799   }
800   // Six options...
801   template<class M0t, class M1t, class M2t, class M3t,
802            class M4t, class M5t>
803   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
804       const M4t &M4, const M5t &M5) {
805     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
806     apply(M4, this); apply(M5, this);
807     done();
808   }
809   // Seven options...
810   template<class M0t, class M1t, class M2t, class M3t,
811            class M4t, class M5t, class M6t>
812   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
813       const M4t &M4, const M5t &M5, const M6t &M6) {
814     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
815     apply(M4, this); apply(M5, this); apply(M6, this);
816     done();
817   }
818   // Eight options...
819   template<class M0t, class M1t, class M2t, class M3t,
820            class M4t, class M5t, class M6t, class M7t>
821   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
822       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
823     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
824     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
825     done();
826   }
827 };
828
829 //===----------------------------------------------------------------------===//
830 // list_storage class
831
832 // Default storage class definition: external storage.  This implementation
833 // assumes the user will specify a variable to store the data into with the
834 // cl::location(x) modifier.
835 //
836 template<class DataType, class StorageClass>
837 class list_storage {
838   StorageClass *Location;   // Where to store the object...
839
840 public:
841   list_storage() : Location(0) {}
842
843   bool setLocation(Option &O, StorageClass &L) {
844     if (Location)
845       return O.error(": cl::location(x) specified more than once!");
846     Location = &L;
847     return false;
848   }
849
850   template<class T>
851   void addValue(const T &V) {
852     assert(Location != 0 && "cl::location(...) not specified for a command "
853            "line option with external storage!");
854     Location->push_back(V);
855   }
856 };
857
858
859 // Define how to hold a class type object, such as a string.  Since we can
860 // inherit from a class, we do so.  This makes us exactly compatible with the
861 // object in all cases that it is used.
862 //
863 template<class DataType>
864 struct list_storage<DataType, bool> : public std::vector<DataType> {
865
866   template<class T>
867   void addValue(const T &V) { push_back(V); }
868 };
869
870
871 //===----------------------------------------------------------------------===//
872 // list - A list of command line options.
873 //
874 template <class DataType, class Storage = bool,
875           class ParserClass = parser<DataType> >
876 class list : public Option, public list_storage<DataType, Storage> {
877   ParserClass Parser;
878
879   virtual enum NumOccurrences getNumOccurrencesFlagDefault() const { 
880     return ZeroOrMore;
881   }
882   virtual enum ValueExpected getValueExpectedFlagDefault() const {
883     return Parser.getValueExpectedFlagDefault();
884   }
885
886   virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
887     typename ParserClass::parser_data_type Val;
888     if (Parser.parse(*this, ArgName, Arg, Val))
889       return true;  // Parse Error!
890     addValue(Val);
891     return false;
892   }
893
894   // Forward printing stuff to the parser...
895   virtual unsigned getOptionWidth() const {return Parser.getOptionWidth(*this);}
896   virtual void printOptionInfo(unsigned GlobalWidth) const {
897     Parser.printOptionInfo(*this, GlobalWidth);
898   }
899
900   void done() {
901     addArgument(ArgStr);
902     Parser.initialize(*this);
903   }
904 public:
905   ParserClass &getParser() { return Parser; }
906
907   // One option...
908   template<class M0t>
909   list(const M0t &M0) {
910     apply(M0, this);
911     done();
912   }
913   // Two options...
914   template<class M0t, class M1t>
915   list(const M0t &M0, const M1t &M1) {
916     apply(M0, this); apply(M1, this);
917     done();
918   }
919   // Three options...
920   template<class M0t, class M1t, class M2t>
921   list(const M0t &M0, const M1t &M1, const M2t &M2) {
922     apply(M0, this); apply(M1, this); apply(M2, this);
923     done();
924   }
925   // Four options...
926   template<class M0t, class M1t, class M2t, class M3t>
927   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) {
928     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
929     done();
930   }
931   // Five options...
932   template<class M0t, class M1t, class M2t, class M3t, class M4t>
933   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
934        const M4t &M4) {
935     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
936     apply(M4, this);
937     done();
938   }
939   // Six options...
940   template<class M0t, class M1t, class M2t, class M3t,
941            class M4t, class M5t>
942   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
943        const M4t &M4, const M5t &M5) {
944     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
945     apply(M4, this); apply(M5, this);
946     done();
947   }
948   // Seven options...
949   template<class M0t, class M1t, class M2t, class M3t,
950            class M4t, class M5t, class M6t>
951   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
952       const M4t &M4, const M5t &M5, const M6t &M6) {
953     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
954     apply(M4, this); apply(M5, this); apply(M6, this);
955     done();
956   }
957   // Eight options...
958   template<class M0t, class M1t, class M2t, class M3t,
959            class M4t, class M5t, class M6t, class M7t>
960   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
961       const M4t &M4, const M5t &M5, const M6t &M6, const M7t &M7) {
962     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
963     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
964     done();
965   }
966 };
967
968
969
970 //===----------------------------------------------------------------------===//
971 // Aliased command line option (alias this name to a preexisting name)
972 //
973
974 class alias : public Option {
975   Option *AliasFor;
976   virtual bool handleOccurrence(const char *ArgName, const std::string &Arg) {
977     return AliasFor->handleOccurrence(AliasFor->ArgStr, Arg);
978   }
979   // Aliases default to be hidden...
980   virtual enum OptionHidden getOptionHiddenFlagDefault() const {return Hidden;}
981
982   // Handle printing stuff...
983   virtual unsigned getOptionWidth() const;
984   virtual void printOptionInfo(unsigned GlobalWidth) const;
985
986   void done() {
987     if (!hasArgStr())
988       error(": cl::alias must have argument name specified!");
989     if (AliasFor == 0)
990       error(": cl::alias must have an cl::aliasopt(option) specified!");
991     addArgument(ArgStr);
992   }
993 public:
994   void setAliasFor(Option &O) {
995     if (AliasFor)
996       error(": cl::alias must only have one cl::aliasopt(...) specified!");
997     AliasFor = &O;
998   }
999
1000   // One option...
1001   template<class M0t>
1002   alias(const M0t &M0) : AliasFor(0) {
1003     apply(M0, this);
1004     done();
1005   }
1006   // Two options...
1007   template<class M0t, class M1t>
1008   alias(const M0t &M0, const M1t &M1) : AliasFor(0) {
1009     apply(M0, this); apply(M1, this);
1010     done();
1011   }
1012   // Three options...
1013   template<class M0t, class M1t, class M2t>
1014   alias(const M0t &M0, const M1t &M1, const M2t &M2) : AliasFor(0) {
1015     apply(M0, this); apply(M1, this); apply(M2, this);
1016     done();
1017   }
1018   // Four options...
1019   template<class M0t, class M1t, class M2t, class M3t>
1020   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1021     : AliasFor(0) {
1022     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1023     done();
1024   }
1025 };
1026
1027 // aliasfor - Modifier to set the option an alias aliases.
1028 struct aliasopt {
1029   Option &Opt;
1030   aliasopt(Option &O) : Opt(O) {}
1031   void apply(alias &A) const { A.setAliasFor(Opt); }
1032 };
1033
1034 } // End namespace cl
1035
1036 } // End namespace llvm
1037
1038 #endif