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"
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.
109 // Default ctor provided explicitly as it is not provided implicitly due to
110 // the presence of the (deleted) copy ctor above.
112 // Virtual to provide a vtable anchor and because -Wnon-virtua-dtor warns, not
113 // because this type is ever actually destroyed polymorphically.
121 /// append - Append \p A to the arg list.
124 arglist_type &getArgs() { return Args; }
125 const arglist_type &getArgs() const { return Args; }
127 unsigned size() const { return Args.size(); }
130 /// @name Arg Iteration
133 iterator begin() { return Args.begin(); }
134 iterator end() { return Args.end(); }
136 reverse_iterator rbegin() { return Args.rbegin(); }
137 reverse_iterator rend() { return Args.rend(); }
139 const_iterator begin() const { return Args.begin(); }
140 const_iterator end() const { return Args.end(); }
142 const_reverse_iterator rbegin() const { return Args.rbegin(); }
143 const_reverse_iterator rend() const { return Args.rend(); }
145 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
146 OptSpecifier Id2 = 0U) const {
147 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
149 arg_iterator filtered_end() const {
150 return arg_iterator(Args.end(), *this);
154 /// @name Arg Removal
157 /// eraseArg - Remove any option matching \p Id.
158 void eraseArg(OptSpecifier Id);
164 /// hasArg - Does the arg list contain any option matching \p Id.
166 /// \p Claim Whether the argument should be claimed, if it exists.
167 bool hasArgNoClaim(OptSpecifier Id) const {
168 return getLastArgNoClaim(Id) != nullptr;
170 bool hasArg(OptSpecifier Id) const {
171 return getLastArg(Id) != nullptr;
173 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
174 return getLastArg(Id0, Id1) != nullptr;
176 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
177 return getLastArg(Id0, Id1, Id2) != nullptr;
180 /// getLastArg - Return the last argument matching \p Id, or null.
182 /// \p Claim Whether the argument should be claimed, if it exists.
183 Arg *getLastArgNoClaim(OptSpecifier Id) const;
184 Arg *getLastArg(OptSpecifier Id) const;
185 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
186 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
187 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
188 OptSpecifier Id3) const;
189 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
190 OptSpecifier Id3, OptSpecifier Id4) const;
191 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
192 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
193 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
194 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
195 OptSpecifier Id6) const;
196 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
197 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
198 OptSpecifier Id6, OptSpecifier Id7) const;
200 /// getArgString - Return the input argument string at \p Index.
201 virtual const char *getArgString(unsigned Index) const = 0;
203 /// getNumInputArgStrings - Return the number of original argument strings,
204 /// which are guaranteed to be the first strings in the argument string
206 virtual unsigned getNumInputArgStrings() const = 0;
209 /// @name Argument Lookup Utilities
212 /// getLastArgValue - Return the value of the last argument, or a default.
213 StringRef getLastArgValue(OptSpecifier Id,
214 StringRef Default = "") const;
216 /// getAllArgValues - Get the values of all instances of the given argument
218 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
221 /// @name Translation Utilities
224 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
225 /// true if the option is present, false if the negation is present, and
226 /// \p Default if neither option is given. If both the option and its
227 /// negation are present, the last one wins.
228 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
230 /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
231 /// form \p Neg, return true if the option or its alias is present, false if
232 /// the negation is present, and \p Default if none of the options are
233 /// given. If multiple options are present, the last one wins.
234 bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
235 bool Default = true) const;
237 /// AddLastArg - Render only the last argument match \p Id0, if present.
238 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
239 void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
240 OptSpecifier Id1) const;
242 /// AddAllArgs - Render all arguments matching the given ids.
243 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
244 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
246 /// AddAllArgValues - Render the argument values of all arguments
247 /// matching the given ids.
248 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
249 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
251 /// AddAllArgsTranslated - Render all the arguments matching the
252 /// given ids, but forced to separate args and using the provided
253 /// name instead of the first option value.
255 /// \param Joined - If true, render the argument as joined with
256 /// the option specifier.
257 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
258 const char *Translation,
259 bool Joined = false) const;
261 /// ClaimAllArgs - Claim all arguments which match the given
263 void ClaimAllArgs(OptSpecifier Id0) const;
265 /// ClaimAllArgs - Claim all arguments.
267 void ClaimAllArgs() const;
270 /// @name Arg Synthesis
273 /// MakeArgString - Construct a constant string pointer whose
274 /// lifetime will match that of the ArgList.
275 virtual const char *MakeArgString(StringRef Str) const = 0;
276 const char *MakeArgString(const char *Str) const {
277 return MakeArgString(StringRef(Str));
279 const char *MakeArgString(std::string Str) const {
280 return MakeArgString(StringRef(Str));
282 const char *MakeArgString(const Twine &Str) const;
284 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
285 /// string at \p Index if possible.
286 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
287 StringRef RHS) const;
292 class InputArgList : public ArgList {
294 /// List of argument strings used by the contained Args.
296 /// This is mutable since we treat the ArgList as being the list
297 /// of Args, and allow routines to add new strings (to have a
298 /// convenient place to store the memory) via MakeIndex.
299 mutable ArgStringList ArgStrings;
301 /// Strings for synthesized arguments.
303 /// This is mutable since we treat the ArgList as being the list
304 /// of Args, and allow routines to add new strings (to have a
305 /// convenient place to store the memory) via MakeIndex.
306 mutable std::list<std::string> SynthesizedStrings;
308 /// The number of original input argument strings.
309 unsigned NumInputArgStrings;
312 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
315 const char *getArgString(unsigned Index) const override {
316 return ArgStrings[Index];
319 unsigned getNumInputArgStrings() const override {
320 return NumInputArgStrings;
323 /// @name Arg Synthesis
327 /// MakeIndex - Get an index for the given string(s).
328 unsigned MakeIndex(StringRef String0) const;
329 unsigned MakeIndex(StringRef String0, StringRef String1) const;
331 const char *MakeArgString(StringRef Str) const override;
336 /// DerivedArgList - An ordered collection of driver arguments,
337 /// whose storage may be in another argument list.
338 class DerivedArgList : public ArgList {
339 const InputArgList &BaseArgs;
341 /// The list of arguments we synthesized.
342 mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
345 /// Construct a new derived arg list from \p BaseArgs.
346 DerivedArgList(const InputArgList &BaseArgs);
349 const char *getArgString(unsigned Index) const override {
350 return BaseArgs.getArgString(Index);
353 unsigned getNumInputArgStrings() const override {
354 return BaseArgs.getNumInputArgStrings();
357 const InputArgList &getBaseArgs() const {
361 /// @name Arg Synthesis
364 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
366 void AddSynthesizedArg(Arg *A);
368 const char *MakeArgString(StringRef Str) const override;
370 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
371 /// append it to the argument list.
372 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
373 append(MakeFlagArg(BaseArg, Opt));
376 /// AddPositionalArg - Construct a new Positional arg for the given option
377 /// \p Id, with the provided \p Value and append it to the argument
379 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
381 append(MakePositionalArg(BaseArg, Opt, Value));
385 /// AddSeparateArg - Construct a new Positional arg for the given option
386 /// \p Id, with the provided \p Value and append it to the argument
388 void AddSeparateArg(const Arg *BaseArg, const Option Opt,
390 append(MakeSeparateArg(BaseArg, Opt, Value));
394 /// AddJoinedArg - Construct a new Positional arg for the given option
395 /// \p Id, with the provided \p Value and append it to the argument list.
396 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
398 append(MakeJoinedArg(BaseArg, Opt, Value));
402 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
403 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
405 /// MakePositionalArg - Construct a new Positional arg for the
406 /// given option \p Id, with the provided \p Value.
407 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
408 StringRef Value) const;
410 /// MakeSeparateArg - Construct a new Positional arg for the
411 /// given option \p Id, with the provided \p Value.
412 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
413 StringRef Value) const;
415 /// MakeJoinedArg - Construct a new Positional arg for the
416 /// given option \p Id, with the provided \p Value.
417 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
418 StringRef Value) const;
423 } // end namespace opt
424 } // end namespace llvm