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_SUPPORT_ARGLIST_H_
11 #define LLVM_SUPPORT_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 /// AddLastArg - Render only the last argument match \p Id0, if present.
226 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
228 /// AddAllArgs - Render all arguments matching the given ids.
229 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
230 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
232 /// AddAllArgValues - Render the argument values of all arguments
233 /// matching the given ids.
234 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
235 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
237 /// AddAllArgsTranslated - Render all the arguments matching the
238 /// given ids, but forced to separate args and using the provided
239 /// name instead of the first option value.
241 /// \param Joined - If true, render the argument as joined with
242 /// the option specifier.
243 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
244 const char *Translation,
245 bool Joined = false) const;
247 /// ClaimAllArgs - Claim all arguments which match the given
249 void ClaimAllArgs(OptSpecifier Id0) const;
251 /// ClaimAllArgs - Claim all arguments.
253 void ClaimAllArgs() const;
256 /// @name Arg Synthesis
259 /// MakeArgString - Construct a constant string pointer whose
260 /// lifetime will match that of the ArgList.
261 virtual const char *MakeArgString(StringRef Str) const = 0;
262 const char *MakeArgString(const char *Str) const {
263 return MakeArgString(StringRef(Str));
265 const char *MakeArgString(std::string Str) const {
266 return MakeArgString(StringRef(Str));
268 const char *MakeArgString(const Twine &Str) const;
270 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
271 /// string at \p Index if possible.
272 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
273 StringRef RHS) const;
278 class InputArgList : public ArgList {
280 /// List of argument strings used by the contained Args.
282 /// This is mutable since we treat the ArgList as being the list
283 /// of Args, and allow routines to add new strings (to have a
284 /// convenient place to store the memory) via MakeIndex.
285 mutable ArgStringList ArgStrings;
287 /// Strings for synthesized arguments.
289 /// This is mutable since we treat the ArgList as being the list
290 /// of Args, and allow routines to add new strings (to have a
291 /// convenient place to store the memory) via MakeIndex.
292 mutable std::list<std::string> SynthesizedStrings;
294 /// The number of original input argument strings.
295 unsigned NumInputArgStrings;
298 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
301 virtual const char *getArgString(unsigned Index) const {
302 return ArgStrings[Index];
305 virtual unsigned getNumInputArgStrings() const {
306 return NumInputArgStrings;
309 /// @name Arg Synthesis
313 /// MakeIndex - Get an index for the given string(s).
314 unsigned MakeIndex(StringRef String0) const;
315 unsigned MakeIndex(StringRef String0, StringRef String1) const;
317 virtual const char *MakeArgString(StringRef Str) const;
322 /// DerivedArgList - An ordered collection of driver arguments,
323 /// whose storage may be in another argument list.
324 class DerivedArgList : public ArgList {
325 const InputArgList &BaseArgs;
327 /// The list of arguments we synthesized.
328 mutable arglist_type SynthesizedArgs;
331 /// Construct a new derived arg list from \p BaseArgs.
332 DerivedArgList(const InputArgList &BaseArgs);
335 virtual const char *getArgString(unsigned Index) const {
336 return BaseArgs.getArgString(Index);
339 virtual unsigned getNumInputArgStrings() const {
340 return BaseArgs.getNumInputArgStrings();
343 const InputArgList &getBaseArgs() const {
347 /// @name Arg Synthesis
350 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
352 void AddSynthesizedArg(Arg *A) {
353 SynthesizedArgs.push_back(A);
356 virtual const char *MakeArgString(StringRef Str) const;
358 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
359 /// append it to the argument list.
360 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
361 append(MakeFlagArg(BaseArg, Opt));
364 /// AddPositionalArg - Construct a new Positional arg for the given option
365 /// \p Id, with the provided \p Value and append it to the argument
367 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
369 append(MakePositionalArg(BaseArg, Opt, Value));
373 /// AddSeparateArg - 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 AddSeparateArg(const Arg *BaseArg, const Option Opt,
378 append(MakeSeparateArg(BaseArg, Opt, Value));
382 /// AddJoinedArg - Construct a new Positional arg for the given option
383 /// \p Id, with the provided \p Value and append it to the argument list.
384 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
386 append(MakeJoinedArg(BaseArg, Opt, Value));
390 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
391 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
393 /// MakePositionalArg - Construct a new Positional arg for the
394 /// given option \p Id, with the provided \p Value.
395 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
396 StringRef Value) const;
398 /// MakeSeparateArg - Construct a new Positional arg for the
399 /// given option \p Id, with the provided \p Value.
400 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
401 StringRef Value) const;
403 /// MakeJoinedArg - Construct a new Positional arg for the
404 /// given option \p Id, with the provided \p Value.
405 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
406 StringRef Value) const;
411 } // end namespace opt
412 } // end namespace llvm