Teach Twine to support SmallString.
[oota-llvm.git] / include / llvm / Option / ArgList.h
1 //===--- ArgList.h - Argument List Management -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_OPTION_ARGLIST_H
11 #define LLVM_OPTION_ARGLIST_H
12
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"
19 #include <list>
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 namespace llvm {
25 namespace opt {
26 class Arg;
27 class ArgList;
28 class Option;
29
30 /// arg_iterator - Iterates through arguments stored inside an ArgList.
31 class arg_iterator {
32   /// The current argument.
33   SmallVectorImpl<Arg*>::const_iterator Current;
34
35   /// The argument list we are iterating over.
36   const ArgList &Args;
37
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.
41   //
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;
47
48   void SkipToNextArg();
49
50 public:
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;
56
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) {
61     SkipToNextArg();
62   }
63
64   operator const Arg*() { return *Current; }
65   reference operator*() const { return *Current; }
66   pointer operator->() const { return Current; }
67
68   arg_iterator &operator++() {
69     ++Current;
70     SkipToNextArg();
71     return *this;
72   }
73
74   arg_iterator operator++(int) {
75     arg_iterator tmp(*this);
76     ++(*this);
77     return tmp;
78   }
79
80   friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
81     return LHS.Current == RHS.Current;
82   }
83   friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
84     return !(LHS == RHS);
85   }
86 };
87
88 /// ArgList - Ordered collection of driver arguments.
89 ///
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.
94 class ArgList {
95 private:
96   ArgList(const ArgList &) = delete;
97   void operator=(const ArgList &) = delete;
98
99 public:
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;
105
106 private:
107   /// The internal list of arguments.
108   arglist_type Args;
109
110 protected:
111   // Default ctor provided explicitly as it is not provided implicitly due to
112   // the presence of the (deleted) copy ctor above.
113   ArgList() { }
114   // Virtual to provide a vtable anchor and because -Wnon-virtua-dtor warns, not
115   // because this type is ever actually destroyed polymorphically.
116   virtual ~ArgList();
117
118 public:
119
120   /// @name Arg Access
121   /// @{
122
123   /// append - Append \p A to the arg list.
124   void append(Arg *A);
125
126   arglist_type &getArgs() { return Args; }
127   const arglist_type &getArgs() const { return Args; }
128
129   unsigned size() const { return Args.size(); }
130
131   /// @}
132   /// @name Arg Iteration
133   /// @{
134
135   iterator begin() { return Args.begin(); }
136   iterator end() { return Args.end(); }
137
138   reverse_iterator rbegin() { return Args.rbegin(); }
139   reverse_iterator rend() { return Args.rend(); }
140
141   const_iterator begin() const { return Args.begin(); }
142   const_iterator end() const { return Args.end(); }
143
144   const_reverse_iterator rbegin() const { return Args.rbegin(); }
145   const_reverse_iterator rend() const { return Args.rend(); }
146
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);
150   }
151   arg_iterator filtered_end() const {
152     return arg_iterator(Args.end(), *this);
153   }
154
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());
159   }
160
161   /// @}
162   /// @name Arg Removal
163   /// @{
164
165   /// eraseArg - Remove any option matching \p Id.
166   void eraseArg(OptSpecifier Id);
167
168   /// @}
169   /// @name Arg Access
170   /// @{
171
172   /// hasArg - Does the arg list contain any option matching \p Id.
173   ///
174   /// \p Claim Whether the argument should be claimed, if it exists.
175   bool hasArgNoClaim(OptSpecifier Id) const {
176     return getLastArgNoClaim(Id) != nullptr;
177   }
178   bool hasArg(OptSpecifier Id) const {
179     return getLastArg(Id) != nullptr;
180   }
181   bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
182     return getLastArg(Id0, Id1) != nullptr;
183   }
184   bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
185     return getLastArg(Id0, Id1, Id2) != nullptr;
186   }
187
188   /// getLastArg - Return the last argument matching \p Id, or null.
189   ///
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 *getLastArg(OptSpecifier Id) const;
194   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
195   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
196   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
197                   OptSpecifier Id3) const;
198   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
199                   OptSpecifier Id3, OptSpecifier Id4) const;
200   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
201                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
202   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
203                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
204                   OptSpecifier Id6) const;
205   Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
206                   OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
207                   OptSpecifier Id6, OptSpecifier Id7) const;
208
209   /// getArgString - Return the input argument string at \p Index.
210   virtual const char *getArgString(unsigned Index) const = 0;
211
212   /// getNumInputArgStrings - Return the number of original argument strings,
213   /// which are guaranteed to be the first strings in the argument string
214   /// list.
215   virtual unsigned getNumInputArgStrings() const = 0;
216
217   /// @}
218   /// @name Argument Lookup Utilities
219   /// @{
220
221   /// getLastArgValue - Return the value of the last argument, or a default.
222   StringRef getLastArgValue(OptSpecifier Id,
223                                   StringRef Default = "") const;
224
225   /// getAllArgValues - Get the values of all instances of the given argument
226   /// as strings.
227   std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
228
229   /// @}
230   /// @name Translation Utilities
231   /// @{
232
233   /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
234   /// true if the option is present, false if the negation is present, and
235   /// \p Default if neither option is given. If both the option and its
236   /// negation are present, the last one wins.
237   bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
238
239   /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
240   /// form \p Neg, return true if the option or its alias is present, false if
241   /// the negation is present, and \p Default if none of the options are
242   /// given. If multiple options are present, the last one wins.
243   bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
244                bool Default = true) const;
245
246   /// AddLastArg - Render only the last argument match \p Id0, if present.
247   void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
248   void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
249                   OptSpecifier Id1) const;
250
251   /// AddAllArgs - Render all arguments matching the given ids.
252   void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
253                   OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
254
255   /// AddAllArgValues - Render the argument values of all arguments
256   /// matching the given ids.
257   void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
258                         OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
259
260   /// AddAllArgsTranslated - Render all the arguments matching the
261   /// given ids, but forced to separate args and using the provided
262   /// name instead of the first option value.
263   ///
264   /// \param Joined - If true, render the argument as joined with
265   /// the option specifier.
266   void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
267                             const char *Translation,
268                             bool Joined = false) const;
269
270   /// ClaimAllArgs - Claim all arguments which match the given
271   /// option id.
272   void ClaimAllArgs(OptSpecifier Id0) const;
273
274   /// ClaimAllArgs - Claim all arguments.
275   ///
276   void ClaimAllArgs() const;
277
278   /// @}
279   /// @name Arg Synthesis
280   /// @{
281
282   /// Construct a constant string pointer whose
283   /// lifetime will match that of the ArgList.
284   virtual const char *MakeArgStringRef(StringRef Str) const = 0;
285   const char *MakeArgString(const Twine &Str) const {
286     SmallString<256> Buf;
287     return MakeArgStringRef(Str.toStringRef(Buf));
288   }
289
290   /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
291   /// string at \p Index if possible.
292   const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
293                                         StringRef RHS) const;
294
295   /// @}
296 };
297
298 class InputArgList : public ArgList  {
299 private:
300   /// List of argument strings used by the contained Args.
301   ///
302   /// This is mutable since we treat the ArgList as being the list
303   /// of Args, and allow routines to add new strings (to have a
304   /// convenient place to store the memory) via MakeIndex.
305   mutable ArgStringList ArgStrings;
306
307   /// Strings for synthesized arguments.
308   ///
309   /// This is mutable since we treat the ArgList as being the list
310   /// of Args, and allow routines to add new strings (to have a
311   /// convenient place to store the memory) via MakeIndex.
312   mutable std::list<std::string> SynthesizedStrings;
313
314   /// The number of original input argument strings.
315   unsigned NumInputArgStrings;
316
317 public:
318   InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
319   ~InputArgList();
320
321   const char *getArgString(unsigned Index) const override {
322     return ArgStrings[Index];
323   }
324
325   unsigned getNumInputArgStrings() const override {
326     return NumInputArgStrings;
327   }
328
329   /// @name Arg Synthesis
330   /// @{
331
332 public:
333   /// MakeIndex - Get an index for the given string(s).
334   unsigned MakeIndex(StringRef String0) const;
335   unsigned MakeIndex(StringRef String0, StringRef String1) const;
336
337   using ArgList::MakeArgString;
338   const char *MakeArgStringRef(StringRef Str) const override;
339
340   /// @}
341 };
342
343 /// DerivedArgList - An ordered collection of driver arguments,
344 /// whose storage may be in another argument list.
345 class DerivedArgList : public ArgList {
346   const InputArgList &BaseArgs;
347
348   /// The list of arguments we synthesized.
349   mutable SmallVector<std::unique_ptr<Arg>, 16> SynthesizedArgs;
350
351 public:
352   /// Construct a new derived arg list from \p BaseArgs.
353   DerivedArgList(const InputArgList &BaseArgs);
354   ~DerivedArgList();
355
356   const char *getArgString(unsigned Index) const override {
357     return BaseArgs.getArgString(Index);
358   }
359
360   unsigned getNumInputArgStrings() const override {
361     return BaseArgs.getNumInputArgStrings();
362   }
363
364   const InputArgList &getBaseArgs() const {
365     return BaseArgs;
366   }
367
368   /// @name Arg Synthesis
369   /// @{
370
371   /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
372   /// (to be freed).
373   void AddSynthesizedArg(Arg *A);
374
375   using ArgList::MakeArgString;
376   const char *MakeArgStringRef(StringRef Str) const override;
377
378   /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
379   /// append it to the argument list.
380   void AddFlagArg(const Arg *BaseArg, const Option Opt) {
381     append(MakeFlagArg(BaseArg, Opt));
382   }
383
384   /// AddPositionalArg - Construct a new Positional arg for the given option
385   /// \p Id, with the provided \p Value and append it to the argument
386   /// list.
387   void AddPositionalArg(const Arg *BaseArg, const Option Opt,
388                         StringRef Value) {
389     append(MakePositionalArg(BaseArg, Opt, Value));
390   }
391
392
393   /// AddSeparateArg - Construct a new Positional arg for the given option
394   /// \p Id, with the provided \p Value and append it to the argument
395   /// list.
396   void AddSeparateArg(const Arg *BaseArg, const Option Opt,
397                       StringRef Value) {
398     append(MakeSeparateArg(BaseArg, Opt, Value));
399   }
400
401
402   /// AddJoinedArg - Construct a new Positional arg for the given option
403   /// \p Id, with the provided \p Value and append it to the argument list.
404   void AddJoinedArg(const Arg *BaseArg, const Option Opt,
405                     StringRef Value) {
406     append(MakeJoinedArg(BaseArg, Opt, Value));
407   }
408
409
410   /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
411   Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
412
413   /// MakePositionalArg - Construct a new Positional arg for the
414   /// given option \p Id, with the provided \p Value.
415   Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
416                           StringRef Value) const;
417
418   /// MakeSeparateArg - Construct a new Positional arg for the
419   /// given option \p Id, with the provided \p Value.
420   Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
421                         StringRef Value) const;
422
423   /// MakeJoinedArg - Construct a new Positional arg for the
424   /// given option \p Id, with the provided \p Value.
425   Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
426                       StringRef Value) const;
427
428   /// @}
429 };
430
431 } // end namespace opt
432 } // end namespace llvm
433
434 #endif