1 //===--- ArgList.h - Argument List Management -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_OPTION_ARGLIST_H
11 #define LLVM_OPTION_ARGLIST_H
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Option/OptSpecifier.h"
16 #include "llvm/Option/Option.h"
27 /// arg_iterator - Iterates through arguments stored inside an ArgList.
29 /// The current argument.
30 SmallVectorImpl<Arg*>::const_iterator Current;
32 /// The argument list we are iterating over.
35 /// Optional filters on the arguments which will be match. Most clients
36 /// should never want to iterate over arguments without filters, so we won't
37 /// bother to factor this into two separate iterator implementations.
39 // FIXME: Make efficient; the idea is to provide efficient iteration over
40 // all arguments which match a particular id and then just provide an
41 // iterator combinator which takes multiple iterators which can be
42 // efficiently compared and returns them in order.
43 OptSpecifier Id0, Id1, Id2;
48 typedef Arg * const * value_type;
49 typedef Arg * const & reference;
50 typedef Arg * const * pointer;
51 typedef std::forward_iterator_tag iterator_category;
52 typedef std::ptrdiff_t difference_type;
54 arg_iterator(SmallVectorImpl<Arg*>::const_iterator it,
55 const ArgList &_Args, OptSpecifier _Id0 = 0U,
56 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
57 : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
61 operator const Arg*() { return *Current; }
62 reference operator*() const { return *Current; }
63 pointer operator->() const { return Current; }
65 arg_iterator &operator++() {
71 arg_iterator operator++(int) {
72 arg_iterator tmp(*this);
77 friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
78 return LHS.Current == RHS.Current;
80 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
85 /// ArgList - Ordered collection of driver arguments.
87 /// The ArgList class manages a list of Arg instances as well as
88 /// auxiliary data and convenience methods to allow Tools to quickly
89 /// check for the presence of Arg instances for a particular Option
90 /// and to iterate over groups of arguments.
93 ArgList(const ArgList &) LLVM_DELETED_FUNCTION;
94 void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
97 typedef SmallVector<Arg*, 16> arglist_type;
98 typedef arglist_type::iterator iterator;
99 typedef arglist_type::const_iterator const_iterator;
100 typedef arglist_type::reverse_iterator reverse_iterator;
101 typedef arglist_type::const_reverse_iterator const_reverse_iterator;
104 /// The internal list of arguments.
116 /// append - Append \p A to the arg list.
119 arglist_type &getArgs() { return Args; }
120 const arglist_type &getArgs() const { return Args; }
122 unsigned size() const { return Args.size(); }
125 /// @name Arg Iteration
128 iterator begin() { return Args.begin(); }
129 iterator end() { return Args.end(); }
131 reverse_iterator rbegin() { return Args.rbegin(); }
132 reverse_iterator rend() { return Args.rend(); }
134 const_iterator begin() const { return Args.begin(); }
135 const_iterator end() const { return Args.end(); }
137 const_reverse_iterator rbegin() const { return Args.rbegin(); }
138 const_reverse_iterator rend() const { return Args.rend(); }
140 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
141 OptSpecifier Id2 = 0U) const {
142 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
144 arg_iterator filtered_end() const {
145 return arg_iterator(Args.end(), *this);
149 /// @name Arg Removal
152 /// eraseArg - Remove any option matching \p Id.
153 void eraseArg(OptSpecifier Id);
159 /// hasArg - Does the arg list contain any option matching \p Id.
161 /// \p Claim Whether the argument should be claimed, if it exists.
162 bool hasArgNoClaim(OptSpecifier Id) const {
163 return getLastArgNoClaim(Id) != 0;
165 bool hasArg(OptSpecifier Id) const {
166 return getLastArg(Id) != 0;
168 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
169 return getLastArg(Id0, Id1) != 0;
171 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
172 return getLastArg(Id0, Id1, Id2) != 0;
175 /// getLastArg - Return the last argument matching \p Id, or null.
177 /// \p Claim Whether the argument should be claimed, if it exists.
178 Arg *getLastArgNoClaim(OptSpecifier Id) const;
179 Arg *getLastArg(OptSpecifier Id) const;
180 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
181 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
182 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
183 OptSpecifier Id3) const;
184 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
185 OptSpecifier Id3, OptSpecifier Id4) const;
186 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
187 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
188 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
189 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
190 OptSpecifier Id6) const;
191 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
192 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
193 OptSpecifier Id6, OptSpecifier Id7) const;
195 /// getArgString - Return the input argument string at \p Index.
196 virtual const char *getArgString(unsigned Index) const = 0;
198 /// getNumInputArgStrings - Return the number of original argument strings,
199 /// which are guaranteed to be the first strings in the argument string
201 virtual unsigned getNumInputArgStrings() const = 0;
204 /// @name Argument Lookup Utilities
207 /// getLastArgValue - Return the value of the last argument, or a default.
208 StringRef getLastArgValue(OptSpecifier Id,
209 StringRef Default = "") const;
211 /// getAllArgValues - Get the values of all instances of the given argument
213 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
216 /// @name Translation Utilities
219 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
220 /// true if the option is present, false if the negation is present, and
221 /// \p Default if neither option is given. If both the option and its
222 /// negation are present, the last one wins.
223 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
225 /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
226 /// form \p Neg, return true if the option or its alias is present, false if
227 /// the negation is present, and \p Default if none of the options are
228 /// given. If multiple options are present, the last one wins.
229 bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
230 bool Default = true) const;
232 /// AddLastArg - Render only the last argument match \p Id0, if present.
233 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
234 void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
235 OptSpecifier Id1) const;
237 /// AddAllArgs - Render all arguments matching the given ids.
238 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
239 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
241 /// AddAllArgValues - Render the argument values of all arguments
242 /// matching the given ids.
243 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
244 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
246 /// AddAllArgsTranslated - Render all the arguments matching the
247 /// given ids, but forced to separate args and using the provided
248 /// name instead of the first option value.
250 /// \param Joined - If true, render the argument as joined with
251 /// the option specifier.
252 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
253 const char *Translation,
254 bool Joined = false) const;
256 /// ClaimAllArgs - Claim all arguments which match the given
258 void ClaimAllArgs(OptSpecifier Id0) const;
260 /// ClaimAllArgs - Claim all arguments.
262 void ClaimAllArgs() const;
265 /// @name Arg Synthesis
268 /// MakeArgString - Construct a constant string pointer whose
269 /// lifetime will match that of the ArgList.
270 virtual const char *MakeArgString(StringRef Str) const = 0;
271 const char *MakeArgString(const char *Str) const {
272 return MakeArgString(StringRef(Str));
274 const char *MakeArgString(std::string Str) const {
275 return MakeArgString(StringRef(Str));
277 const char *MakeArgString(const Twine &Str) const;
279 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
280 /// string at \p Index if possible.
281 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
282 StringRef RHS) const;
287 class InputArgList : public ArgList {
289 /// List of argument strings used by the contained Args.
291 /// This is mutable since we treat the ArgList as being the list
292 /// of Args, and allow routines to add new strings (to have a
293 /// convenient place to store the memory) via MakeIndex.
294 mutable ArgStringList ArgStrings;
296 /// Strings for synthesized arguments.
298 /// This is mutable since we treat the ArgList as being the list
299 /// of Args, and allow routines to add new strings (to have a
300 /// convenient place to store the memory) via MakeIndex.
301 mutable std::list<std::string> SynthesizedStrings;
303 /// The number of original input argument strings.
304 unsigned NumInputArgStrings;
307 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
310 virtual const char *getArgString(unsigned Index) const {
311 return ArgStrings[Index];
314 virtual unsigned getNumInputArgStrings() const {
315 return NumInputArgStrings;
318 /// @name Arg Synthesis
322 /// MakeIndex - Get an index for the given string(s).
323 unsigned MakeIndex(StringRef String0) const;
324 unsigned MakeIndex(StringRef String0, StringRef String1) const;
326 virtual const char *MakeArgString(StringRef Str) const;
331 /// DerivedArgList - An ordered collection of driver arguments,
332 /// whose storage may be in another argument list.
333 class DerivedArgList : public ArgList {
334 const InputArgList &BaseArgs;
336 /// The list of arguments we synthesized.
337 mutable arglist_type SynthesizedArgs;
340 /// Construct a new derived arg list from \p BaseArgs.
341 DerivedArgList(const InputArgList &BaseArgs);
344 virtual const char *getArgString(unsigned Index) const {
345 return BaseArgs.getArgString(Index);
348 virtual unsigned getNumInputArgStrings() const {
349 return BaseArgs.getNumInputArgStrings();
352 const InputArgList &getBaseArgs() const {
356 /// @name Arg Synthesis
359 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
361 void AddSynthesizedArg(Arg *A) {
362 SynthesizedArgs.push_back(A);
365 virtual const char *MakeArgString(StringRef Str) const;
367 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
368 /// append it to the argument list.
369 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
370 append(MakeFlagArg(BaseArg, Opt));
373 /// AddPositionalArg - Construct a new Positional arg for the given option
374 /// \p Id, with the provided \p Value and append it to the argument
376 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
378 append(MakePositionalArg(BaseArg, Opt, Value));
382 /// AddSeparateArg - Construct a new Positional arg for the given option
383 /// \p Id, with the provided \p Value and append it to the argument
385 void AddSeparateArg(const Arg *BaseArg, const Option Opt,
387 append(MakeSeparateArg(BaseArg, Opt, Value));
391 /// AddJoinedArg - Construct a new Positional arg for the given option
392 /// \p Id, with the provided \p Value and append it to the argument list.
393 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
395 append(MakeJoinedArg(BaseArg, Opt, Value));
399 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
400 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
402 /// MakePositionalArg - Construct a new Positional arg for the
403 /// given option \p Id, with the provided \p Value.
404 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
405 StringRef Value) const;
407 /// MakeSeparateArg - Construct a new Positional arg for the
408 /// given option \p Id, with the provided \p Value.
409 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
410 StringRef Value) const;
412 /// MakeJoinedArg - Construct a new Positional arg for the
413 /// given option \p Id, with the provided \p Value.
414 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
415 StringRef Value) const;
420 } // end namespace opt
421 } // end namespace llvm