PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/3rd_party/llvm/include/llvm/Support/CommandLine.h

http://salvia.codeplex.com
C++ Header | 1689 lines | 1057 code | 259 blank | 373 comment | 46 complexity | cffb8466b2fd9ee7302245f39274da2e MD5 | raw file
Possible License(s): GPL-2.0, JSON, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file