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