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/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Option/OptSpecifier.h"
18 #include "llvm/Option/Option.h"
30 /// arg_iterator - Iterates through arguments stored inside an ArgList.
32 /// The current argument.
33 SmallVectorImpl<Arg*>::const_iterator Current;
35 /// The argument list we are iterating over.
38 /// Optional filters on the arguments which will be match. Most clients
39 /// should never want to iterate over arguments without filters, so we won't
40 /// bother to factor this into two separate iterator implementations.
42 // FIXME: Make efficient; the idea is to provide efficient iteration over
43 // all arguments which match a particular id and then just provide an
44 // iterator combinator which takes multiple iterators which can be
45 // efficiently compared and returns them in order.
46 OptSpecifier Id0, Id1, Id2;
51 typedef Arg * const * value_type;
52 typedef Arg * const & reference;
53 typedef Arg * const * pointer;
54 typedef std::forward_iterator_tag iterator_category;
55 typedef std::ptrdiff_t difference_type;
57 arg_iterator(SmallVectorImpl<Arg *>::const_iterator it, const ArgList &Args,
58 OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
59 OptSpecifier Id2 = 0U)
60 : Current(it), Args(Args), Id0(Id0), Id1(Id1), Id2(Id2) {
64 operator const Arg*() { return *Current; }
65 reference operator*() const { return *Current; }
66 pointer operator->() const { return Current; }
68 arg_iterator &operator++() {
74 arg_iterator operator++(int) {
75 arg_iterator tmp(*this);
80 friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
81 return LHS.Current == RHS.Current;
83 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
88 /// ArgList - Ordered collection of driver arguments.
90 /// The ArgList class manages a list of Arg instances as well as
91 /// auxiliary data and convenience methods to allow Tools to quickly
92 /// check for the presence of Arg instances for a particular Option
93 /// and to iterate over groups of arguments.
96 ArgList(const ArgList &) = delete;
97 void operator=(const ArgList &) = delete;
100 typedef SmallVector<Arg*, 16> arglist_type;
101 typedef arglist_type::iterator iterator;
102 typedef arglist_type::const_iterator const_iterator;
103 typedef arglist_type::reverse_iterator reverse_iterator;
104 typedef arglist_type::const_reverse_iterator const_reverse_iterator;
107 /// The internal list of arguments.
111 // Default ctor provided explicitly as it is not provided implicitly due to
112 // the presence of the (deleted) copy ctor above.
114 // Virtual to provide a vtable anchor and because -Wnon-virtua-dtor warns, not
115 // because this type is ever actually destroyed polymorphically.
123 /// append - Append \p A to the arg list.
126 arglist_type &getArgs() { return Args; }
127 const arglist_type &getArgs() const { return Args; }
129 unsigned size() const { return Args.size(); }
132 /// @name Arg Iteration
135 iterator begin() { return Args.begin(); }
136 iterator end() { return Args.end(); }
138 reverse_iterator rbegin() { return Args.rbegin(); }
139 reverse_iterator rend() { return Args.rend(); }
141 const_iterator begin() const { return Args.begin(); }
142 const_iterator end() const { return Args.end(); }
144 const_reverse_iterator rbegin() const { return Args.rbegin(); }
145 const_reverse_iterator rend() const { return Args.rend(); }
147 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
148 OptSpecifier Id2 = 0U) const {
149 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
151 arg_iterator filtered_end() const {
152 return arg_iterator(Args.end(), *this);
155 iterator_range<arg_iterator> filtered(OptSpecifier Id0 = 0U,
156 OptSpecifier Id1 = 0U,
157 OptSpecifier Id2 = 0U) const {
158 return make_range(filtered_begin(Id0, Id1, Id2), filtered_end());
162 /// @name Arg Removal
165 /// eraseArg - Remove any option matching \p Id.
166 void eraseArg(OptSpecifier Id);
172 /// hasArg - Does the arg list contain any option matching \p Id.
174 /// \p Claim Whether the argument should be claimed, if it exists.
175 bool hasArgNoClaim(OptSpecifier Id) const {
176 return getLastArgNoClaim(Id) != nullptr;
178 bool hasArg(OptSpecifier Id) const {
179 return getLastArg(Id) != nullptr;
181 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
182 return getLastArg(Id0, Id1) != nullptr;
184 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
185 return getLastArg(Id0, Id1, Id2) != nullptr;
188 /// getLastArg - Return the last argument matching \p Id, or null.
190 /// \p Claim Whether the argument should be claimed, if it exists.
191 Arg *getLastArgNoClaim(OptSpecifier Id) const;
192 Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1) const;
193 Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1,
194 OptSpecifier Id2) const;
195 Arg *getLastArgNoClaim(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
196 OptSpecifier Id3) const;
197 Arg *getLastArg(OptSpecifier Id) const;
198 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
199 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
200 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
201 OptSpecifier Id3) const;
202 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
203 OptSpecifier Id3, OptSpecifier Id4) const;
204 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
205 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
206 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
207 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
208 OptSpecifier Id6) const;
209 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
210 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
211 OptSpecifier Id6, OptSpecifier Id7) const;
213 /// getArgString - Return the input argument string at \p Index.
214 virtual const char *getArgString(unsigned Index) const = 0;
216 /// getNumInputArgStrings - Return the number of original argument strings,
217 /// which are guaranteed to be the first strings in the argument string
219 virtual unsigned getNumInputArgStrings() const = 0;
222 /// @name Argument Lookup Utilities
225 /// getLastArgValue - Return the value of the last argument, or a default.
226 StringRef getLastArgValue(OptSpecifier Id,
227 StringRef Default = "") const;
229 /// getAllArgValues - Get the values of all instances of the given argument
231 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
234 /// @name Translation Utilities
237 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
238 /// true if the option is present, false if the negation is present, and
239 /// \p Default if neither option is given. If both the option and its
240 /// negation are present, the last one wins.
241 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
243 /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
244 /// form \p Neg, return true if the option or its alias is present, false if
245 /// the negation is present, and \p Default if none of the options are
246 /// given. If multiple options are present, the last one wins.
247 bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
248 bool Default = true) const;
250 /// AddLastArg - Render only the last argument match \p Id0, if present.
251 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
252 void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
253 OptSpecifier Id1) const;
255 /// AddAllArgs - Render all arguments matching the given ids.
256 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
257 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
259 /// AddAllArgValues - Render the argument values of all arguments
260 /// matching the given ids.
261 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
262 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
264 /// AddAllArgsTranslated - Render all the arguments matching the
265 /// given ids, but forced to separate args and using the provided
266 /// name instead of the first option value.
268 /// \param Joined - If true, render the argument as joined with
269 /// the option specifier.
270 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
271 const char *Translation,
272 bool Joined = false) const;
274 /// ClaimAllArgs - Claim all arguments which match the given
276 void ClaimAllArgs(OptSpecifier Id0) const;
278 /// ClaimAllArgs - Claim all arguments.
280 void ClaimAllArgs() const;
283 /// @name Arg Synthesis
286 /// Construct a constant string pointer whose
287 /// lifetime will match that of the ArgList.
288 virtual const char *MakeArgStringRef(StringRef Str) const = 0;
289 const char *MakeArgString(const Twine &Str) const {
290 SmallString<256> Buf;
291 return MakeArgStringRef(Str.toStringRef(Buf));
294 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
295 /// string at \p Index if possible.
296 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
297 StringRef RHS) const;
302 class InputArgList : public ArgList {
304 /// List of argument strings used by the contained Args.
306 /// This is mutable since we treat the ArgList as being the list
307 /// of Args, and allow routines to add new strings (to have a
308 /// convenient place to store the memory) via MakeIndex.
309 mutable ArgStringList ArgStrings;
311 /// Strings for synthesized arguments.
313 /// This is mutable since we treat the ArgList as being the list
314 /// of Args, and allow routines to add new strings (to have a
315 /// convenient place to store the memory) via MakeIndex.
316 mutable std::list<std::string> SynthesizedStrings;
318 /// The number of original input argument strings.
319 unsigned NumInputArgStrings;
322 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
323 ~InputArgList() override;
325 const char *getArgString(unsigned Index) const override {
326 return ArgStrings[Index];
329 unsigned getNumInputArgStrings() const override {
330 return NumInputArgStrings;
333 /// @name Arg Synthesis
337 /// MakeIndex - Get an index for the given string(s).
338 unsigned MakeIndex(StringRef String0) const;
339 unsigned MakeIndex(StringRef String0, StringRef String1) const;
341 using ArgList::MakeArgString;
342 const char *MakeArgStringRef(StringRef Str) const override;
347 /// DerivedArgList - An ordered collection of driver arguments,
348 /// whose storage may be in another argument list.
349 class DerivedArgList : public ArgList {
350 const InputArgList &BaseArgs;
352 /// The list of arguments we synthesized.
353 mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
356 /// Construct a new derived arg list from \p BaseArgs.
357 DerivedArgList(const InputArgList &BaseArgs);
358 ~DerivedArgList() override;
360 const char *getArgString(unsigned Index) const override {
361 return BaseArgs.getArgString(Index);
364 unsigned getNumInputArgStrings() const override {
365 return BaseArgs.getNumInputArgStrings();
368 const InputArgList &getBaseArgs() const {
372 /// @name Arg Synthesis
375 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
377 void AddSynthesizedArg(Arg *A);
379 using ArgList::MakeArgString;
380 const char *MakeArgStringRef(StringRef Str) const override;
382 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
383 /// append it to the argument list.
384 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
385 append(MakeFlagArg(BaseArg, Opt));
388 /// AddPositionalArg - Construct a new Positional arg for the given option
389 /// \p Id, with the provided \p Value and append it to the argument
391 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
393 append(MakePositionalArg(BaseArg, Opt, Value));
397 /// AddSeparateArg - Construct a new Positional arg for the given option
398 /// \p Id, with the provided \p Value and append it to the argument
400 void AddSeparateArg(const Arg *BaseArg, const Option Opt,
402 append(MakeSeparateArg(BaseArg, Opt, Value));
406 /// AddJoinedArg - Construct a new Positional arg for the given option
407 /// \p Id, with the provided \p Value and append it to the argument list.
408 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
410 append(MakeJoinedArg(BaseArg, Opt, Value));
414 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
415 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
417 /// MakePositionalArg - Construct a new Positional arg for the
418 /// given option \p Id, with the provided \p Value.
419 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
420 StringRef Value) const;
422 /// MakeSeparateArg - Construct a new Positional arg for the
423 /// given option \p Id, with the provided \p Value.
424 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
425 StringRef Value) const;
427 /// MakeJoinedArg - Construct a new Positional arg for the
428 /// given option \p Id, with the provided \p Value.
429 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
430 StringRef Value) const;
435 } // end namespace opt
436 } // end namespace llvm