Comment out some unused parameter names to silence out-of-tree -Wunused warnings.
[oota-llvm.git] / include / llvm / Support / CommandLine.h
1 //===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 LLVM_SUPPORT_COMMANDLINE_H
21 #define LLVM_SUPPORT_COMMANDLINE_H
22
23 #include "llvm/Support/type_traits.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Twine.h"
27 #include <cassert>
28 #include <climits>
29 #include <cstdarg>
30 #include <utility>
31 #include <vector>
32
33 namespace llvm {
34
35 /// cl Namespace - This namespace contains all of the command line option
36 /// processing machinery.  It is intentionally a short name to make qualified
37 /// usage concise.
38 namespace cl {
39
40 //===----------------------------------------------------------------------===//
41 // ParseCommandLineOptions - Command line option processing entry point.
42 //
43 void ParseCommandLineOptions(int argc, char **argv,
44                              const char *Overview = 0,
45                              bool ReadResponseFiles = false);
46
47 //===----------------------------------------------------------------------===//
48 // ParseEnvironmentOptions - Environment variable option processing alternate
49 //                           entry point.
50 //
51 void ParseEnvironmentOptions(const char *progName, const char *envvar,
52                              const char *Overview = 0,
53                              bool ReadResponseFiles = false);
54
55 ///===---------------------------------------------------------------------===//
56 /// SetVersionPrinter - Override the default (LLVM specific) version printer
57 ///                     used to print out the version when --version is given
58 ///                     on the command line. This allows other systems using the
59 ///                     CommandLine utilities to print their own version string.
60 void SetVersionPrinter(void (*func)());
61
62
63 // PrintOptionValues - Print option values.
64 // With -print-options print the difference between option values and defaults.
65 // With -print-all-options print all option values.
66 // (Currently not perfect, but best-effort.)
67 void PrintOptionValues();
68
69 // MarkOptionsChanged - Internal helper function.
70 void MarkOptionsChanged();
71
72 //===----------------------------------------------------------------------===//
73 // Flags permitted to be passed to command line arguments
74 //
75
76 enum NumOccurrencesFlag {      // Flags for the number of occurrences allowed
77   Optional        = 0x01,      // Zero or One occurrence
78   ZeroOrMore      = 0x02,      // Zero or more occurrences allowed
79   Required        = 0x03,      // One occurrence required
80   OneOrMore       = 0x04,      // One or more occurrences required
81
82   // ConsumeAfter - Indicates that this option is fed anything that follows the
83   // last positional argument required by the application (it is an error if
84   // there are zero positional arguments, and a ConsumeAfter option is used).
85   // Thus, for example, all arguments to LLI are processed until a filename is
86   // found.  Once a filename is found, all of the succeeding arguments are
87   // passed, unprocessed, to the ConsumeAfter option.
88   //
89   ConsumeAfter    = 0x05,
90
91   OccurrencesMask  = 0x07
92 };
93
94 enum ValueExpected {           // Is a value required for the option?
95   ValueOptional   = 0x08,      // The value can appear... or not
96   ValueRequired   = 0x10,      // The value is required to appear!
97   ValueDisallowed = 0x18,      // A value may not be specified (for flags)
98   ValueMask       = 0x18
99 };
100
101 enum OptionHidden {            // Control whether -help shows this option
102   NotHidden       = 0x20,      // Option included in -help & -help-hidden
103   Hidden          = 0x40,      // -help doesn't, but -help-hidden does
104   ReallyHidden    = 0x60,      // Neither -help nor -help-hidden show this arg
105   HiddenMask      = 0x60
106 };
107
108 // Formatting flags - This controls special features that the option might have
109 // that cause it to be parsed differently...
110 //
111 // Prefix - This option allows arguments that are otherwise unrecognized to be
112 // matched by options that are a prefix of the actual value.  This is useful for
113 // cases like a linker, where options are typically of the form '-lfoo' or
114 // '-L../../include' where -l or -L are the actual flags.  When prefix is
115 // enabled, and used, the value for the flag comes from the suffix of the
116 // argument.
117 //
118 // Grouping - With this option enabled, multiple letter options are allowed to
119 // bunch together with only a single hyphen for the whole group.  This allows
120 // emulation of the behavior that ls uses for example: ls -la === ls -l -a
121 //
122
123 enum FormattingFlags {
124   NormalFormatting = 0x000,     // Nothing special
125   Positional       = 0x080,     // Is a positional argument, no '-' required
126   Prefix           = 0x100,     // Can this option directly prefix its value?
127   Grouping         = 0x180,     // Can this option group with other options?
128   FormattingMask   = 0x180      // Union of the above flags.
129 };
130
131 enum MiscFlags {               // Miscellaneous flags to adjust argument
132   CommaSeparated     = 0x200,  // Should this cl::list split between commas?
133   PositionalEatsArgs = 0x400,  // Should this positional cl::list eat -args?
134   Sink               = 0x800,  // Should this cl::list eat all unknown options?
135   MiscMask           = 0xE00   // Union of the above flags.
136 };
137
138
139
140 //===----------------------------------------------------------------------===//
141 // Option Base class
142 //
143 class alias;
144 class Option {
145   friend class alias;
146
147   // handleOccurrences - Overriden by subclasses to handle the value passed into
148   // an argument.  Should return true if there was an error processing the
149   // argument and the program should exit.
150   //
151   virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
152                                 StringRef Arg) = 0;
153
154   virtual enum ValueExpected getValueExpectedFlagDefault() const {
155     return ValueOptional;
156   }
157
158   // Out of line virtual function to provide home for the class.
159   virtual void anchor();
160
161   int NumOccurrences;     // The number of times specified
162   int Flags;              // Flags for the argument
163   unsigned Position;      // Position of last occurrence of the option
164   unsigned AdditionalVals;// Greater than 0 for multi-valued option.
165   Option *NextRegistered; // Singly linked list of registered options.
166 public:
167   const char *ArgStr;     // The argument string itself (ex: "help", "o")
168   const char *HelpStr;    // The descriptive text message for -help
169   const char *ValueStr;   // String describing what the value of this option is
170
171   inline enum NumOccurrencesFlag getNumOccurrencesFlag() const {
172     return static_cast<enum NumOccurrencesFlag>(Flags & OccurrencesMask);
173   }
174   inline enum ValueExpected getValueExpectedFlag() const {
175     int VE = Flags & ValueMask;
176     return VE ? static_cast<enum ValueExpected>(VE)
177               : getValueExpectedFlagDefault();
178   }
179   inline enum OptionHidden getOptionHiddenFlag() const {
180     return static_cast<enum OptionHidden>(Flags & HiddenMask);
181   }
182   inline enum FormattingFlags getFormattingFlag() const {
183     return static_cast<enum FormattingFlags>(Flags & FormattingMask);
184   }
185   inline unsigned getMiscFlags() const {
186     return Flags & MiscMask;
187   }
188   inline unsigned getPosition() const { return Position; }
189   inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
190
191   // hasArgStr - Return true if the argstr != ""
192   bool hasArgStr() const { return ArgStr[0] != 0; }
193
194   //-------------------------------------------------------------------------===
195   // Accessor functions set by OptionModifiers
196   //
197   void setArgStr(const char *S) { ArgStr = S; }
198   void setDescription(const char *S) { HelpStr = S; }
199   void setValueStr(const char *S) { ValueStr = S; }
200
201   void setFlag(unsigned Flag, unsigned FlagMask) {
202     Flags &= ~FlagMask;
203     Flags |= Flag;
204   }
205
206   void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) {
207     setFlag(Val, OccurrencesMask);
208   }
209   void setValueExpectedFlag(enum ValueExpected Val) { setFlag(Val, ValueMask); }
210   void setHiddenFlag(enum OptionHidden Val) { setFlag(Val, HiddenMask); }
211   void setFormattingFlag(enum FormattingFlags V) { setFlag(V, FormattingMask); }
212   void setMiscFlag(enum MiscFlags M) { setFlag(M, M); }
213   void setPosition(unsigned pos) { Position = pos; }
214 protected:
215   explicit Option(unsigned DefaultFlags)
216     : NumOccurrences(0), Flags(DefaultFlags | NormalFormatting), Position(0),
217       AdditionalVals(0), NextRegistered(0),
218       ArgStr(""), HelpStr(""), ValueStr("") {
219     assert(getNumOccurrencesFlag() != 0 &&
220            getOptionHiddenFlag() != 0 && "Not all default flags specified!");
221   }
222
223   inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }
224 public:
225   // addArgument - Register this argument with the commandline system.
226   //
227   void addArgument();
228
229   Option *getNextRegisteredOption() const { return NextRegistered; }
230
231   // Return the width of the option tag for printing...
232   virtual size_t getOptionWidth() const = 0;
233
234   // printOptionInfo - Print out information about this option.  The
235   // to-be-maintained width is specified.
236   //
237   virtual void printOptionInfo(size_t GlobalWidth) const = 0;
238
239   virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
240
241   virtual void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
242
243   // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
244   //
245   bool addOccurrence(unsigned pos, StringRef ArgName,
246                      StringRef Value, bool MultiArg = false);
247
248   // Prints option name followed by message.  Always returns true.
249   bool error(const Twine &Message, StringRef ArgName = StringRef());
250
251 public:
252   inline int getNumOccurrences() const { return NumOccurrences; }
253   virtual ~Option() {}
254 };
255
256
257 //===----------------------------------------------------------------------===//
258 // Command line option modifiers that can be used to modify the behavior of
259 // command line option parsers...
260 //
261
262 // desc - Modifier to set the description shown in the -help output...
263 struct desc {
264   const char *Desc;
265   desc(const char *Str) : Desc(Str) {}
266   void apply(Option &O) const { O.setDescription(Desc); }
267 };
268
269 // value_desc - Modifier to set the value description shown in the -help
270 // output...
271 struct value_desc {
272   const char *Desc;
273   value_desc(const char *Str) : Desc(Str) {}
274   void apply(Option &O) const { O.setValueStr(Desc); }
275 };
276
277 // init - Specify a default (initial) value for the command line argument, if
278 // the default constructor for the argument type does not give you what you
279 // want.  This is only valid on "opt" arguments, not on "list" arguments.
280 //
281 template<class Ty>
282 struct initializer {
283   const Ty &Init;
284   initializer(const Ty &Val) : Init(Val) {}
285
286   template<class Opt>
287   void apply(Opt &O) const { O.setInitialValue(Init); }
288 };
289
290 template<class Ty>
291 initializer<Ty> init(const Ty &Val) {
292   return initializer<Ty>(Val);
293 }
294
295
296 // location - Allow the user to specify which external variable they want to
297 // store the results of the command line argument processing into, if they don't
298 // want to store it in the option itself.
299 //
300 template<class Ty>
301 struct LocationClass {
302   Ty &Loc;
303   LocationClass(Ty &L) : Loc(L) {}
304
305   template<class Opt>
306   void apply(Opt &O) const { O.setLocation(O, Loc); }
307 };
308
309 template<class Ty>
310 LocationClass<Ty> location(Ty &L) { return LocationClass<Ty>(L); }
311
312
313 //===----------------------------------------------------------------------===//
314 // OptionValue class
315
316 // Support value comparison outside the template.
317 struct GenericOptionValue {
318   virtual ~GenericOptionValue() {}
319   virtual bool compare(const GenericOptionValue &V) const = 0;
320 };
321
322 template<class DataType> struct OptionValue;
323
324 // The default value safely does nothing. Option value printing is only
325 // best-effort.
326 template<class DataType, bool isClass>
327 struct OptionValueBase : public GenericOptionValue {
328   // Temporary storage for argument passing.
329   typedef OptionValue<DataType> WrapperType;
330
331   bool hasValue() const { return false; }
332
333   const DataType &getValue() const { assert(false && "no default value"); }
334
335   // Some options may take their value from a different data type.
336   template<class DT>
337   void setValue(const DT& /*V*/) {}
338
339   bool compare(const DataType &/*V*/) const { return false; }
340
341   virtual bool compare(const GenericOptionValue& /*V*/) const { return false; }
342 };
343
344 // Simple copy of the option value.
345 template<class DataType>
346 class OptionValueCopy : public GenericOptionValue {
347   DataType Value;
348   bool Valid;
349 public:
350   OptionValueCopy() : Valid(false) {}
351
352   bool hasValue() const { return Valid; }
353
354   const DataType &getValue() const {
355     assert(Valid && "invalid option value");
356     return Value;
357   }
358
359   void setValue(const DataType &V) { Valid = true; Value = V; }
360
361   bool compare(const DataType &V) const {
362     return Valid && (Value != V);
363   }
364
365   virtual bool compare(const GenericOptionValue &V) const {
366     const OptionValueCopy<DataType> &VC =
367       static_cast< const OptionValueCopy<DataType>& >(V);
368     if (!VC.hasValue()) return false;
369     return compare(VC.getValue());
370   }
371 };
372
373 // Non-class option values.
374 template<class DataType>
375 struct OptionValueBase<DataType, false> : OptionValueCopy<DataType> {
376   typedef DataType WrapperType;
377 };
378
379 // Top-level option class.
380 template<class DataType>
381 struct OptionValue : OptionValueBase<DataType, is_class<DataType>::value> {
382   OptionValue() {}
383
384   OptionValue(const DataType& V) {
385     this->setValue(V);
386   }
387   // Some options may take their value from a different data type.
388   template<class DT>
389   OptionValue<DataType> &operator=(const DT& V) {
390     this->setValue(V);
391     return *this;
392   }
393 };
394
395 // Other safe-to-copy-by-value common option types.
396 enum boolOrDefault { BOU_UNSET, BOU_TRUE, BOU_FALSE };
397 template<>
398 struct OptionValue<cl::boolOrDefault> : OptionValueCopy<cl::boolOrDefault> {
399   typedef cl::boolOrDefault WrapperType;
400
401   OptionValue() {}
402
403   OptionValue(const cl::boolOrDefault& V) {
404     this->setValue(V);
405   }
406   OptionValue<cl::boolOrDefault> &operator=(const cl::boolOrDefault& V) {
407     setValue(V);
408     return *this;
409   }
410 };
411
412 template<>
413 struct OptionValue<std::string> : OptionValueCopy<std::string> {
414   typedef StringRef WrapperType;
415
416   OptionValue() {}
417
418   OptionValue(const std::string& V) {
419     this->setValue(V);
420   }
421   OptionValue<std::string> &operator=(const std::string& V) {
422     setValue(V);
423     return *this;
424   }
425 };
426
427 //===----------------------------------------------------------------------===//
428 // Enum valued command line option
429 //
430 #define clEnumVal(ENUMVAL, DESC) #ENUMVAL, int(ENUMVAL), DESC
431 #define clEnumValN(ENUMVAL, FLAGNAME, DESC) FLAGNAME, int(ENUMVAL), DESC
432 #define clEnumValEnd (reinterpret_cast<void*>(0))
433
434 // values - For custom data types, allow specifying a group of values together
435 // as the values that go into the mapping that the option handler uses.  Note
436 // that the values list must always have a 0 at the end of the list to indicate
437 // that the list has ended.
438 //
439 template<class DataType>
440 class ValuesClass {
441   // Use a vector instead of a map, because the lists should be short,
442   // the overhead is less, and most importantly, it keeps them in the order
443   // inserted so we can print our option out nicely.
444   SmallVector<std::pair<const char *, std::pair<int, const char *> >,4> Values;
445   void processValues(va_list Vals);
446 public:
447   ValuesClass(const char *EnumName, DataType Val, const char *Desc,
448               va_list ValueArgs) {
449     // Insert the first value, which is required.
450     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
451
452     // Process the varargs portion of the values...
453     while (const char *enumName = va_arg(ValueArgs, const char *)) {
454       DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
455       const char *EnumDesc = va_arg(ValueArgs, const char *);
456       Values.push_back(std::make_pair(enumName,      // Add value to value map
457                                       std::make_pair(EnumVal, EnumDesc)));
458     }
459   }
460
461   template<class Opt>
462   void apply(Opt &O) const {
463     for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
464          i != e; ++i)
465       O.getParser().addLiteralOption(Values[i].first, Values[i].second.first,
466                                      Values[i].second.second);
467   }
468 };
469
470 template<class DataType>
471 ValuesClass<DataType> END_WITH_NULL values(const char *Arg, DataType Val,
472                                            const char *Desc, ...) {
473     va_list ValueArgs;
474     va_start(ValueArgs, Desc);
475     ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
476     va_end(ValueArgs);
477     return Vals;
478 }
479
480 //===----------------------------------------------------------------------===//
481 // parser class - Parameterizable parser for different data types.  By default,
482 // known data types (string, int, bool) have specialized parsers, that do what
483 // you would expect.  The default parser, used for data types that are not
484 // built-in, uses a mapping table to map specific options to values, which is
485 // used, among other things, to handle enum types.
486
487 //--------------------------------------------------
488 // generic_parser_base - This class holds all the non-generic code that we do
489 // not need replicated for every instance of the generic parser.  This also
490 // allows us to put stuff into CommandLine.cpp
491 //
492 class generic_parser_base {
493 protected:
494   class GenericOptionInfo {
495   public:
496     GenericOptionInfo(const char *name, const char *helpStr) :
497       Name(name), HelpStr(helpStr) {}
498     const char *Name;
499     const char *HelpStr;
500   };
501 public:
502   virtual ~generic_parser_base() {}  // Base class should have virtual-dtor
503
504   // getNumOptions - Virtual function implemented by generic subclass to
505   // indicate how many entries are in Values.
506   //
507   virtual unsigned getNumOptions() const = 0;
508
509   // getOption - Return option name N.
510   virtual const char *getOption(unsigned N) const = 0;
511
512   // getDescription - Return description N
513   virtual const char *getDescription(unsigned N) const = 0;
514
515   // Return the width of the option tag for printing...
516   virtual size_t getOptionWidth(const Option &O) const;
517
518   virtual const GenericOptionValue &getOptionValue(unsigned N) const = 0;
519
520   // printOptionInfo - Print out information about this option.  The
521   // to-be-maintained width is specified.
522   //
523   virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const;
524
525   void printGenericOptionDiff(const Option &O, const GenericOptionValue &V,
526                               const GenericOptionValue &Default,
527                               size_t GlobalWidth) const;
528
529   // printOptionDiff - print the value of an option and it's default.
530   //
531   // Template definition ensures that the option and default have the same
532   // DataType (via the same AnyOptionValue).
533   template<class AnyOptionValue>
534   void printOptionDiff(const Option &O, const AnyOptionValue &V,
535                        const AnyOptionValue &Default,
536                        size_t GlobalWidth) const {
537     printGenericOptionDiff(O, V, Default, GlobalWidth);
538   }
539
540   void initialize(Option &O) {
541     // All of the modifiers for the option have been processed by now, so the
542     // argstr field should be stable, copy it down now.
543     //
544     hasArgStr = O.hasArgStr();
545   }
546
547   void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
548     // If there has been no argstr specified, that means that we need to add an
549     // argument for every possible option.  This ensures that our options are
550     // vectored to us.
551     if (!hasArgStr)
552       for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
553         OptionNames.push_back(getOption(i));
554   }
555
556
557   enum ValueExpected getValueExpectedFlagDefault() const {
558     // If there is an ArgStr specified, then we are of the form:
559     //
560     //    -opt=O2   or   -opt O2  or  -optO2
561     //
562     // In which case, the value is required.  Otherwise if an arg str has not
563     // been specified, we are of the form:
564     //
565     //    -O2 or O2 or -la (where -l and -a are separate options)
566     //
567     // If this is the case, we cannot allow a value.
568     //
569     if (hasArgStr)
570       return ValueRequired;
571     else
572       return ValueDisallowed;
573   }
574
575   // findOption - Return the option number corresponding to the specified
576   // argument string.  If the option is not found, getNumOptions() is returned.
577   //
578   unsigned findOption(const char *Name);
579
580 protected:
581   bool hasArgStr;
582 };
583
584 // Default parser implementation - This implementation depends on having a
585 // mapping of recognized options to values of some sort.  In addition to this,
586 // each entry in the mapping also tracks a help message that is printed with the
587 // command line option for -help.  Because this is a simple mapping parser, the
588 // data type can be any unsupported type.
589 //
590 template <class DataType>
591 class parser : public generic_parser_base {
592 protected:
593   class OptionInfo : public GenericOptionInfo {
594   public:
595     OptionInfo(const char *name, DataType v, const char *helpStr) :
596       GenericOptionInfo(name, helpStr), V(v) {}
597     OptionValue<DataType> V;
598   };
599   SmallVector<OptionInfo, 8> Values;
600 public:
601   typedef DataType parser_data_type;
602
603   // Implement virtual functions needed by generic_parser_base
604   unsigned getNumOptions() const { return unsigned(Values.size()); }
605   const char *getOption(unsigned N) const { return Values[N].Name; }
606   const char *getDescription(unsigned N) const {
607     return Values[N].HelpStr;
608   }
609
610   // getOptionValue - Return the value of option name N.
611   virtual const GenericOptionValue &getOptionValue(unsigned N) const {
612     return Values[N].V;
613   }
614
615   // parse - Return true on error.
616   bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V) {
617     StringRef ArgVal;
618     if (hasArgStr)
619       ArgVal = Arg;
620     else
621       ArgVal = ArgName;
622
623     for (unsigned i = 0, e = static_cast<unsigned>(Values.size());
624          i != e; ++i)
625       if (Values[i].Name == ArgVal) {
626         V = Values[i].V.getValue();
627         return false;
628       }
629
630     return O.error("Cannot find option named '" + ArgVal + "'!");
631   }
632
633   /// addLiteralOption - Add an entry to the mapping table.
634   ///
635   template <class DT>
636   void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
637     assert(findOption(Name) == Values.size() && "Option already exists!");
638     OptionInfo X(Name, static_cast<DataType>(V), HelpStr);
639     Values.push_back(X);
640     MarkOptionsChanged();
641   }
642
643   /// removeLiteralOption - Remove the specified option.
644   ///
645   void removeLiteralOption(const char *Name) {
646     unsigned N = findOption(Name);
647     assert(N != Values.size() && "Option not found!");
648     Values.erase(Values.begin()+N);
649   }
650 };
651
652 //--------------------------------------------------
653 // basic_parser - Super class of parsers to provide boilerplate code
654 //
655 class basic_parser_impl {  // non-template implementation of basic_parser<t>
656 public:
657   virtual ~basic_parser_impl() {}
658
659   enum ValueExpected getValueExpectedFlagDefault() const {
660     return ValueRequired;
661   }
662
663   void getExtraOptionNames(SmallVectorImpl<const char*> &) {}
664
665   void initialize(Option &) {}
666
667   // Return the width of the option tag for printing...
668   size_t getOptionWidth(const Option &O) const;
669
670   // printOptionInfo - Print out information about this option.  The
671   // to-be-maintained width is specified.
672   //
673   void printOptionInfo(const Option &O, size_t GlobalWidth) const;
674
675   // printOptionNoValue - Print a placeholder for options that don't yet support
676   // printOptionDiff().
677   void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
678
679   // getValueName - Overload in subclass to provide a better default value.
680   virtual const char *getValueName() const { return "value"; }
681
682   // An out-of-line virtual method to provide a 'home' for this class.
683   virtual void anchor();
684
685 protected:
686   // A helper for basic_parser::printOptionDiff.
687   void printOptionName(const Option &O, size_t GlobalWidth) const;
688 };
689
690 // basic_parser - The real basic parser is just a template wrapper that provides
691 // a typedef for the provided data type.
692 //
693 template<class DataType>
694 class basic_parser : public basic_parser_impl {
695 public:
696   typedef DataType parser_data_type;
697   typedef OptionValue<DataType> OptVal;
698 };
699
700 //--------------------------------------------------
701 // parser<bool>
702 //
703 template<>
704 class parser<bool> : public basic_parser<bool> {
705   const char *ArgStr;
706 public:
707
708   // parse - Return true on error.
709   bool parse(Option &O, StringRef ArgName, StringRef Arg, bool &Val);
710
711   template <class Opt>
712   void initialize(Opt &O) {
713     ArgStr = O.ArgStr;
714   }
715
716   enum ValueExpected getValueExpectedFlagDefault() const {
717     return ValueOptional;
718   }
719
720   // getValueName - Do not print =<value> at all.
721   virtual const char *getValueName() const { return 0; }
722
723   void printOptionDiff(const Option &O, bool V, OptVal Default,
724                        size_t GlobalWidth) const;
725
726   // An out-of-line virtual method to provide a 'home' for this class.
727   virtual void anchor();
728 };
729
730 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<bool>);
731
732 //--------------------------------------------------
733 // parser<boolOrDefault>
734 template<>
735 class parser<boolOrDefault> : public basic_parser<boolOrDefault> {
736 public:
737   // parse - Return true on error.
738   bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val);
739
740   enum ValueExpected getValueExpectedFlagDefault() const {
741     return ValueOptional;
742   }
743
744   // getValueName - Do not print =<value> at all.
745   virtual const char *getValueName() const { return 0; }
746
747   void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
748                        size_t GlobalWidth) const;
749
750   // An out-of-line virtual method to provide a 'home' for this class.
751   virtual void anchor();
752 };
753
754 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<boolOrDefault>);
755
756 //--------------------------------------------------
757 // parser<int>
758 //
759 template<>
760 class parser<int> : public basic_parser<int> {
761 public:
762   // parse - Return true on error.
763   bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
764
765   // getValueName - Overload in subclass to provide a better default value.
766   virtual const char *getValueName() const { return "int"; }
767
768   void printOptionDiff(const Option &O, int V, OptVal Default,
769                        size_t GlobalWidth) const;
770
771   // An out-of-line virtual method to provide a 'home' for this class.
772   virtual void anchor();
773 };
774
775 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<int>);
776
777
778 //--------------------------------------------------
779 // parser<unsigned>
780 //
781 template<>
782 class parser<unsigned> : public basic_parser<unsigned> {
783 public:
784   // parse - Return true on error.
785   bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
786
787   // getValueName - Overload in subclass to provide a better default value.
788   virtual const char *getValueName() const { return "uint"; }
789
790   void printOptionDiff(const Option &O, unsigned V, OptVal Default,
791                        size_t GlobalWidth) const;
792
793   // An out-of-line virtual method to provide a 'home' for this class.
794   virtual void anchor();
795 };
796
797 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
798
799 //--------------------------------------------------
800 // parser<double>
801 //
802 template<>
803 class parser<double> : public basic_parser<double> {
804 public:
805   // parse - Return true on error.
806   bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
807
808   // getValueName - Overload in subclass to provide a better default value.
809   virtual const char *getValueName() const { return "number"; }
810
811   void printOptionDiff(const Option &O, double V, OptVal Default,
812                        size_t GlobalWidth) const;
813
814   // An out-of-line virtual method to provide a 'home' for this class.
815   virtual void anchor();
816 };
817
818 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<double>);
819
820 //--------------------------------------------------
821 // parser<float>
822 //
823 template<>
824 class parser<float> : public basic_parser<float> {
825 public:
826   // parse - Return true on error.
827   bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
828
829   // getValueName - Overload in subclass to provide a better default value.
830   virtual const char *getValueName() const { return "number"; }
831
832   void printOptionDiff(const Option &O, float V, OptVal Default,
833                        size_t GlobalWidth) const;
834
835   // An out-of-line virtual method to provide a 'home' for this class.
836   virtual void anchor();
837 };
838
839 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<float>);
840
841 //--------------------------------------------------
842 // parser<std::string>
843 //
844 template<>
845 class parser<std::string> : public basic_parser<std::string> {
846 public:
847   // parse - Return true on error.
848   bool parse(Option &, StringRef, StringRef Arg, std::string &Value) {
849     Value = Arg.str();
850     return false;
851   }
852
853   // getValueName - Overload in subclass to provide a better default value.
854   virtual const char *getValueName() const { return "string"; }
855
856   void printOptionDiff(const Option &O, StringRef V, OptVal Default,
857                        size_t GlobalWidth) const;
858
859   // An out-of-line virtual method to provide a 'home' for this class.
860   virtual void anchor();
861 };
862
863 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
864
865 //--------------------------------------------------
866 // parser<char>
867 //
868 template<>
869 class parser<char> : public basic_parser<char> {
870 public:
871   // parse - Return true on error.
872   bool parse(Option &, StringRef, StringRef Arg, char &Value) {
873     Value = Arg[0];
874     return false;
875   }
876
877   // getValueName - Overload in subclass to provide a better default value.
878   virtual const char *getValueName() const { return "char"; }
879
880   void printOptionDiff(const Option &O, char V, OptVal Default,
881                        size_t GlobalWidth) const;
882
883   // An out-of-line virtual method to provide a 'home' for this class.
884   virtual void anchor();
885 };
886
887 EXTERN_TEMPLATE_INSTANTIATION(class basic_parser<char>);
888
889 //--------------------------------------------------
890 // PrintOptionDiff
891 //
892 // This collection of wrappers is the intermediary between class opt and class
893 // parser to handle all the template nastiness.
894
895 // This overloaded function is selected by the generic parser.
896 template<class ParserClass, class DT>
897 void printOptionDiff(const Option &O, const generic_parser_base &P, const DT &V,
898                      const OptionValue<DT> &Default, size_t GlobalWidth) {
899   OptionValue<DT> OV = V;
900   P.printOptionDiff(O, OV, Default, GlobalWidth);
901 }
902
903 // This is instantiated for basic parsers when the parsed value has a different
904 // type than the option value. e.g. HelpPrinter.
905 template<class ParserDT, class ValDT>
906 struct OptionDiffPrinter {
907   void print(const Option &O, const parser<ParserDT> P, const ValDT &/*V*/,
908              const OptionValue<ValDT> &/*Default*/, size_t GlobalWidth) {
909     P.printOptionNoValue(O, GlobalWidth);
910   }
911 };
912
913 // This is instantiated for basic parsers when the parsed value has the same
914 // type as the option value.
915 template<class DT>
916 struct OptionDiffPrinter<DT, DT> {
917   void print(const Option &O, const parser<DT> P, const DT &V,
918              const OptionValue<DT> &Default, size_t GlobalWidth) {
919     P.printOptionDiff(O, V, Default, GlobalWidth);
920   }
921 };
922
923 // This overloaded function is selected by the basic parser, which may parse a
924 // different type than the option type.
925 template<class ParserClass, class ValDT>
926 void printOptionDiff(
927   const Option &O,
928   const basic_parser<typename ParserClass::parser_data_type> &P,
929   const ValDT &V, const OptionValue<ValDT> &Default,
930   size_t GlobalWidth) {
931
932   OptionDiffPrinter<typename ParserClass::parser_data_type, ValDT> printer;
933   printer.print(O, static_cast<const ParserClass&>(P), V, Default,
934                 GlobalWidth);
935 }
936
937 //===----------------------------------------------------------------------===//
938 // applicator class - This class is used because we must use partial
939 // specialization to handle literal string arguments specially (const char* does
940 // not correctly respond to the apply method).  Because the syntax to use this
941 // is a pain, we have the 'apply' method below to handle the nastiness...
942 //
943 template<class Mod> struct applicator {
944   template<class Opt>
945   static void opt(const Mod &M, Opt &O) { M.apply(O); }
946 };
947
948 // Handle const char* as a special case...
949 template<unsigned n> struct applicator<char[n]> {
950   template<class Opt>
951   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
952 };
953 template<unsigned n> struct applicator<const char[n]> {
954   template<class Opt>
955   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
956 };
957 template<> struct applicator<const char*> {
958   template<class Opt>
959   static void opt(const char *Str, Opt &O) { O.setArgStr(Str); }
960 };
961
962 template<> struct applicator<NumOccurrencesFlag> {
963   static void opt(NumOccurrencesFlag NO, Option &O) {
964     O.setNumOccurrencesFlag(NO);
965   }
966 };
967 template<> struct applicator<ValueExpected> {
968   static void opt(ValueExpected VE, Option &O) { O.setValueExpectedFlag(VE); }
969 };
970 template<> struct applicator<OptionHidden> {
971   static void opt(OptionHidden OH, Option &O) { O.setHiddenFlag(OH); }
972 };
973 template<> struct applicator<FormattingFlags> {
974   static void opt(FormattingFlags FF, Option &O) { O.setFormattingFlag(FF); }
975 };
976 template<> struct applicator<MiscFlags> {
977   static void opt(MiscFlags MF, Option &O) { O.setMiscFlag(MF); }
978 };
979
980 // apply method - Apply a modifier to an option in a type safe way.
981 template<class Mod, class Opt>
982 void apply(const Mod &M, Opt *O) {
983   applicator<Mod>::opt(M, *O);
984 }
985
986 //===----------------------------------------------------------------------===//
987 // opt_storage class
988
989 // Default storage class definition: external storage.  This implementation
990 // assumes the user will specify a variable to store the data into with the
991 // cl::location(x) modifier.
992 //
993 template<class DataType, bool ExternalStorage, bool isClass>
994 class opt_storage {
995   DataType *Location;   // Where to store the object...
996   OptionValue<DataType> Default;
997
998   void check() const {
999     assert(Location != 0 && "cl::location(...) not specified for a command "
1000            "line option with external storage, "
1001            "or cl::init specified before cl::location()!!");
1002   }
1003 public:
1004   opt_storage() : Location(0) {}
1005
1006   bool setLocation(Option &O, DataType &L) {
1007     if (Location)
1008       return O.error("cl::location(x) specified more than once!");
1009     Location = &L;
1010     Default = L;
1011     return false;
1012   }
1013
1014   template<class T>
1015   void setValue(const T &V, bool initial = false) {
1016     check();
1017     *Location = V;
1018     if (initial)
1019       Default = V;
1020   }
1021
1022   DataType &getValue() { check(); return *Location; }
1023   const DataType &getValue() const { check(); return *Location; }
1024
1025   operator DataType() const { return this->getValue(); }
1026
1027   const OptionValue<DataType> &getDefault() const { return Default; }
1028 };
1029
1030 // Define how to hold a class type object, such as a string.  Since we can
1031 // inherit from a class, we do so.  This makes us exactly compatible with the
1032 // object in all cases that it is used.
1033 //
1034 template<class DataType>
1035 class opt_storage<DataType,false,true> : public DataType {
1036 public:
1037   OptionValue<DataType> Default;
1038
1039   template<class T>
1040   void setValue(const T &V, bool initial = false) {
1041     DataType::operator=(V);
1042     if (initial)
1043       Default = V;
1044   }
1045
1046   DataType &getValue() { return *this; }
1047   const DataType &getValue() const { return *this; }
1048
1049   const OptionValue<DataType> &getDefault() const { return Default; }
1050 };
1051
1052 // Define a partial specialization to handle things we cannot inherit from.  In
1053 // this case, we store an instance through containment, and overload operators
1054 // to get at the value.
1055 //
1056 template<class DataType>
1057 class opt_storage<DataType, false, false> {
1058 public:
1059   DataType Value;
1060   OptionValue<DataType> Default;
1061
1062   // Make sure we initialize the value with the default constructor for the
1063   // type.
1064   opt_storage() : Value(DataType()) {}
1065
1066   template<class T>
1067   void setValue(const T &V, bool initial = false) {
1068     Value = V;
1069     if (initial)
1070       Default = V;
1071   }
1072   DataType &getValue() { return Value; }
1073   DataType getValue() const { return Value; }
1074
1075   const OptionValue<DataType> &getDefault() const { return Default; }
1076
1077   operator DataType() const { return getValue(); }
1078
1079   // If the datatype is a pointer, support -> on it.
1080   DataType operator->() const { return Value; }
1081 };
1082
1083
1084 //===----------------------------------------------------------------------===//
1085 // opt - A scalar command line option.
1086 //
1087 template <class DataType, bool ExternalStorage = false,
1088           class ParserClass = parser<DataType> >
1089 class opt : public Option,
1090             public opt_storage<DataType, ExternalStorage,
1091                                is_class<DataType>::value> {
1092   ParserClass Parser;
1093
1094   virtual bool handleOccurrence(unsigned pos, StringRef ArgName,
1095                                 StringRef Arg) {
1096     typename ParserClass::parser_data_type Val =
1097        typename ParserClass::parser_data_type();
1098     if (Parser.parse(*this, ArgName, Arg, Val))
1099       return true;                            // Parse error!
1100     this->setValue(Val);
1101     this->setPosition(pos);
1102     return false;
1103   }
1104
1105   virtual enum ValueExpected getValueExpectedFlagDefault() const {
1106     return Parser.getValueExpectedFlagDefault();
1107   }
1108   virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
1109     return Parser.getExtraOptionNames(OptionNames);
1110   }
1111
1112   // Forward printing stuff to the parser...
1113   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1114   virtual void printOptionInfo(size_t GlobalWidth) const {
1115     Parser.printOptionInfo(*this, GlobalWidth);
1116   }
1117
1118   virtual void printOptionValue(size_t GlobalWidth, bool Force) const {
1119     if (Force || this->getDefault().compare(this->getValue())) {
1120       cl::printOptionDiff<ParserClass>(
1121         *this, Parser, this->getValue(), this->getDefault(), GlobalWidth);
1122     }
1123   }
1124
1125   void done() {
1126     addArgument();
1127     Parser.initialize(*this);
1128   }
1129 public:
1130   // setInitialValue - Used by the cl::init modifier...
1131   void setInitialValue(const DataType &V) { this->setValue(V, true); }
1132
1133   ParserClass &getParser() { return Parser; }
1134
1135   template<class T>
1136   DataType &operator=(const T &Val) {
1137     this->setValue(Val);
1138     return this->getValue();
1139   }
1140
1141   // One option...
1142   template<class M0t>
1143   explicit opt(const M0t &M0) : Option(Optional | NotHidden) {
1144     apply(M0, this);
1145     done();
1146   }
1147
1148   // Two options...
1149   template<class M0t, class M1t>
1150   opt(const M0t &M0, const M1t &M1) : Option(Optional | NotHidden) {
1151     apply(M0, this); apply(M1, this);
1152     done();
1153   }
1154
1155   // Three options...
1156   template<class M0t, class M1t, class M2t>
1157   opt(const M0t &M0, const M1t &M1,
1158       const M2t &M2) : Option(Optional | NotHidden) {
1159     apply(M0, this); apply(M1, this); apply(M2, this);
1160     done();
1161   }
1162   // Four options...
1163   template<class M0t, class M1t, class M2t, class M3t>
1164   opt(const M0t &M0, const M1t &M1, const M2t &M2,
1165       const M3t &M3) : Option(Optional | NotHidden) {
1166     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1167     done();
1168   }
1169   // Five options...
1170   template<class M0t, class M1t, class M2t, class M3t, class M4t>
1171   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1172       const M4t &M4) : Option(Optional | NotHidden) {
1173     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1174     apply(M4, this);
1175     done();
1176   }
1177   // Six options...
1178   template<class M0t, class M1t, class M2t, class M3t,
1179            class M4t, class M5t>
1180   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1181       const M4t &M4, const M5t &M5) : Option(Optional | NotHidden) {
1182     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1183     apply(M4, this); apply(M5, this);
1184     done();
1185   }
1186   // Seven options...
1187   template<class M0t, class M1t, class M2t, class M3t,
1188            class M4t, class M5t, class M6t>
1189   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1190       const M4t &M4, const M5t &M5,
1191       const M6t &M6) : Option(Optional | NotHidden) {
1192     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1193     apply(M4, this); apply(M5, this); apply(M6, this);
1194     done();
1195   }
1196   // Eight options...
1197   template<class M0t, class M1t, class M2t, class M3t,
1198            class M4t, class M5t, class M6t, class M7t>
1199   opt(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1200       const M4t &M4, const M5t &M5, const M6t &M6,
1201       const M7t &M7) : Option(Optional | NotHidden) {
1202     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1203     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1204     done();
1205   }
1206 };
1207
1208 EXTERN_TEMPLATE_INSTANTIATION(class opt<unsigned>);
1209 EXTERN_TEMPLATE_INSTANTIATION(class opt<int>);
1210 EXTERN_TEMPLATE_INSTANTIATION(class opt<std::string>);
1211 EXTERN_TEMPLATE_INSTANTIATION(class opt<char>);
1212 EXTERN_TEMPLATE_INSTANTIATION(class opt<bool>);
1213
1214 //===----------------------------------------------------------------------===//
1215 // list_storage class
1216
1217 // Default storage class definition: external storage.  This implementation
1218 // assumes the user will specify a variable to store the data into with the
1219 // cl::location(x) modifier.
1220 //
1221 template<class DataType, class StorageClass>
1222 class list_storage {
1223   StorageClass *Location;   // Where to store the object...
1224
1225 public:
1226   list_storage() : Location(0) {}
1227
1228   bool setLocation(Option &O, StorageClass &L) {
1229     if (Location)
1230       return O.error("cl::location(x) specified more than once!");
1231     Location = &L;
1232     return false;
1233   }
1234
1235   template<class T>
1236   void addValue(const T &V) {
1237     assert(Location != 0 && "cl::location(...) not specified for a command "
1238            "line option with external storage!");
1239     Location->push_back(V);
1240   }
1241 };
1242
1243
1244 // Define how to hold a class type object, such as a string.  Since we can
1245 // inherit from a class, we do so.  This makes us exactly compatible with the
1246 // object in all cases that it is used.
1247 //
1248 template<class DataType>
1249 class list_storage<DataType, bool> : public std::vector<DataType> {
1250 public:
1251   template<class T>
1252   void addValue(const T &V) { std::vector<DataType>::push_back(V); }
1253 };
1254
1255
1256 //===----------------------------------------------------------------------===//
1257 // list - A list of command line options.
1258 //
1259 template <class DataType, class Storage = bool,
1260           class ParserClass = parser<DataType> >
1261 class list : public Option, public list_storage<DataType, Storage> {
1262   std::vector<unsigned> Positions;
1263   ParserClass Parser;
1264
1265   virtual enum ValueExpected getValueExpectedFlagDefault() const {
1266     return Parser.getValueExpectedFlagDefault();
1267   }
1268   virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
1269     return Parser.getExtraOptionNames(OptionNames);
1270   }
1271
1272   virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
1273     typename ParserClass::parser_data_type Val =
1274       typename ParserClass::parser_data_type();
1275     if (Parser.parse(*this, ArgName, Arg, Val))
1276       return true;  // Parse Error!
1277     list_storage<DataType, Storage>::addValue(Val);
1278     setPosition(pos);
1279     Positions.push_back(pos);
1280     return false;
1281   }
1282
1283   // Forward printing stuff to the parser...
1284   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1285   virtual void printOptionInfo(size_t GlobalWidth) const {
1286     Parser.printOptionInfo(*this, GlobalWidth);
1287   }
1288
1289   // Unimplemented: list options don't currently store their default value.
1290   virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {}
1291
1292   void done() {
1293     addArgument();
1294     Parser.initialize(*this);
1295   }
1296 public:
1297   ParserClass &getParser() { return Parser; }
1298
1299   unsigned getPosition(unsigned optnum) const {
1300     assert(optnum < this->size() && "Invalid option index");
1301     return Positions[optnum];
1302   }
1303
1304   void setNumAdditionalVals(unsigned n) {
1305     Option::setNumAdditionalVals(n);
1306   }
1307
1308   // One option...
1309   template<class M0t>
1310   explicit list(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1311     apply(M0, this);
1312     done();
1313   }
1314   // Two options...
1315   template<class M0t, class M1t>
1316   list(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1317     apply(M0, this); apply(M1, this);
1318     done();
1319   }
1320   // Three options...
1321   template<class M0t, class M1t, class M2t>
1322   list(const M0t &M0, const M1t &M1, const M2t &M2)
1323     : Option(ZeroOrMore | NotHidden) {
1324     apply(M0, this); apply(M1, this); apply(M2, this);
1325     done();
1326   }
1327   // Four options...
1328   template<class M0t, class M1t, class M2t, class M3t>
1329   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1330     : Option(ZeroOrMore | NotHidden) {
1331     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1332     done();
1333   }
1334   // Five options...
1335   template<class M0t, class M1t, class M2t, class M3t, class M4t>
1336   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1337        const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1338     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1339     apply(M4, this);
1340     done();
1341   }
1342   // Six options...
1343   template<class M0t, class M1t, class M2t, class M3t,
1344            class M4t, class M5t>
1345   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1346        const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1347     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1348     apply(M4, this); apply(M5, this);
1349     done();
1350   }
1351   // Seven options...
1352   template<class M0t, class M1t, class M2t, class M3t,
1353            class M4t, class M5t, class M6t>
1354   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1355        const M4t &M4, const M5t &M5, const M6t &M6)
1356     : Option(ZeroOrMore | NotHidden) {
1357     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1358     apply(M4, this); apply(M5, this); apply(M6, this);
1359     done();
1360   }
1361   // Eight options...
1362   template<class M0t, class M1t, class M2t, class M3t,
1363            class M4t, class M5t, class M6t, class M7t>
1364   list(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1365        const M4t &M4, const M5t &M5, const M6t &M6,
1366        const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1367     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1368     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1369     done();
1370   }
1371 };
1372
1373 // multi_val - Modifier to set the number of additional values.
1374 struct multi_val {
1375   unsigned AdditionalVals;
1376   explicit multi_val(unsigned N) : AdditionalVals(N) {}
1377
1378   template <typename D, typename S, typename P>
1379   void apply(list<D, S, P> &L) const { L.setNumAdditionalVals(AdditionalVals); }
1380 };
1381
1382
1383 //===----------------------------------------------------------------------===//
1384 // bits_storage class
1385
1386 // Default storage class definition: external storage.  This implementation
1387 // assumes the user will specify a variable to store the data into with the
1388 // cl::location(x) modifier.
1389 //
1390 template<class DataType, class StorageClass>
1391 class bits_storage {
1392   unsigned *Location;   // Where to store the bits...
1393
1394   template<class T>
1395   static unsigned Bit(const T &V) {
1396     unsigned BitPos = reinterpret_cast<unsigned>(V);
1397     assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1398           "enum exceeds width of bit vector!");
1399     return 1 << BitPos;
1400   }
1401
1402 public:
1403   bits_storage() : Location(0) {}
1404
1405   bool setLocation(Option &O, unsigned &L) {
1406     if (Location)
1407       return O.error("cl::location(x) specified more than once!");
1408     Location = &L;
1409     return false;
1410   }
1411
1412   template<class T>
1413   void addValue(const T &V) {
1414     assert(Location != 0 && "cl::location(...) not specified for a command "
1415            "line option with external storage!");
1416     *Location |= Bit(V);
1417   }
1418
1419   unsigned getBits() { return *Location; }
1420
1421   template<class T>
1422   bool isSet(const T &V) {
1423     return (*Location & Bit(V)) != 0;
1424   }
1425 };
1426
1427
1428 // Define how to hold bits.  Since we can inherit from a class, we do so.
1429 // This makes us exactly compatible with the bits in all cases that it is used.
1430 //
1431 template<class DataType>
1432 class bits_storage<DataType, bool> {
1433   unsigned Bits;   // Where to store the bits...
1434
1435   template<class T>
1436   static unsigned Bit(const T &V) {
1437     unsigned BitPos = (unsigned)V;
1438     assert(BitPos < sizeof(unsigned) * CHAR_BIT &&
1439           "enum exceeds width of bit vector!");
1440     return 1 << BitPos;
1441   }
1442
1443 public:
1444   template<class T>
1445   void addValue(const T &V) {
1446     Bits |=  Bit(V);
1447   }
1448
1449   unsigned getBits() { return Bits; }
1450
1451   template<class T>
1452   bool isSet(const T &V) {
1453     return (Bits & Bit(V)) != 0;
1454   }
1455 };
1456
1457
1458 //===----------------------------------------------------------------------===//
1459 // bits - A bit vector of command options.
1460 //
1461 template <class DataType, class Storage = bool,
1462           class ParserClass = parser<DataType> >
1463 class bits : public Option, public bits_storage<DataType, Storage> {
1464   std::vector<unsigned> Positions;
1465   ParserClass Parser;
1466
1467   virtual enum ValueExpected getValueExpectedFlagDefault() const {
1468     return Parser.getValueExpectedFlagDefault();
1469   }
1470   virtual void getExtraOptionNames(SmallVectorImpl<const char*> &OptionNames) {
1471     return Parser.getExtraOptionNames(OptionNames);
1472   }
1473
1474   virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg){
1475     typename ParserClass::parser_data_type Val =
1476       typename ParserClass::parser_data_type();
1477     if (Parser.parse(*this, ArgName, Arg, Val))
1478       return true;  // Parse Error!
1479     addValue(Val);
1480     setPosition(pos);
1481     Positions.push_back(pos);
1482     return false;
1483   }
1484
1485   // Forward printing stuff to the parser...
1486   virtual size_t getOptionWidth() const {return Parser.getOptionWidth(*this);}
1487   virtual void printOptionInfo(size_t GlobalWidth) const {
1488     Parser.printOptionInfo(*this, GlobalWidth);
1489   }
1490
1491   // Unimplemented: bits options don't currently store their default values.
1492   virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {}
1493
1494   void done() {
1495     addArgument();
1496     Parser.initialize(*this);
1497   }
1498 public:
1499   ParserClass &getParser() { return Parser; }
1500
1501   unsigned getPosition(unsigned optnum) const {
1502     assert(optnum < this->size() && "Invalid option index");
1503     return Positions[optnum];
1504   }
1505
1506   // One option...
1507   template<class M0t>
1508   explicit bits(const M0t &M0) : Option(ZeroOrMore | NotHidden) {
1509     apply(M0, this);
1510     done();
1511   }
1512   // Two options...
1513   template<class M0t, class M1t>
1514   bits(const M0t &M0, const M1t &M1) : Option(ZeroOrMore | NotHidden) {
1515     apply(M0, this); apply(M1, this);
1516     done();
1517   }
1518   // Three options...
1519   template<class M0t, class M1t, class M2t>
1520   bits(const M0t &M0, const M1t &M1, const M2t &M2)
1521     : Option(ZeroOrMore | NotHidden) {
1522     apply(M0, this); apply(M1, this); apply(M2, this);
1523     done();
1524   }
1525   // Four options...
1526   template<class M0t, class M1t, class M2t, class M3t>
1527   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1528     : Option(ZeroOrMore | NotHidden) {
1529     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1530     done();
1531   }
1532   // Five options...
1533   template<class M0t, class M1t, class M2t, class M3t, class M4t>
1534   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1535        const M4t &M4) : Option(ZeroOrMore | NotHidden) {
1536     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1537     apply(M4, this);
1538     done();
1539   }
1540   // Six options...
1541   template<class M0t, class M1t, class M2t, class M3t,
1542            class M4t, class M5t>
1543   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1544        const M4t &M4, const M5t &M5) : Option(ZeroOrMore | NotHidden) {
1545     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1546     apply(M4, this); apply(M5, this);
1547     done();
1548   }
1549   // Seven options...
1550   template<class M0t, class M1t, class M2t, class M3t,
1551            class M4t, class M5t, class M6t>
1552   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1553        const M4t &M4, const M5t &M5, const M6t &M6)
1554     : Option(ZeroOrMore | NotHidden) {
1555     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1556     apply(M4, this); apply(M5, this); apply(M6, this);
1557     done();
1558   }
1559   // Eight options...
1560   template<class M0t, class M1t, class M2t, class M3t,
1561            class M4t, class M5t, class M6t, class M7t>
1562   bits(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3,
1563        const M4t &M4, const M5t &M5, const M6t &M6,
1564        const M7t &M7) : Option(ZeroOrMore | NotHidden) {
1565     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1566     apply(M4, this); apply(M5, this); apply(M6, this); apply(M7, this);
1567     done();
1568   }
1569 };
1570
1571 //===----------------------------------------------------------------------===//
1572 // Aliased command line option (alias this name to a preexisting name)
1573 //
1574
1575 class alias : public Option {
1576   Option *AliasFor;
1577   virtual bool handleOccurrence(unsigned pos, StringRef /*ArgName*/,
1578                                 StringRef Arg) {
1579     return AliasFor->handleOccurrence(pos, AliasFor->ArgStr, Arg);
1580   }
1581   // Handle printing stuff...
1582   virtual size_t getOptionWidth() const;
1583   virtual void printOptionInfo(size_t GlobalWidth) const;
1584
1585   // Aliases do not need to print their values.
1586   virtual void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const {}
1587
1588   void done() {
1589     if (!hasArgStr())
1590       error("cl::alias must have argument name specified!");
1591     if (AliasFor == 0)
1592       error("cl::alias must have an cl::aliasopt(option) specified!");
1593       addArgument();
1594   }
1595 public:
1596   void setAliasFor(Option &O) {
1597     if (AliasFor)
1598       error("cl::alias must only have one cl::aliasopt(...) specified!");
1599     AliasFor = &O;
1600   }
1601
1602   // One option...
1603   template<class M0t>
1604   explicit alias(const M0t &M0) : Option(Optional | Hidden), AliasFor(0) {
1605     apply(M0, this);
1606     done();
1607   }
1608   // Two options...
1609   template<class M0t, class M1t>
1610   alias(const M0t &M0, const M1t &M1) : Option(Optional | Hidden), AliasFor(0) {
1611     apply(M0, this); apply(M1, this);
1612     done();
1613   }
1614   // Three options...
1615   template<class M0t, class M1t, class M2t>
1616   alias(const M0t &M0, const M1t &M1, const M2t &M2)
1617     : Option(Optional | Hidden), AliasFor(0) {
1618     apply(M0, this); apply(M1, this); apply(M2, this);
1619     done();
1620   }
1621   // Four options...
1622   template<class M0t, class M1t, class M2t, class M3t>
1623   alias(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
1624     : Option(Optional | Hidden), AliasFor(0) {
1625     apply(M0, this); apply(M1, this); apply(M2, this); apply(M3, this);
1626     done();
1627   }
1628 };
1629
1630 // aliasfor - Modifier to set the option an alias aliases.
1631 struct aliasopt {
1632   Option &Opt;
1633   explicit aliasopt(Option &O) : Opt(O) {}
1634   void apply(alias &A) const { A.setAliasFor(Opt); }
1635 };
1636
1637 // extrahelp - provide additional help at the end of the normal help
1638 // output. All occurrences of cl::extrahelp will be accumulated and
1639 // printed to stderr at the end of the regular help, just before
1640 // exit is called.
1641 struct extrahelp {
1642   const char * morehelp;
1643   explicit extrahelp(const char* help);
1644 };
1645
1646 void PrintVersionMessage();
1647 // This function just prints the help message, exactly the same way as if the
1648 // -help option had been given on the command line.
1649 // NOTE: THIS FUNCTION TERMINATES THE PROGRAM!
1650 void PrintHelpMessage();
1651
1652 } // End namespace cl
1653
1654 } // End namespace llvm
1655
1656 #endif