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/Option.h"
16 #include "llvm/Option/OptSpecifier.h"
28 /// arg_iterator - Iterates through arguments stored inside an ArgList.
30 /// The current argument.
31 SmallVectorImpl<Arg*>::const_iterator Current;
33 /// The argument list we are iterating over.
36 /// Optional filters on the arguments which will be match. Most clients
37 /// should never want to iterate over arguments without filters, so we won't
38 /// bother to factor this into two separate iterator implementations.
40 // FIXME: Make efficient; the idea is to provide efficient iteration over
41 // all arguments which match a particular id and then just provide an
42 // iterator combinator which takes multiple iterators which can be
43 // efficiently compared and returns them in order.
44 OptSpecifier Id0, Id1, Id2;
49 typedef Arg * const * value_type;
50 typedef Arg * const & reference;
51 typedef Arg * const * pointer;
52 typedef std::forward_iterator_tag iterator_category;
53 typedef std::ptrdiff_t difference_type;
55 arg_iterator(SmallVectorImpl<Arg*>::const_iterator it,
56 const ArgList &_Args, OptSpecifier _Id0 = 0U,
57 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
58 : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
62 operator const Arg*() { return *Current; }
63 reference operator*() const { return *Current; }
64 pointer operator->() const { return Current; }
66 arg_iterator &operator++() {
72 arg_iterator operator++(int) {
73 arg_iterator tmp(*this);
78 friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
79 return LHS.Current == RHS.Current;
81 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
86 /// ArgList - Ordered collection of driver arguments.
88 /// The ArgList class manages a list of Arg instances as well as
89 /// auxiliary data and convenience methods to allow Tools to quickly
90 /// check for the presence of Arg instances for a particular Option
91 /// and to iterate over groups of arguments.
94 ArgList(const ArgList &) LLVM_DELETED_FUNCTION;
95 void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
98 typedef SmallVector<Arg*, 16> arglist_type;
99 typedef arglist_type::iterator iterator;
100 typedef arglist_type::const_iterator const_iterator;
101 typedef arglist_type::reverse_iterator reverse_iterator;
102 typedef arglist_type::const_reverse_iterator const_reverse_iterator;
105 /// The internal list of arguments.
117 /// append - Append \p A to the arg list.
120 arglist_type &getArgs() { return Args; }
121 const arglist_type &getArgs() const { return Args; }
123 unsigned size() const { return Args.size(); }
126 /// @name Arg Iteration
129 iterator begin() { return Args.begin(); }
130 iterator end() { return Args.end(); }
132 reverse_iterator rbegin() { return Args.rbegin(); }
133 reverse_iterator rend() { return Args.rend(); }
135 const_iterator begin() const { return Args.begin(); }
136 const_iterator end() const { return Args.end(); }
138 const_reverse_iterator rbegin() const { return Args.rbegin(); }
139 const_reverse_iterator rend() const { return Args.rend(); }
141 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
142 OptSpecifier Id2 = 0U) const {
143 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
145 arg_iterator filtered_end() const {
146 return arg_iterator(Args.end(), *this);
150 /// @name Arg Removal
153 /// eraseArg - Remove any option matching \p Id.
154 void eraseArg(OptSpecifier Id);
160 /// hasArg - Does the arg list contain any option matching \p Id.
162 /// \p Claim Whether the argument should be claimed, if it exists.
163 bool hasArgNoClaim(OptSpecifier Id) const {
164 return getLastArgNoClaim(Id) != 0;
166 bool hasArg(OptSpecifier Id) const {
167 return getLastArg(Id) != 0;
169 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
170 return getLastArg(Id0, Id1) != 0;
172 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
173 return getLastArg(Id0, Id1, Id2) != 0;
176 /// getLastArg - Return the last argument matching \p Id, or null.
178 /// \p Claim Whether the argument should be claimed, if it exists.
179 Arg *getLastArgNoClaim(OptSpecifier Id) const;
180 Arg *getLastArg(OptSpecifier Id) const;
181 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
182 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
183 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
184 OptSpecifier Id3) const;
185 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
186 OptSpecifier Id3, OptSpecifier Id4) const;
187 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
188 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
189 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
190 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
191 OptSpecifier Id6) const;
192 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
193 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
194 OptSpecifier Id6, OptSpecifier Id7) const;
196 /// getArgString - Return the input argument string at \p Index.
197 virtual const char *getArgString(unsigned Index) const = 0;
199 /// getNumInputArgStrings - Return the number of original argument strings,
200 /// which are guaranteed to be the first strings in the argument string
202 virtual unsigned getNumInputArgStrings() const = 0;
205 /// @name Argument Lookup Utilities
208 /// getLastArgValue - Return the value of the last argument, or a default.
209 StringRef getLastArgValue(OptSpecifier Id,
210 StringRef Default = "") const;
212 /// getAllArgValues - Get the values of all instances of the given argument
214 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
217 /// @name Translation Utilities
220 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
221 /// true if the option is present, false if the negation is present, and
222 /// \p Default if neither option is given. If both the option and its
223 /// negation are present, the last one wins.
224 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
226 /// AddLastArg - Render only the last argument match \p Id0, if present.
227 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
229 /// AddAllArgs - Render all arguments matching the given ids.
230 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
231 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
233 /// AddAllArgValues - Render the argument values of all arguments
234 /// matching the given ids.
235 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
236 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
238 /// AddAllArgsTranslated - Render all the arguments matching the
239 /// given ids, but forced to separate args and using the provided
240 /// name instead of the first option value.
242 /// \param Joined - If true, render the argument as joined with
243 /// the option specifier.
244 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
245 const char *Translation,
246 bool Joined = false) const;
248 /// ClaimAllArgs - Claim all arguments which match the given
250 void ClaimAllArgs(OptSpecifier Id0) const;
252 /// ClaimAllArgs - Claim all arguments.
254 void ClaimAllArgs() const;
257 /// @name Arg Synthesis
260 /// MakeArgString - Construct a constant string pointer whose
261 /// lifetime will match that of the ArgList.
262 virtual const char *MakeArgString(StringRef Str) const = 0;
263 const char *MakeArgString(const char *Str) const {
264 return MakeArgString(StringRef(Str));
266 const char *MakeArgString(std::string Str) const {
267 return MakeArgString(StringRef(Str));
269 const char *MakeArgString(const Twine &Str) const;
271 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
272 /// string at \p Index if possible.
273 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
274 StringRef RHS) const;
279 class InputArgList : public ArgList {
281 /// List of argument strings used by the contained Args.
283 /// This is mutable since we treat the ArgList as being the list
284 /// of Args, and allow routines to add new strings (to have a
285 /// convenient place to store the memory) via MakeIndex.
286 mutable ArgStringList ArgStrings;
288 /// Strings for synthesized arguments.
290 /// This is mutable since we treat the ArgList as being the list
291 /// of Args, and allow routines to add new strings (to have a
292 /// convenient place to store the memory) via MakeIndex.
293 mutable std::list<std::string> SynthesizedStrings;
295 /// The number of original input argument strings.
296 unsigned NumInputArgStrings;
299 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
302 virtual const char *getArgString(unsigned Index) const {
303 return ArgStrings[Index];
306 virtual unsigned getNumInputArgStrings() const {
307 return NumInputArgStrings;
310 /// @name Arg Synthesis
314 /// MakeIndex - Get an index for the given string(s).
315 unsigned MakeIndex(StringRef String0) const;
316 unsigned MakeIndex(StringRef String0, StringRef String1) const;
318 virtual const char *MakeArgString(StringRef Str) const;
323 /// DerivedArgList - An ordered collection of driver arguments,
324 /// whose storage may be in another argument list.
325 class DerivedArgList : public ArgList {
326 const InputArgList &BaseArgs;
328 /// The list of arguments we synthesized.
329 mutable arglist_type SynthesizedArgs;
332 /// Construct a new derived arg list from \p BaseArgs.
333 DerivedArgList(const InputArgList &BaseArgs);
336 virtual const char *getArgString(unsigned Index) const {
337 return BaseArgs.getArgString(Index);
340 virtual unsigned getNumInputArgStrings() const {
341 return BaseArgs.getNumInputArgStrings();
344 const InputArgList &getBaseArgs() const {
348 /// @name Arg Synthesis
351 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
353 void AddSynthesizedArg(Arg *A) {
354 SynthesizedArgs.push_back(A);
357 virtual const char *MakeArgString(StringRef Str) const;
359 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
360 /// append it to the argument list.
361 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
362 append(MakeFlagArg(BaseArg, Opt));
365 /// AddPositionalArg - Construct a new Positional arg for the given option
366 /// \p Id, with the provided \p Value and append it to the argument
368 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
370 append(MakePositionalArg(BaseArg, Opt, Value));
374 /// AddSeparateArg - Construct a new Positional arg for the given option
375 /// \p Id, with the provided \p Value and append it to the argument
377 void AddSeparateArg(const Arg *BaseArg, const Option Opt,
379 append(MakeSeparateArg(BaseArg, Opt, Value));
383 /// AddJoinedArg - Construct a new Positional arg for the given option
384 /// \p Id, with the provided \p Value and append it to the argument list.
385 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
387 append(MakeJoinedArg(BaseArg, Opt, Value));
391 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
392 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
394 /// MakePositionalArg - Construct a new Positional arg for the
395 /// given option \p Id, with the provided \p Value.
396 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
397 StringRef Value) const;
399 /// MakeSeparateArg - Construct a new Positional arg for the
400 /// given option \p Id, with the provided \p Value.
401 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
402 StringRef Value) const;
404 /// MakeJoinedArg - Construct a new Positional arg for the
405 /// given option \p Id, with the provided \p Value.
406 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
407 StringRef Value) const;
412 } // end namespace opt
413 } // end namespace llvm