summaryrefslogtreecommitdiffstats
path: root/include/clang/Driver/Arg.h
blob: 6bed2b8cbdef3b7e5e0137e5ff7ab7091d4e089b (plain)
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
//===--- Arg.h - Parsed Argument Classes ------------------------*- 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_ARG_H_
#define CLANG_DRIVER_ARG_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;

#include "Util.h"
#include <vector>
#include <string>

namespace clang {
namespace driver {
  class ArgList;
  class Option;

  /// Arg - A concrete instance of a particular driver option.
  ///
  /// The Arg class encodes just enough information to be able to
  /// derive the argument values efficiently. In addition, Arg
  /// instances have an intrusive double linked list which is used by
  /// ArgList to provide efficient iteration over all instances of a
  /// particular option.
  class Arg {
  public:
    enum ArgClass {
      FlagClass = 0,
      PositionalClass,
      JoinedClass,
      SeparateClass,
      CommaJoinedClass,
      JoinedAndSeparateClass
    };

  private:
    ArgClass Kind;

    /// The option this argument is an instance of.
    const Option *Opt;
    
    /// The argument this argument was derived from (during tool chain
    /// argument translation), if any.
    const Arg *BaseArg;

    /// The index at which this argument appears in the containing
    /// ArgList.
    unsigned Index;

    /// Flag indicating whether this argument was used to effect
    /// compilation; used for generating "argument unused"
    /// diagnostics.
    mutable bool Claimed;

  protected:
    Arg(ArgClass Kind, const Option *Opt, unsigned Index,
        const Arg *BaseArg = 0);
    
  public:
    Arg(const Arg &);
    virtual ~Arg();

    ArgClass getKind() const { return Kind; }
    const Option &getOption() const { return *Opt; }
    unsigned getIndex() const { return Index; }
    
    /// getBaseArg - Return the base argument which generated this
    /// arg; this is either the argument itself or the argument it was
    /// derived from during tool chain specific argument translation.
    const Arg &getBaseArg() const { 
      return BaseArg ? *BaseArg : *this; 
    }
    void setBaseArg(const Arg *_BaseArg) {
      BaseArg = _BaseArg;
    }

    bool isClaimed() const { return getBaseArg().Claimed; }

    /// claim - Set the Arg claimed bit.
    
    // FIXME: We need to deal with derived arguments and set the bit
    // in the original argument; not the derived one.
    void claim() const { getBaseArg().Claimed = true; }

    virtual unsigned getNumValues() const = 0;
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const = 0;
    
    /// render - Append the argument onto the given array as strings.
    virtual void render(const ArgList &Args, ArgStringList &Output) const = 0;

    /// renderAsInput - Append the argument, render as an input, onto
    /// the given array as strings. The distinction is that some
    /// options only render their values when rendered as a input
    /// (e.g., Xlinker).
    void renderAsInput(const ArgList &Args, ArgStringList &Output) const;

    static bool classof(const Arg *) { return true; }    

    void dump() const;

    /// getAsString - Return a formatted version of the argument and
    /// its values, for debugging and diagnostics.
    std::string getAsString(const ArgList &Args) const;
  };

  /// FlagArg - An argument with no value.
  class FlagArg : public Arg {
  public:
    FlagArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return 0; }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::FlagClass; 
    }
    static bool classof(const FlagArg *) { return true; }
  };

  /// PositionalArg - A simple positional argument.
  class PositionalArg : public Arg {
  public:
    PositionalArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return 1; }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::PositionalClass; 
    }
    static bool classof(const PositionalArg *) { return true; }
  };

  /// JoinedArg - A single value argument where the value is joined
  /// (suffixed) to the option.
  class JoinedArg : public Arg {
  public:
    JoinedArg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return 1; }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::JoinedClass; 
    }
    static bool classof(const JoinedArg *) { return true; }
  };

  /// SeparateArg - An argument where one or more values follow the
  /// option specifier immediately in the argument vector.
  class SeparateArg : public Arg {
    unsigned NumValues;

  public:
    SeparateArg(const Option *Opt, unsigned Index, unsigned NumValues, 
                const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return NumValues; }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::SeparateClass; 
    }
    static bool classof(const SeparateArg *) { return true; }
  };

  /// CommaJoinedArg - An argument with multiple values joined by
  /// commas and joined (suffixed) to the option specifier.
  ///
  /// The key point of this arg is that it renders its values into
  /// separate arguments, which allows it to be used as a generic
  /// mechanism for passing arguments through to tools.
  class CommaJoinedArg : public Arg {
    std::vector<std::string> Values;

  public:
    CommaJoinedArg(const Option *Opt, unsigned Index, const char *Str, 
                   const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return Values.size(); }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::CommaJoinedClass; 
    }
    static bool classof(const CommaJoinedArg *) { return true; }
  };

  /// JoinedAndSeparateArg - An argument with both joined and separate
  /// values.
  class JoinedAndSeparateArg : public Arg {
  public:
    JoinedAndSeparateArg(const Option *Opt, unsigned Index, 
                         const Arg *BaseArg = 0);

    virtual void render(const ArgList &Args, ArgStringList &Output) const;

    virtual unsigned getNumValues() const { return 2; }
    virtual const char *getValue(const ArgList &Args, unsigned N=0) const;

    static bool classof(const Arg *A) { 
      return A->getKind() == Arg::JoinedAndSeparateClass; 
    }
    static bool classof(const JoinedAndSeparateArg *) { return true; }
  };
} // end namespace driver
} // end namespace clang

#endif
OpenPOWER on IntegriCloud