Options.h revision a79a2b5bf23d1422eed9be3793186ebbba7532ec
1//===--- Options.h - Option info & table ------------------------*- 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#ifndef CLANG_DRIVER_OPTIONS_H_
11#define CLANG_DRIVER_OPTIONS_H_
12
13#include <cassert>
14
15namespace clang {
16namespace driver {
17namespace options {
18  enum DriverFlag {
19    DriverOption     = (1 << 0),
20    LinkerInput      = (1 << 1),
21    NoArgumentUnused = (1 << 2),
22    RenderAsInput    = (1 << 3),
23    RenderJoined     = (1 << 4),
24    RenderSeparate   = (1 << 5),
25    Unsupported      = (1 << 6)
26  };
27}
28
29  class Arg;
30  class InputArgList;
31  class Option;
32
33  /// OptTable - Provide access to the Option info table.
34  ///
35  /// The OptTable class provides a layer of indirection which allows Option
36  /// instance to be created lazily. In the common case, only a few options will
37  /// be needed at runtime; the OptTable class maintains enough information to
38  /// parse command lines without instantiating Options, while letting other
39  /// parts of the driver still use Option instances where convenient.
40  //
41  // FIXME: Introduce an OptionSpecifier class to wrap the option ID
42  // variant?
43  class OptTable {
44  public:
45    /// Info - Entry for a single option instance in the option data table.
46    struct Info {
47      const char *Name;
48      const char *HelpText;
49      const char *MetaVar;
50      unsigned char Kind;
51      unsigned char Flags;
52      unsigned char Param;
53      unsigned short GroupID;
54      unsigned short AliasID;
55    };
56
57  private:
58    /// The static option information table.
59    const Info *OptionInfos;
60    unsigned NumOptionInfos;
61
62    /// The lazily constructed options table, indexed by option::ID - 1.
63    mutable Option **Options;
64
65    /// Prebound input option instance.
66    const Option *TheInputOption;
67
68    /// Prebound unknown option instance.
69    const Option *TheUnknownOption;
70
71    /// The index of the first option which can be parsed (i.e., is not a
72    /// special option like 'input' or 'unknown', and is not an option group).
73    unsigned FirstSearchableIndex;
74
75  private:
76    const Info &getInfo(unsigned id) const {
77      assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
78      return OptionInfos[id - 1];
79    }
80
81    Option *CreateOption(unsigned id) const;
82
83  protected:
84    OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos);
85  public:
86    ~OptTable();
87
88    /// getNumOptions - Return the total number of option classes.
89    unsigned getNumOptions() const { return NumOptionInfos; }
90
91    /// getOption - Get the given \arg id's Option instance, lazily creating it
92    /// if necessary.
93    ///
94    /// \return The option, or null for the INVALID option id.
95    const Option *getOption(unsigned id) const {
96      if (id == 0)
97        return 0;
98
99      assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
100      Option *&Entry = Options[id - 1];
101      if (!Entry)
102        Entry = CreateOption(id);
103      return Entry;
104    }
105
106    /// getOptionName - Lookup the name of the given option.
107    const char *getOptionName(unsigned id) const {
108      return getInfo(id).Name;
109    }
110
111    /// getOptionKind - Get the kind of the given option.
112    unsigned getOptionKind(unsigned id) const {
113      return getInfo(id).Kind;
114    }
115
116    /// getOptionHelpText - Get the help text to use to describe this option.
117    const char *getOptionHelpText(unsigned id) const {
118      return getInfo(id).HelpText;
119    }
120
121    /// getOptionMetaVar - Get the meta-variable name to use when describing
122    /// this options values in the help text.
123    const char *getOptionMetaVar(unsigned id) const {
124      return getInfo(id).MetaVar;
125    }
126
127    /// parseOneArg - Parse a single argument; returning the new argument and
128    /// updating Index.
129    ///
130    /// \param [in] [out] Index - The current parsing position in the argument
131    /// string list; on return this will be the index of the next argument
132    /// string to parse.
133    ///
134    /// \return - The parsed argument, or 0 if the argument is missing values
135    /// (in which case Index still points at the conceptual next argument string
136    /// to parse).
137    Arg *ParseOneArg(const InputArgList &Args, unsigned &Index) const;
138  };
139
140namespace options {
141  enum ID {
142    OPT_INVALID = 0, // This is not an option ID.
143    OPT_INPUT,       // Reserved ID for input option.
144    OPT_UNKNOWN,     // Reserved ID for unknown option.
145#define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
146               HELPTEXT, METAVAR) OPT_##ID,
147#include "clang/Driver/Options.def"
148    LastOption
149#undef OPTION
150  };
151}
152
153  OptTable *createDriverOptTable();
154}
155}
156
157#endif
158