1249259Sdim//===--- OptTable.h - Option Table ------------------------------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim
10249259Sdim#ifndef LLVM_OPTION_OPTTABLE_H
11249259Sdim#define LLVM_OPTION_OPTTABLE_H
12249259Sdim
13249259Sdim#include "llvm/ADT/StringSet.h"
14249259Sdim#include "llvm/Option/OptSpecifier.h"
15249259Sdim
16249259Sdimnamespace llvm {
17249259Sdimclass raw_ostream;
18249259Sdimnamespace opt {
19249259Sdimclass Arg;
20249259Sdimclass ArgList;
21249259Sdimclass InputArgList;
22249259Sdimclass Option;
23249259Sdim
24249259Sdim/// \brief Provide access to the Option info table.
25249259Sdim///
26249259Sdim/// The OptTable class provides a layer of indirection which allows Option
27249259Sdim/// instance to be created lazily. In the common case, only a few options will
28249259Sdim/// be needed at runtime; the OptTable class maintains enough information to
29249259Sdim/// parse command lines without instantiating Options, while letting other
30249259Sdim/// parts of the driver still use Option instances where convenient.
31249259Sdimclass OptTable {
32249259Sdimpublic:
33249259Sdim  /// \brief Entry for a single option instance in the option data table.
34249259Sdim  struct Info {
35249259Sdim    /// A null terminated array of prefix strings to apply to name while
36249259Sdim    /// matching.
37249259Sdim    const char *const *Prefixes;
38249259Sdim    const char *Name;
39249259Sdim    const char *HelpText;
40249259Sdim    const char *MetaVar;
41249259Sdim    unsigned ID;
42249259Sdim    unsigned char Kind;
43249259Sdim    unsigned char Param;
44249259Sdim    unsigned short Flags;
45249259Sdim    unsigned short GroupID;
46249259Sdim    unsigned short AliasID;
47263509Sdim    const char *AliasArgs;
48249259Sdim  };
49249259Sdim
50249259Sdimprivate:
51249259Sdim  /// \brief The static option information table.
52249259Sdim  const Info *OptionInfos;
53249259Sdim  unsigned NumOptionInfos;
54263509Sdim  bool IgnoreCase;
55249259Sdim
56249259Sdim  unsigned TheInputOptionID;
57249259Sdim  unsigned TheUnknownOptionID;
58249259Sdim
59249259Sdim  /// The index of the first option which can be parsed (i.e., is not a
60249259Sdim  /// special option like 'input' or 'unknown', and is not an option group).
61249259Sdim  unsigned FirstSearchableIndex;
62249259Sdim
63249259Sdim  /// The union of all option prefixes. If an argument does not begin with
64249259Sdim  /// one of these, it is an input.
65249259Sdim  StringSet<> PrefixesUnion;
66249259Sdim  std::string PrefixChars;
67249259Sdim
68249259Sdimprivate:
69249259Sdim  const Info &getInfo(OptSpecifier Opt) const {
70249259Sdim    unsigned id = Opt.getID();
71249259Sdim    assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
72249259Sdim    return OptionInfos[id - 1];
73249259Sdim  }
74249259Sdim
75249259Sdimprotected:
76263509Sdim  OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
77263509Sdim           bool _IgnoreCase = false);
78249259Sdimpublic:
79249259Sdim  ~OptTable();
80249259Sdim
81249259Sdim  /// \brief Return the total number of option classes.
82249259Sdim  unsigned getNumOptions() const { return NumOptionInfos; }
83249259Sdim
84249259Sdim  /// \brief Get the given Opt's Option instance, lazily creating it
85249259Sdim  /// if necessary.
86249259Sdim  ///
87249259Sdim  /// \return The option, or null for the INVALID option id.
88249259Sdim  const Option getOption(OptSpecifier Opt) const;
89249259Sdim
90249259Sdim  /// \brief Lookup the name of the given option.
91249259Sdim  const char *getOptionName(OptSpecifier id) const {
92249259Sdim    return getInfo(id).Name;
93249259Sdim  }
94249259Sdim
95249259Sdim  /// \brief Get the kind of the given option.
96249259Sdim  unsigned getOptionKind(OptSpecifier id) const {
97249259Sdim    return getInfo(id).Kind;
98249259Sdim  }
99249259Sdim
100249259Sdim  /// \brief Get the group id for the given option.
101249259Sdim  unsigned getOptionGroupID(OptSpecifier id) const {
102249259Sdim    return getInfo(id).GroupID;
103249259Sdim  }
104249259Sdim
105249259Sdim  /// \brief Get the help text to use to describe this option.
106249259Sdim  const char *getOptionHelpText(OptSpecifier id) const {
107249259Sdim    return getInfo(id).HelpText;
108249259Sdim  }
109249259Sdim
110249259Sdim  /// \brief Get the meta-variable name to use when describing
111249259Sdim  /// this options values in the help text.
112249259Sdim  const char *getOptionMetaVar(OptSpecifier id) const {
113249259Sdim    return getInfo(id).MetaVar;
114249259Sdim  }
115249259Sdim
116249259Sdim  /// \brief Parse a single argument; returning the new argument and
117249259Sdim  /// updating Index.
118249259Sdim  ///
119249259Sdim  /// \param [in,out] Index - The current parsing position in the argument
120249259Sdim  /// string list; on return this will be the index of the next argument
121249259Sdim  /// string to parse.
122263509Sdim  /// \param [in] FlagsToInclude - Only parse options with any of these flags.
123263509Sdim  /// Zero is the default which includes all flags.
124263509Sdim  /// \param [in] FlagsToExclude - Don't parse options with this flag.  Zero
125263509Sdim  /// is the default and means exclude nothing.
126249259Sdim  ///
127249259Sdim  /// \return The parsed argument, or 0 if the argument is missing values
128249259Sdim  /// (in which case Index still points at the conceptual next argument string
129249259Sdim  /// to parse).
130263509Sdim  Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
131263509Sdim                   unsigned FlagsToInclude = 0,
132263509Sdim                   unsigned FlagsToExclude = 0) const;
133249259Sdim
134249259Sdim  /// \brief Parse an list of arguments into an InputArgList.
135249259Sdim  ///
136249259Sdim  /// The resulting InputArgList will reference the strings in [\p ArgBegin,
137249259Sdim  /// \p ArgEnd), and their lifetime should extend past that of the returned
138249259Sdim  /// InputArgList.
139249259Sdim  ///
140249259Sdim  /// The only error that can occur in this routine is if an argument is
141249259Sdim  /// missing values; in this case \p MissingArgCount will be non-zero.
142249259Sdim  ///
143249259Sdim  /// \param ArgBegin - The beginning of the argument vector.
144249259Sdim  /// \param ArgEnd - The end of the argument vector.
145249259Sdim  /// \param MissingArgIndex - On error, the index of the option which could
146249259Sdim  /// not be parsed.
147249259Sdim  /// \param MissingArgCount - On error, the number of missing options.
148263509Sdim  /// \param FlagsToInclude - Only parse options with any of these flags.
149263509Sdim  /// Zero is the default which includes all flags.
150263509Sdim  /// \param FlagsToExclude - Don't parse options with this flag.  Zero
151263509Sdim  /// is the default and means exclude nothing.
152249259Sdim  /// \return An InputArgList; on error this will contain all the options
153249259Sdim  /// which could be parsed.
154249259Sdim  InputArgList *ParseArgs(const char* const *ArgBegin,
155249259Sdim                          const char* const *ArgEnd,
156249259Sdim                          unsigned &MissingArgIndex,
157263509Sdim                          unsigned &MissingArgCount,
158263509Sdim                          unsigned FlagsToInclude = 0,
159263509Sdim                          unsigned FlagsToExclude = 0) const;
160249259Sdim
161249259Sdim  /// \brief Render the help text for an option table.
162249259Sdim  ///
163249259Sdim  /// \param OS - The stream to write the help text to.
164249259Sdim  /// \param Name - The name to use in the usage line.
165249259Sdim  /// \param Title - The title to use in the usage line.
166263509Sdim  /// \param FlagsToInclude - If non-zero, only include options with any
167263509Sdim  ///                         of these flags set.
168263509Sdim  /// \param FlagsToExclude - Exclude options with any of these flags set.
169249259Sdim  void PrintHelp(raw_ostream &OS, const char *Name,
170263509Sdim                 const char *Title, unsigned FlagsToInclude,
171263509Sdim                 unsigned FlagsToExclude) const;
172263509Sdim
173263509Sdim  void PrintHelp(raw_ostream &OS, const char *Name,
174249259Sdim                  const char *Title, bool ShowHidden = false) const;
175249259Sdim};
176249259Sdim} // end namespace opt
177249259Sdim} // end namespace llvm
178249259Sdim
179249259Sdim#endif
180