aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Driver/Option.h
blob: c59faef897aee7c4707be3738691c4241eb3d3b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//===--- Option.h - Abstract Driver Options ---------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_DRIVER_OPTION_H_
#define CLANG_DRIVER_OPTION_H_

#include "Options.h"

#include "llvm/Support/Casting.h"
using llvm::isa;
using llvm::cast;
using llvm::cast_or_null;
using llvm::dyn_cast;
using llvm::dyn_cast_or_null;

namespace clang {
namespace driver {
  class Arg;
  class InputArgList;
  class OptionGroup;
  
  /// Option - Abstract representation for a single form of driver
  /// argument.
  ///
  /// An Option class represents a form of option that the driver
  /// takes, for example how many arguments the option has and how
  /// they can be provided. Individual option instances store
  /// additional information about what group the option is a member
  /// of (if any), if the option is an alias, and a number of
  /// flags. At runtime the driver parses the command line into
  /// concrete Arg instances, each of which corresponds to a
  /// particular Option instance.
  class Option {
  public:
    enum OptionClass {
      GroupClass = 0,
      InputClass,
      UnknownClass,
      FlagClass,
      JoinedClass,
      SeparateClass,
      CommaJoinedClass,
      MultiArgClass,
      JoinedOrSeparateClass,
      JoinedAndSeparateClass
    };

  private:
    OptionClass Kind;

    options::ID ID;

    /// The option name.
    const char *Name; 

    /// Group this option is a member of, if any.
    const OptionGroup *Group; 

    /// Option that this is an alias for, if any.
    const Option *Alias;

    /// Unsupported options will not be rejected.
    bool Unsupported : 1;

    /// Treat this option like a linker input?
    bool LinkerInput : 1;
    
    /// When rendering as an input, don't render the option.

    // FIXME: We should ditch the render/renderAsInput distinction.
    bool NoOptAsInput : 1;

    /// Always render this option as separate form its value.
    bool ForceSeparateRender : 1;
    
    /// Always render this option joined with its value.
    bool ForceJoinedRender : 1;    

    /// This option is only consumed by the driver.
    bool DriverOption : 1;    

    /// This option should not report argument unused errors.
    bool NoArgumentUnused : 1;    

  protected:
    Option(OptionClass Kind, options::ID ID, const char *Name, 
           const OptionGroup *Group, const Option *Alias);
  public:
    virtual ~Option();

    options::ID getId() const { return ID; }
    OptionClass getKind() const { return Kind; }
    const char *getName() const { return Name; }
    const OptionGroup *getGroup() const { return Group; }
    const Option *getAlias() const { return Alias; }

    bool isUnsupported() const { return Unsupported; }
    void setUnsupported(bool Value) { Unsupported = Value; }

    bool isLinkerInput() const { return LinkerInput; }
    void setLinkerInput(bool Value) { LinkerInput = Value; }

    bool hasNoOptAsInput() const { return NoOptAsInput; }
    void setNoOptAsInput(bool Value) { NoOptAsInput = Value; }
    
    bool hasForceSeparateRender() const { return ForceSeparateRender; }
    void setForceSeparateRender(bool Value) { ForceSeparateRender = Value; }
    
    bool hasForceJoinedRender() const { return ForceJoinedRender; }
    void setForceJoinedRender(bool Value) { ForceJoinedRender = Value; }
    
    bool isDriverOption() const { return DriverOption; }
    void setDriverOption(bool Value) { DriverOption = Value; }

    bool hasNoArgumentUnused() const { return NoArgumentUnused; }
    void setNoArgumentUnused(bool Value) { NoArgumentUnused = Value; }

    bool hasForwardToGCC() const { return !DriverOption && !LinkerInput; }

    /// getUnaliasedOption - Return the final option this option
    /// aliases (itself, if the option has no alias).
    const Option *getUnaliasedOption() const { 
      if (Alias) return Alias->getUnaliasedOption();
      return this;
    }

    /// getRenderName - Return the name to use when rendering this
    /// option.
    const char *getRenderName() const {
      return getUnaliasedOption()->getName();
    }

    /// matches - Predicate for whether this option is part of the
    /// given option (which may be a group).
    bool matches(const Option *Opt) const;
    bool matches(options::ID Id) const;

    /// accept - Potentially accept the current argument, returning a
    /// new Arg instance, or 0 if the option does not accept this
    /// argument (or the argument is missing values).
    ///
    /// If the option accepts the current argument, accept() sets
    /// Index to the position where argument parsing should resume
    /// (even if the argument is missing values).
    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const = 0;
    
    void dump() const;

    static bool classof(const Option *) { return true; }
  };
  
  /// OptionGroup - A set of options which are can be handled uniformly
  /// by the driver.
  class OptionGroup : public Option {
  public:
    OptionGroup(options::ID ID, const char *Name, const OptionGroup *Group);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::GroupClass; 
    }
    static bool classof(const OptionGroup *) { return true; }
  };
  
  // Dummy option classes.
  
  /// InputOption - Dummy option class for representing driver inputs.
  class InputOption : public Option {
  public:
    InputOption();

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::InputClass; 
    }
    static bool classof(const InputOption *) { return true; }
  };

  /// UnknownOption - Dummy option class for represent unknown arguments.
  class UnknownOption : public Option {
  public:
    UnknownOption();

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::UnknownClass; 
    }
    static bool classof(const UnknownOption *) { return true; }
  };

  // Normal options.

  class FlagOption : public Option {
  public:
    FlagOption(options::ID ID, const char *Name, const OptionGroup *Group, 
               const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::FlagClass; 
    }
    static bool classof(const FlagOption *) { return true; }
  };

  class JoinedOption : public Option {
  public:
    JoinedOption(options::ID ID, const char *Name, const OptionGroup *Group, 
                 const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::JoinedClass; 
    }
    static bool classof(const JoinedOption *) { return true; }
  };

  class SeparateOption : public Option {
  public:
    SeparateOption(options::ID ID, const char *Name, const OptionGroup *Group, 
                   const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::SeparateClass; 
    }
    static bool classof(const SeparateOption *) { return true; }
  };

  class CommaJoinedOption : public Option {
  public:
    CommaJoinedOption(options::ID ID, const char *Name, 
                      const OptionGroup *Group, const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::CommaJoinedClass; 
    }
    static bool classof(const CommaJoinedOption *) { return true; }
  };

  // FIXME: Fold MultiArgOption into SeparateOption?

  /// MultiArgOption - An option which takes multiple arguments (these
  /// are always separate arguments).
  class MultiArgOption : public Option {
    unsigned NumArgs;

  public:
    MultiArgOption(options::ID ID, const char *Name, const OptionGroup *Group, 
                   const Option *Alias, unsigned NumArgs);

    unsigned getNumArgs() const { return NumArgs; }

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::MultiArgClass; 
    }
    static bool classof(const MultiArgOption *) { return true; }
  };

  /// JoinedOrSeparateOption - An option which either literally
  /// prefixes its (non-empty) value, or is follwed by a value.
  class JoinedOrSeparateOption : public Option {
  public:
    JoinedOrSeparateOption(options::ID ID, const char *Name, 
                           const OptionGroup *Group, const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::JoinedOrSeparateClass; 
    }
    static bool classof(const JoinedOrSeparateOption *) { return true; }
  };

  /// JoinedAndSeparateOption - An option which literally prefixes its
  /// value and is followed by another value.
  class JoinedAndSeparateOption : public Option {
  public:
    JoinedAndSeparateOption(options::ID ID, const char *Name, 
                            const OptionGroup *Group, const Option *Alias);

    virtual Arg *accept(const InputArgList &Args, unsigned &Index) const;

    static bool classof(const Option *O) { 
      return O->getKind() == Option::JoinedAndSeparateClass; 
    }
    static bool classof(const JoinedAndSeparateOption *) { return true; }
  };

} // end namespace driver
} // end namespace clang

#endif