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