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;
47249259Sdim  };
48249259Sdim
49249259Sdimprivate:
50249259Sdim  /// \brief The static option information table.
51249259Sdim  const Info *OptionInfos;
52249259Sdim  unsigned NumOptionInfos;
53249259Sdim
54249259Sdim  unsigned TheInputOptionID;
55249259Sdim  unsigned TheUnknownOptionID;
56249259Sdim
57249259Sdim  /// The index of the first option which can be parsed (i.e., is not a
58249259Sdim  /// special option like 'input' or 'unknown', and is not an option group).
59249259Sdim  unsigned FirstSearchableIndex;
60249259Sdim
61249259Sdim  /// The union of all option prefixes. If an argument does not begin with
62249259Sdim  /// one of these, it is an input.
63249259Sdim  StringSet<> PrefixesUnion;
64249259Sdim  std::string PrefixChars;
65249259Sdim
66249259Sdimprivate:
67249259Sdim  const Info &getInfo(OptSpecifier Opt) const {
68249259Sdim    unsigned id = Opt.getID();
69249259Sdim    assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
70249259Sdim    return OptionInfos[id - 1];
71249259Sdim  }
72249259Sdim
73249259Sdimprotected:
74249259Sdim  OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
75249259Sdimpublic:
76249259Sdim  ~OptTable();
77249259Sdim
78249259Sdim  /// \brief Return the total number of option classes.
79249259Sdim  unsigned getNumOptions() const { return NumOptionInfos; }
80249259Sdim
81249259Sdim  /// \brief Get the given Opt's Option instance, lazily creating it
82249259Sdim  /// if necessary.
83249259Sdim  ///
84249259Sdim  /// \return The option, or null for the INVALID option id.
85249259Sdim  const Option getOption(OptSpecifier Opt) const;
86249259Sdim
87249259Sdim  /// \brief Lookup the name of the given option.
88249259Sdim  const char *getOptionName(OptSpecifier id) const {
89249259Sdim    return getInfo(id).Name;
90249259Sdim  }
91249259Sdim
92249259Sdim  /// \brief Get the kind of the given option.
93249259Sdim  unsigned getOptionKind(OptSpecifier id) const {
94249259Sdim    return getInfo(id).Kind;
95249259Sdim  }
96249259Sdim
97249259Sdim  /// \brief Get the group id for the given option.
98249259Sdim  unsigned getOptionGroupID(OptSpecifier id) const {
99249259Sdim    return getInfo(id).GroupID;
100249259Sdim  }
101249259Sdim
102249259Sdim  /// \brief Should the help for the given option be hidden by default.
103249259Sdim  bool isOptionHelpHidden(OptSpecifier id) const;
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.
122249259Sdim  ///
123249259Sdim  /// \return The parsed argument, or 0 if the argument is missing values
124249259Sdim  /// (in which case Index still points at the conceptual next argument string
125249259Sdim  /// to parse).
126249259Sdim  Arg *ParseOneArg(const ArgList &Args, unsigned &Index) const;
127249259Sdim
128249259Sdim  /// \brief Parse an list of arguments into an InputArgList.
129249259Sdim  ///
130249259Sdim  /// The resulting InputArgList will reference the strings in [\p ArgBegin,
131249259Sdim  /// \p ArgEnd), and their lifetime should extend past that of the returned
132249259Sdim  /// InputArgList.
133249259Sdim  ///
134249259Sdim  /// The only error that can occur in this routine is if an argument is
135249259Sdim  /// missing values; in this case \p MissingArgCount will be non-zero.
136249259Sdim  ///
137249259Sdim  /// \param ArgBegin - The beginning of the argument vector.
138249259Sdim  /// \param ArgEnd - The end of the argument vector.
139249259Sdim  /// \param MissingArgIndex - On error, the index of the option which could
140249259Sdim  /// not be parsed.
141249259Sdim  /// \param MissingArgCount - On error, the number of missing options.
142249259Sdim  /// \return An InputArgList; on error this will contain all the options
143249259Sdim  /// which could be parsed.
144249259Sdim  InputArgList *ParseArgs(const char* const *ArgBegin,
145249259Sdim                          const char* const *ArgEnd,
146249259Sdim                          unsigned &MissingArgIndex,
147249259Sdim                          unsigned &MissingArgCount) const;
148249259Sdim
149249259Sdim  /// \brief Render the help text for an option table.
150249259Sdim  ///
151249259Sdim  /// \param OS - The stream to write the help text to.
152249259Sdim  /// \param Name - The name to use in the usage line.
153249259Sdim  /// \param Title - The title to use in the usage line.
154249259Sdim  /// \param ShowHidden - Whether help-hidden arguments should be shown.
155249259Sdim  void PrintHelp(raw_ostream &OS, const char *Name,
156249259Sdim                  const char *Title, bool ShowHidden = false) const;
157249259Sdim};
158249259Sdim} // end namespace opt
159249259Sdim} // end namespace llvm
160249259Sdim
161249259Sdim#endif
162