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